-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpression evaluation.txt
200 lines (185 loc) · 3.78 KB
/
Expression evaluation.txt
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
#include <stdio.h>
#include<malloc.h>
#include<math.h>
typedef int ElemType;
typedef char SElemType;
#define maxsize 30
#define Maxsize 30
typedef struct {
ElemType* base;
ElemType* top;
int size;
}SqStackInt, * ListStackInt;
typedef struct {
SElemType* base;
SElemType* top;
int size;
}SqStack, * ListStack;
void StackInitInt(ListStackInt& S) {
S = (ListStackInt)malloc(sizeof(SqStackInt));
S->base = (ElemType*)malloc(maxsize * sizeof(ElemType));
S->top = S->base;
S->size = maxsize;
}
void StackPushInt(ListStackInt S, ElemType e) {
if (S->base == S->top - S->size)
{
return;
}
*S->top++ = e;
}
ElemType StackPopInt(ListStackInt S) {
if (S->base == S->top) return 0;
ElemType e;
e = *(--S->top);
return e;
}
void StackDisplayInt(ListStackInt S) {
for (ElemType* i = S->base; i < S->top; i++)
{
printf("%d ", *i); //字符修改
}
}
ElemType StackGetInt(ListStackInt S) {
if (S->base == S->top) { return 0; }
else { return *(S->top - 1); }
}
void StackInit(ListStack& S) {
S = (ListStack)malloc(sizeof(SqStack));
S->base = (SElemType*)malloc(Maxsize * sizeof(SElemType));
S->top = S->base;
S->size = Maxsize;
}
void StackPush(ListStack S, SElemType e) {
if (S->base == S->top - S->size)
{
return;
}
*S->top++ = e;
}
SElemType StackPop(ListStack S) {
if (S->base == S->top) return 0;
SElemType e;
e = *(--S->top);
return e;
}
void StackDisplay(ListStack S) {
for (SElemType* i = S->base; i < S->top; i++)
{
printf("%c ", *i);
}
}
SElemType StackGet(ListStack S) {
if (S->base == S->top) { return 0; }
else { return *(S->top - 1); }
}
int IfDigtal(SElemType s) {
switch (s)
{
case '0': return 0;break;
case '1': return 1;break;
case '2': return 2;break;
case '3': return 3;break;
case '4': return 4;break;
case '5': return 5;break;
case '6': return 6;break;
case '7': return 7;break;
case '8': return 8;break;
case '9': return 9;break;
default: return 10; break;
}
}
int contrast(char symbol) {
switch (symbol)
{
case '+':return 1;break;
case '-':return 2;break;
case '*':return 3;break;
case '/':return 4;break;
case '(':return 5;break;
case ')':return 6;break;
case '=':return 7;break;
default:return 0;break;
}
}
int compare(char Symboltop, char Symboljoin) {
int topdata;
int joindata;
int prior[8][8] = { {0,0,0,0,0,0,4,0},
{0,1,1,0,0,0,1,1},
{0,1,1,0,0,0,1,1},
{0,1,1,1,1,0,1,1},
{0,1,1,1,1,0,1,1},
{0,0,0,0,0,0,3,4},
{0,1,1,1,1,4,1,1},
{0,4,4,4,4,4,4,4} };
topdata = contrast(Symboltop);
joindata = contrast(Symboljoin);
return prior[topdata][joindata];
}
int get(SElemType** Formula, int n) {
int data = 0;
int count = 0;
while (IfDigtal(**Formula)!=10)
{
(*Formula)++;
count++;
}
for ( int j = count; j >0; j--)
{
(*Formula)--;
}
for (count; count>0; count--)
{
data += IfDigtal (**Formula)* pow(10, count - 1);
(*Formula)++;
}
return data;
}
int calculation(int data1, int data2, char data3) {
switch (data3)
{
case'+':return data1 + data2;break;
case'-':return data1 - data2;break;
case'*':return data1 * data2;break;
case'/':return data1 / data2;break;
default:
break;
}
}
void Computation(SElemType *Formula,int n){
ListStack C;StackInit(C);ListStackInt S;StackInitInt(S);
while (*Formula!='\0')
{
if (IfDigtal(*Formula)!=10)
{
StackPushInt(S, get(&Formula,n));
}
else if(IfDigtal(*Formula) == 10)
{
switch (compare(StackGet(C), *Formula))
{
case 0:
StackPush(C, *Formula);
Formula++;
break;
case 1:
StackPushInt(S, calculation(StackPopInt(S), StackPopInt(S), StackPop(C)));break;
case 3:
StackPop(C);
Formula++;
break;
}
}
StackDisplay(C);
printf("\n");
StackDisplayInt(S);
printf("\n\n");
}
StackDisplayInt(S);
}
int main(){
char *data = "(12+12)*12-1/15=";
Computation(data, 20);
return 0;
}