sdut 2132 数据结构实验之栈二:一般算术表达式转换成后缀式

#include<stdio.h>
int fun(char str)
{
if(str=='+'||str=='-') return 1;
if(str=='*'||str=='/') return 2;
if(str=='(') return 3;
if(str==')') return 4;
}
int main()
{
char opnd[100];
char oprt[100],str;
int i=0,j=0;
while(scanf("%c",&str),str!='#')
{
if(str>='a'&&str<='z')
printf("%c",str);
else
{
if(j==0) oprt[++j]=str;
else
{
if(fun(str)>fun(oprt[j]))
{
if(fun(str)==4)
{
while(oprt[j]!='(')
printf("%c",oprt[j--]);

j--;
}
else
oprt[++j]=str;
}

else
{

if(oprt[j]!='(')
{
printf("%c",oprt[j]);
oprt[j]=str;
}
else oprt[++j]=str;
}
}
}
}
while(j!=0)
{
printf("%c",oprt[j--]);
}return 0;
}
posted @ 2011-10-06 21:32  skyming  阅读(211)  评论(0)    收藏  举报