-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchar_operate.c
More file actions
35 lines (31 loc) · 982 Bytes
/
char_operate.c
File metadata and controls
35 lines (31 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "char_operate.h"
#include <math.h>
int isOperator( char c ) {
if (c == '+') return OP_ADD;
else if (c == '-') return OP_SUB;
else if (c == '*') return OP_MUL;
else if (c == '/') return OP_DIV;
else if (c == '^') return OP_POW;
else if (c == ')') return OP_RIGHT_BRACE;
else if (c == '(') return OP_LEFT_BRACE;
else if (c == '_') return OP_NEG;
else if (c == '#') return OP_POS;
else return -1;
}
long double returnCalcAnswer( long double c, long double a, long double b ) {
int op = (int) c;
if (op == OP_ADD) return a + b;
else if (op == OP_SUB) return a - b;
else if (op == OP_MUL) return a * b;
else if (op == OP_DIV) return a * 1.0 / b;
else if (op == OP_POW) {
return pow( a, b );
}
}
int precede( char c ) {
if (c == '+' || c == '-') return 1;
else if (c == '*' || c == '/') return 2;
else if (c == '^') return 3;
else if (c == '_' || c == '#') return 4;
return -1;
}