-
Notifications
You must be signed in to change notification settings - Fork 1
/
Calculator.cpp
429 lines (428 loc) · 7.52 KB
/
Calculator.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*lab_1*calculator*/
/**expression:
term
expression "+" term //addition
expression "-" term //subtraction
***term:
factorial
term "*" factorial //multiplication
term "/" factorial //division
term "%" factorial //remainder(modulo)
***primary:
number
"("expression")" //grouping
***number:
floating-point-literal
"+"number //positive
"-"number //negative
print: ;
quit: q
*/
#include <iostream>
#include <math.h>
using namespace std;
//------------------------------------------------------------------------------------------
//函数声明
double expression();
double primary();
double factorial(double);
double term();
void clean_up_mess();
//结果简æ´åŒ–
const char number = '8'; //æ•°å—
const char quit = 'q'; //退�
const char print = ';'; //输出结果
const string prompt = ">";//输入æ示
const string result = "= "; //ç‰äºŽ
extern double ANS = 0;//å˜å‚¨ä¸Šä¸€æ¬¡çš„å€?
extern int flag1 = 0;//记录'('出现次数
extern int flag2 = 0;//记录')'出现次数
//------------------------------------------------------------------------------------------
inline void error(const string& s)
{
throw runtime_error(s);
}
//------------------------------------------------------------------------------------------
//定义TokenèŽ·å¾—è¾“å…¥å…ƒç´ ä¿¡æ¯
struct Token
{
char kind;//类型
double value;//�
};
//Tokenæµ?
class Token_stream
{
public:
Token get();
void putback(Token t);
void ignore(char c);
private:
bool full{ false };
Token buffer;
};
Token_stream ts;
//放回函数,类似缓冲区
void Token_stream::putback(Token t)
{
if (full)
error("error");
buffer = t;
full = true;
}
//获å–一个Token
Token Token_stream::get()
{
if (full)
{
full = false;
return buffer;
}
char ch;
cin >> ch;
switch (ch)//è¿ç®—符ã€æ•°å—ã€ä¸åˆæ³•ç¬¦å·çš„ä¸åŒå¤„ç†æ–¹æ³?
{
//处ç†è¿ç®—ç¬?
case '(':
case ')':
case ';':
case 'q':
case '+':
case '-':
case '*':
case '/':
case '%':
case '!':
case'A':
case'N':
case'S':
return Token{ ch };
//处ç†æ•°å—
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
cin.putback(ch);
double val;
cin >> val;
return Token{ number,val };
}
//éžæ³•è¾“å…¥
default:
error("error");
return Token{};
}
}
//忽略å—符直到找到åˆé€‚çš„å—符
void Token_stream::ignore(char c)
{
if (full && c == buffer.kind)
{
full = false;
return;
}
full = false;
char ch = 0;
while (cin >> ch)
if (ch == c)
return;
}
//------------------------------------------------------------------------------------------
//clean up mess
void clean_up_mess()
{
ts.ignore(print);
}
//------------------------------------------------------------------------------------------
//expression
//处ç†+,-
double expression()
{
double left = term();
while (true)
{
Token t = ts.get();
switch (t.kind)
{
case '+':
{
left += term();
break;
}
case '-':
{
left -= term();
break;
}
case '(':
error("error");
case ')':
flag2++;//')'å‡ºçŽ°æ¬¡æ•°åŠ ä¸€
default:
ts.putback(t);
return left;
}
}
}
//term
//处ç†*,/,%
double term()
{
double left = primary();
while (true)
{
Token t = ts.get();
switch (t.kind)
{
case '*':
left *= primary();
break;
case '/':
{
double d = primary();
if (d == 0) //除数ä¸èƒ½ä¸?0
error("error");
left /= d;
break;
}
case '%':
{
double d = primary();
int i1 = int(left);
int i2 = int(d);
if (i1 != left || i2 != d || i2 == 0)//ä¸èƒ½æ˜¯å°æ•°å–模,也ä¸èƒ½å–0
error("error");
left = i1 % i2;
break;
}
default:
ts.putback(t);
return left;
}
}
}
//factorial
//处ç†é˜¶ä¹˜
double factorial(double d)
{
double num = 1;
for (int i = 1; i <= d; i++)
num *= i;
return num;
}
//primary
//处ç†æ•°å—,æ£å·è´Ÿå·,带括å·çš„,阶乘,ANS
double primary()
{
Token t = ts.get();
switch (t.kind)
{
case'A'://处ç†ANS
{
Token t1 = ts.get();
Token t2 = ts.get();
Token t3 = ts.get();
if (t1.kind != 'N' || t2.kind != 'S' || t3.kind == '(' )
{
error("error");
ts.putback(t3);
}
if (t1.kind == 'N'&& t2.kind == 'S' && t3.kind == '!')//处ç†ANSåŽé¢æ˜¯é˜¶ä¹˜çš„情况
{
Token t4 = ts.get();
if (t4.kind == '!' || t4.kind == number || t4.kind == '(' || t4.kind == ')'||t4.kind=='A'||t4.kind=='N'||t4.kind=='S')
error("error");
else
{
ts.putback(t4);
if (ANS == 0)
return 1;
else if (ANS < 0)
error("error");
else
return factorial(ANS);
}
}
else
{
ts.putback(t3);
return ANS;
}
}
case '(':
{
flag1 ++;//'('å‡ºçŽ°æ¬¡æ•°åŠ ä¸€
double d = expression();
t = ts.get();
if (t.kind != ')')
{
ts.putback(t);
error("error");
}
t = ts.get();
if (t.kind != '!')
{
ts.putback(t);
return d;
}
else
{
Token t1 = ts.get();
if (t1.kind == '!'||t1.kind == number||t1.kind=='('||t1.kind==')'||t1.kind=='A'||t1.kind=='N'||t1.kind=='S')
error("error");
else
{
ts.putback(t1);
if (d<0 || floor(d) != d)
error("error");
else if (d == 0)
return 1;
else
return factorial(d);
}
}
}
case number://处ç†æ•°å—
{
double d = t.value;
Token t1 = ts.get();
if (t1.kind == '(')
error("error");
if (t1.kind != '!')
{
ts.putback(t1);
return d;
}
else
{
Token t2 = ts.get();
if (t2.kind == '!'|| t2.kind == number||t2.kind=='('||t2.kind==')'||t2.kind=='A'||t2.kind=='N'||t2.kind=='S')
error("error");
else
{
ts.putback(t2);
if (d == 0)
return 1;
else if (floor(d) != d)
error("error");
else if (d<0)
return -factorial(-d);
else
return factorial(d);
}
}
}
case '-'://处ç†è´Ÿå·
{
double d = primary();
t = ts.get();
if (t.kind != '!')
{
ts.putback(t);
return -d;
}
else
{
Token t1 = ts.get();
if (t1.kind == '!'||t1.kind==number||t1.kind=='('||t1.kind==')'||t1.kind=='A'||t1.kind=='N'||t1.kind=='S')
error("error");
else
{
ts.putback(t1);
if (d == 0)
return 1;
else if (floor(d) != d)
error("error");
else if (d<0)
return -factorial(-d);
else
return factorial(d);
}
}
}
case '+'://处ç†æ£å·
{
double d = primary();
t = ts.get();
if (t.kind != '!')
{
ts.putback(t);
return d;
}
else
{
Token t1 = ts.get();
if (t1.kind == '!'||t1.kind == number||t1.kind=='('||t1.kind==')'||t1.kind==')'||t1.kind=='A'||t1.kind=='N'||t1.kind=='S')
error("error");
else
{
ts.putback(t1);
if (d == 0)
return 1;
else if (floor(d) != d)
error("error");
else if (d<0)
return -factorial(-d);
else
return factorial(d);
}
}
}
default:
ts.putback(t);
error("error");
return 0;
}
}
//calculate
void calculate()
{
while (cin)
try
{
cout << prompt;
Token t = ts.get();
while (t.kind == print)//输出
t = ts.get();
if (t.kind == quit)//退�
return;
ts.putback(t);
ANS = expression();//记录ANS
if (flag1 != flag2)//如果'('ã€?')'出现次数ä¸ä¸€æ ·ä¸ºéžæ³•å¼å
error("error");
else
cout << result << ANS << endl;//输出结果
flag1 = 0;//é‡ç½®æ‹¬å·å‡ºçŽ°æ¬¡æ•°
flag2 = 0;
}
catch (exception& e)
{
flag1 = 0;//é‡ç½®æ‹¬å·å‡ºçŽ°æ¬¡æ•°
flag2 = 0;
cerr << e.what() << endl;
clean_up_mess();
}
}
//------------------------------------------------------------------------------------------
//主函�
int main()
{
try
{
calculate();
return 0;
}
catch (...)
{
error("error");
return 2;
}
}
//------------------------------------------------------------------------------------------