-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
351 lines (346 loc) · 8.11 KB
/
main.cpp
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
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
class fuhao
{
private:
string s1;
string s2;
public:
fuhao()
{
}
fuhao(string s1,string s2)
{
this->s1 = s1;
this->s2 = s2;
}
string getID()
{
return s1;
}
string getRESULT()
{
return s2;
}
void printALL()
{
printf("( %s , %s )",s1.c_str(),s2.c_str());
}
void input(string s1,string s2)
{
this->s1 = s1;
this->s2 = s2;
}
};
class stack
{
private:
int i;
fuhao mystack[200];
public:
stack()
{
i = -1;
}
void push(fuhao f)
{
i++;
mystack[i] = f;
}
void push(string s1,string s2)
{
i++;
mystack[i].input(s1,s2);
}
fuhao pop()
{
fuhao r("NULL","NULL");
if(i >= 0)
{
r = mystack[i];
i--;
}
else
{
printf("The stack is empty");
}
return r;
}
fuhao top()
{
fuhao r("NULL","NULL");
if(i >= 0)
{
r = mystack[i];
}
else
{
printf("The stack is empty");
}
return r;
}
bool isempty()
{
if(i <= -1)
return true;
else
return false;
}
void empty()
{
i = -1;
}
void printALL()
{
int j = i;
printf("{ ");
for(j = 0 ; j <= i ; j++)
{
printf("[%s , %s] ",mystack[j].getID().c_str(),mystack[j].getRESULT().c_str());
if(j != i)
printf(",");
}
printf(" }\n");
}
};
bool verification_num(string s);//judge the string whether is a double or not
double stod(string s);//string to double
int f(string s);
int g(string s);
void chansheng(string s);
bool ferror(string s1,string s2);
fuhao cal(fuhao s1,fuhao s2,fuhao s3);
int main()
{
FILE *input = fopen("cal","r");
stack mystack;
stack Estack;
fuhao input_data[100];
int ii = 0;
//cifa
string word = "";
char get_c;
get_c = getc(input);
do
{
if(isdigit(get_c))
{
do
{
word += get_c;
get_c = getc(input);
}
while(isdigit(get_c) || get_c == '.');
if(verification_num(word))
{
input_data[ii++].input("id",word);
}
else
printf("This is not a current double\n");
word = "";
}
else if(get_c == '+' || get_c == '-' || get_c == '*' || get_c == '/' || get_c == '(' || get_c == ')')
{
word = get_c;
input_data[ii++].input(word,word);
word = "";
get_c = getc(input);
}
else if(get_c == ' ' || get_c == '\n'){
get_c = getc(input);
}
else
{
printf("Can't understand the Vocabulary: %c\n",get_c);
break;
}
//printf("%c\n",get_c);
}
while(get_c != EOF);
input_data[ii].input("#","#");
for(int i = 0; i <=ii ; i++)
printf("%s ",input_data[i].getID().c_str());
printf("\n");
//yufa
int i = 0;
int numid = 0;
fuhao last_pop;
mystack.push("#","#");
while(!(input_data[i].getID() == "#" && mystack.top().getID() == "#"))
//for(int k = 0 ; k < 25 ; k ++)
{
if(f(mystack.top().getID()) <= g(input_data[i].getID()))
{
//printf("%d %s %d %s\n",f(mystack.top().getID()),mystack.top().getID().c_str(),g(input_data[i].getID()),input_data[i].getID().c_str());
mystack.push(input_data[i]);
//mystack.printALL();
i++;
}
else if (f(mystack.top().getID()) > g(input_data[i].getID()))
{
do
{
last_pop = mystack.pop();
//chansheng(last_pop.getID());
if(last_pop.getID() == "id"){
Estack.push(last_pop);
//Estack.printALL();
}
else if(last_pop.getID() == "+" ||last_pop.getID() == "-"
||last_pop.getID() == "*" ||last_pop.getID() == "/")
{
fuhao s1 = Estack.pop();
fuhao s2 = Estack.pop();
fuhao r = cal(s2,last_pop,s1);
Estack.push(r);
printf("%s ",last_pop.getRESULT().c_str());
s2.printALL();
s1.printALL();
r.printALL();
printf("\n");
//Estack.printALL();
}
//Estack.pop(last_pop);
//printf("%d\n",numid);
if(ferror(mystack.top().getID(),last_pop.getID()))
return 0;
//mystack.printALL();
}
while(f(mystack.top().getID()) >= g(last_pop.getID()));
}
}
fuhao result = Estack.pop();
if(!Estack.isempty())
printf("There are more OP\n");
else
{
printf("assign ");
result.printALL();
printf(" ");
result.printALL();
printf("\n");
}
return 0;
}
bool verification_num(string s)
{
int float_n = 0;
int last_point = -1;
if(s[0] == '0' && s.length() != 1 && s[1] != '.')
return false;
else
{
for(int i = 0 ; i < s.length(); i++)
{
if(s[i] == '.')
{
last_point = i;
float_n++;
}
}
if(float_n > 1 || (last_point == s.length() - 1)|| (last_point == 0))
return false;
}
return true;
}
double stod(string s)
{
double result = 0;
int shi = 1;
int flag = false;//when read the '.' the flag will be true
for(int i = 0 ;i < s.length();i++)
{
if(s[i] == '.')
flag = true;
else
{
if(flag)
shi *= 10;
result = result * 10 + (s[i] - '0');
}
}
result /= shi;
return result;
}
int f(string s)
{
if(s == "+" || s == "-")
return 2;
else if(s == "*" || s == "/")
return 4;
else if(s == "(" || s == "#")
return 0;
else if(s == ")" || s == "id")
return 6;
else
return -1;
}
int g(string s)
{
if(s == "+" || s == "-")
return 1;
else if(s == "*" || s == "/")
return 3;
else if(s == "(" || s == "id")
return 5;
else if(s == ")" || s == "#")
return 0;
else
return -1;
}
void chansheng(string s)
{
if(s == "(" || s == ")" || s =="id")
{
}
else if(s == "id")
printf("E-> id\n");
else if(s == "#")
printf("S->E\n");
else
printf("E -> E %s E\n",s.c_str());
}
bool ferror(string s1,string s2)
{
bool r = false;
if(s1 == ")" && s2 == "(")
r = true;
else if(s1 == "id" && s2 == "(")
r = true;
else if(s1 == ")" && s2 == "id")
r = true;
else if(s1 == "(" && s2 == "#")
r = true;
else if(s1 == "#" && s2 == ")")
r = true;
else
r = false;
if(r)
printf("UNEXCEPT %s behind %s!!\n",s2.c_str(),s1.c_str());
}
fuhao cal(fuhao s1,fuhao s2,fuhao s3)
{
fuhao tmp;
if(s1.getID() == "id" && s3.getID() == "id"
&& (s2.getID() == "+" || s2.getID() == "-" || s2.getID() == "*" || s2.getID() == "/"))
{
double a1 = stod(s1.getRESULT());
double a2 = stod(s3.getRESULT());
double a3;
if(s2.getID() == "+")
a3 = a1 + a2;
else if(s2.getID() == "-")
a3 = a1 - a2;
else if(s2.getID() == "*")
a3 = a1 * a2;
else if(s2.getID() == "/")
a3 = a1 / a2;
char s4[30];
sprintf(s4,"%f",a3);
//printf("%s %s %s %s\n",s1.getRESULT().c_str(),s2.getRESULT().c_str(),s3.getRESULT().c_str(),s4);
tmp.input("id",string(s4));
return tmp;
}
}