-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.CalculateExpression.h
301 lines (241 loc) · 7.31 KB
/
function.CalculateExpression.h
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// make strOut = strIn[start] to strIn[end]
void makeStrStartEnd(char* strIn, char* strOut, int start, int end){
int i;
for (i = start; i <= end; i++) strOut[i - start] = strIn[i];
strOut[end - start + 1] = '\0';
}
// calculate expression
void calExpression(char* strIn, char* strOut, char* ans){
// case \0
if (strlen(strIn) == 0){
strcpy(strOut, "SYNTAX ERROR");
return;
}
// initialize
int length = strlen(strIn);
int countBracket = 0; // work as a stack, +1 for '(', -1 for ')'
int i;
// case + -
// divide strIn into 2 parts: left and right of the operator, then do the math
// i.g. calExpression("2*3+4-3") = addRealNum(calExpression("2*3"), calExpression("4-3"));
for (i = length - 1; i >= 0; i--){
// count bracket
if (strIn[i] == ')') countBracket++;
if (strIn[i] == '(') countBracket--;
// check + -
if (((strIn[i] == '+') || (strIn[i] == '-')) && (countBracket == 0)){
// divide strIn to 2 parts: strIn1 and strIn2
char strIn1[MAX_LENGTH], strIn2[MAX_LENGTH], strOut1[MAX_LENGTH], strOut2[MAX_LENGTH];
makeStrStartEnd(strIn, strIn1, 0, i - 1);
makeStrStartEnd(strIn, strIn2, i + 1, length - 1);
// calculate strIn1 and strIn2
calExpression(strIn1, strOut1, ans); if(strIn1[0] == '\0') strcpy(strOut1, "0");
calExpression(strIn2, strOut2, ans); // strOut1 and strOut2 are numbers
// check for MATH ERROR or SYNTAX ERROR
if ((strOut1[0] == 'M') || (strOut2[0] == 'M')){
strcpy(strOut, "MATH ERROR!");
return;
}
if ((strOut1[0] == 'S') || (strOut2[0] == 'S')){
strcpy(strOut, "SYNTAX ERROR!");
return;
}
// calculate strIn
if (strIn[i] == '+') addRealNum(strOut1, strOut2, strOut);
else subtractRealNum(strOut1, strOut2, strOut);
// exit
return;
}
}
// case x /
for (i = length - 1; i >= 0; i--){
// count bracket
if (strIn[i] == ')') countBracket++;
if (strIn[i] == '(') countBracket--;
// check x /
if (((strIn[i] == 'x') || (strIn[i] == '/')) && (countBracket == 0)){
// divide strIn into 2 parts: strIn1 and strIn2
char strIn1[MAX_LENGTH], strIn2[MAX_LENGTH], strOut1[MAX_LENGTH], strOut2[MAX_LENGTH];
makeStrStartEnd(strIn, strIn1, 0, i - 1);
makeStrStartEnd(strIn, strIn2, i + 1, length - 1);
// calculate strIn1 and strIn2
calExpression(strIn1, strOut1, ans);
calExpression(strIn2, strOut2, ans);
// check for MATH ERROR or SYNTAX ERROR
if ((strOut1[0] == 'M') || (strOut2[0] == 'M')){
strcpy(strOut, "MATH ERROR!");
return;
}
if ((strOut1[0] == 'S') || (strOut2[0] == 'S')){
strcpy(strOut, "SYNTAX ERROR!");
return;
}
// calculate strIn
if (strIn[i] == 'x') multiplyRealNum(strOut1, strOut2, strOut);
else divideRealNum(strOut1, strOut2, strOut);
// exit
return;
}
}
// case ^
for (i = 0; i <= length - 1; i++){
// count bracket
if (strIn[i] == '(') countBracket++;
if (strIn[i] == ')') countBracket--;
// check ^
if ((strIn[i] == '^') && (countBracket == 0)){
// divide strIn to 2 parts: strIn1 and strIn2
char strIn1[MAX_LENGTH], strIn2[MAX_LENGTH], strOut1[MAX_LENGTH], strOut2[MAX_LENGTH];
makeStrStartEnd(strIn, strIn1, 0, i - 1);
makeStrStartEnd(strIn, strIn2, i + 1, length - 1);
// calculate strIn1 and strIn2
calExpression(strIn1, strOut1, ans);
calExpression(strIn2, strOut2, ans);
// check for MATH ERROR or SYNTAX ERROR
if ((strOut1[0] == 'M') || (strOut2[0] == 'M')){
strcpy(strOut, "MATH ERROR!");
return;
}
if ((strOut1[0] == 'S') || (strOut2[0] == 'S')){
strcpy(strOut, "SYNTAX ERROR!");
return;
}
// calculate strIn
powerRealNum(strOut1, strOut2, strOut);
// exit
return;
}
}
// case !
if (strIn[length - 1] == '!'){
// make temporary string
char str[MAX_LENGTH], strOut1[MAX_LENGTH];
makeStrStartEnd(strIn, str, 0, length - 2);
// calculate the number to calculate factorial
calExpression(str, strOut1, ans);
// check for MATH ERROR or SYNTAX ERROR
if (strOut1[0] == 'M'){
strcpy(strOut, "MATH ERROR!");
return;
}
if (strOut1[0] == 'S'){
strcpy(strOut, "SYNTAX ERROR!");
return;
}
// calculate strIn
getFactorial(strOut1, strOut);
// exit
return;
}
// case sqrt()
if (strIn[0] == -5){
// make temporary string
char str[MAX_LENGTH], strOut1[MAX_LENGTH], strOut2[MAX_LENGTH];
makeStrStartEnd(strIn, str, 1, length - 1);
// calculate the number to calculate square root
calExpression(str, strOut1, ans);
strcpy(strOut2, "0.5");
// check for MATH ERROR or SYNTAX ERROR
if (strOut1[0] == 'M'){
strcpy(strOut, "MATH ERROR!");
return;
}
if (strOut1[0] == 'S'){
strcpy(strOut, "SYNTAX ERROR!");
return;
}
// calculate strIn
powerRealNum(strOut1, strOut2, strOut);
// exit
return;
}
// case sin() cos() tan() cot() ln() log() abs()
int sincos = 0;
if (strstr(strIn, "sin") == strIn) sincos = 1;
if (strstr(strIn, "cos") == strIn) sincos = 2;
if (strstr(strIn, "tan") == strIn) sincos = 3;
if (strstr(strIn, "cot") == strIn) sincos = 4;
if (strstr(strIn, "ln") == strIn) sincos = 5;
if (strstr(strIn, "log") == strIn) sincos = 6;
if (strstr(strIn, "abs") == strIn) sincos = 7;
if (sincos != 0){
// make temporary string
char str[MAX_LENGTH], strOut1[MAX_LENGTH];
makeStrStartEnd(strIn, str, (sincos == 5) ? 2 : 3, length - 1);
// calculate the number to calculate the function
calExpression(str, strOut1, ans);
// check for MATH ERROR or SYNTAX ERROR
if (strOut1[0] == 'M'){
strcpy(strOut, "MATH ERROR!");
return;
}
if (strOut1[0] == 'S'){
strcpy(strOut, "SYNTAX ERROR!");
return;
}
// calculate strIn
switch (sincos){
case 1: getSinRadian(strOut1, strOut); break;
case 2: getCosRadian(strOut1, strOut); break;
case 3: getTanRadian(strOut1, strOut); break;
case 4: getCotRadian(strOut1, strOut); break;
case 5: getNaturalLog(strOut1, strOut); break;
case 6: getLog10(strOut1, strOut); break;
case 7:
strcpy(strOut, strOut1);
getAbs(strOut);
break;
default: break;
}
// exit
return;
}
// case ( )
if ((strIn[0] == '(') && (strIn[length - 1] == ')')){
// make temporary string
char str[MAX_LENGTH];
makeStrStartEnd(strIn, str, 1, length - 2);
// proceed calculating
calExpression(str, strOut, ans);
// exit
return;
}
// case e
if (strcmp(strIn, "e") == 0){
strcpy(strOut, "2.7182818284590452353602874713527");
return;
}
// case pi
if ((strIn[0] == -29) && (length == 1)){
strcpy(strOut, "3.1415926535897932384626433832795");
return;
}
// case ans
if (strcmp(strIn, "ans") == 0){
strcpy(strOut, ans);
return;
}
// case number
if ((strIn[0] == '-') || ((strIn[0] >= '0') && (strIn[0] <= '9'))){
// check '-'
int minus = 0;
if (strIn[0] == '-') minus = 1;
for (i = 1; i <= length - 1; i++) if (strIn[i] == '-') goto syntaxError;
// check '.'
int dot = 0;
for (i = minus; i <= length - 1; i++)
if (strIn[i] == '.'){
if ((i == minus) || (i == length - 1) || (dot == 1)) goto syntaxError;
dot++;
}
// check ['0', '9']
for (i = 0; i <= length - 1; i++)
if (!(((strIn[i] >= '0') && (strIn[i] <= '9')) || (strIn[i] == '.') || (strIn[i] == '-'))) goto syntaxError;
// return number
strcpy(strOut, strIn);
return;
}
// SYNTAX ERROR
syntaxError:
strcpy(strOut, "SYNTAX ERROR!");
}