设计将中缀表达式转换为后缀表达式的算法.docx

设计将中缀表达式转换为后缀表达式的算法.docx

ID:57442357

大小:10.20 KB

页数:5页

时间:2020-08-17

设计将中缀表达式转换为后缀表达式的算法.docx_第1页
设计将中缀表达式转换为后缀表达式的算法.docx_第2页
设计将中缀表达式转换为后缀表达式的算法.docx_第3页
设计将中缀表达式转换为后缀表达式的算法.docx_第4页
设计将中缀表达式转换为后缀表达式的算法.docx_第5页
资源描述:

《设计将中缀表达式转换为后缀表达式的算法.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、设计将中缀表达式转换为后缀表达式的算法整代码如下:#include#include#include#include"Stack.h"#defineMAX30intPrecedence(charch){if(ch=='+'

2、

3、ch=='-')return2;elseif(ch=='*'

4、

5、ch=='/')return3;elseif(ch=='('

6、

7、ch==')')return4;elseif(ch=='@')return1;elsereturn0;}voidCh

8、ange(char*s1,char*s2){StackS=NewEmpty(MAX);inti=0,j=0,one=0;charch=s1[i];Push('@',S);while(ch!='@'){if(ch=='')ch=s1[++i];elseif(isalnum(ch)!=0){if(one==1){s2[j++]='';}s2[j++]=ch;ch=s1[++i];one=0;}elseif(ch=='('){Push(ch,S);ch=s1[++i];one=1;}elseif(ch==')'){while

9、(Peek(S)!='(')s2[j++]=Pop(S);Pop(S);ch=s1[++i];one=1;}elseif(ch=='+'

10、

11、ch=='-'

12、

13、ch=='*'

14、

15、ch=='/'){while(Peek(S)!='('&&Precedence(Peek(S))>=Precedence(ch)){s2[j++]=Pop(S);one=1;}Push(ch,S);ch=s1[++i];one=1;}}while(StackEmpty(S)!=1){s2[j++]=Pop(S);one=1;}s2[j]=''

16、;}intmain(){chars1[MAX],s2[MAX];printf("Entertheequation:");gets(s1);Change(s1,s2);printf("%s",s2);return0;}其中:Stack.h如下:#ifndefSTACK_H#defineSTACK_H#include#includetypedefstructastack*Stack;typedefstructastack{inttop;intmaxtop;char*data;}

17、Astack;StackNewEmpty(intsize){StackS=(Stack)malloc(sizeof(Astack));S->maxtop=size;S->top=-1;S->data=(char*)malloc(size*sizeof(char));returnS;}intStackEmpty(StackS){returnS->top<0;}intStackFull(StackS){returnS->top==S->maxtop;}intPeek(StackS){returnS->data[S->top

18、];}voidPush(charx,StackS){if(StackFull(S)){printf("Stackisfull!");exit(1);}elseS->data[++S->top]=x;}intPop(StackS){if(StackEmpty(S)){printf("Stackisempty!");exit(1);}elsereturnS->data[S->top--];}StackNewStack(intsize){StackS=NewEmpty(size);inti,x,num;printf(

19、"Pleaseenterthenumberofdata:");scanf("%d",&num);for(i=0;itop;i++){printf("%c",S->data[i]);}printf("");}#endif

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

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

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