中缀表达式是一种通用的算术或逻辑公式表示方法,操作符以中缀形式处于操作数的中间。中缀表达式是人们常用的算术表示方法。虽然人的大脑很容易理解与分析中缀表达式,但对计算机来说中缀表达式却是很复杂的,因此计算表达式的值时,通常需要先将中缀表达式转换为前缀或后缀表达式,然后再进行求值。对计算机来说,计算前缀或后缀表达式的值非常简单。例如:(3 + 4) × 5 - 6 就是中缀表达式。
后缀表达式(后缀记法、逆波兰式)
后缀表达式与前缀表达式类似,只是运算符位于操作数之后。例如:3 4 + 5 × 6 - 就是后缀表达式。
后缀表达式的计算机求值:
从左至右扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对它们做相应的计算(次顶元素 op 栈顶元素),并将结果入栈;重复上述过程直到表达式最右端,最后运算得出的值即为表达式的结果。
例如后缀表达式“3 4 + 5 × 6 -”:
(1) 从左至右扫描,将3和4压入堆栈;
(2) 遇到+运算符,因此弹出4和3(4为栈顶元素,3为次顶元素),计算出3+4的值,得7,再将7入栈;
(3) 将5入栈;
(4) 接下来是×运算符,因此弹出5和7,计算出7×5=35,将35入栈;
(5) 将6入栈;
(6) 最后是-运算符,计算出35-6的值,即29,由此得出最终结果。
将中缀表达式转换为后缀表达式
遵循以下步骤:
(1) 初始化两个栈:运算符栈S1和储存中间结果的栈S2;
(2) 从左至右扫描中缀表达式;
(3) 遇到操作数时,将其压入S2;
(4) 遇到运算符时,比较其与S1栈顶运算符的优先级:
(4-1) 如果S1为空,或栈顶运算符为左括号“(”,则直接将此运算符入栈;
(4-2) 否则,若优先级比栈顶运算符的高,也将运算符压入S1(注意转换为前缀表达式时是优先级较高或相同,而这里则不包括相同的情况);
(4-3) 否则,将S1栈顶的运算符弹出并压入到S2中,再次转到(4-1)与S1中新的栈顶运算符相比较;
(5) 遇到括号时:
(5-1) 如果是左括号“(”,则直接压入S1;
(5-2) 如果是右括号“)”,则依次弹出S1栈顶的运算符,并压入S2,直到遇到左括号为止,此时将这一对括号丢弃;
(6) 重复步骤(2)至(5),直到表达式的最右边;
(7) 将S1中剩余的运算符依次弹出并压入S2;
(8) 依次弹出S2中的元素并输出,结果的逆序即为中缀表达式对应的后缀表达式(转换为前缀表达式时不用逆序)。
下面代码实现了将中缀表达式转后缀表达式并求计算结果。支持+、-、*、\、%、^(乘方)及括号运算,支持小数运算。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<string>
#include<stack>
#include<vector>
#include<cmath>
using namespace std;
bool isOperator(char c)
{
if(c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^' || c == '(' || c == ')')
return true;
return false;
}
int prior(char c)
{
if(c == '+' || c == '-')
return 0;
if(c == '*' || c == '/' || c == '%')
return 1;
if(c == '^')
return 2;
if(c == '(' || c == ')')
return -1;
return -1;
}
int main()
{
string str;//原始字符
while(cin >> str){
vector<string> out;//存放转化后用于输出的字符
stack<char> st1;//存放操作符
for(size_t i = 0; i < str.length();){
if(!isOperator(str[i])){//不是操作符
char temp[20];//临时存放操作数字符串
int flag = 0;
for(; !isOperator(str[i]) && i < str.length(); i++){
temp[flag++] = str[i];
}
temp[flag++] = 0;
string tempstr(temp,strlen(temp));
out.push_back(tempstr);
}else{
if('(' == str[i])
st1.push(str[i]);
else if(')' == str[i]){
while(!st1.empty() && (st1.top() != '(')){
char c = st1.top();
char cc[2];
cc[0] = c;
cc[1] = 0;
string tempstr(cc,1);
out.push_back(tempstr);
st1.pop();
}
st1.pop();
}else{//其它操作符
if(!st1.empty()){//栈非空,先比较
if(prior(str[i]) > prior(st1.top()))//高于栈顶元素,入栈
st1.push(str[i]);
else{//相等或小于
while((!st1.empty()) && (prior(str[i]) <= prior(st1.top()))){//一直弹到比栈顶高
char c = st1.top();
char cc[2];
cc[0] = c;
cc[1] = 0;
string tempstr(cc,1);
out.push_back(tempstr);
st1.pop();
}
st1.push(str[i]);
}
}else{//栈为空,直接入栈
st1.push(str[i]);
}
}
i++;
}
}
while(!st1.empty()){
char c = st1.top();
char cc[2];
cc[0] = c;
cc[1] = 0;
string tempstr(cc,1);
out.push_back(tempstr);
st1.pop();
}
cout << out[0];
for(size_t i = 1; i < out.size(); i++){
string str1 = out[i];
string str = out[i-1];
if((isdigit(str1[0]) && isdigit(str[0])) || (isOperator(str1[0]) && isdigit(str[0])))
cout << " ";
cout << str1;
}
cout << endl;
//求值
stack<double> st2;
for(size_t i = 0; i < out.size(); i++){
string str = out[i];
if(!isOperator(str[0])){
const char* p = str.c_str();
double ff = atof(p);
st2.push(ff);
}else{
double a = st2.top();
st2.pop();
double b = st2.top();
st2.pop();
char c = str[0];
switch(c){
case '+':
st2.push(b + a);
break;
case '-':
st2.push(b - a);
break;
case '*':
st2.push(b * a);
break;
case '/':
st2.push(b / a);
break;
case '%':
st2.push((int)b % (int)a);
break;
case '^':
st2.push(pow(b,a));
break;
}
}
}
cout << "The answer is " << st2.top() << "." << endl << endl;
}
}
执行结果如下:
原表达式:((1.5*(1.5+2.5))/2)^3
后缀表达式:1.5 1.5 2.5 +*2 /3 ^
运算结果:The answer is 27.