-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.cpp
More file actions
463 lines (434 loc) · 10.2 KB
/
Calculator.cpp
File metadata and controls
463 lines (434 loc) · 10.2 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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include<iostream>
using namespace std;
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include"Complex.h"
#include"Fraction.h"
#include"Calculator.h"
struct Op * Calculator::create_stack(int max, enum Optype type)
{
struct Op *op;
op = (Op*)malloc(sizeof(*op));
if (op == NULL)
nullerr();
op->era = (Era*)calloc(sizeof(union Era), max);
if (op->era == NULL)
nullerr();
op->max = max;
op->top = EMPTY;
op->type = type;
return op;
}
void Calculator::push(union Era era, struct Op *op)
{
if (op->top >= op->max) {
if (op->type == ND)
printf("error: stack full, can't push %g\n", era.nd);
else
printf("error: stack full, can't push %c\n", era.tor);
}
else
op->era[op->top++] = era;
}
union Era Calculator::pop(struct Op *op)
{
if (op->top > EMPTY)
return op->era[--op->top];
else {
// printf("error: stack empty\n");////////////////////////////////////////////////////
if (op->type == ND)
op->era[EMPTY].nd = 0.0;
else
op->era[EMPTY].tor = 0;
return op->era[EMPTY];
}
}
void Calculator::nullerr(void)
{
printf("not enough memory\n");
exit(1);
}
int Calculator::getop(char s[]) // [K&R] section 4.3
{
int i, c;
while ((s[0] = c = getchar()) == ' ' || c == '\t')
//do nothing
s[1] = '\0';
if (!isdigit(c) && c != '.')
return c; // not a number
i = 0;
if (isdigit(c)) // collect integer part
while (isdigit(s[++i] = c = getchar()))
;
if (c == '.') // collect fraction part
while (isdigit(s[++i] = c = getchar()))
//do nothing
s[i] = '\0';
if (c != EOF)
ungetc(c, stdin);
return NUMBER;
}
double Calculator::operate(struct Op *opnd, struct Op *optor)
{
union Era nd1, nd2, tor;
nd2 = pop(opnd);
nd1 = pop(opnd);
tor = pop(optor);
switch (tor.tor) {
case '+':
return nd1.nd + nd2.nd;
break;
case '-':
return nd1.nd - nd2.nd;
break;
case '*':
return nd1.nd * nd2.nd;
break;
case '/':
return nd1.nd / nd2.nd;
break;
default:
return 0.0;
break;
}
}
void Calculator::print_stack(struct Op *op)
{
int i;
for (i = op->top; i > 0; i--)
printf("%g\n", op->era[i].nd);
}
char Calculator::top_tor(struct Op *op)
{
return op->era[op->top-1].tor; // after push, top plus one
}
void Calculator::display()
{
system("CLS");
opnd = create_stack(MAXOP, ND);
optor = create_stack(MAXOP, TOR);
cout<<" 本程序可进行“加减乘除(+ - * /)”四则混合运算。"<<endl;
cout<<" 输入 q 退出程序。"<<endl;
cout<<" 输入算式后按回车即可算出答案:"<<endl;
while ((c = getop(s)) != EOF)
{
switch (c)
{
case NUMBER:
if (opnd->top > optor->top + 1)
{ // 如果是逆波兰算式
cout<<"\tparse error\n";
while (getchar() != '\n')
/*void*/;
opnd->top = EMPTY;
era.nd = 0.0;
}
else
era.nd = atof(s);
push(era, opnd);
break;
case '+': // + 和 - 优先级最低,把除了 '(' 以外的全部操作符弹出后入栈
case '-':
if (optor->top == EMPTY)
{
era.tor = c;
push(era, optor);
}
else
{
while (optor->top > EMPTY)
if (top_tor(optor) == '(')
break;
else
{
era.nd = operate(opnd, optor);
push(era, opnd);
}
era.tor = c;
push(era, optor);
}
break;
case '*': // 乘除优先级最高,把相同优先级的乘和除弹出后入栈
case '/':
if (optor->top == EMPTY)
{
era.tor = c;
push(era, optor);
}
else
{
while (optor->top > EMPTY)
if (((era.tor = top_tor(optor)) == '+')
|| (era.tor == '-')
|| (era.tor == '('))
break;
else
{
era.nd = operate(opnd, optor);
push(era, opnd);
}
era.tor = c;
push(era, optor);
}
break;
case '(': // '(' 不作运算,直接放入optor栈。
era.tor = c;
push(era, optor);
break;
case ')': // 把 '(' 之前的全部操作符弹出,计算后把 '(' 也弹出
while (optor->top > EMPTY)
if (top_tor(optor) == '(')
break;
else
{
era.nd = operate(opnd, optor);
push(era, opnd);
}
pop(optor);
break;
case '\n': // 把余下的操作符全部弹出,计算后输出结果
if (opnd->top == EMPTY)
; //do nothing
else
while (optor->top > EMPTY)
if (top_tor(optor) == '(')
pop(optor);
else
{
era.nd = operate(opnd, optor);
push(era, opnd);
}
era = pop(opnd);
cout<<"\tans="<<era.nd<<endl;
break;
case 'p':
print_stack(opnd);
getchar();
break;
case 'q':
exit(0);
break;
default:
cout<<"error: unknown command"<<endl;
break;
}
}
}
/*void Calculator::set_complex()
{
a.set();
b.set();
}
void Calculator::display_complex()
{
system("CLS");
int sel=-1;
while(sel!=0)
{
system("CLS");
cout<<"\n\n\n";
cout<<"\t\t\t请输入你的选择:\n"<<endl;
cout<<"\t\t\t输入1进行复数加\n"<<endl;
cout<<"\t\t\t输入2进行复数减\n"<<endl;
cout<<"\t\t\t输入3进行复数乘\n"<<endl;
cout<<"\t\t\t输入4进行复数除\n"<<endl;
cout<<"\t\t\t输入0退出"<<endl;
cin>>sel;
if(sel!=0)
{
set_complex();
}
Complex c;
switch (sel)
{
case 1:
c=a+b;
cout<<"结果为:"<<a<<"+"<<b<<"="<<c<<endl;
system("pause");
cout<<endl;
break;
case 2:
c=a-b;
cout<<"结果为:"<<a<<"-"<<b<<"="<<c<<endl;
system("pause");
cout<<endl;
break;
case 3:
c=a*b;
cout<<"结果为:"<<a<<"*"<<b<<"="<<c<<endl;
system("pause");
cout<<endl;
break;
case 4:
c=a/b;
cout<<"结果为:"<<a<<"/"<<b<<"="<<c<<endl;
system("pause");
cout<<endl;
break;
case 0:
cout<<"退出"<<endl;
exit(0);
break;
default:
cout<<"请输入正确的函数!"<<endl;
break;
}
}
}*/
void Calculator::set_fraction()
{
cout<<"分数a:"<<endl;
a1.set();
cout<<"分数b:"<<endl;
b1.set();
}
void Calculator::display_fraction()
{
system("CLS");
int sel=-1;
while(sel!=0)
{
system("CLS");
cout<<"\n\n\n";
cout<<"\t\t\t请输入你的选择:\n"<<endl;
cout<<"\t\t\t输入1进行分数加\n"<<endl;
cout<<"\t\t\t输入2进行分数减\n"<<endl;
cout<<"\t\t\t输入3进行分数乘\n"<<endl;
cout<<"\t\t\t输入4进行分数除\n"<<endl;
cout<<"\t\t\t输入0退出"<<endl;
cin>>sel;
if(sel!=0)
{
set_fraction();
}
Fraction c;
switch (sel)
{
case 1:
c=a1+b1;
cout<<"结果为:"<<"("<<a1<<")"<<"+"<<"("<<b1<<")"<<"="<<c<<endl;
system("pause");
cout<<endl;
break;
case 2:
c=a1-b1;
cout<<"结果为:"<<"("<<a1<<")"<<"-"<<"("<<b1<<")"<<"="<<c<<endl;
system("pause");
cout<<endl;
break;
case 3:
c=a1*b1;
cout<<"结果为:"<<"("<<a1<<")"<<"*"<<"("<<b1<<")"<<"="<<c<<endl;
system("pause");
cout<<endl;
break;
case 4:
c=a1/b1;
cout<<"结果为:"<<"("<<a1<<")"<<"/"<<"("<<b1<<")"<<"="<<c<<endl;
system("pause");
cout<<endl;
break;
case 0:
cout<<"退出"<<endl;
exit(0);
break;
default:
cout<<"请输入正确的数!"<<endl;
break;
}
}
}
const Calculator operator+( const Calculator& add1, const Calculator& add2)
{
Calculator temp(add1.rpart+add2.rpart,add1.ipart+add2.ipart);
return temp;
}
const Calculator operator-(const Calculator &sub1, const Calculator &sub2)
{
Calculator temp(sub1.rpart-sub2.rpart,sub1.ipart-sub2.ipart);
return temp;
}
const Calculator operator*( const Calculator &c1, const Calculator &c2)
{
Calculator temp((c1.rpart*c2.rpart-c1.ipart*c2.ipart),(c1.rpart*c2.ipart+c1.ipart*c2.rpart));
return temp;
}
const Calculator operator/(const Calculator &c1,const Calculator &c2)
{
Calculator temp( (c1.rpart*c2.rpart+c1.ipart*c2.ipart)/(c2.rpart*c2.rpart+c2.ipart*c2.ipart), (c1.ipart*c2.rpart-c1.rpart*c2.ipart)/(c2.rpart*c2.rpart+c2.ipart*c2.ipart));
return temp;
}
ostream& operator<<(ostream& os, const Calculator& com)
{
os << "(" << com.rpart;
if(com.ipart)
{
os << "," << com.ipart << "i";
}
os << ")" ;
return os;
}
istream& operator>>(istream& is, Calculator& com)
{
is >> com.rpart >>com.ipart;
return is;
}
void display()
{
Calculator a,b;
system("CLS");
int sel=-1;
while(sel!=0)
{
system("CLS");
cout<<"\n\n\n";
cout<<"\t\t\t请输入你的选择:\n"<<endl;
cout<<"\t\t\t输入1进行复数加\n"<<endl;
cout<<"\t\t\t输入2进行复数减\n"<<endl;
cout<<"\t\t\t输入3进行复数乘\n"<<endl;
cout<<"\t\t\t输入4进行复数除\n"<<endl;
cout<<"\t\t\t输入0退出"<<endl;
cin>>sel;
if(sel!=0)
{
a.set();
b.set();
}
Complex c;
switch (sel)
{
case 1:
c=a+b;
cout<<"结果为:"<<a<<"+"<<b<<"="<<c<<endl;
system("pause");
cout<<endl;
break;
case 2:
c=a-b;
cout<<"结果为:"<<a<<"-"<<b<<"="<<c<<endl;
system("pause");
cout<<endl;
break;
case 3:
c=a*b;
cout<<"结果为:"<<a<<"*"<<b<<"="<<c<<endl;
system("pause");
cout<<endl;
break;
case 4:
c=a/b;
cout<<"结果为:"<<a<<"/"<<b<<"="<<c<<endl;
system("pause");
cout<<endl;
break;
case 0:
cout<<"退出"<<endl;
exit(0);
break;
default:
cout<<"请输入正确的函数!"<<endl;
break;
}
}
}