1、图1.4(3)按“2”选择中缀表达式转变为后缀表达式并求值,如图1.5所示。图1.5(4)按“3”选择中缀表达式转变为前缀表达式并求值,如图1.6所示。图1.6附录:源代码#includestdlib.h#define MAXNUM 100typedef int selemtype1; /定义运算数栈的结点typedef struct /定义运算数栈的类型selemtype1 *base; selemtype1 *top;sqstack1;void InitStack1(sqstack1 &s) /新建一个空运算数栈s.base=(selemtype1 *)malloc(MAXNUM*size
2、of(selemtype1); s.top=s.base; if(!s.base) printf(出错:申请空间失败!n); void Push1(sqstack1 &s,selemtype1 &e) /运算数栈,入栈:插入元素e为新的栈顶元素 if(s.top-s.base=MAXNUM) printf(表达式过长!1n *s.top+ =e;void GetTop1(sqstack1 s,selemtype1 &e) /运算数栈,用e返回栈顶元素e=*(s.top-1);void Popopnd1(sqstack1 &e) /运算数栈,退栈:删除栈顶元素,并用e返回其值e=*-s.top;
3、int stackempy1(sqstack1 s) /运算数栈,若为空栈返回1,否则返回0if(s.top=s.base) return 1; else return 0;typedef char selemtype2; /定义运算符栈的结点类型typedef struct /定义运算符栈类型selemtype2 *base; selemtype2 *top;sqstack2;void InitStack2(sqstack2 &s) /新建一个空运算符栈s.base=(selemtype2 *)malloc(MAXNUM*sizeof(selemtype2);void Push2(sqsta
4、ck2 &s,selemtype2 &e) /运算符栈,入栈:2nvoid GetTop2(sqstack2 s,selemtype2 &e) /运算符栈,用e返回栈顶元素void Popopnd2(sqstack2 &e) /运算符栈,退栈:int stackempy2(sqstack2 s) /运算符栈,若为空栈返回1,否则返回0void priority(char c,int &i) /确定运算符优先级if (c=*|c=/%) i=2 ; else if (c=+-) i=1 ; else i=0;int compare(char a,char b) /比较栈顶元素运算符与外部运算符优
5、先级大小,外部优先级大则返回1,反之返回0int in,out; priority(a,in); priority(b,out); if(outin) return 1;void Operat(sqstack1 &OPND,sqstack2 &OPTR)int num1,num2,num; char c; Popopnd1(OPND,num2); Popopnd1(OPND,num1); Popopnd2(OPTR,c); switch(c) case :num=num1+num2;break; case num=num1-num2;num=num1*num2;num=num1/num2;nu
6、m=num1%num2; Push1(OPND,num);void Operatqianzhui(sqstack1 &void houzhuiqiuzhi(Lnode *p,int &e) /后缀表达式求值sqstack1 OPND; /运算数栈 sqstack2 OPTR; /运算符栈 int n; p=p-next; InitStack1(OPND); InitStack2(OPTR); while(p) switch(p-data) case 1:n=p-data1; Push1(OPND,n); break; case 2:c=p-data2; Push2(OPTR,c); Opera
7、t(OPND,OPTR); default:printf(结点有误Popopnd1(OPND,n);e=n;void zhongzhui(Lnode *p) /中缀表达式求值 char c,c2; Lnode *first; first=p; while(!stackempy2(OPTR)|p) while(p) if(stackempy2(OPTR) Push2(OPTR,c); else switch(c)( Push2(OPTR,c);) GetTop2(OPTR,c2); while(c2!=) Operat(OPND,OPTR); Popopnd2(OPTR,c2); if(comp
8、are(c2,c) Push2(OPTR,c); else Operat(OPND,OPTR);stackempy2(OPTR) Operat(OPND,OPTR); Popopnd1(OPND,n); p=first- if(p-data=1) printf(%d ,p-data1); if(p-data=2) printf(%cdata2);=%d ,n);void houzhui(Lnode *p) /中缀表达式转化为后缀表达式sqstack2 OPTR; Lnode *r,*q,*head; q=(Lnode*)malloc(sizeof(struct Node); head=q; s
9、witch(p- r=(Lnode*)malloc(sizeof(struct Node); q-next=r; q=q-data=1;data1=n; case r=(Lnode*)malloc(sizeof(struct Node);data=2;data2=c2;compare(c2,c) Popopnd2(OPTR,c2); break;next=NULL; q=head- while(q) if(q-,q- if(q- houzhuiqiuzhi(head,n);void qianzhuiqiuzhi(Lnode2 *p,int &e) /前缀表达式求值 Lnode2 *head;
10、head=p; while(p!=head) Operatqianzhui(OPND,OPTR);void qianzhui(Lnode *p) /中缀表达式转化为前缀表达式 Lnode2 *q,*head,*r,*head2,*s; q=(Lnode2*)malloc(sizeof(struct Node2); /建立存中缀表达式的双向循环链表 r=(Lnode2*)malloc(sizeof(struct Node2); r-prior=q;data=p-data;data1=p-data2=p-next=head; head- s=(Lnode2*)malloc(sizeof(struc
11、t Node2); /建立存前缀表达式的双向循环链表 head2=s; while(q! switch(q-n=q- r=(Lnode2*)malloc(sizeof(struct Node2); s-prior=s; s=s-c=q- else GetTop2(OPTR,c2); if(c2=) Push2(OPTR,c); else switch(c) case Push2(OPTR,c); r=(Lnode2*)malloc(sizeof(struct Node2);prior;next=head2; head2- while(s!=head2) if(s-,s- if(s- qianz
12、huiqiuzhi(head2,n);int main() char n10; int i,j,k,a,b,z,y,e; Lnode *p,*q,*first; i=0;e=1;a=0;b=1;z=0;y=0; p=(Lnode*)malloc(sizeof(struct Node);请输入中缀表达式 do c = getchar(); if(0=c&cn0next=q; for(k=0;ki;k+) for(j=0;jdata=2)data2!c! b=0;data2=c; if(c=) z+;) y+; while (c != if(z!=y) b=0;p-if(b=0)输入中缀表达式有误else printf(输入1中缀表达式求值,输入2后缀表达式求值,输入3前缀表达式求值 scanf(%d,&b); if(b=1) zhongzhui(first); if(b=2) houzhui(first); if(b=3) qianzhui(first);return 1;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1