中缀表达式转后缀表达式

中缀表达式转后缀表达式

ID:34733382

大小:108.18 KB

页数:7页

时间:2019-03-10

中缀表达式转后缀表达式_第1页
中缀表达式转后缀表达式_第2页
中缀表达式转后缀表达式_第3页
中缀表达式转后缀表达式_第4页
中缀表达式转后缀表达式_第5页
资源描述:

《中缀表达式转后缀表达式》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、昆明理工大学信息工程与自动化学院学生实验报告(2011—2012学年第1学期)课程名称:数据结构开课实验室:信自楼4422011年11月04日年级、专业、班学号姓名成绩实验项目名称中缀表达式转后缀表达式指导教师教师评语教师签名:年月日一、程序功能:(1).中缀表达式转换为后缀表达式的主要函数设置如下:主程序:main(){structnode*top,*head;intm;top=Initialization();//建立一个链栈,并返回栈顶指针printf("请输入表达式:");head=assort(top);//中缀转化为后缀表达式calcolate(head

2、);//后缀表达式的计算}(2).程序代码如下:#include#include#include#include#defineMAX60#defineDEMAX15#defineNULL0charstring1[MAX];charstring2[MAX];intj=0;structnode{chardata;intnum;structnode*next;};structnode*Initialization()//初始化栈链,链栈不带头结点{structnode*top;top=(stru

3、ctnode*)malloc(sizeof(structnode));top->data='@';top->num=0;top->next=NULL;returntop;}structnode*assort(structnode*s)//输入字符串{structnode*p,*top;inti;top=s;intm;chara;gets(string1);m=strlen(string1);for(i=0;i<=m;i++){a=string1[i];if('0'<=string1[i]&&string1[i]<='9’){string2[j]=string1[i]

4、;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=='*')

5、

6、(top->data=='/')){string2[j]=top->data;j++;//比其高,现将栈顶运算符出栈,再进栈。top->data=a;break;}else{p=(structnode*)malloc(sizeof(structno

7、de));//否,直接进栈p->data=a;p->next=top;top=p;break;}case'+':case'-':{string2[j]='';j++;if(top->data=='+'

8、

9、top->data=='-'

10、

11、top->data=='*'

12、

13、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')':

14、{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("转化后的后缀表

15、达式为:%s",string2);returntop;}structnode*calcolate(structnode*s){structnode*top,*p;char*q;intx,y,a;inti,n;top=s;//指向栈顶的指针for(i=0;i<=j;i++)//遍历字符串string2{if(string2[i]>='0'&&string2[i]<='9'){q=&string2[i];a=atoi(q);for(n=i;string2[n]>='0'&&string2[n]<='9';n++){}p=(structnode*)malloc(s

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。