数据表达式转换成c语言表达式的步骤,【C语言《数据结构》实验报告】表达式的转换...

《【C语言《数据结构》实验报告】表达式的转换》由会员分享,可在线阅读,更多相关《【C语言《数据结构》实验报告】表达式的转换(13页珍藏版)》请在人人文库网上搜索。

1、数据结构实验报告实验题目: 使用键盘输入表达式,计算表达式的值并输出;将表达式转化成后缀表达式输出,利用后缀表达式求表达式的值并输出。实验目的:使用栈的操作编写关于数据结构的程序。实验内容:写出程序并上机调试、通过。一、需求分析1、演示程序以用户和计算机的对话方式执行,即在计算机终端上显示“请输入表达式”时输入中缀表达式。然后计算机终端输出转换后的后缀表达式及计算后的结果。2、程序执行的命令包括:(1)构造链栈;(2)输入数据;(3)判断输入的表达式是否为非法表达式;(4)将中缀表达式转换为后缀表达式;(5)计算表达式的值;(6)输出。(7)结束4、本程序能将中缀表达式转换为后缀表达式,并且能。

2、计算表达式的值。5、输入及输出示例:例1:请输入表达式6+3*(6+5)后缀表达式:6 3 6 5 + * +计算结果为:39Press any key to continue 例2:请输入表达式6-3*(7+1ERROR:表达式错误Press any key to continue 二 概要设计1基本操作(1)、struct node操作结果:创建结构体(2)、int Searchexpression(char string1)初始条件:表达式string1已经存在。操作结果:判断表达式是否非法(3)、struct node *Initialization()操作结果:创建栈链。(4)、st。

3、ruct node *assort(struct node *s)初始条件:string1、string2已存在。操作结果:将中缀表达式转换为后缀表达式并存在string2中。(5)、struct node *calcolate(struct node *s)操作结果:求出表达式的值2、模块调用图主程序模块创建结构体判断表达式是否非法将中缀表达式转换为后缀表达式表达式求值三 详细设计1、每个模块:(1) 定义结构体struct node char data;int num;struct node *next; (2) 判断表达式是否非法int Searchexpression(char str。

4、ing1) int i1,b1,b2;int m;m=strlen(string1);if(string109)printf(ERROR:表达式缺操作数!n);return(WRONG);for(i1=0;i1data=a;p-next=top;top=p;break;case *:case /:string2j= ;j+;if(top-data=*)|(top-data=/)string2j=top-data;j+; /比其高,现将栈顶运算符出栈,再进栈。top-data=a;break;elsep=(struct node *)malloc(sizeof(struct node);/否,直。

5、接进栈p-data=a;p-next=top;top=p;break;case +:case -:string2j= ;j+;if(top-data=+|top-data=-|top-data=*|top-data=/)string2j=top-data;j+;top-data=a;break;else p=(struct node *)malloc(sizeof(struct node);p-data=a;p-next=top;top=p;break;case ):string2j= ;j+;if(top-data=)printf(input error);break;while(top-d。

6、ata!=()string2j=top-data;j+;p=top;top=top-next;free(p);p=top;top=top-next;free(p);break;while(top-data!=)string2j=top-data;j+;p=top;top=top-next;free(p);string2j=#;printf(后缀表达式为:);for(i=0;i=0&string2i=0&string2nnum=a;p-next=top;top=p;i=n-1;elseif(string2i=#) /遇#号结束标志,输出栈中的最后计算结果printf(计算结果为:%dn,top-。

7、num);elseif(string2i= )elsey=top-num;p=top;top=top-next;free(p);x=top-num;p=top;top=top-next;free(p);switch(string2i)case +:a=x+y;p=(struct node *)malloc(sizeof(struct node);p-num=a;p-next=top;top=p;break;case -:a=x-y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-next=top;top=p;break;case *。

8、:a=x*y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-next=top;top=p;break;case /:if(y=0)printf(ERROR:除数为零!n);a=(float)x/y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-next=top;top=p;break;return 0;(5)、主函数void main()struct node *top,*head;top=Initialization();/建立一个链栈,并返回栈顶指针printf(。

9、请输入表达式:n);gets(string1);if(Searchexpression(string1)head=assort(top);/中缀转化为后缀表达式calcolate(head);2、完整函数#include#include#include#include#define MAX 60#define RIGHT 1#define WRONG 0#define DEMAX 15#define NULL 0char string1MAX;char string2MAX;int j=0;struct node /定义结构体。char data;int num;struct node *ne。

10、xt;int Searchexpression(char string1) /判断非法表达式int i1,b1,b2;int m;m=strlen(string1);if(string109)printf(ERROR:表达式缺操作数!n);return(WRONG);for(i1=0;i1data=;top-num=0;top-next=NULL;return top;struct node *assort(struct node *s)/输入字符串struct node *p,*top;int i;top=s;int m;char a;m=strlen(string1);for(i=0;id。

11、ata=a;p-next=top;top=p;break;case *:case /:string2j= ;j+;if(top-data=*)|(top-data=/)string2j=top-data;j+; /比其高,现将栈顶运算符出栈,再进栈。top-data=a;break;elsep=(struct node *)malloc(sizeof(struct node);/否,直接进栈p-data=a;p-next=top;top=p;break;case +:case -:string2j= ;j+;if(top-data=+|top-data=-|top-data=*|top-dat。

12、a=/)string2j=top-data;j+;top-data=a;break;else p=(struct node *)malloc(sizeof(struct node);p-data=a;p-next=top;top=p;break;case ):string2j= ;j+;if(top-data=)printf(input error);break;while(top-data!=()string2j=top-data;j+;p=top;top=top-next;free(p);p=top;top=top-next;free(p);break;while(top-data!=)s。

13、tring2j=top-data;j+;p=top;top=top-next;free(p);string2j=#;printf(后缀表达式为:);for(i=0;i=0&string2i=0&string2nnum=a;p-next=top;top=p;i=n-1;elseif(string2i=#) /遇#号结束标志,输出栈中的最后计算结果printf(计算结果为:%dn,top-num);elseif(string2i= )elsey=top-num;p=top;top=top-next;free(p);x=top-num;p=top;top=top-next;free(p);switc。

14、h(string2i)case +:a=x+y;p=(struct node *)malloc(sizeof(struct node);p-num=a;p-next=top;top=p;break;case -:a=x-y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-next=top;top=p;break;case *:a=x*y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-next=top;top=p;break;case /:if(y=0)printf(ER。

15、ROR:除数为零!n);a=(float)x/y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-next=top;top=p;break;return 0;void main()/主函数struct node *top,*head;top=Initialization();/建立一个链栈,并返回栈顶指针printf(请输入表达式:n);gets(string1);if(Searchexpression(string1)head=assort(top);/中缀转化为后缀表达式calcolate(head);四 使用说明、测试分析及结果1、运行界面:Microsoft Visual C/C+2、测试结果例1:请输入表达式6+3*(6+5)后缀表达式:6 3 6 5 + * +计算结果为:39Press any key to continue 例2:请输入表达式6-3*(7+1ERROR:表达式错误Press any key to continue 五、实验总结教师评语:实验成绩:指导教师签名: 批阅日期:。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值