-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcalExp.cpp
107 lines (96 loc) · 2.85 KB
/
calExp.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
#include <iostream>
#include <stack>
using namespace std;
// 判断字符是否为运算符(包括加减乘除)
bool isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
// 判断运算符的优先级
int priority(char c) {
if (c == '+' || c == '-') {
return 1;
} else if (c == '*' || c == '/') {
return 2;
} else {
return 3; // 括号优先级最大
}
}
string reversePolarExp(string &s) {
string res;
stack<char> st; // 存放操作符
for(auto& c: s) {
if (c == ' ') {
continue;
} else if (isalnum(c)) {
res += c; // 操作数直接输出
} else {
// 1. 栈为空或者左括号直接入栈
if (st.empty() || c == '(') {
st.push(c);
continue;
}
// 3. 只有遇到右括号才弹左括号
if (c == ')') {
while (st.top() != '(') {
res += st.top();
st.pop();
}
st.pop(); // 弹出左括号
continue;
}
// 2. 否则,弹出比 c 优先级大于或者等于的操作符,除了左括号
while (!st.empty() && st.top() != '(' && priority(c) <= priority(st.top())) {
res += st.top();
st.pop();
}
st.push(c);
}
}
while(!st.empty()) { // 4. 最后弹出剩余的操作符
res += st.top();
st.pop();
}
return res;
}
// 计算逆波兰表达式的值
float calculateRPE(const string& rpn) {
stack<float> operands; // 操作数栈
for (char c : rpn) {
if (isdigit(c)) {
// 如果是数字,则转换为float并入栈
operands.push(static_cast<float>(c - '0'));
} else if (isOperator(c)) {
// 如果是运算符,则从栈中取出操作数进行计算,并将结果入栈
float operand2 = operands.top();
operands.pop();
float operand1 = operands.top();
operands.pop();
switch (c) {
case '+':
operands.push(operand1 + operand2);
break;
case '-':
operands.push(operand1 - operand2);
break;
case '*':
operands.push(operand1 * operand2);
break;
case '/':
operands.push(operand1 / operand2);
break;
default:
break;
}
}
}
// 返回最终的计算结果
return operands.top();
}
int main()
{
string s("1+2*3+(1*2+3)*4");
string rpe = reversePolarExp(s);
cout << rpe << endl;
cout << calculateRPE(rpe) << endl;
return 0;
}