题目描述
对于一个基于二元运算符的算术表达式,转换为对应的后缀式,并输出之。
输入
输入一个算术表达式,以‘#’字符作为结束标志。
输出
输出该表达式转换所得到的后缀式。
示例输入
a*b+(c-d/e)*f#
示例输出
ab*cde/-f*+
#include <bits/stdc++.h>
using namespace std;
stack <char>p;
int main()
{
char st[110];
int i;
scanf("%s",st);
for(i=0;st[i]!='#';i++)
{
if(st[i]>='a'&&st[i]<='z')
{
printf("%c",st[i]);
}
else if(st[i]=='(')
{
p.push(st[i]);
}
else if( st[i]== ')' )
{
while(!p.empty()&&p.top()!='(')
{
printf("%c",p.top());
p.pop();
}
p.pop();//删除左括号
}
else if(st[i]=='+' || st[i]=='-')
{
while(!p.empty()&&p.top()!='(')
{
printf("%c",p.top());
p.pop();
}
p.push(st[i]);
}
else if(st[i]=='*'||st[i]=='/')
{
while(!p.empty()&&p.top()!='('&&(p.top()=='*'||p.top()=='/'))
{
printf("%c",p.top());
p.pop();
}
p.push(st[i]);
}
}
while(!p.empty())
{
printf("%c",p.top());
p.pop();
}
cout<< endl;
return 0;
}