中缀转后缀
2025-11-22 14:56:10
发布于:浙江
#include <bits/stdc++.h>
using namespace std;
int xyz(char op){
if(op=='+' or op=='-') return 0;
else return 1;
}
int main(){
stack<char> st;
string s;
cin>>s;
for(int i=0;i<s.size();i++){
if(s[i]>='a' && s[i]<='z'){
cout<<s[i];
}else if(s[i]= ='('){
st.push('(');
}else if(s[i]= ='+' or s[i]= ='-' or s[i]= ='*' or s[i]= ='/'){
while(!st.empty() and st.top()!='(' and xyz(s[i])<=xyz(st.top())) {cout<<st.top();st.pop();}
st.push(s[i]);
}else if(s[i]= =')'){
while(st.top()!='(' and !st.empty()){
cout<<st.top();
st.pop();
}
if(!st.empty() and st.top()= ='('){
st.pop();
}
}
}
while(!st.empty()){cout<<st.top();st.pop();}
return 0;
}
这里空空如也

















有帮助,赞一个