#include
#include
typedef struct
{
char base;
char *top;
int stacksize;
}SqStack;
char InitStack(SqStack &S)
{
S.base=new char[100];
S.top=S.base;
S.stacksize=100;
}
char Push(SqStack &S,char e)
{
*S.top=e;
S.top++;
}
char Pop(SqStack &S)
{
char e;
e=(S.top-1);
S.top--;
return e;
}
int bj(char a,char b)
{
if(a=='(')
return 0;
else if(a=='*'||a=='/')
return 1;
else
{
if(b=='+'||b=='-')
return 1;
else
return 0;
}
}
int main()
{
SqStack s;
InitStack(s);
char q[200];
int i,j;
gets(q);
j=strlen(q);
for(i=0;i
{
if((q[i]='a')||(q[i]<='Z'&&q[i]>='A'))
printf("%c",q[i]);
else if(q[i]=='+'||q[i]=='-'||q[i]=='*'||q[i]=='/')
{
if(s.top==s.base)
Push(s,q[i]);
else
{
if(bj(*(s.top-1),q[i])==1)
{
printf("%c",Pop(s));
}
if(bj(*(s.top-1),q[i])==1)
{
printf("%c",Pop(s));
}
Push(s,q[i]);
}
}
else if(q[i]=='(')
{
Push(s,q[i]);
}
else
{
while(*(s.top-1)!='(')
printf("%c",Pop(s));
if(*(s.top-1)=='(')
Pop(s);
}
}
while(s.top!=s.base)
printf("%c",Pop(s));
return 0;
}
![](https://profile-avatar.csdnimg.cn/cfc7837e92ae40d8838e6160ad848fbf_i__hong.jpg!4)
中缀表达式转后缀表达式,什么问题?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-