「已注销」
2017-06-15 15:21中缀表达式转后缀表达式,什么问题?
#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;
}
- 点赞
- 回答
- 收藏
- 复制链接分享
3条回答
为你推荐
- 中缀表达式转后缀表达式,什么问题?
- c
- c++
- 3个回答
- 后缀表达式转中缀表达式
- 后缀转中缀
- 栈
- 2个回答
- Java-以后缀表达式构造表达式二叉树
- java
- 1个回答
- 像这样的(9 3 1 - 3 * + 10 2 / + )后缀表达式怎么样求值呢?
- php
- 1个回答
- sizeof算出的数组大小怎么不对呢?
- 1个回答