#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;
}
中缀表达式转后缀表达式,什么问题?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答
悬赏问题
- ¥15 #MATLAB仿真#车辆换道路径规划
- ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
- ¥15 数据可视化Python
- ¥15 要给毕业设计添加扫码登录的功能!!有偿
- ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
- ¥15 微信公众号自制会员卡没有收款渠道啊
- ¥100 Jenkins自动化部署—悬赏100元
- ¥15 关于#python#的问题:求帮写python代码
- ¥20 MATLAB画图图形出现上下震荡的线条
- ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘