MR____Z 2017-08-16 10:42 采纳率: 0%
浏览 2877

前缀表达式转后缀表达式

题目:前缀表达式和后缀表达式的文法分别为:前e->(e.e)|+(e.e)|a后e->(e.e)|(e.e)+|a编一个程序。输入一个前缀表达式,输出一个与之等价的后缀表达式:假设输入的前缀表达式没有语法错误。例如:输入+(*(a,+(a,a)),a)输出((a,(a,a)+)*,a)+

#include
#include
#include
#include
#define MaxSize 50
//前缀表达式和后缀表达式的文法分别为:前e->(e.e)|+(e.e)|a后e->(e.e)|(e.e)+|a编一个程序。输入一个前缀表达式,输出一个与之等价的后缀表达式:假设输入的前缀表达式没有语法错误。例如:输入+(*(a,+(a,a)),a)输出((a,(a,a)+)*,a)+
typedef char Elemtype;
struct Stack
{
Elemtype data[MaxSize];
int top;
};
int main()
{
struct Stack s;
void InitStack(struct Stack *s);
bool Pop (struct Stack *s,Elemtype *x);
bool Push (struct Stack *s,Elemtype x);
Elemtype x;
int i=0;
char str[MaxSize];
printf("请输入前缀表达式:");
scanf("%s",str);
printf("对应的后缀表达式:");
InitStack(s);
while(str[i]!='\0')
{
if(str[i]=='('||(str[i]>='a'&&str[i]<='z'))
{
printf("%c",str[i]);
continue;
}
if(str[i]=='+'||str[i]=='-'||str[i]=='
'||str[i]=='/')
{
Push(s,str[i]);
continue;
}
if(str[i]==')')
{
printf("%c",str[i]);
Pop(s,x);
printf("%c",x);
continue;
}
i++;
}

}

void InitStack(struct Stack *s)
{
(*s).top=-1;
}
bool Push (struct Stack *s,Elemtype x)
{
if((*s).top==MaxSize-1)
return false;
(*s).data[++(*s).top]=x;
return true;
}
bool Pop (struct Stack *s,Elemtype *x)
{
if((*s).top==-1)
return false;
x=(*s).data[(*s).top--];
return true;
}

求大神改正,没报错,但是一跑就崩溃(因为题目说了输入的前缀一定正确,就没判断)

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-08-16 15:45
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序