-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path224. Basic Calculator.c
138 lines (120 loc) · 3.85 KB
/
224. Basic Calculator.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
224. Basic Calculator
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23
Note: Do not use the eval built-in library function.
*/
typedef struct {
int *p;
int sz;
int n;
} s_t;
int parse(char **sp, int *k) {
char *s = *sp;
while (*s == ' ') s ++;
*k = 0;
if (*s == 0) return 0;
if (*s == '+' || *s == '-' || *s == '*' || *s == '/' || *s == '(' || *s == ')') {
*k = *s == '+' ? 1 :
*s == '-' ? 2 :
*s == '*' ? 3 :
*s == '/' ? 4 :
*s == '(' ? 5 : 6;
*sp = ++ s;
return 1;
}
while (*s >= '0' && *s <= '9') {
*k = (*k) * 10 + *s - '0';
s ++;
}
*sp = s;
return 2;
}
void push(s_t *stack, int k) {
if (stack->sz == stack->n) {
stack->sz *= 2;
stack->p = realloc(stack->p, stack->sz * sizeof(int));
//assert(stack->p);
}
stack->p[stack->n ++] = k;
}
int low_op(s_t *ops, int k) {
const int priority[] = { 0, 2, 2, 3, 3, 1, 1 }; // null, +, -, *, /, (, )
return (priority[ops->p[ops->n - 1]] >= priority[k]) ? 1 : 0;
}
int calculate(char* s) {
s_t data = { 0 }, ops = { 0 };
int x, k, d1, d2, o;
data.n = ops.n = 0;
data.sz = ops.sz = 10;
data.p = malloc(data.sz * sizeof(int));
ops.p = malloc(ops.sz * sizeof(int));
push(&data, 0); // put a zero in case of with a null input
push(&ops, 0); // put a null operator on top of operator stack
do {
x = parse(&s, &k);
if (x == 2) { // data, push to stack
push(&data, k);
} else if (k == 5) { // left parenthese, always push
push(&ops, k);
} else { // operator
while (low_op(&ops, k)) {
o = ops.p[-- ops.n];
if (o == 0 || o == 5) break; // end of input or left parenthese
d2 = data.p[-- data.n];
d1 = data.p[-- data.n];
switch (o) {
case 1: // '+'
d1 = d1 + d2;
break;
case 2: // '-'
d1 = d1 - d2;
break;
case 3: // '*'
d1 = d1 * d2;
break;
case 4: // '/'
d1 = d1 / d2;
break;
default:
break;
}
push(&data, d1);
}
if (k && k != 6) push(&ops, k); // ignore end or right parenthese
}
} while (x);
//assert(ops.n == 0);
k = data.p[data.n - 1];
free(data.p);
free(ops.p);
return k;
}
/*
Difficulty:Hard
Total Accepted:51.6K
Total Submissions:190.7K
Companies Google
Related Topics Stack Math
Similar Questions
Evaluate Reverse Polish Notation
Basic Calculator II
Different Ways to Add Parentheses
Expression Add Operators
*/