Compare commits

...

2 Commits

Author SHA1 Message Date
10ab8da848 added sum function to shellscript version 2023-09-30 16:30:56 +01:00
25ea50ae8a Switched to shellscript
(less overhead overall)
2022-08-21 14:27:44 +01:00
7 changed files with 18 additions and 126 deletions

View File

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

25
add.c
View File

@ -1,25 +0,0 @@
#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;
}

25
div.c
View File

@ -1,25 +0,0 @@
#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;
}

25
mod.c
View File

@ -1,25 +0,0 @@
#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;
}

25
mul.c
View File

@ -1,25 +0,0 @@
#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;
}

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
}

25
sub.c
View File

@ -1,25 +0,0 @@
#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;
}