




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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(string10<'0'|string10>'9')printf("ERROR:表达式缺操作数!n");return(WRONG);for(i1=0;i1<=m;i1+)if(string1i1='(')b1+;elseif(string1i1=')')b2+;if(b1!=b2)printf("ERROR:缺少括号n");return(WRONG);for(i1=0;i1<m
5、;i1+)if('0'<=string1i1&&string1i1<='9'&&'0'<=string1i1+1&&string1i1+1<='9') printf("ERROR:表达式缺操作符!n");return(WRONG);for(i1=0;i1<=m;i1+)if(string1i1='+'&&(string1i1+1='+'|string1i1+1='-'|s
6、tring1i1+1='*'|string1i1+1='/') printf("ERROR:表达式缺操作数!n");return(WRONG);elseif(string1i1='-'&&(string1i1+1='+'|string1i1+1='-'|string1i1+1='*'|string1i1+1='/') printf("ERROR:表达式缺操作数!n");return(WRONG);elseif(string1i1
7、='*'&&(string1i1+1='+'|string1i1+1='-'|string1i1+1='*'|string1i1+1='/')printf("ERROR:表达式缺操作数!n");return(WRONG);elseif(string1i1='/'&&(string1i1+1='+'|string1i1+1='-'|string1i1+1='*'|string1i1+1='/
8、39;)printf("ERROR:表达式缺操作数!n");return(WRONG);return(RIGHT); (3)、将中缀表达式转换为后缀表达式struct node *assort(struct node *s)/输入字符串struct node *p,*top;int i;top=s;int m;char a;m=strlen(string1); for(i=0;i<=m;i+)a=string1i;if('0'<=string1i&&string1i<='9')string2j=string1
9、i;j+;elseswitch(a)case '(':p=(struct node *)malloc(sizeof(struct node);p->data=a;p->next=top;top=p;break; case '*':case '/':string2j=' 'j+;if(top->data='*')|(top->data='/')string2j=top->data;j+; /比其高,现将栈顶运算符出栈,再进栈。top->data=a;break;el
10、sep=(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->data='/')string2j=top->data;j+;top->data=a;break;e
11、lse 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->
12、next;free(p);break;while(top->data!='')string2j=top->data;j+;p=top;top=top->next;free(p);string2j='#'printf("后缀表达式为:");for(i=0;i<j;i+)if(string2i!=' ')printf("%c ",string2i);printf("n ");return top; (4)表达式求值struct node *calcolate(stru
13、ct node *s)struct node *top,*p;char *q;int x,y,a;int i,n;top=s;/指向栈顶的指针for(i=0;i<=j;i+)/遍历字符串string2if(string2i>='0'&&string2i<='9')q=&string2i;a=atoi(q); for(n=i;string2n>='0'&&string2n<='9'n+)p=(struct node *)malloc(sizeof(struct n
14、ode );p->num=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);switch(string2i)case '+':a=x+y;p=(
15、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 &
16、#39;/':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("请输入表达式:n");gets(string1);if(Searchexpression(st
17、ring1)head=assort(top);/中缀转化为后缀表达式calcolate(head);2、完整函数#include<stdio.h>#include<malloc.h>#include<string.h>#include<stdlib.h>#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 nu
18、m;struct node *next;int Searchexpression(char string1) /判断非法表达式int i1,b1,b2;int m;m=strlen(string1);if(string10<'0'|string10>'9')printf("ERROR:表达式缺操作数!n");return(WRONG);for(i1=0;i1<=m;i1+)if(string1i1='(')b1+;elseif(string1i1=')')b2+;if(b1!=b2)print
19、f("ERROR:缺少括号n");return(WRONG);for(i1=0;i1<m;i1+)if('0'<=string1i1&&string1i1<='9'&&'0'<=string1i1+1&&string1i1+1<='9') printf("ERROR:表达式缺操作符!n");return(WRONG);for(i1=0;i1<=m;i1+)if(string1i1='+'&am
20、p;&(string1i1+1='+'|string1i1+1='-'|string1i1+1='*'|string1i1+1='/') printf("ERROR:表达式缺操作数!n");return(WRONG);elseif(string1i1='-'&&(string1i1+1='+'|string1i1+1='-'|string1i1+1='*'|string1i1+1='/') printf(&q
21、uot;ERROR:表达式缺操作数!n");return(WRONG);elseif(string1i1='*'&&(string1i1+1='+'|string1i1+1='-'|string1i1+1='*'|string1i1+1='/')printf("ERROR:表达式缺操作数!n");return(WRONG);elseif(string1i1='/'&&(string1i1+1='+'|string1i1+1
22、='-'|string1i1+1='*'|string1i1+1='/')printf("ERROR:表达式缺操作数!n");return(WRONG);return(RIGHT);struct node *Initialization()/初始化栈链,链栈不带头结点struct node *top;top=(struct node *)malloc(sizeof(struct node);top->data=''top->num=0;top->next=NULL;return top;str
23、uct node *assort(struct node *s)/输入字符串struct node *p,*top;int i;top=s;int m;char a;m=strlen(string1); for(i=0;i<=m;i+)a=string1i;if('0'<=string1i&&string1i<='9')string2j=string1i;j+;elseswitch(a)case '(':p=(struct node *)malloc(sizeof(struct node);p->data=
24、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;bre
25、ak;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; c
26、ase ')':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!='')string2j=top->data;j+;p=top;top=top-&
27、gt;next;free(p);string2j='#'printf("后缀表达式为:");for(i=0;i<j;i+)if(string2i!=' ')printf("%c ",string2i);printf("n ");return top;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<=j;i+)/
28、遍历字符串string2if(string2i>='0'&&string2i<='9')q=&string2i;a=atoi(q); for(n=i;string2n>='0'&&string2n<='9'n+)p=(struct node *)malloc(sizeof(struct node );p->num=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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【白城】2025年吉林白城市洮北区面向下半年应征入伍高校毕业生招聘事业单位工作人员17人笔试历年典型考题及考点剖析附带答案详解
- 【乐山】2025年上半年四川峨眉山市赴四川师范大学招聘事业单位工作人员15人笔试历年典型考题及考点剖析附带答案详解
- 【湘潭】2025年湖南湘潭市岳塘区事业单位引进急需紧缺专业人才55人笔试历年典型考题及考点剖析附带答案详解
- 古迹作文教学课件
- 2025年绿色规划视角下低碳城市建设实践案例分析报告
- 2025年共享出行平台信用积分兑换机制与市场响应策略研究报告
- 跨境电商保税仓仓储资源整合与共享模式研究报告
- 绿色金融产品创新与2025年绿色金融科技应用报告
- 企业合同履约管理办法
- 企业存储收费管理办法
- 企业人力资源管理创新与发展趋势分析
- 2022城市轨道交通列车驾驶员技能及素质要求第1部分:地铁、轻轨和单轨
- 《人工智能基础与应用(第2版)》全套教学课件
- 2025年宿迁市公需考试试题
- 建设工程项目的组织协调保障措施
- 2025年春季学期国家开放大学行政管理专业毕业论文选题指南
- CO2还原合成甲醇反应器研究进展
- DB3713T-291-2023工程建设项目“多测合一”技术规程
- 苏教版四年级下册数学计算题每日一练带答案(共20天)
- 江苏连云港某公司“12.9”爆炸事故报告
- 人教版三年级上下数学试卷合集-综合素质训练
评论
0/150
提交评论