-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path202002523_Calculator.cpp
More file actions
535 lines (507 loc) · 16.8 KB
/
202002523_Calculator.cpp
File metadata and controls
535 lines (507 loc) · 16.8 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include <iostream>
#include <string>
#include <stack>
#include <vector>
using namespace std;
//대충 주석
//
// ��Ŭ���� Number String�� ������� �ϴ� 10����, 16����, 2���� ��ü
//
class NumberString {
protected:
int num;
public:
// �Է¹��� ���ڿ��� ������ ������� 10������ ����
virtual void setNum(string numstr) = 0;
// ����� ���ڸ� ���
virtual int getNum() = 0;
};
//10���� Ŭ����
class DecNumberString : public NumberString {
public:
DecNumberString(string numstr) {
this->setNum(numstr);
}
void setNum(string numstr) {
this->num = stoi(numstr);
}
int getNum() {
return this->num;
}
};
//16���� Ŭ����
class HexNumberString : public NumberString {
public:
HexNumberString(string numstr) {
this->setNum(numstr);
}
void setNum(string numstr) {
this->num = stoi(numstr.substr(2, numstr.length() - 2), nullptr, 16);
}
int getNum() {
return this->num;
}
};
//2���� Ŭ����
class BinNumberString : public NumberString {
public:
BinNumberString(string numstr) {
this->setNum(numstr);
}
void setNum(string numstr) {
this->num = stoi(numstr.substr(2,numstr.length()-2),nullptr,2);
}
int getNum() {
return this->num;
}
};
//
// +, -, *, /, ( �� ���� ǥ������� ��ȯ�ϱ� ���� ���ؿ� ����� ��ü�� �����ϴ� Ŭ����
// operator�� �켱������ �� ���꿡 ���� �ٸ� ����� ������.
//
//��Ŭ���� Oper
class Oper {
protected:
//�켱����
int prior;
public:
//�����ڸ� ���ڿ��� ���
virtual string getOper() = 0;
//�켱���� �� ���� ���
virtual int getPrior() = 0;
// �Է¹� ���� 2���� ����
virtual int calculrate(int a, int b) = 0;
};
// ���� Ŭ����
class AddOper : public Oper {
public:
AddOper() {
this->prior = 2;
// ����/������ ����/���������� �ʰ� ����ǹǷ� �߰� �켱���� 2
}
string getOper() {
return "+";
}
int getPrior() {
return this->prior;
}
int calculrate(int a, int b) {
return a + b;
}
};
// ���� Ŭ����
class SubOper : public Oper {
public:
SubOper() {
this->prior = 2;
// ����/������ ����/���������� �ʰ� ����ǹǷ� �߰� �켱���� 2
}
string getOper() {
return "-";
}
int getPrior() {
return this->prior;
}
int calculrate(int a, int b) {
return a - b;
}
};
// ���� Ŭ����
class MulOper : public Oper {
public:
MulOper() {
this->prior = 3;
// ����/�������� ����/�������� ���� ����ǹǷ� ����/�������� �� ���� �켱���� 3
}
string getOper() {
return "*";
}
int getPrior() {
return this->prior;
}
int calculrate(int a, int b) {
return a * b;
}
};
// ������ Ŭ����
class DivOper : public Oper {
public:
DivOper() {
this->prior = 3;
// ����/�������� ����/�������� ���� ����ǹǷ� ����/�������� �� ���� �켱���� 1
}
string getOper() {
return "/";
}
int getPrior() {
return this->prior;
}
int calculrate(int a, int b) {
return a / b;
}
};
// ���� ��ȣ Ŭ����
class BraOper : public Oper {
public:
BraOper() {
this->prior = 1;
// ���� ǥ������� ��ȯ�ϱ� �վ� ��ȣ�� �ݴ� ��ȣ�� ���ö����� pop�Ǹ� �ȵǹǷ� ���� ���� �켱����
}
string getOper() {
return NULL; // ���� ǥ������� ��ȣ�� ������ �����Ƿ� �ʱ�ȭ�� ��.
}
int getPrior() {
return this->prior;
}
int calculrate(int a, int b) {
return 0; // ���� ǥ��� ��꿡�� ��ȣ�� ������ �����Ƿ� �ʱ�ȭ�� ��.
}
};
//
// ��Ŭ���� Oper�� �����Ǵ� �Լ��� ����߷� ���� �� �ֵ��� �ٸ� ������ ���� Ŭ����
// ��, �ѹ��� ��ü�� �����ϰ� �����ڴ� ���������� �ٲٱ� ���� �Ű������� �ƿ´�.
//
class Calculate {
protected:
Oper *oper;
public:
Calculate(Oper *oper) {
this->oper = oper;
}
virtual int operCalculate(int b, int a, Oper* oper) = 0;
};
//������ ������ 2���� ���ڸ� �����ϸ� ������ Ȯ���� �ô´�.
class Calculator : public Calculate {
private:
int numA, numB;
public:
Calculator(int numA, int numB, Oper *oper) : Calculate(oper) {
this->numA = numA;
this->numB = numB;
}
int operCalculate(int b, int a, Oper* oper) {
this->numA = a;
this->numB = b;
return oper->calculrate(numA, numB);
}
};
//
// ������ �ùٸ��� Ȯ���ϸ� splitString�� �߶� ����
// ����ó���� ����ϵ� ���ü� �ִ� ������ ������ �Ʒ��� ���� �����Ͽ���.
// ��ȣ ���� / ���� ��ø / �ùٸ��� ���� ���� / �������� ����/����(+2 or 2-) /
//
bool IsValid(string aString, vector<string> *splitString) {
//�ƿ� ���ڿ����� ���⸸ �����Ѵ�.
aString.erase(remove(aString.begin(), aString.end(), ' '), aString.end());
int strLength = aString.length();
int nestingOper = 0; // ���� ��ø Ȯ�ο�, ��� ������ 2��ø�� ���� ��
int invalidBracket = 0; // ��ȣ ���� Ȯ�ο�, ������ �������ų� ��ü �۾��� �������� 0�� �ƴϸ� ���� ��
try {
int i = 0;
if (aString[i] == '*' || aString[i] == '/' || aString[i] == '+' || aString[i] == '-') {
throw string("�������� ����");
}
for (i; i < strLength; i++) {
//��ȣ ó��
if (aString[i] == '(') {
invalidBracket++;
if (aString[i + 1] == '*' || aString[i + 1] == '/' || aString[i + 1] == '+' || aString[i + 1] == '-') {
throw string("�������� ����");
}
splitString->push_back(aString.substr(i,1));
}
else if (aString[i] == ')') {
if (--invalidBracket < 0) {
throw string("��ȣ ����");
}
else {
if (aString[i - 1] == '*' || aString[i - 1] == '/' || aString[i - 1] == '+' || aString[i - 1] == '-') {
throw string("�������� ����");
}
}
splitString->push_back(aString.substr(i,1));
}
//��Ģ ���� ó��
else if (aString[i] == '*' || aString[i] == '/' || aString[i] == '+' || aString[i] == '-') {
if (++nestingOper >= 2) {
throw string("���� ��ø");
}
splitString->push_back(aString.substr(i,1));
}
//2����, 16���� ó��
else if (aString[i] == '0') {
int j = 1;
if (aString[i + j] == 'x') { // 16���� ó��
nestingOper = 0;
j = 2;
while (1) {
if ((aString[i + j] >= '0' && aString[i + j] <= '9') || (aString[i + j] >= 'A' && aString[i + j] <= 'F')) {
j++;
}
else if(aString[i + j] == ')' || aString[i + j] == '*' || aString[i + j] == '/' || aString[i + j] == '+' || aString[i + j] == '-' || aString[i + j] == NULL){
splitString->push_back(aString.substr(i, j));
i += j - 1;
break;
}
else {
throw string("�ùٸ��� ���� ���� or ��ȣ");
}
}
}
else if (aString[i + j] == 'b') { // 2���� ó��
nestingOper = 0;
j = 2;
while (1) {
if (aString[i + j] >= '0' && aString[i + j] <= '1') {
j++;
}
else if (aString[i + j] == ')' || aString[i + j] == '*' || aString[i + j] == '/' || aString[i + j] == '+' || aString[i + j] == '-' || aString[i + j] == NULL) {
splitString->push_back(aString.substr(i, j));
i += j - 1;
break;
}
else {
throw string("�ùٸ��� ���� ���� or ��ȣ");
}
}
}
else { //0���� �����ϴ� ���� ó��
nestingOper = 0;
while (1) {
if (aString[i + j] >= '0' && aString[i + j] <= '9') {
j++;
}
else if (aString[i + j] == ')' || aString[i + j] == '*' || aString[i + j] == '/' || aString[i + j] == '+' || aString[i + j] == '-' || aString[i + j] == NULL) {
splitString->push_back(aString.substr(i, j));
i += j - 1;
break;
}
else {
throw string("�ùٸ��� ���� ���� or ��ȣ");
}
}
}
}//10���� ó��
else if (aString[i] >= '0' && aString[i] <= '9') {
nestingOper = 0;
int j = 1;
while (1) {
if (aString[i + j] >= '0' && aString[i + j] <= '9') {
j++;
}
else if (aString[i + j] == ')' || aString[i + j] == '*' || aString[i + j] == '/' || aString[i + j] == '+' || aString[i + j] == '-' || aString[i + j] == NULL) {
splitString->push_back(aString.substr(i, j));
i += j - 1;
break;
}
else {
throw string("�ùٸ��� ���� ���� or ��ȣ");
}
}
}
else if (aString[i] == ' ') {
}
else {
throw string("�ùٸ��� ���� ����");
}
}
if (aString[i - 1] == '*' || aString[i - 1] == '/' || aString[i - 1] == '+' || aString[i - 1] == '-') {
throw string("�������� ����");
}
if (invalidBracket != 0) {
throw string("��ȣ ����");
}
}
catch (string exception) {
cout << "Error- Error- Error- Error- Error- Error\n�Է� ������ �߰ߵǾ����ϴ�. : " << exception << "\n"<< endl;
splitString->clear();
return 0;
}
return 1;
}
// �Ʒ�InToPost, PostfixCalculate�ּ� ���۷����� ��ü�� ���� ���ؼ� �̸� �ʱ�ȭ�� �صд�.
Oper* addOper = new AddOper();
Oper* subOper = new SubOper();
Oper* mulOper = new MulOper();
Oper* divOper = new DivOper();
Oper* braOper = new BraOper();
//
// ����ǥ��� -> ����ǥ������� �����Ŵ.
//
void InToPost(vector<string> *infixString) {
//����� ���ڿ��� ����� ����.
vector<string> *postfixString = new vector<string>;
//�����ڸ� ������ ����.
stack<Oper*> *aStack = new stack<Oper*>;
for (int i = 0; i < infixString->size(); i++) {
string data = infixString->at(i);
if (data == "(") {
//��ȣ�� �ݵ�� push�Ѵ�.
aStack->push(braOper);
}
else if (data == "+") {
// �켱���� �� null������ ������ ���ϱ� ���� ������� ������ �ٷ� push��
if (!aStack->empty()) {
// ���� ž�� �ִ� �켱�������� ũ�ų� ������ �켱������ �۾��������� pop
while (aStack->top()->getPrior() >= addOper->getPrior()) {
postfixString->push_back(aStack->top()->getOper());
aStack->pop();
if (aStack->empty()) {
break;
}
}
}
aStack->push(addOper);
}
else if (data == "-") {
if (!aStack->empty()) {
while (aStack->top()->getPrior() >= subOper->getPrior()) {
postfixString->push_back(aStack->top()->getOper());
aStack->pop();
if (aStack->empty()) {
break;
}
}
}
aStack->push(subOper);
}
else if (data == "*") {
if (!aStack->empty()) {
while (aStack->top()->getPrior() >= mulOper->getPrior()) {
postfixString->push_back(aStack->top()->getOper());
aStack->pop();
if (aStack->empty()) {
break;
}
}
}
aStack->push(mulOper);
}
else if (data == "/") {
if (!aStack->empty()) {
while (aStack->top()->getPrior() >= divOper->getPrior()) {
postfixString->push_back(aStack->top()->getOper());
aStack->pop();
if (aStack->empty()) {
break;
}
}
}
aStack->push(divOper);
}
//��ȣ �ݱⰡ �������� ��ȣ�� ���ö����� pop���ش�.
else if (data == ")") {
while (aStack->top()->getPrior() != 1) {
postfixString->push_back(aStack->top()->getOper());
aStack->pop();
}
aStack->pop();
}
else {
// ���� �Է��� ������ �� ó�������Ƿ� ������ ������ ���� �ٷ� ��Ʈ���� �����Ѵ�.
postfixString->push_back(data);
}
}
//�Է��� ������ ������ ��� �����ڸ� pop�Ѵ�.
while (!aStack->empty()) {
postfixString->push_back(aStack->top()->getOper());
aStack->pop();
}
//�Է¹��� �߰� ǥ��� ������ �����ϰ� ������.
infixString->clear();
for (int i = 0; i < postfixString->size(); i++) {
infixString->push_back(postfixString->at(i));
}
}
//
//����ǥ��� ���
// �긴�� ���� ���
//
int PostfixCalculate(vector<string>* postfixString) {
//����� ���ڸ� ������ stack
stack<int> *numStack = new stack<int>;
// ����� �������� Calculator ��ü�� �����ϰ� �ǹ� ���� ������ �ʱ�ȭ���ش�.
Calculate* calculator = new Calculator(0, 0, braOper);
for(int i = 0; i < postfixString->size(); i++) {
//�Է¹��� data ����
string data = postfixString->at(i);
int a, b;
if (data == "+") {
//����� ���ڸ� ���� ������ �� pop�ϰ�
a = numStack->top();
numStack->pop();
//���� ���ڸ� �������� �Ͽ� ���� ������ �����.
b = numStack->top();
numStack->pop();
numStack->push(calculator->operCalculate(a , b,addOper));
}
else if (data == "-") {
a = numStack->top();
numStack->pop();
b = numStack->top();
numStack->pop();
numStack->push(calculator->operCalculate(a, b, subOper));
}
else if (data == "*") {
a = numStack->top();
numStack->pop();
b = numStack->top();
numStack->pop();
numStack->push(calculator->operCalculate(a, b, mulOper));
}
else if (data == "/") {
a = numStack->top();
numStack->pop();
b = numStack->top();
numStack->pop();
numStack->push(calculator->operCalculate(a, b, divOper));
}
//2���� 16������ ������ x,b�� ����� ���� ���ǹ�.
else if(data.length() > 2){
if (data.at(1) == 'x') {//16����
//���ڴ� �ٷ� ���ؿ� �����Ѵ�.
numStack->push((new HexNumberString(data))->getNum());
}
else if (data.at(1) == 'b') {//2����
numStack->push((new BinNumberString(data))->getNum());
}
else {// 2�ڸ� �̻� 10����
numStack->push((new DecNumberString(data))->getNum());
}
}//����ó���� �������Ƿ� �������� ���ڸ� 10����
else {
numStack->push((new DecNumberString(data))->getNum());
}
}//�������� ���ؿ� ���� ����� ��������Ƿ� top�ؼ� ��ȯ�Ѵ�.
return numStack->top();
}
int main() {
//���� �Է�
string input;
cout << "������ �Է��ϼ��� : ";
getline(cin, input);
// ����� ��Ʈ�� �迭/����
vector<string>* splitString = new vector<string>;
// �Է¹��� ���Ŀ� ������ �ֳ� Ȯ��
while (!IsValid(input, splitString)) {
cout << "������ �Է��ϼ��� : ";
getline(cin, input);
}
// �Է¹��� ������ ����Ѵ�.
cout << "\n�Էµ� ���� : ";
for (int i = 0; i < splitString->size(); i++) {
cout << splitString->at(i) << " ";
}
cout << endl;
//���� ǥ������� ��ȯ�Ѵ�.
InToPost(splitString);
// ��ȯ�� ������ ����Ѵ�.
cout << "\n���� ǥ������� ��ȯ�� ���� : ";
for (int i = 0; i < splitString->size(); i++) {
cout << splitString->at(i) << " ";
}
cout << endl;
//��� ��� ���
cout <<"\n���� : " << PostfixCalculate(splitString) << endl;
return 0;
}