#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <map>
using namespace std;
#define maxn 500
map<char,int>kk;
stack<char>s;
char k[maxn];
char c[maxn];
inline int change(){
int i=0,cnt=0;
while(c[i]!='@'){
if(c[i]>='0' and c[i]<='9') k[++cnt]=c[i];
if(c[i]=='(') s.push(c[i]);
if(c[i]==')'){
int num=s.size();
for(int w=0;w<num;w++){
if(s.top()!='('){
int f=s.top();
k[++cnt]=f;
s.pop();
}
if(s.top()=='('){
s.pop(); break;
}
}
}
if(c[i]=='+' or c[i]=='-' or c[i]=='*' or c[i]=='/'){
if(!s.empty()){
int f=s.top();
//cout<<kk[c[i]]<<" "<<kk[f];
while(kk[c[i]]<=kk[f] and f!='('){
k[++cnt]=f;
s.pop();
}
s.push(c[i]);
}
else s.push(c[i]);
}
i++;
}
if(!s.empty()){
int num=s.size();
for(int i=0;i<num;i++){
k[++cnt]=s.top();
s.pop();
}
}
for(int i=1;i<=cnt;i++) cout<<k[i];
}
int main(){
cin>>c;
int j=0;
while(c[j]!='@'){
if(c[j]=='+' or c[j]=='-') kk[c[j]]=1;
if(c[j]=='*' or c[j]=='/') kk[c[j]]=2;
j++;
}
change();
}
将中缀表达式转换为后缀表达式的算法思想:
·开始扫描;
·数字时,加入后缀表达式;
·运算符:
a. 若为 '(',入栈;
b. 若为 ')',则依次把栈中的的运算符加入后缀表达式中,直到出现'(',从栈中删除'(' ;
c. 若为 除括号外的其他运算符, 当其优先级高于除'('以外的栈顶运算符时,直接入栈。否则从栈顶开始,依次弹出比当前处理的运算符优先级高和优先级相等的运算符,直到一个比它优先级低的或者遇到了一个左括号为止。