baidu_34387683 2016-03-22 09:59 采纳率: 0%
浏览 1483

中缀表达式转换成后缀表达式,求大神帮忙找下错误,谢谢

#include
#include
typedef struct huozhui{
char c;
struct huozhui next;
}huozhui;
void push(huozhui *L,char x)
{
huozhui *p=(huozhui *)malloc(sizeof(huozhui));
p->c=x;
p->next=L->next;
L->next=p;
}
int isoperator(char x)
{
if(x=='+'||x=='-'||x=='
'||x=='/'||x=='('||x==')')
return 0;
else
return 1;
}
int bijiao(char x)
{
switch(x)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;

}
return -1;
}
int isempty(huozhui *L)
{
return L->next==NULL;
}
void pop(huozhui *L)
{
if(L->next==NULL)
return ;
else
{
huozhui *p;
p=L->next;
L->next=p->next;
free(p);
}
}
char top(huozhui *L)
{
if(L->next==NULL)
return 0;
else
return L->next->c;

}
int main()
{
huozhui *L=(huozhui *)malloc(sizeof(huozhui));
L->next=NULL;
char s[100],key[200];
gets(s);
int i,k=0;
for(i=0;s[i]!='\0';i++)
{
if(isoperator(s[i])==1)
{
key[k++]=s[i];
}
else
{
if(isempty(L))
{
push(L,s[i]);
}
else
{
if(s[i]==')')
{
while(!isempty(L)&&top(L)!='(')
{
key[k++]=top(L);
pop(L);
}
pop(L);
}
else
{
if(s[i]=='(')
push(L,s[i]);
else
{
while(bijiao(top(L))>=bijiao(s[i])&&!isempty(L))
{
key[k++]=top(L);
pop(L);
}
push(L,s[i]);
}
}
}
}
}
huozhui *p=L->next;
while(p!=NULL)
{
key[k]=p->c;
p=p->next;
k++;
}
key[k]='\0';
for(i=0;i<k;i++)
{
if(i!=0)
printf(" ");
printf("%c",key[i]);
}
puts("");
return 0;
}

  • 写回答

2条回答 默认 最新

  • devmiao 2016-03-22 10:01
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!