-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.c
217 lines (175 loc) · 4.43 KB
/
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* Copyright © 2021-2023 Chee Bin HOH. All rights reserved.
*
* A simple calculator that evaluates integer expression with support of 4
* binary operators +, -, *, / and negative unary operator '-', and also
* parenthese to override default precedence.
*
* It does not do syntax and semantic check.
*/
#include <ctype.h>
#include <stdio.h>
struct OpToken {
char op;
int opStartPos;
int opEndPos;
};
char *getNumber(char s[], int *retValue) {
int value = 0;
while (isdigit(*s)) {
value = value * 10 + (*s - '0');
s++;
}
*retValue = value;
return s;
}
char *getOperator(char s[], char *op) {
if ('+' == *s || '-' == *s || '*' == *s || '/' == *s) {
*op = *s;
s++;
}
return s;
}
char *skipWhitespace(char s[]) {
while (isspace(*s))
s++;
return s;
}
int getPrecedence(char op) {
int pos = 0;
switch (op) {
case '+':
case '-':
pos = 2;
break;
case '*':
pos = 1;
break;
default:
break;
}
return op;
}
int performBinaryOperation(int opr1, int opr2, char op) {
int value;
switch (op) {
case '*':
value = opr1 * opr2;
break;
case '/':
value = opr1 / opr2; // let native exception kick in if opr2 is zero
break;
case '+':
value = opr1 + opr2;
break;
case '-':
value = opr1 - opr2;
break;
}
return value;
}
int evaluateBinaryExpr(int number[], int *numberIndex, struct OpToken op[],
int *opIndex) {
int prevOp;
int result;
int opr1;
int opr2;
prevOp = op[--(*opIndex)].op;
switch (prevOp) {
case '+':
case '-':
opr1 = number[--(*numberIndex)];
opr2 = number[--(*numberIndex)];
result = performBinaryOperation(opr1, opr2, prevOp);
break;
case '*':
case '/':
opr1 = number[--(*numberIndex)];
opr2 = number[--(*numberIndex)];
result = performBinaryOperation(opr1, opr2, prevOp);
break;
}
number[(*numberIndex)++] = result;
return result;
}
int evaluate(char s[]) {
char *tmpP;
char *p;
int numberIndex;
int opIndex;
int precedenceIndex;
int number[100];
struct OpToken op[100];
int precedence[100];
char newOp;
int newNumber;
p = s;
numberIndex = 0;
opIndex = 0;
precedenceIndex = 0;
while ('\0' != *p) {
p = skipWhitespace(p);
tmpP = p;
if ('(' == *p) {
precedence[precedenceIndex++] = numberIndex;
p++;
} else if (')' == *p) {
int startNumberIndex = precedence[--precedenceIndex];
if (opIndex > 0) {
int loopCnt = numberIndex - startNumberIndex;
while (loopCnt >= 2) {
evaluateBinaryExpr(number, &numberIndex, op, &opIndex);
loopCnt--;
}
}
p++;
} else if ((p = getNumber(p, &newNumber)) != tmpP) {
if (opIndex > 0) {
struct OpToken top;
top = op[opIndex - 1];
if ('-' == top.op && top.opEndPos == (tmpP - s)) {
newNumber = -newNumber;
opIndex--;
}
}
number[numberIndex++] = newNumber;
} else if ((p = getOperator(p, &newOp)) != tmpP) {
if ('-' == newOp && isdigit(*p)) {
// do nothing as lookahead indicates that the negative sign (-) is
// prefix to a digit
} else if (opIndex > 0) {
int prevOp = op[opIndex - 1].op;
int prevOpPos = getPrecedence(prevOp);
int newOpPos = getPrecedence(newOp);
int opr1;
int opr2;
int result;
if (prevOpPos <= newOpPos &&
(precedenceIndex <= 0 ||
(numberIndex - precedence[precedenceIndex - 1]) >= 2)) {
evaluateBinaryExpr(number, &numberIndex, op, &opIndex);
}
}
op[opIndex].opStartPos = tmpP - s;
op[opIndex].opEndPos = op[opIndex].opStartPos +
1; // all operator are one character long now.
op[opIndex].op = newOp;
opIndex++;
}
}
while (opIndex > 0)
evaluateBinaryExpr(number, &numberIndex, op, &opIndex);
return numberIndex <= 0 ? 0 : number[numberIndex - 1];
}
int main(int argc, char *argv[]) {
char s1[] = "1 + 2 * 3";
char s2[] = "( ( 2 + 3 ) * ( 2 + 1 ) ) * 4";
char s3[] = "( 2 ) * 3";
char s4[] = "3 + -2";
char s5[] = "( 3 + ( -1 ) ) * 3";
printf("calculation of %s = %d\n", s1, evaluate(s1));
printf("calculation of %s = %d\n", s2, evaluate(s2));
printf("calculation of %s = %d\n", s3, evaluate(s3));
printf("calculation of %s = %d\n", s4, evaluate(s4));
printf("calculation of %s = %d\n", s5, evaluate(s5));
return 0;
}