-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalcSuffix.cpp
86 lines (81 loc) · 2.36 KB
/
calcSuffix.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
//
// Created by FanBig on 2023/3/15.
//
#include "calcSuffix.h"
double calcSuffix::CalculateSuffix(const std::string &str, myCalculate mc) {
std::stack<double> sta;
for (char op: str) {
// while (op >= 0 && op <= 9 || op == '.') {
// int arr[10] = {1};
// double realNumber = 0;
//
// for (int i = 0; i < str.length(); ++i) {
// arr[i + 1] = arr[i] * 10;
// }
//
// for (int i = 0; i < str.length(); ++i) {
// realNumber += arr[str.length() - i - 1] * int(str[i] - '0');
// }
// }
switch (op) {
case '+': {
double num2 = sta.top();
sta.pop();
double num1 = sta.top();
sta.pop();
sta.push(mc.Calc(num1, op, num2));
break;
}
case '-': {
double num2 = sta.top();
sta.pop();
double num1 = sta.top();
sta.pop();
sta.push(mc.Calc(num1, op, num2));
break;
}
case '*': {
double num2 = sta.top();
sta.pop();
double num1 = sta.top();
sta.pop();
sta.push(mc.Calc(num1, op, num2));
break;
}
case '/': {
double num2 = sta.top();
sta.pop();
double num1 = sta.top();
sta.pop();
sta.push(mc.Calc(num1, op, num2));
break;
}
case '^': {
double num2 = sta.top();
sta.pop();
double num1 = sta.top();
sta.pop();
sta.push(mc.Calc(num1, op, num2));
break;
}
case '!': {
double num = sta.top();
sta.pop();
sta.push(mc.Calc(op, int(num)));
break;
}
default:
if (isdigit(op)) {
op -= 48;
sta.push(op);
} else {
std::cout << " Illegal operator : [" << op << "] " << std::endl;
}
// exit(0);
break;
}
}
double ret = sta.top();
sta.pop();
return ret;
}