forked from VladHlushchyk/Math-Calculator-DIY
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
87 lines (73 loc) · 1.66 KB
/
main.c
File metadata and controls
87 lines (73 loc) · 1.66 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <string.h>
typedef float (*Solver_t)(float a, float b);
float add(float a, float b) { return a + b; }
float sub(float a, float b) { return a - b; }
float mul(float a, float b) { return a * b; }
float div(float a, float b) { return a / b; }
Solver_t Eval[256] = {
['+'] = add,
['-'] = sub,
['*'] = mul,
['/'] = div
};
typedef enum { INVALID, CHAR, FLOAT } toktype_t;
typedef struct {
toktype_t type;
union {
char c;
float f;
} val;
} Token;
toktype_t GetToken(Token *t) {
int c;
do { c = getchar(); } while (c == ' ' || c == '\n');
if (c == EOF) { t->type = INVALID; return INVALID; }
if (c >= '0' && c <= '9') {
float val = 0;
do {
val = val*10 + (c-'0');
c = getchar();
} while (c >= '0' && c <= '9');
ungetc(c, stdin);
t->type = FLOAT;
t->val.f = val;
return FLOAT;
}
t->type = CHAR;
t->val.c = c;
return CHAR;
}
float BinaryOp(Token *t, const char *ops);
float Term(Token *t) { return BinaryOp(t, "*/"); }
float Expr(Token *t) { return BinaryOp(t, "+-"); }
float Factor(Token *t) {
if (t->type == CHAR && t->val.c == '(') {
GetToken(t);
float res = Expr(t);
if (t->type == CHAR && t->val.c == ')') GetToken(t);
return res;
}
if (t->type == FLOAT) {
float res = t->val.f;
GetToken(t);
return res;
}
return 0.0f;
}
float BinaryOp(Token *t, const char *ops) {
float res = Factor(t);
while (t->type == CHAR && strchr(ops, t->val.c)) {
char op = t->val.c;
GetToken(t);
float rhs = (ops[0]=='+'||ops[0]=='-') ? Term(t) : Factor(t);
res = Eval[(unsigned char)op](res, rhs);
}
return res;
}
int main(void) {
Token t;
GetToken(&t);
float result = Expr(&t);
printf("result = %f\n", result);
}