首页 将中缀表达式转换为后缀表达式并计算

将中缀表达式转换为后缀表达式并计算

举报
开通vip

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

将中缀表达式转换为后缀表达式并计算
.《数据结构》实验报告◎实验题目:使用键盘输入 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 达式,计算表达式的值并输出;将表达式转化成后缀表达式输出,利用后缀表达式求表达式的值并输出。◎实验目的:使用栈的操作编写关于数据结构的程序。◎实验内容:写出程序并上机调试、通过。一、需求分析1、演示程序以用户和计算机的对话方式执行,即在计算机终端上显示“请输入表达式”时输入中缀表达式。然后计算机终端输出转换后的后缀表达式及计算后的结果。2、程序执行的命令包括:(1)构造链栈;(2)输入数据;(3)判断输入的表达式是否为非法表达式;(4)将中缀表达式转换为后缀表达式;(5)计算表达式的值;(6)输出。(7)结束4、本程序能将中缀表达式转换为后缀表达式,并且能计算表达式的值。5、输入及输出示例:例1:请输入表达式.6+3*(6+5)后缀表达式:6365+*+计算结果为:39Pressanykeytocontinue例2:请输入表达式6-3*(7+1ERROR:表达式错误Pressanykeytocontinue二概要设计1.基本操作(1)、structnode操作结果:创建结构体(2)、intSearchexpression(charstring1[])初始条件:表达式string1已经存在。操作结果:判断表达式是否非法(3)、structnode*Initialization()操作结果:创建栈链。(4)、structnode*assort(structnode*s)初始条件:string1、string2已存在。操作结果:将中缀表达式转换为后缀表达式并存在string2中。(5)、structnode*calcolate(structnode*s).操作结果:求出表达式的值2、模块调用图主程序模块创建结构体判断表达式是否非法将中缀表达式转换为后缀表达式表达式求值三详细设计1、每个模块:(1)定义结构体structnode{chardata;intnum;structnode*next;};(2)判断表达式是否非法intSearchexpression(charstring1[]){.inti1,b1,b2;intm;m=strlen(string1);if(string1[0]<'0'||string1[0]>'9'){printf("ERROR:表达式缺操作数!\n");return(WRONG);}for(i1=0;i1<=m;i1++){if(string1[i1]=='(')b1++;elseif(string1[i1]==')')b2++;}if(b1!=b2){printf("ERROR:缺少括号\n");return(WRONG);}for(i1=0;i1data=a;p->next=top;top=p;break;}case'*':case'/':string2[j]='';j++;if((top->data=='*')||(top->data=='/')){string2[j]=top->data;j++;//比其高,现将栈顶运算符出栈,再进栈。top->data=a;break;}else.{p=(structnode*)malloc(sizeof(structnode));//否,直接进栈p->data=a;p->next=top;top=p;break;}case'+':case'-':{string2[j]='';j++;if(top->data=='+'||top->data=='-'||top->data=='*'||top->data=='/'){string2[j]=top->data;j++;top->data=a;break;}else{p=(structnode*)malloc(sizeof(structnode));.p->data=a;p->next=top;top=p;break;}}case')':{string2[j]='';j++;if(top->data=='@'){printf("inputerror");break;}while(top->data!='('){string2[j]=top->data;j++;p=top;top=top->next;free(p);}p=top;top=top->next;free(p);break;}}.}}while(top->data!='@'){string2[j]=top->data;j++;p=top;top=top->next;free(p);}string2[j]='#';printf("后缀表达式为:");for(i=0;i='0'&&string2[i]<='9'){q=&string2[i];a=atoi(q);for(n=i;string2[n]>='0'&&string2[n]<='9';n++){}p=(structnode*)malloc(sizeof(structnode));p->num=a;p->next=top;top=p;i=n-1;}elseif(string2[i]=='#')//遇#号结束标志,输出栈中的最后计算结果printf("计算结果为:%d\n",top->num);else{if(string2[i]==''){}.else{y=top->num;p=top;top=top->next;free(p);x=top->num;p=top;top=top->next;free(p);switch(string2[i]){case'+':{a=x+y;p=(structnode*)malloc(sizeof(structnode));p->num=a;p->next=top;top=p;break;}case'-':{a=x-y;p=(structnode*)malloc(sizeof(structnode));p->num=a;p->next=top;top=p;break;}case'*':{a=x*y;p=(structnode*)malloc(sizeof(structnode));p->num=a;p->next=top;top=p;break;}case'/':{if(y==0)printf("ERROR:除数为零!\n");a=(float)x/y;p=(structnode*)malloc(sizeof(structnode));p->num=a;p->next=top;top=p;.break;}}}}}return0;}(5)、主函数voidmain(){structnode*top,*head;top=Initialization();//建立一个链栈,并返回栈顶指针printf("请输入表达式:\n");gets(string1);if(Searchexpression(string1)){head=assort(top);//中缀转化为后缀表达式calcolate(head);}}2、完整函数#include.#include#include#include#defineMAX60#defineRIGHT1#defineWRONG0#defineDEMAX15#defineNULL0charstring1[MAX];charstring2[MAX];intj=0;structnode//定义结构体。{chardata;intnum;structnode*next;};intSearchexpression(charstring1[])//判断非法表达式{inti1,b1,b2;intm;m=strlen(string1);.if(string1[0]<'0'||string1[0]>'9'){printf("ERROR:表达式缺操作数!\n");return(WRONG);}for(i1=0;i1<=m;i1++){if(string1[i1]=='(')b1++;elseif(string1[i1]==')')b2++;}if(b1!=b2){printf("ERROR:缺少括号\n");return(WRONG);}for(i1=0;i1data='@';top->num=0;top->next=NULL;returntop;}structnode*assort(structnode*s)//输入字符串{structnode*p,*top;inti;top=s;intm;chara;m=strlen(string1);for(i=0;i<=m;i++).{a=string1[i];if('0'<=string1[i]&&string1[i]<='9'){string2[j]=string1[i];j++;}else{switch(a){case'(':{p=(structnode*)malloc(sizeof(structnode));p->data=a;p->next=top;top=p;break;}case'*':case'/':string2[j]='';j++;if((top->data=='*')||(top->data=='/')){string2[j]=top->data;j++;//比其高,现将栈顶运算符出.栈,再进栈。top->data=a;break;}else{p=(structnode*)malloc(sizeof(structnode));//否,直接进栈p->data=a;p->next=top;top=p;break;}case'+':case'-':{string2[j]='';j++;if(top->data=='+'||top->data=='-'||top->data=='*'||top->data=='/'){string2[j]=top->data;j++;top->data=a;break;.}else{p=(structnode*)malloc(sizeof(structnode));p->data=a;p->next=top;top=p;break;}}case')':{string2[j]='';j++;if(top->data=='@'){printf("inputerror");break;}while(top->data!='('){string2[j]=top->data;j++;p=top;top=top->next;free(p);}p=top;top=top->next;free(p);.break;}}}}while(top->data!='@'){string2[j]=top->data;j++;p=top;top=top->next;free(p);}string2[j]='#';printf("后缀表达式为:");for(i=0;i='0'&&string2[i]<='9'){q=&string2[i];a=atoi(q);for(n=i;string2[n]>='0'&&string2[n]<='9';n++){}p=(structnode*)malloc(sizeof(structnode));p->num=a;p->next=top;top=p;i=n-1;}elseif(string2[i]=='#')//遇#号结束标志,输出栈中的最后计算结果printf("计算结果为:%d\n",top->num);else.{if(string2[i]==''){}else{y=top->num;p=top;top=top->next;free(p);x=top->num;p=top;top=top->next;free(p);switch(string2[i]){case'+':{a=x+y;p=(structnode*)malloc(sizeof(structnode));p->num=a;p->next=top;top=p;break;}case'-':{a=x-y;p=(structnode*)malloc(sizeof(structnode));p->num=a;p->next=top;top=p;break;}case'*':{a=x*y;p=(structnode*)malloc(sizeof(structnode));p->num=a;p->next=top;top=p;break;}case'/':{if(y==0)printf("ERROR:除数为零!\n");a=(float)x/y;.p=(structnode*)malloc(sizeof(structnode));p->num=a;p->next=top;top=p;break;}}}}}return0;}voidmain()//主函数{structnode*top,*head;top=Initialization();//建立一个链栈,并返回栈顶指针printf("请输入表达式:\n");gets(string1);if(Searchexpression(string1)){head=assort(top);//中缀转化为后缀表达式calcolate(head);}}.四使用说明、测试分析及结果1、运行界面:MicrosoftVisualC/C++2、测试结果例1:请输入表达式6+3*(6+5)后缀表达式:6365+*+计算结果为:39Pressanykeytocontinue例2:请输入表达式6-3*(7+1ERROR:表达式错误Pressanykeytocontinue五、实验总结教师评语:实验成绩:.指导教师签名:批阅日期:
本文档为【将中缀表达式转换为后缀表达式并计算】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
二姐爱干净
暂无简介~
格式:pdf
大小:517KB
软件:PDF阅读器
页数:26
分类:
上传时间:2023-04-17
浏览量:8