版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 con ti nue例2 :请输入表达式6-3* (7+1ERROR表达式错误Press any key to con ti nue二概要设计1. 基本操作、struct node操作结果:创建结构体(2) 、int Searchexpression(char string1)初始条件:表达式string1已经存在。操作结果:判断表达式是否非法(3) 、 struct node *lnitialization()操作结果:创
3、建栈链。(4) 、struct node *assort(struct node *s)string2 中。初始条件:string1、string2已存在。操作结果:将中缀表达式转换为后缀表达式并存在(5) 、struct node *calcolate(struct node *s)操作结果:求出表达式的值2、模块调用图主程序模块 创建结构体 判断表达式是否非法 将中缀表达式转换为后缀表达式 表达式求值三详细设计1、每个模块:(1) 定义结构体struct nodechar data;int num;struct node *n ext;;(2) 判断表达式是否非法int Searchexp
4、ressi on( char stri ng1)int i1,b1,b2;int m;m=strle n( stri ng1);if(stri ng10v0|stri ng109)printf(ERROR:表达式缺操作数!n);return(WRONG);for(i1=0;i1=m;i1+)if(stri ng1i1=()b1+;elseif(stri ng1i1=)b2+;if(b1!=b2)printf(ERROR:缺少括号 n);return(WRONG);for(i1=0;i1m;i1+)if(0=stri ng1i1&stri ng1i1v=9&0=stri ng1i1+1&stri
5、n g1i1+1=9) printf(ERROR:表达式缺操作符! n);return(WRONG);for(i1=0;i1=m;i1+)if(stri ng1i1=+&(stri ng1i1+1=+|stri ng1i1+1=-|stri ng1i1+1=*|stri ng1i1+1=/) printf(ERROR:表达式缺操作数!n);return(WRONG); elseif(stri ng1i1=-&(stri ng1i1+1=+|stri ng1i1+1=-|string1i1+1=*|stri ng1i1+1=/) printf(ERROR:表达式缺操作数!n);return(WR
6、ONG);elseif(stri ng1i1=*&(stri ng1i1+1=+|stri ng1i1+1=-|stri n g1i1+1=*|stri ng1i1+1=/)pri ntf(ERROR:表达式缺操作数!n);return(WRONG);elseif(stri ng1i1=/&(stri ng1i1+1=+|stri ng1i1+1=-|string1i1+1=*|stri ng1i1+1=/)pri ntf(ERROR: 表达式缺操作数!n);return(WRONG);return(RIGHT);(3) 、将中缀表达式转换为后缀表达式struct node *assort(s
7、truct node *s)输入字符串struct node *p,*top;int i;top=s;int m;char a;m=strle n( stri ng1);for(i=0;i=m;i+)a=stri ng1i;if(0data=a;p-n ext=top;top=p;break;case *:case /:stri ng2j= ;j+;if(top-data=*)|(top-data=/)string2j=top-data;j+; /比其高,现将栈顶运算符出栈,再进栈。top-data=a;break; else否,p=(struct node *)malloc(sizeof(s
8、truct no de);/直接进栈p-data=a;p-n ext=top;top=p;break;case +:case -:stri ng2j= ;j+;if(top-data=+|top-data=-|top-data=*|top-data=/)stri ng2j=top-data;j+;top-data=a;break;elsep-data=a;p-n ext=top;top=p; break;case ):stri ng2j= ;j+;if(top-data=)pri ntf(i nput error);break;while(top-data!=() stri ng2j=top-
9、data;j+;p=top;top=top-n ext;free(p);p=top;top=top-n ext;free(p);break;while(top-data!=)stri ng2j=top-data;j+;p=top;top=top-n ext;free(p);stri ng2j=#;printf(后缀表达式为:);for(i=0;ij;i+)if(stri ng2i!=)prin tf(%c ,stri ng2i);prin tf(n );return top;(4) 表达式求值struct node *calcolate(struct node *s)struct node *
10、top,*p;char *q;int x,y,a;int i,n;top=s;指向栈顶的指针for(i=0;i=0&stri ng2iv=9)q=&stri ng2i;a=atoi(q);for(n=i;stri ng2 n=0&stri ng2 nv=9 ;n+)p=(struct node *)malloc(sizeof(struct node );p-num=a;p-n ext=top;top=p;i=n-1;elseif(string2i=#)/遇#号结束标志,输出栈中的最后计算结果printf(计算结果为:%dn,top-num);elseif(string2i=)else y=to
11、p-nu m;p=top;top=top-n ext;free(p);x=top-nu m;p=top;top=top-n ext;free(p);switch(stri ng2i)case +:a=x+y;p=(struct node *)malloc(sizeof(struct no de);p-num=a;p-n ext=top;top=p;break;case -:a=x-y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-n ext=top;top=p;break;case *:a=x*y;p=(struct node *
12、)malloc(sizeof(struct node );p-num=a;p-n ext=top;top=p;break;case 7:if(y=0)printf(ERROR:除数为零!n);a=(float)x/y;p-num=a;p-n ext=top;top=p;p=(struct node *)malloc(sizeof(struct node );break;return 0;(5) 、主函数void mai n()struct node *top,*head;top=l ni tializatio n();建立一个链栈,并返回栈顶指针printf(请输入表达式:n);gets(st
13、ri ng1);if(Searchexpressio n(stri ng1)head=assort(top);/中缀转化为后缀表达式calcolate(head);2、完整函数 #i nclude#i nclude#i ncludevstri ng.h#i nclude#defi ne MAX 60#define RIGHT 1#defi ne WRONG 0#defi ne DEMAX 15#defi ne NULL 0char stri ng1MAX;char stri ng2MAX;int j=0;struct n ode / 定义结构体。char data;int num;struct
14、 node *n ext;/判断非法表达式int Searchexpressi on( char stri ng1)int i1,b1,b2;int m;m=strle n(stri ng1);if(stri ng10v0|stri ng109)printf(ERROR:表达式缺操作数! n);return(WRONG);for(i1=0;i1=m;i1+)if(stri ng1i1=()b1+;elseif(stri ng1i1=)b2+;if(b1!=b2)printf(ERROR:缺少括号 n);return(WRONG);for(i 1= 0;i1m;i1+)if(0=stri ng1
15、i1&stri ng1i1v=9& 0=stri ng1i1+1&stri ng1i1+1v=9) 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=strle n(stri ng1);for(i=0;i=m;i+)a=stri ng1i;if(Ov=stri ng1i&stri ng1i
16、data=a;p-n ext=top;top=p;break;case /:strin g2j= ;j+;if(top-data=*)|(top-data=/)stri ng2j=top-data;j+;/比其咼,现将栈顶运算符出栈,再进栈top-data=a;break;else否,直接p=(struct node *)malloc(sizeof(struct no de);/进栈p-data=a;p-n ext=top;top=p;break;case +:case -:stri ng2j= ;j+;if(top-data=+|top-data=-|top-data=*|top-data=
17、7)stri ng2j=top-data;j+;top-data=a;break;else p=(struct node *)malloc(sizeof(struct no de);p-data=a;p-n ext=top;top=p;break;case ):stri ng2j= ;j+;if(top-data=)pri ntf(i nput error);break;while(top-data!=()stri ng2j=top-data;j+;p=top;top=top-n ext;free(p); p=top;top=top-n ext;free(p);break;while(top-
18、data!=)stri ng2j=top-data;j+;p=top;top=top-n ext;free(p);strin g2j=#;printf(后缀表达式为:);for(i=0;ij;i+)if(stri ng2i!=)prin tf(%c ,stri ng2i);return top;prin tf(n );/计算表达式的值struct node *calcolate(struct node *s) struct node *top,*p;char *q;int x,y,a;int i,n;top=s;/指向栈顶的指针for(i=0;i=0&stri ng2iv=9)q=&stri
19、ng2i;a=atoi(q);for(n=i;stri ng2 n=0&stri ng2 nnum=a;p-n ext=top;top=p;i=n-1;elseif(string2i=#)/遇#号结束标志,输出栈中的最后计算结果 elseprintf(计算结果为:%dn,top-num);if(stri ng2i= ) else y=top-nu m;p=top;top=top-n ext;free(p);x=top-nu m;p=top;top=top-n ext;free(p);switch(stri ng2i)case +:a=x+y;p=(struct node *)malloc(sizeof(struct no de);p-num=a;p-n ext=top;top=p;break;case -:a=x-y;p=(struct node *)malloc(sizeof(struct node );p-
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 员工复工安全教育课件
- 有限空间作业安全培训考试试题及答案
- 母乳喂养测试题及答案题库大全
- 水利水电工程师资格考试试题及答案解析
- 叉车应急预案顺口溜(3篇)
- 2025年国家司法考试(卷三)全真模拟题及答案
- 浅析国有企业员工激励机制存在的问题及对策
- 公务员在社会福利与社会保障中的贡献与影响
- 国企改制过程中的遗留问题及解决途径
- 临沂大学工商管理专业2025-2025学年第一学期管理学原理
- 人美版4年级上册《美术》期末考试试题及答案
- 氮气(MSDS)安全技术说明书
- 安全标准化自评管理制度范本(二篇)
- 骨和关节系统病理学
- 孤独症的音乐治疗
- 部编版四年级下册语文分层作业设计单
- GB/T 16507.4-2013水管锅炉第4部分:受压元件强度计算
- GB/T 13006-2013离心泵、混流泵和轴流泵汽蚀余量
- 2022年11月2022中水珠江规划勘测设计有限公司(原水利部珠江水利委员会设计研究院)招聘考前冲刺卷Ⅰ【3套】附带答案详解
- 中医药学概论绪论课件
- 矿山规模划分标准2019
评论
0/150
提交评论