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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿