-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcalExpression.cpp
More file actions
176 lines (165 loc) · 3.75 KB
/
calExpression.cpp
File metadata and controls
176 lines (165 loc) · 3.75 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
//实例:(5*(12-3)+4)/2=24;4+2*3-10/5=8;
#include<stdio.h>
#define MAXSIZE 30
char op[MAXSIZE], num[MAXSIZE];//全局变量
int opTop, numTop;
//void calculate(SqStack op, SqStack num){
void calculate(){
/*
SElemType op1, op2;
SElemType opf;
pop(num, op2);
pop(num, op1);
pop(op, opf);
SElemType tmpResult;
*/
char op1, op2;
int opf;
op2 = num[numTop-1];
numTop--;
op1 = num[numTop-1];
numTop--;
opf = op[opTop-1];
opTop--;
int tmpResult = 0;
switch(opf){
case '+':
tmpResult = op1 + op2;
break;
case '-':
tmpResult = op1 - op2;
break;
case '*':
tmpResult = op1 * op2;
break;
case '/':
tmpResult = op1 / op2;
break;
}
//push(num, tmpResult);
num[numTop] = tmpResult;
numTop++;
}
//处理简单表达式
void dealExpression(){
//SqStack op, num;
//initStack(op);
//initStack(num);
//初始化op栈和num栈:栈为空:top=0;栈满:top=MAXSIZE
opTop = 0, numTop = 0;
printf("请输入简单表达式(以#号结束):");
char currentChar;
scanf("%c", ¤tChar);
//printf("currentChar=%c", currentChar);
while(currentChar!='#'){
//printf("test1");
//printf("currentChar=%c", currentChar);
switch(currentChar){
case '+':
case '-':
/*
if(!isEmpty(op)){
SElemType e;
getTopElem(op, e);
while(e != '('){//op栈不为空且栈顶元素不为'(',即为'+'、'-'、'*'、'/',都应该进行运算
calculate();
}
//此时e为'(',则应入栈
push(op, currentChar);
scanf("%c", currentChar);
}*/
//printf("test4");
while(opTop&&op[opTop-1]!='('){
calculate();
}
op[opTop] = currentChar;
opTop++;
scanf("%c", ¤tChar);
//printf("currentChar=%c", currentChar);
break;
case '*':
case '/':
/*
if(!isEmpty(op)){
SElemType e;
getTopElem(op, e);
if(e == '*'||e == '/'){//则应进行计算
calculate();
}
push(op, currentChar);
scanf("%c", currentChar);
}*/
if(opTop&&(op[opTop-1]=='*'||op[opTop-1]=='/')){
calculate();
}
op[opTop] = currentChar;
opTop++;
scanf("%c", ¤tChar);
//printf("currentChar=%c", currentChar);
break;
case '(':
op[opTop] = currentChar;
opTop++;
scanf("%c", ¤tChar);
//printf("currentChar=%c", currentChar);
break;
case ')':
/*
if(!isEmpty(op)){//栈不为空
SElemType e;
getTopElem(op, e);
while(e!='('){
calculate();
}
//此时op栈顶元素e为'(',应该弹出左括号
SElemType popElem;
pop(op, popElem);
scanf("%c", currentChar);
}*/
while(op[opTop-1]!='('){//当前是'(',则op栈一定能有'('匹配到,即op栈一定不为空
calculate();
}
opTop--;//出栈,弹出左括号
scanf("%c", ¤tChar);
//printf("currentChar=%c", currentChar);
break;
default://currentChar为数字,则入num栈
//printf("testNum");
int opNum = 0;
do{
opNum = opNum*10 + currentChar-'0';
//printf("%d",opNum);
scanf("%c", ¤tChar);
//printf("%c",currentChar);
}while(currentChar>='0'&¤tChar<='9');
//push(num, opNum);
//printf("%d",opNum);
num[numTop] = opNum;
numTop++;
//scanf("%c", currentChar);
break;
}//end switch
//printf("test3");
}
//while结束,说明表达式已经输入结束,则只要op栈不为空,就应进行计算
//当op栈为空时,则弹出num栈栈顶元素即为运算结果
//SElemType e;
//getTopElem(op, e);
/*
while(isEmpty()){
calculate();
}
SElemType result;
getTopElem(op, result);
printf("计算结果是%d\n", result);
*/
while(opTop){
calculate();
}
int result = num[numTop-1];
printf("计算结果是%d\n", result);
}
int main(){
dealExpression();
return 0;
}