Repository navigation
Expand file tree
/
Copy pathCExpression.cpp
More file actions
371 lines (334 loc) · 6.73 KB
/
Copy pathCExpression.cpp
File metadata and controls
371 lines (334 loc) · 6.73 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include<bits/stdc++.h>//可做加减乘除取模和幂运算,可检查表达式是否正确
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OF -1
#define OK 1
#define ERROR 0
#define N 0xffffff
using namespace std;
typedef double Status;
typedef double SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}CMyStack_i;
Status InitStack_i(CMyStack_i *S) //构造一个空栈
{
S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S->base) exit(OF);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
Status Push_i(CMyStack_i *S,SElemType e) //插入元素e为新的栈顶元素
{
if(S->top-S->base>=S->stacksize)
{
S->base=(SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S->base) exit(OF);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return OK;
}
Status Pop_i(CMyStack_i *S) //用e返回栈顶元素并删除栈顶元素
{
double ret;
if(S->top==S->base)
return ERROR;
ret=*--S->top;
return OK;
}
Status GetTop_i(CMyStack_i S) //返回栈顶元素
{
double ret;
if(S.top==S.base)
return ERROR;
ret=*(--S.top);
return ret;
}
Status StackEmpty_i(CMyStack_i S) //若栈为空,返回OK,否则返回ERROR
{
if(S.top==S.base)
return OK;
else
return ERROR;
}
Status StackFull_i(CMyStack_i S) //判断栈满
{
if(STACK_INIT_SIZE == (S.top-S.base))
return OK;
else
return ERROR;
}
int StackLength_i(CMyStack_i S) //返回栈的长度
{
return (S.top-S.base);
}
Status PrintElem_i(SElemType e) //visit指针函数的调用
{
printf("%d\t",e);
return OK;
}
Status StackTraverse_i(CMyStack_i S,Status(*visit)(SElemType)) //栈的遍历
{
SElemType *p;
p=S.base;
while(p!=S.top)
{
if(!visit(*p))
return ERROR;
p++;
}
return OK;
}
Status ClearStack_i(CMyStack_i *S) //置为空栈
{
S->top=S->base;
return OK;
}
Status DestroyStack_i(CMyStack_i *S) //销毁栈
{
free(S->base);
S->base=NULL;
S->top=NULL;
S->stacksize=0;
return OK;
}
///////////////---------------///////////////////
typedef struct{
char *base;
char *top;
int stacksize;
}CMyStack_c;
char InitStack_c(CMyStack_c *S) //构造一个空栈
{
S->base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!S->base) exit(OF);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
char Push_c(CMyStack_c *S,char e) //插入元素e为新的栈顶元素
{
if(S->top-S->base>=S->stacksize)
{
S->base=(char*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(char));
if(!S->base) exit(OF);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return OK;
}
char Pop_c(CMyStack_c *S) //用e返回栈顶元素并删除栈顶元素
{
char ret;
if(S->top==S->base)
return ERROR;
ret=*--S->top;
return OK;
}
char GetTop_c(CMyStack_c S) //返回栈顶元素
{
char ret;
if(S.top==S.base)
return ERROR;
ret=*(--S.top);
return ret;
}
char StackEmpty_c(CMyStack_c S) //若栈为空,返回OK,否则返回ERROR
{
if(S.top==S.base)
return OK;
else
return ERROR;
}
char StackFull_c(CMyStack_c S) //判断栈满
{
if(STACK_INIT_SIZE == (S.top-S.base))
return OK;
else
return ERROR;
}
int StackLength_c(CMyStack_c S) //返回栈的长度
{
return (S.top-S.base);
}
char PrintElem_c(char e) //visit指针函数的调用
{
printf("%c\t",e);
return OK;
}
char StackTraverse_c(CMyStack_c S,char(*visit)(char)) //栈的遍历
{
char *p;
p=S.base;
while(p!=S.top)
{
if(!visit(*p))
return ERROR;
p++;
}
return OK;
}
char ClearStack_c(CMyStack_c *S) //置为空栈
{
S->top=S->base;
return OK;
}
char DestroyStack_c(CMyStack_c *S) //销毁栈
{
free(S->base);
S->base=NULL;
S->top=NULL;
S->stacksize=0;
return OK;
}
class CExpression{
private:
CMyStack_c oper;
CMyStack_i num;
char operation[9] = {'+','-','*','/','(',')','#','^','%'};
int check[9][9] = {
{ 1, 1,-1,-1,-1, 1, 1,-1,-1},
{ 1, 1,-1,-1,-1, 1, 1,-1,-1},
{ 1, 1, 1, 1,-1, 1, 1,-1, 1},
{ 1, 1, 1, 1,-1, 1, 1,-1, 1},
{-1,-1,-1,-1,-1, 0,-2,-1,-1},
{ 1, 1, 1, 1,-2, 1, 1, 1, 1},
{-1,-1,-1,-1,-1,-2, 0,-1,-1},
{ 1, 1, 1, 1,-1, 1, 1,-1, 1},
{ 1, 1, 1, 1,-1, 1, 1,-1, 1}
};
public:
void init(){
InitStack_c(&oper);
InitStack_i(&num);
}
double operate(char ch, double a, double b){
switch(ch){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':if(b) return a/b;
else{
printf("Divide 0.\n");
return N;
}
case '%':return (int)a%(int)b;
case '^':if(b>=0)return pow(a,b);
else{
printf("error.\n");
return N;
}
default: printf("error.\n");
return N;
}
}
int cmp(char a, char b){
int x,y;
for(int i = 0; i < 9; i++){
if(operation[i] == a) {x = i;break;}
}
for(int i = 0; i < 9; i++){
if(operation[i] == b){y = i; break;}
}
return check[x][y];
}
bool Isnum(char ch){
if(ch >= '0' && ch <= '9') return true;
else return false;
}
bool opif(char ch){
if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '^') return true;
else return false;
}
int Tonum(char ch){
int p = ch - '0';
return p;
}
double GetValue(string s, int len){
while(!StackEmpty_c(oper)) Pop_c(&oper);
while(!StackEmpty_i(num)) Pop_i(&num);
Push_c(&oper,'#'),s[len] = '#';
int type = 2;
if(s[0] == '-' && Isnum(s[1])) s[0] = '0';
for(int i = 1; i < len; i++)
if(!Isnum(s[i-1]) && (s[i] =='-')) s[i] = '0';
for(int i = 0; i < len || GetTop_c(oper) != '#';){
if(Isnum(s[i])){
if(type == 0){
double temp = GetTop_i(num);
Pop_i(&num);
if(temp <= 0) Push_i(&num,temp*10 + Tonum(s[i++])*(-1));
else Push_i(&num,temp*10 + Tonum(s[i++]));
type = 0;
}
else Push_i(&num,Tonum(s[i++])),type = 0;
}
else{
if(type == 1){
if(opif(s[i])){
printf("error.\n");
return ERROR;
}
}
if(s[i] == '(') type = 1;
else type = 2;
if(StackEmpty_c(oper)){
printf("error.\n");
return ERROR;
}
else{
char ch = GetTop_c(oper);
Pop_c(&oper);
int pr = cmp(ch,s[i]);
if(pr == -2){
printf("error.\n");
return ERROR;
}
else if(pr == -1){
Push_c(&oper,ch);
Push_c(&oper,s[i++]);
continue;
}
else if(pr == 0){i++; continue;}
else{
double b = GetTop_i(num);
Pop_i(&num);
double a = GetTop_i(num);
Pop_i(&num);
double k = operate(ch,a,b);
if(k == N) return ERROR;
else Push_i(&num,k);
continue;
}
i++;
}
}
}
if(StackLength_i(num) == 1 && StackLength_c(oper) == 1) return GetTop_i(num);
else{
while(!StackEmpty_c(oper)){
Pop_c(&oper);
}
printf("error.\n");
return ERROR;
}
}
};
int main(){
int n;
scanf("%d", &n);
while(n--){
string s;
cin >> s;
CExpression ex;
ex.init();
int len = s.length();
cout << ex.GetValue(s,len) << endl;
}
}