Przeglądaj źródła

add some more math operations.

Lucas Stadler 11 lat temu
rodzic
commit
c2d258df51
1 zmienionych plików z 6 dodań i 1 usunięć
  1. 6 1
      c/mul/main.c

+ 6 - 1
c/mul/main.c

@ -1,3 +1,4 @@
1
#include <math.h>
1 2
#include <stdio.h>
2 3
3 4
#include <editline/readline.h>
@ -9,6 +10,10 @@ long eval_op(long x, char* op, long y) {
9 10
	if (strcmp(op, "-") == 0) { return x - y; }
10 11
	if (strcmp(op, "*") == 0) { return x * y; }
11 12
	if (strcmp(op, "/") == 0) { return x / y; }
13
	if (strcmp(op, "%") == 0) { return x % y; }
14
	if (strcmp(op, "^") == 0) { return pow(x, y); }
15
	if (strcmp(op, "min") == 0) { return x < y ? x : y; }
16
	if (strcmp(op, "max") == 0) { return x > y ? x : y; }
12 17
	return 0;
13 18
}
14 19
@ -38,7 +43,7 @@ int main(int argc, char** argv) {
38 43
	mpca_lang(MPC_LANG_DEFAULT,
39 44
		"\
40 45
number   : /-?[0-9]+(\\.[0-9]+)?/ ; \
41
operator : '+' | '-' | '*' | '/' | /[a-zA-Z-]+/ ; \
46
operator : '+' | '-' | '*' | '/' | '%' | '^' | /[a-zA-Z-]+/ ; \
42 47
expr     : <number> | '(' <operator> <expr>+ ')' ; \
43 48
lang     : /^/ <expr>+ /$/ ; \
44 49
",