设计将中缀表达式转换为后缀表达式的算法_第1页
设计将中缀表达式转换为后缀表达式的算法_第2页
设计将中缀表达式转换为后缀表达式的算法_第3页
设计将中缀表达式转换为后缀表达式的算法_第4页
设计将中缀表达式转换为后缀表达式的算法_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、设计将中缀表达式转换为后缀表达式的算法整代码如下:#include#include#include#includeStack.h#define MAX 30int Precedence(char ch)if(ch=+|ch=-)return 2;else if(ch=*|ch=/)return 3;else if(ch=(|ch=)return 4;else if(ch=)return 1;else return 0;void Change(char *s1,char *s2)Stack S=NewEmpty(MAX);int i=0,j=0,one=0;char ch=s1i;Push(,S

2、);while(ch!=)if(ch= )ch=s1+i;else if(isalnum(ch)!=0)if(one=1)s2j+= ;s2j+=ch;ch=s1+i;one=0;else if(ch=()Push(ch,S);ch=s1+i;one=1;else if(ch=)while(Peek(S)!=()s2j+=Pop(S);Pop(S);ch=s1+i;one=1;else if(ch=+|ch=-|ch=*|ch=/)while(Peek(S)!=(&Precedence(Peek(S)=Precedence(ch)s2j+=Pop(S);one=1;Push(ch,S);ch=

3、s1+i;one=1;while(StackEmpty(S)!=1)s2j+=Pop(S);one=1;s2j=0;int main()char s1MAX,s2MAX;printf(Enter the equation:n);gets(s1);Change(s1,s2);printf(%sn,s2);return 0;其中:Stack.h如下:#ifndef STACK_H#define STACK_H#include#includetypedef struct astack *Stack;typedef struct astackint top;int maxtop;char* data;

4、Astack;Stack NewEmpty(int size)Stack S=(Stack)malloc(sizeof(Astack);S-maxtop=size;S-top=-1;S-data=(char*)malloc(size*sizeof(char);return S;int StackEmpty(Stack S)return S-toptop=S-maxtop;int Peek(Stack S)return S-dataS-top;void Push(char x,Stack S)if(StackFull(S)printf(Stack is full!n);exit(1);elseS

5、-data+S-top=x;int Pop(Stack S)if(StackEmpty(S)printf(Stack is empty!n);exit(1);else return S-dataS-top-;Stack NewStack(int size)Stack S=NewEmpty(size);int i,x,num;printf(Please enter the number of data:n);scanf(%d,&num);for(i=0;inum;i+)printf(Please enter the %d date:n,i+1);scanf(%c,&x);Push(x,S);return S; vo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论