Compare commits
2 Commits
b3c4b5e81e
...
10ab8da848
Author | SHA1 | Date | |
---|---|---|---|
10ab8da848 | |||
25ea50ae8a |
3
Makefile
3
Makefile
@ -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
25
add.c
@ -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
25
div.c
@ -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
25
mod.c
@ -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
25
mul.c
@ -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
16
shellmath.sh
Normal 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
25
sub.c
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user