#include
// 中缀转后缀
using namespace std;
void duqu(vector
if(x-'0'>=0&&x-'0'<=9){ b.push_back(x); return;}
else{
if(a.empty()){ a.push_back(x); return;}
else if(x=='('||a.back()=='('){ a.push_back(x); return;}
else if(x=='*'||x=='/'&&(a.back()=='+'||a.back()=='-')){a.push_back(x); return; }
else if((x=='*'||x=='/')&&(a.back()=='*'||a.back()=='/')){ //同级
while(!(a.back()!='*'&&a.back()!='/')){
b.push_back(a.back());a.pop_back();
}
a.push_back(x); return;
}
else if((x=='+'||x=='-')&&(a.back()=='+'||a.back()=='-'||a.back()=='*'||a.back()=='/')){
while(a.back()!='('&&!a.empty()){
b.push_back(a.back());a.pop_back();
}
a.push_back(x); return;
}
else if(x==')'){
while(a.back()!='('){
b.push_back(a.back());a.pop_back();
}
a.pop_back(); return;//pop '(';
}
}
}
int getres(vector
for(auto i=b.begin();i if(*i-'0'>=0&&*i-'0'<=9){ res.push_back(*i-'0');} if(*i=='+'){int temp=*(res.end()-1)+*(res.end()-2); res.erase(res.end()-2,res.end());res.push_back(temp);} if(*i=='-'){int temp= *(res.end()-1)- *(res.end()-2);res.erase(res.end()-2,res.end());res.push_back(temp);} if(*i=='*'){int temp= *(res.end()-2)*(*(res.end()-1));res.erase(res.end()-2,res.end());res.push_back(temp);} if(*i=='/'){int temp =*(res.end()-2)/(*(res.end()-1));res.erase(res.end()-2,res.end());res.push_back(temp);} } return res.back(); } int main(){ string str; cout<<"输入表达式:n"; while(cin>>str){ vector vector for(int i=0;i duqu(a,b,str[i]); } while(!a.empty()){ b.push_back(a.back());a.pop_back(); } for(auto i=b.begin();i< b.end();i++){ cout<<*i<<" "; } cout< cout< } return 0; } 欢迎分享,转载请注明来源:内存溢出
评论列表(0条)