#include<iostream> using namespace std; int youxian(char s) { if(s=='+'||s=='-') return 1; else if(s=='*'||s=='/') return 2; else if(s=='(') return 3; else if(s==')') return 4; } int main() { int top=0; char s,b[110]; while(cin>>s && s!='#') { if(s>='a' && s<='z') cout<<s; else { if(top==0) { b[top++]=s; } else { if(youxian(s)>youxian(b[top-1])) { if(youxian(s)==4) { while(b[top-1]!='(') { cout<<b[--top]; } top--; } else { b[top++]=s; } } else { if(b[top-1]=='(') { b[top++]=s; } else { cout<<b[top-1]; b[top-1]=s; } } } } } while(top!=0) { cout<<b[top-1]; top--; } cout<<endl; return 0; }
时间: 2024-10-11 07:43:25