Compare commits

...

4 Commits

Author SHA1 Message Date
Atlas Cove b2ff6f88e3 added back C version 2023-09-30 16:55:18 +01:00
Atlas Cove 10ab8da848 added sum function to shellscript version 2023-09-30 16:30:56 +01:00
Atlas Cove 25ea50ae8a Switched to shellscript
(less overhead overall)
2022-08-21 14:27:44 +01:00
Atlas Cove b3c4b5e81e Added modulo program 2022-06-28 21:05:34 +01:00
9 changed files with 43 additions and 1 deletions

View File

@ -1,4 +1,5 @@
files = add div mean mul sub sum
#TODO: find way of installing shell library with make
files = c/add c/div c/mean c/mod c/mul c/sub c/sum
all: $(files)

View File

View File

View File

25
c/mod.c Normal file
View File

@ -0,0 +1,25 @@
#include <stdio.h>
#ifndef eprintf
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#endif
int main(int argc, char **argv) {
int v1;
int v2;
int out;
int e1;
int e2;
e1 = sscanf(argv[1], "%i", &v1);
e2 = sscanf(argv[2], "%i", &v2);
if(e1==EOF) {
eprintf("%s: argument 1 cannot be parsed as int", *argv);
return 1;
} else if(e2==EOF) {
eprintf("%s: argument 2 cannot be parsed as int", *argv);
return 1;
}
out=v1%v2;
printf("%d\n",out);
return 0;
}

View File

View File

View File

16
shellmath.sh Normal file
View File

@ -0,0 +1,16 @@
# Shellmath v2
# Works in most bourne-type shells.
# Partially reimplemented in shell script due to less overhead.
function add { echo $(($1+$2)); }
function sub { echo $(($1-$2)); }
function mul { echo $(($1*$2)); }
function div { echo $(($1/$2)); }
function mdl { echo $(($1%$2)); }
function randr { echo $(($1+$RANDOM%$2)); }
function sum {
for i in $@; do
acc=$((acc+i))
done
echo $acc
}