欢迎大家批评纠正
中缀表达式转后缀表达式 以及 后缀表达式的计算
一、中缀表达式 转 后缀表达式
步骤:
1.准备两个空栈S1,S2
2.当遇到数字直接压入S1
3.当遇到操作符号,如果S2为空,或者当前符号的优先级大于S2栈顶操作符优先级,或者遇到‘(’左括号,或者S2栈顶为‘(’左括号,当前操作符直接压入S2
4.当遇到操作符,如果当前操作符的优先级小于等于S2栈顶操作符的优先级,S2弹出栈顶,压入S1,直到S2为空,或者为左括号‘(’,或者为优先级小于当前符号优先级,将当前符号压入S2
5.当遇到右括号‘)’,S2弹出栈顶,压入S1直到弹出左括号‘(’,将括号略去
6.最后将S2栈顶弹出,压入S1,直到S2为空;
此时S1储存的相反顺序即为后缀表达式
例子:
将 1+(3-4x2+5)/2 转化为后缀表达式:
首先,拿来两个空栈S1、S2,按顺序从遍历式子。
‘1’是数字,直接压入S1;
‘+’是符号,S2为空,压入S2;
‘( ’左括号直接压入S2;
‘3’是数字,直接压入S1;
‘-’是符号,S2栈顶为‘(’,将‘-’直接压入S2;
‘4’是数字,直接压入S1;
‘x’是符号,S2栈顶为‘-’,优先级 ‘x’ > ‘-’,将‘x’压入S2;
‘2’是数字,直接压入S1;
‘+’是符号,S2栈顶为‘x’,优先级 ‘x’ > ‘+’,弹出S2栈顶,压入S1,直到S2栈顶 优先级 小于 ‘+’,将‘+’压入S2;
‘2’是数字,直接压入S1;
‘)’遇到右括号,开始不断 将S2栈顶弹出,压入S1;知道遇到左括号,之后舍弃括号;
‘/’是符号,S2栈顶为‘-’,优先级 ‘/’ > ‘-’,将‘x’压入S2;
之后,依次弹出S2栈顶,压入S1,知道S2为空;
之后,依次弹出S1栈顶,压入S2,知道S1为空;
之后,依次弹出S2栈顶,输出;
此时输出序列为:
1 3 4 2 x - 5+2 / +
这就是1+(3-4x2+5)/2的后缀表达式。
//中缀表达式转后缀表达式
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int to_int(char c){
return c - '0';
}
int to_r(char c){//计算优先级
if (c == '+' || c == '-')return 1;
else if (c == '*' || c == '/')return 2;
}
int main(){
stack<char> s1, s2;
string str; cin >> str;
for (int i = 0; i < str.length(); i++){
if (str[i] >= '0'&&str[i] <= '9'){
s1.push(str[i]);
}
else{
if (s2.empty()||str[i] == '('){//判断s2是否为空 必须优先判断
s2.push(str[i]);
}
else if (str[i] == ')'){ //其次,当前是否为 右括号 必须优先
while (s2.top() != '('){
s1.push(s2.top());
s2.pop();
}
s2.pop();
}
else if (s2.top() == '('||to_r(str[i])>to_r(s2.top()) ){
s2.push(str[i]);
}
else if (to_r(str[i]) <= to_r(s2.top())){
while (!s2.empty() && to_r(str[i]) <= to_r(s2.top())){
s1.push(s2.top());
s2.pop();
if (s2.top() == '(')break;
}
s2.push(str[i]);
}
}
}
while (!s2.empty()){
s1.push(s2.top());
s2.pop();
}
while (!s1.empty()){
s2.push(s1.top());
s1.pop();
}
while(!s2.empty()){
cout << s2.top() << " ";
s2.pop();
}
system("pause");
return 0;
}
二、 后缀表达式 的计算
步骤:
1.重新拿来一个 栈s3;
2.将上述s2栈顶弹出,压入s3,直到遇到 操作符;
3.用s3栈顶的下一位 加/减/乘/除 栈顶 ,将结果压入s3;
3.如此直到s2为空,s3此时只有一个元素,即为后缀表达式的运算结果;
例子:
上述后缀表达式 1 3 4 2 x - 5+2 / +
1入栈,3入栈,4入栈,2入栈,x 计算4x2 得8,入栈。
此时栈中元素为 1 3 8 (8为栈顶);
‘-’ 计算 3-8 得 -5 入栈, 此时栈元素为 1 -5 (-5为栈顶)
5入栈,+ 计算-5 +5 得0 入栈, 此时栈元素为 1 0 (0为栈顶)
2入栈,/ 计算0/2 得 0 入栈,此时栈元素为 1 0 (0为栈顶)
‘+’计算1+0得1 即为后缀表达式得结果;
代码
//中缀表达式转后缀表达式 以及 后缀表达式的计算
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int to_int(char c){
return c - '0';
}
int to_r(char c){//计算优先级
if (c == '+' || c == '-')return 1;
else if (c == '*' || c == '/')return 2;
}
int op(int a, int b, char c){ //计算
if (c == '+')return a + b;
else if (c == '-')return a - b;
else if (c == '*')return a*b;
else if (c == '/')return a / b;
}
int main(){
stack<char> s1, s2;
string str; cin >> str;
for (int i = 0; i < str.length(); i++){
if (str[i] >= '0'&&str[i] <= '9'){
s1.push(str[i]);
}
else{
if (s2.empty()||str[i] == '('){
s2.push(str[i]);
}
else if (str[i] == ')'){
while (s2.top() != '('){
s1.push(s2.top());
s2.pop();
}
s2.pop();
}
else if (s2.top() == '('||to_r(str[i])>to_r(s2.top()) ){
s2.push(str[i]);
}
else if (to_r(str[i]) <= to_r(s2.top())){
while (!s2.empty() && to_r(str[i]) <= to_r(s2.top())){
s1.push(s2.top());
s2.pop();
if (s2.top() == '(')break;
}
s2.push(str[i]);
}
}
}
while (!s2.empty()){
s1.push(s2.top());
s2.pop();
}
while (!s1.empty()){
s2.push(s1.top());
s1.pop();
}
stack<int> s3;
while (!s2.empty()){
if (s2.top() >= '0'&&s2.top() <= '9'){
s3.push(to_int(s2.top()));
s2.pop();
}
else{
int a = s3.top(); s3.pop();
int b = s3.top(); s3.pop();
int c = op(b, a, s2.top());
s2.pop();
s3.push(c);
}
}
cout << s3.top() << endl;
system("pause");
return 0;
}