#include<stack> #include<iostream> #include<string> using namespace std; //优先级判断 char compare(char opt, char si) { if((opt==‘+‘||opt==‘-‘)&&(si==‘*‘||si==‘/‘) return ‘<‘; else if(opt==‘#‘) return ‘<‘; return ‘>‘; } //判断是否为运算符 bool isOp(char c) { if(c==‘+‘||c==‘-‘||c==‘*‘||c==‘/‘) return true; return false; } int main() { stack<char>op; stack<char>num; op.push(‘#‘); num.push(‘#‘); string s; cin>>s; for(int i=0;i<s.size();i++) { if(!isOp(s[i])) num.push(s[i]); else { char c=compare(op.top(),s[i]); if(c==‘<‘) op.push(s[i]); else { num.push(op.top()); op.pop(); op.push(s[i]); } } } while(op.top()!=‘#‘) { num.push(op.top()); op.pop(); } string s1=""; while(num.top!=‘#‘) { s1=s1+num.top(); num.pop(); } for(int i=0;i<s1.size()/2;i++) { char temp=s1[i]; s1[i]=s1[s1.size()-1-i]; s1[s1.size()-1-i]=temp; } cout<<s1<<endl; return 0; }
时间: 2024-10-16 09:48:50