




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、c語(yǔ)言實(shí)現(xiàn)中綴,后綴,前綴表達(dá)式轉(zhuǎn)換并求值 九 #include<stdio.h> #include<stdlib.h>#define MAXNUM 100typedef struct Node /定義存儲(chǔ)中綴表達(dá)式的結(jié)點(diǎn)類型int data;int data1;char data2;struct Node *next;Lnode; typedef struct Node2 /定義存儲(chǔ)前綴表達(dá)式的結(jié)點(diǎn)類型int data;int data1;char data2;struct Node2 *next;struct Node2 *prior
2、;Lnode2; typedef int selemtype1; /定義運(yùn)算數(shù)棧的結(jié)點(diǎn)typedef struct /定義運(yùn)算數(shù)棧的類型selemtype1 *base;selemtype1 *top;sqstack1;void InitStack1(sqstack1 &s) /新建一個(gè)空運(yùn)算數(shù)棧s.base=(selemtype1 *)malloc(MAXNUM*sizeof(selemtype1);s.top=s.base;if(!s.base) printf("出錯(cuò):申請(qǐng)空間失?。"); void Push1(sqstack1 &am
3、p;amp;s,selemtype1 &e) /運(yùn)算數(shù)棧,入棧:插入元素e為新的棧頂元素 if(s.top-s.base>=MAXNUM)printf("出錯(cuò):表達(dá)式過(guò)長(zhǎng)!1n");*s.top+ =e; void GetTop1(sqstack1 s,selemtype1 &e) /運(yùn)算數(shù)棧,用e返回棧頂元素e=*(s.top-1);void Popopnd1(sqstack1 &s,selemtype1 &e) /運(yùn)算數(shù)棧,退棧:刪除棧頂元素,并用e返回其值e=*-s.top;
4、int stackempy1(sqstack1 s) /運(yùn)算數(shù)棧,若為空棧返回1,否則返回0if(s.top=s.base) return 1;else return 0;typedef char selemtype2; /定義運(yùn)算符棧的結(jié)點(diǎn)類型typedef struct /定義運(yùn)算符棧類型selemtype2 *base;selemtype2 *top;sqstack2;void InitStack2(sqstack2 &s) /新建一個(gè)空運(yùn)算符棧s.base=(selemtype2 *)malloc(MAXNUM*sizeof(selemtype2);s.top=s.ba
5、se;if(!s.base) printf("出錯(cuò):申請(qǐng)空間失??!n"); void Push2(sqstack2 &s,selemtype2 &e) /運(yùn)算符棧,入棧:插入元素e為新的棧頂元素 if(s.top-s.base>=MAXNUM)printf("出錯(cuò):表達(dá)式過(guò)長(zhǎng)!2n");*s.top+ =e;void GetTop2(sqstack2 s,selemtype2 &e) /運(yùn)算符棧,用e返回棧頂元素e=*(s.top-1);void Popopnd
6、2(sqstack2 &s,selemtype2 &e) /運(yùn)算符棧,退棧:刪除棧頂元素,并用e返回其值e=*-s.top;int stackempy2(sqstack2 s) /運(yùn)算符棧,若為空棧返回1,否則返回0if(s.top=s.base) return 1;else return 0;void priority(char c,int &i) /確定運(yùn)算符優(yōu)先級(jí)if (c='*'|c='/'|c='%') i=2 ;else if (c=&am
7、p;#39;+'|c='-') i=1 ;else i=0;int compare(char a,char b) /比較棧頂元素運(yùn)算符與外部運(yùn)算符優(yōu)先級(jí)大小,外部?jī)?yōu)先級(jí)大則返回1,反之返回0int in,out;priority(a,in);priority(b,out);if(out>in) return 1;else return 0;void Operat(sqstack1 &OPND,sqstack2 &OPTR)int num1,num2,num;char c;Popopnd1(OPND,n
8、um2);Popopnd1(OPND,num1);Popopnd2(OPTR,c);switch(c)case '+':num=num1+num2;break;case '-':num=num1-num2;break;case '*':num=num1*num2;break;case '/':num=num1/num2;break;case '%':num=num1%num2;break;Push1(OPND, num); void O
9、peratqianzhui(sqstack1 &OPND,sqstack2 &OPTR)int num1,num2,num;char c;Popopnd1(OPND,num1);Popopnd1(OPND,num2);Popopnd2(OPTR,c);switch(c)case '+':num=num1+num2;break;case '-':num=num1-num2;break;case '*':num=num1*num2;break;case '
10、/':num=num1/num2;break;case '%':num=num1%num2;break;Push1(OPND,num); void houzhuiqiuzhi(Lnode *p,int &e) /后綴表達(dá)式求值sqstack1 OPND; /運(yùn)算數(shù)棧sqstack2 OPTR; /運(yùn)算符棧int n;char c;p=p->next;InitStack1(OPND);InitStack2(OPTR);while(p)switch(p->data)case 1:n=p->da
11、ta1;Push1(OPND,n);break;case 2:c=p->data2;Push2(OPTR,c);Operat(OPND,OPTR);break;default:printf("結(jié)點(diǎn)有誤");break;p=p->next;Popopnd1(OPND,n);e=n;void zhongzhui(Lnode *p) /中綴表達(dá)式求值sqstack1 OPND; /運(yùn)算數(shù)棧sqstack2 OPTR; /運(yùn)算符棧int n;char c,c2;Lnode *first;first=p;p=p->next;I
12、nitStack1(OPND);InitStack2(OPTR);while(!stackempy2(OPTR)|p)while(p)switch(p->data)case 1:n=p->data1;Push1(OPND,n);break;case 2:c=p->data2;if(stackempy2(OPTR) Push2(OPTR,c);else switch(c)case '(': Push2(OPTR,c);break;case ')': GetTop2(OPTR,c2);whil
13、e(c2!='(')Operat(OPND,OPTR);GetTop2(OPTR,c2);Popopnd2(OPTR,c2);break;default: GetTop2(OPTR,c2);if(compare(c2,c) Push2(OPTR,c);else Operat(OPND,OPTR);Push2(OPTR,c);break;break;default: printf("結(jié)點(diǎn)有誤");break;p=p->next;while(!stackempy2(OPTR)Operat(OPND,OPTR);Pop
14、opnd1(OPND,n);p=first->next;while(p)if(p->data=1) printf("%d ",p->data1);if(p->data=2) printf("%c",p->data2);p=p->next;printf("=%d ",n);void houzhui(Lnode *p) /中綴表達(dá)式轉(zhuǎn)化為后綴表達(dá)式sqstack2 OPTR; /運(yùn)算符棧Lnode *r,*q
15、,*head;int n;char c,c2;InitStack2(OPTR);p=p->next;q=(Lnode*)malloc(sizeof(struct Node);head=q;while(p) switch(p->data)case 1:n=p->data1;r=(Lnode*)malloc(sizeof(struct Node); q->next=r;q=q->next;q->data=1;q->data1=n; break;case 2:c=p->data2;if(s
16、tackempy2(OPTR) Push2(OPTR,c);else switch(c) case '(': Push2(OPTR,c);break;case ')': Popopnd2(OPTR,c2);while(c2!='(') r=(Lnode*)malloc(sizeof(struct Node); q->next=r;q=q->next;q->data=2;q->data2=c2; Popopnd2(OPTR,c2);break;d
17、efault: GetTop2(OPTR,c2);while(!compare(c2,c) Popopnd2(OPTR,c2);r=(Lnode*)malloc(sizeof(struct Node); q->next=r;q=q->next;q->data=2;q->data2=c2;GetTop2(OPTR,c2); Push2(OPTR,c);break; break;default: printf("結(jié)點(diǎn)有誤");break;p=p->next;while(!stackempy2(
18、OPTR) Popopnd2(OPTR,c2);r=(Lnode*)malloc(sizeof(struct Node); q->next=r;q=q->next;q->data=2;q->data2=c2; q->next=NULL;q=head->next;while(q)if(q->data=1) printf("%d ",q->data1);if(q->data=2) printf("%c"
19、,q->data2);q=q->next;houzhuiqiuzhi(head,n);printf("=%d ",n);void qianzhuiqiuzhi(Lnode2 *p,int &e) /前綴表達(dá)式求值sqstack1 OPND; /運(yùn)算數(shù)棧sqstack2 OPTR; /運(yùn)算符棧int n;char c;Lnode2 *head;head=p;p=p->next;InitStack1(OPND);InitStack2(OPTR);while(p!=head)switch(p->
20、;data)case 1:n=p->data1;Push1(OPND,n);break;case 2:c=p->data2;Push2(OPTR,c);Operatqianzhui(OPND,OPTR);break;default:printf("結(jié)點(diǎn)有誤");break;p=p->next;Popopnd1(OPND,n);e=n;void qianzhui(Lnode *p) /中綴表達(dá)式轉(zhuǎn)化為前綴表達(dá)式sqstack2 OPTR; /運(yùn)算符棧InitStack2(OPTR);int n;char c,c2;Ln
21、ode *first;Lnode2 *q,*head,*r,*head2,*s;first=p;p=p->next;q=(Lnode2*)malloc(sizeof(struct Node2); /建立存中綴表達(dá)式的雙向循環(huán)鏈表head=q;while(p)r=(Lnode2*)malloc(sizeof(struct Node2);q->next=r;r->prior=q;q=q->next;q->data=p->data;q->data1=p->data1;q->d
22、ata2=p->data2;p=p->next;q->next=head;head->prior=q;s=(Lnode2*)malloc(sizeof(struct Node2); /建立存前綴表達(dá)式的雙向循環(huán)鏈表head2=s;while(q!=head)switch(q->data)case 1:n=q->data1;r=(Lnode2*)ma lloc(sizeof(struct Node2);s->next=r;r->prior=s;s=s->next;s-&a
23、mp;gt;data=1;s->data1=n; break;case 2:c=q->data2;if(stackempy2(OPTR) Push2(OPTR,c);else GetTop2(OPTR,c2);if(c2=')') Push2(OPTR,c);else switch(c) case ')':Push2(OPTR,c);break;case '(': Popopnd2(OPTR,c2);while(c2!=')') r=(Ln
24、ode2*)malloc(sizeof(struct Node2);s->next=r;r->prior=s;s=s->next;s->data=2;s->data2=c2; Popopnd2(OPTR,c2);break;default: GetTop2(OPTR,c2);while(!compare(c2,c) Popopnd2(OPTR,c2);r=(Lnode2*)malloc(sizeof(struct Node2);s->next=r;r->prior=s;s=s->ne
25、xt;s->data=2;s->data2=c2;GetTop2(OPTR,c2); Push2(OPTR,c);break; break;default:printf("結(jié)點(diǎn)有誤");break;q=q->prior;while(!stackempy2(OPTR) Popopnd2(OPTR,c2);r=(Lnode2*)malloc(sizeof(struct Node2);s->next=r;r->prior=s;s=s->next;s->data=2;s
26、->data2=c2; s->next=head2;head2->prior=s;while(s!=head2)if(s->data=1) printf("%d ",s->data1);if(s->data=2) printf("%c",s->data2);s=s->prior;qianzhuiqiuzhi(head2,n);printf("=%d ",n);int main(
27、) char n10;char c;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);first=p;printf("請(qǐng)輸入中綴表達(dá)式");do c = getchar();if('0'<=c&&c<='9') ni=c;i+; else switch (c) case '
28、+': case '-': case '*': case '/': case '%': case '(': case ')': case 'n': if(n0>'0'&&n0<='9') q=(Lnode*)malloc(sizeof(struct Node);p->next=q;p=p->next;for(k=0;k<i;k+) for(j=0;j<=i-k-2;j+)e=e*10; a=a+(nk-'0')*e;e=1;nk='0'p-&
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 湖北省課題申報(bào)評(píng)審書
- 婦聯(lián)調(diào)研課題申報(bào)書
- 課題申報(bào)書序號(hào)
- 節(jié)水潔具研究課題申報(bào)書
- Unit 3 Keep Fit 單元檢測(cè)練習(xí)(含答案)七年級(jí)英語(yǔ)下冊(cè)(人教版2024)
- 員工合同范本32條
- 學(xué)校美育工作課題申報(bào)書
- 付款保證合同范本
- 三拆除工程合同范本
- 農(nóng)村梯田出租合同范本
- 網(wǎng)絡(luò)安全用戶實(shí)體行為分析技術(shù)UEBA白皮書
- 室內(nèi)設(shè)計(jì)-中式古典風(fēng)格課件
- 軌道鋪設(shè)施工專項(xiàng)方案
- MOC3061驅(qū)動(dòng)BT134雙向可控硅
- 七下地理《俄羅斯》PPT課件
- 員工勞動(dòng)合同(易才簽訂要求)
- 無(wú)線通信與網(wǎng)絡(luò)復(fù)習(xí)資料
- 八大員考試試題——?jiǎng)趧?wù)員題庫(kù)
- 第七章 住院患者營(yíng)養(yǎng)風(fēng)險(xiǎn)篩查與評(píng)價(jià)
- 人教版小學(xué)數(shù)學(xué)五年級(jí)下冊(cè)教材分析
- 省十一屆人大三次會(huì)議秘書處工作總結(jié)
評(píng)論
0/150
提交評(píng)論