一、转换成后缀表达式后计算
中缀表达式:操作符以中缀形式处于操作数的中间
后缀表达式(逆波兰表达式):将运算符放在两个运算对象的后面
例如,中缀表达式 A+B*(C-D)-E/F 对应的后缀表达式为 ABCD-*+EF/-,一般有三种方法进行转换
得到后缀表达式后的算术表达式求解过程如下:
int stringToNumber(const string &s)
{
int sign = 1;
int start = 0;
if (s[0] == '-') {
sign = -1;
start = 1;
}
int res = 0;
for (size_t i = start; i < s.size(); i++) {
res = res * 10 + s[i] - '0';
}
return res * sign;
}
int eval(int a, int b, char op)
{
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
}
return 0;
}
int evalRPN(const vector<string> &postfix)
{
stack<string> stk;
for (auto e : postfix) {
if (e == "+" || e == "-" || e == "*" || e == "/") {
int a = stringToNumber(stk.top());
stk.pop();
int b = stringToNumber(stk.top());
stk.pop();
int ans = eval(b, a, e[0]);
stk.push(to_string(ans));
} else {
stk.push(e);
}
}
return stringToNumber(stk.top());
}
1.1 使用栈
设置两个栈来实现,stack栈用来存放运算符,post栈用来存放最后的后缀表达式。
在将中缀表达式转换成后缀表达式时,其转换的原则是:从左到右扫描中缀表达式,若读到的是操作数,直接存入post栈,若读到的是运算符
1) 该运算符是 "(",则直接存入stack栈
2) 该运算符是 ")",则将栈元素弹出,将弹出的操作符输出知道遇到左括号为止。注意,左括号只太初并不输出。
3) 若该运算符为非括号,则将该运算符和stack栈顶运算符作比较:若高于或等于栈顶运算符,则直接存入stack栈,否则将低于或者等于当前运算符的栈元素出栈,存入post栈。
4) 当扫描完后,stack栈中还有运算符时,则将所有的运算符出栈,存入post栈。
A+B*(C-D)-E/F转换成对应的后缀表达式的过程如下:
stack | post | |
---|---|---|
读入A,将A放入post中 | A | |
读入+,将+放入stack中 | + | A |
读入B,将B放入post中 | + | AB |
读入*,由于*的优先级比+高,直接存入stack栈 | +* | AB |
读入(,直接存入stack栈 | +*( | AB |
读入C,将C放入post中 | +*( | ABC |
读入-,将-放入stack中 | +*(- | ABC |
读入D,将D放入post中 | +*(- | ABCD |
读入),将stack栈中(后元素都存入post中 | +* | ABCD- |
读入-,将优先级小于等于-的运算符都弹出到post中,并将-压入stack中 | - | ABCD-*+ |
读入E,将E放入post中 | - | ABCD-*+E |
读入/,由于/的优先级比较高,将/放入stack中 | -/ | ABCD-*+E |
读入F,将F放入post中 | -/ | ABCD-*+EF |
到达最右端,将所有运算符都出栈 | ABCD-*+EF/- |
相应代码如下:
string infixToPostfix(const string& infix)
{
string post;
string oper;
for (auto c : infix) {
if (isdigit(c) || isalpha(c)) {
post += c;
} else if (c == '(') {
oper += c;
} else if (c == '*' || c == '/') {
while (!oper.empty() && oper.back() == '*' || oper.back() == '/') {
post.push_back(oper.back());
oper.pop_back();
}
oper.push_back(c);
} else if (c == '+' || c == '-') {
while (!oper.empty() && (oper.back() == '*' || oper.back() == '/' || oper.back() == '+' || oper.back() == '-')) {
post.push_back(oper.back());
oper.pop_back();
}
oper.push_back(c);
} else if (c == ')') {
while (!oper.empty() && oper.back() != '(') {
post.push_back(oper.back());
oper.pop_back();
}
oper.pop_back();
}
}
while (!oper.empty()) {
post.push_back(oper.back());
oper.pop_back();
}
return post;
}
1.2 使用语法树
先将中缀表达式用二叉树表示出来,再后序遍历该二叉树,就得到其相应的后缀表达式。
例如:中缀表达式 a+(b-c)*d 的二叉树为:
1.3 加括号法
1) 先按照运算符的优先级对中缀表达式加括号,变成 ((a+(b*c))+(((d*e)+f)*g))
2) 将运算符移到括号的后面,变成((a(bc)*)+(((de)*f)+g)*)+
3) 去掉括号,得到 abc*+de*f+g*+
二、使用双栈
解法一经过了中间过程,先转换为后缀表达式然后进行求值。我们其实可以使用两个栈,边遍历边求解。和解法一类似:
1) 使用两个栈,stack0用于存储操作数,stack1用于存储操作符
2) 从左往右扫描,遇到操作数入栈stack0
3) 遇到操作符时,如果当前优先级低于或等于栈顶操作符优先级,则从stack0弹出两个元素,从stack1弹出一个操作符,进行运算,将结果压入stack0中,继续与栈顶操作符比较优先级
4) 如果遇到操作符高于栈顶操作符优先级,则直接入栈stack1
5) 遇到左括号,直接入栈stack1
6) 遇到右括号,则从stack0弹出两个元素,从stack1弹出一个操作符进行计算,并将结果加入到stack0中,重复这步直到遇到左括号
相应代码如下:
int evalOperator(int num1, int num2, char op)
{
switch (op) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
}
return 0;
}
int eval(const string &s)
{
stack<int> opNum;
stack<char> oprt;
size_t i = 0;
while (i < s.size()) {
if (isdigit(s[i])) {
int num = s[i++] - '0';
while (i < s.size() && isdigit(s[i])) {
num = num * 10 + s[i] - '0';
i++;
}
opNum.push(num);
num = 0;
continue;
}
if (s[i] == '(') {
oprt.push(s[i]);
} else if (s[i] == '+' || s[i] == '-') {
while (!oprt.empty() && (oprt.top() == '+' || oprt.top() == '-')) {
int num1 = opNum.top();
opNum.pop();
int num2 = opNum.top();
opNum.pop();
int n = evalOperator(num2, num1, oprt.top());
opNum.push(n);
oprt.pop();
}
} else if (s[i] == '*' || s[i] == '/') {
while (!oprt.empty() && (oprt.top() == '*' || oprt.top() == '/' || oprt.top() == '+' || oprt.top() == '-')) {
int num1 = opNum.top();
opNum.pop();
int num2 = opNum.top();
opNum.pop();
int n = evalOperator(num2, num1, oprt.top());
opNum.push(n);
oprt.pop();
}
} else if (s[i] == ')') {
while (!oprt.empty() && oprt.top() != '(') {
int num1 = opNum.top();
opNum.pop();
int num2 = opNum.top();
opNum.pop();
int n = evalOperator(num2, num1, oprt.top());
opNum.push(n);
oprt.pop();
}
}
i++;
}
return opNum.top();
}