中缀转后缀
#include<iostream>
#include<stack>
#include<string>
using namespace std;
string s="A+(B*(C-D)-E)/F";
stack<char> op;
int level(char c)
{
if(c=='+'||c=='-') return 1;
else if(c=='*'||c=='/') return 2;
else if(c=='('||c==')') return 3;
else return 0;
}
bool isSuper(char op1,char op2)
{
if(level(op2)<=level(op1)) return true;
else return false;
}
int main()
{
string res;
for(int i=0;i<s.length();i++)
{
cout<<"s[i]\t"<<s[i]<<endl;
cout<<"res\t"<<res<<endl;
cout<<endl;
switch (s[i])
{
case '+':case '-':case '*':
case '/':{
if(op.size()==0) op.push(s[i]);
else
{
while(op.size()>0 && isSuper(op.top(),s[i]) && op.top()!='(')
{
res.push_back(op.top());
op.pop();
}
op.push(s[i]);
}
break;
}
case '(':op.push(s[i]);break;
case ')':
{
while(op.top()!='(')
{
cout<<"op:"<<op.top()<<endl;
res.push_back(op.top());op.pop();
cout<<"res:"<<res<<endl;
cout<<endl;
}
op.pop();
break;
}
default:res.push_back(s[i]);break;
}
}
while(op.size()>0)
{
res.push_back(op.top());
op.pop();
}
cout<<res<<endl;
return 0;
}