题目描述
中缀(infix)表示
<操作数><操作符><操作数> 如A+B
前缀(prefix)表示(波兰表示法)
<操作符><操作数><操作数> 如+AB
输入
一个正确的中缀表达式,长度不大于20,仅有数字和圆括号基础运算 (加减乘除以及模运算和幂运算)
输出
一个逆波兰表达式
样例输入
7-(3+5*6)/5+4
样例输出
7 3 5 6 * + 5 / - 4 +
Code:
#include<bits/stdc++.h>
using namespace std;
string s;
stack<char>a;
map<char,int>op;
int main(){
op['+']=0;
op['-']=0;
op['*']=1;
op['/']=1;
op['%']=1;
op['(']=-1;
op['^']=2;
cin>>s;
for(int i=0;i<s.size();i++){
if(s[i]>='0'&&s[i]<='9'){
cout<<s[i]<<" ";
}else{
if(s[i]=='('){
a.push('(');
}else if(s[i]==')'){
while(!a.empty()&&a.top()!='('){
cout<<a.top()<<" ";
a.pop();
}
a.pop();
}else{
while(!a.empty()&&op[s[i]]<=op[a.top()]){
cout<<a.top()<<" ";
a.pop();
}
a.push(s[i]);
}
}
}
while(!a.empty()){
cout<<a.top()<<" ";
a.pop();
}
return 0;
}



















