




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、數(shù)據結構實驗報告實驗題目:使用鍵盤輸入表達式,計算表達式的值并輸出;將表達式轉化成后 綴表達式輸出,利用后綴表達式求表達式的值并輸出。實驗目的:使用棧的操作編寫關于數(shù)據結構的程序。實驗內容:寫出程序并上機調試、通過。一、需求分析1、演示程序以用戶和計算機的對話方式執(zhí)行,即在計算機終端上顯示“請輸入表達式”時輸入中綴表達式。然后計算機終端輸出轉換后的后綴表達式及計算后的結果。2、程序執(zhí)行的命令包括:(1)構造鏈棧;(2)輸入數(shù)據;(3)判斷輸入的表達式是否為非法表達式;(4)將中綴表達式轉換為后綴表達式;(5)計算表達式的值;(6)輸出。(7) 結束4、本程序能將中綴表達式轉換為后綴表達式,并且
2、能計算表達式的值。5、輸入及輸出示例:例1 : 請輸入表達式6+3* (6+5 ) 后綴表達式:6 3 6 5 + * +計算結果為:39Press any key to con ti nue例2 :請輸入表達式6-3* (7+1ERROR表達式錯誤Press any key to con ti nue二概要設計1. 基本操作、struct node操作結果:創(chuàng)建結構體(2) 、int Searchexpression(char string1)初始條件:表達式string1已經存在。操作結果:判斷表達式是否非法(3) 、 struct node *lnitialization()操作結果:創(chuàng)
3、建棧鏈。(4) 、struct node *assort(struct node *s)string2 中。初始條件:string1、string2已存在。操作結果:將中綴表達式轉換為后綴表達式并存在(5) 、struct node *calcolate(struct node *s)操作結果:求出表達式的值2、模塊調用圖主程序模塊創(chuàng)建結構體判斷表達式是否非法將中綴表達式轉換為后綴表達式表達式求值三詳細設計1、每個模塊:(1) 定義結構體struct nodechar data;int num;struct node *n ext;;(2) 判斷表達式是否非法int Searchexpress
4、i on( char stri ng1)int i1,b1,b2;int m;m=strle n( stri ng1);if(stri ng10v0|stri ng109)printf(ERROR:表達式缺操作數(shù)!n);return(WRONG);for(i1=0;i1=m;i1+)if(stri ng1i1=()b1+;elseif(stri ng1i1=)b2+;if(b1!=b2)printf(ERROR:缺少括號 n);return(WRONG);for(i1=0;i1m;i1+)if(0=stri ng1i1&stri ng1i1v=9&0=stri ng1i1+1&strin g1
5、i1+1=9) printf(ERROR:表達式缺操作符! n);return(WRONG);for(i1=0;i1=m;i1+)if(stri ng1i1=+&(stri ng1i1+1=+|stri ng1i1+1=-|stri ng1i1+1=*|stri ng1i1+1=/) printf(ERROR:表達式缺操作數(shù)!n);return(WRONG); elseif(stri ng1i1=-&(stri ng1i1+1=+|stri ng1i1+1=-|string1i1+1=*|stri ng1i1+1=/) printf(ERROR:表達式缺操作數(shù)!n);return(WRONG)
6、;elseif(stri ng1i1=*&(stri ng1i1+1=+|stri ng1i1+1=-|stri n g1i1+1=*|stri ng1i1+1=/)pri ntf(ERROR:表達式缺操作數(shù)!n);return(WRONG);elseif(stri ng1i1=/&(stri ng1i1+1=+|stri ng1i1+1=-|string1i1+1=*|stri ng1i1+1=/)pri ntf(ERROR: 表達式缺操作數(shù)!n);return(WRONG);return(RIGHT);(3) 、將中綴表達式轉換為后綴表達式struct node *assort(struc
7、t node *s)輸入字符串struct node *p,*top;int i;top=s;int m;char a;m=strle n( stri ng1);for(i=0;i=m;i+)a=stri ng1i;if(0data=a;p-n ext=top;top=p;break;case *:case /:stri ng2j= ;j+;if(top-data=*)|(top-data=/)string2j=top-data;j+; /比其高,現(xiàn)將棧頂運算符出棧,再進棧。top-data=a;break; else否,p=(struct node *)malloc(sizeof(struc
8、t no de);/直接進棧p-data=a;p-n ext=top;top=p;break;case +:case -:stri ng2j= ;j+;if(top-data=+|top-data=-|top-data=*|top-data=/)stri ng2j=top-data;j+;top-data=a;break;elsep-data=a;p-n ext=top;top=p; break;case ):stri ng2j= ;j+;if(top-data=)pri ntf(i nput error);break;while(top-data!=() stri ng2j=top-data
9、;j+;p=top;top=top-n ext;free(p);p=top;top=top-n ext;free(p);break;while(top-data!=)stri ng2j=top-data;j+;p=top;top=top-n ext;free(p);stri ng2j=#;printf(后綴表達式為:);for(i=0;ij;i+)if(stri ng2i!=)prin tf(%c ,stri ng2i);prin tf(n );return top;(4)表達式求值struct node *calcolate(struct node *s)struct node *top,*
10、p;char *q;int x,y,a;int i,n;top=s;指向棧頂?shù)闹羔榝or(i=0;i=0&stri ng2iv=9)q=&stri ng2i;a=atoi(q);for(n=i;stri ng2 n=0&stri ng2 nv=9 ;n+)p=(struct node *)malloc(sizeof(struct node );p-num=a;p-n ext=top;top=p;i=n-1;elseif(string2i=#)/遇#號結束標志,輸出棧中的最后計算結果printf(計算結果為:%dn,top-num);elseif(string2i=)else y=top-nu
11、m;p=top;top=top-n ext;free(p);x=top-nu m;p=top;top=top-n ext;free(p);switch(stri ng2i)case +:a=x+y;p=(struct node *)malloc(sizeof(struct no de);p-num=a;p-n ext=top;top=p;break;case -:a=x-y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-n ext=top;top=p;break;case *:a=x*y;p=(struct node *)mall
12、oc(sizeof(struct node );p-num=a;p-n ext=top;top=p;break;case 7:if(y=0)printf(ERROR:除數(shù)為零!n);a=(float)x/y;p-num=a;p-n ext=top;top=p;p=(struct node *)malloc(sizeof(struct node );break;return 0;(5)、主函數(shù)void mai n()struct node *top,*head;top=l ni tializatio n();建立一個鏈棧,并返回棧頂指針printf(請輸入表達式:n);gets(stri ng1
13、);if(Searchexpressio n(stri ng1)head=assort(top);/中綴轉化為后綴表達式calcolate(head);2、完整函數(shù) #i nclude#i nclude#i ncludevstri ng.h#i nclude#defi ne MAX 60#define RIGHT 1#defi ne WRONG 0#defi ne DEMAX 15#defi ne NULL 0char stri ng1MAX;char stri ng2MAX;int j=0;struct n ode / 定義結構體。char data;int num;struct node
14、*n ext;/判斷非法表達式;int Searchexpressi on( char stri ng1)int i1,b1,b2;int m;m=strle n(stri ng1);if(stri ng10v0|stri ng109)printf(ERROR:表達式缺操作數(shù)! n);return(WRONG);for(i1=0;i1=m;i1+)if(stri ng1i1=()b1+;elseif(stri ng1i1=)b2+;if(b1!=b2)printf(ERROR:缺少括號 n);return(WRONG);for(i 1= 0;i1m;i1+)if(0=stri ng1i1&st
15、ri ng1i1v=9& 0=stri ng1i1+1&stri ng1i1+1v=9) printf(ERROR:表達式缺操作符! n);return(WRONG);for(i1=0;i1data=;top-num=0;top- next=NULL;return top;struct node *assort(struct node *s)/輸入字符串struct node *p,*top;int i;top=s;int m;char a;m=strle n(stri ng1);for(i=0;i=m;i+)a=stri ng1i;if(Ov=stri ng1i&stri ng1idata=
16、a;p-n ext=top;top=p;break;case /:strin g2j= ;j+;/比其咼,現(xiàn)將棧頂運算符出if(top-data=*)|(top-data=/)stri ng2j=top-data;j+;棧,再進棧top-data=a;break;else否,直接p=(struct node *)malloc(sizeof(struct no de);/進棧p-data=a;p-n ext=top;top=p;break;case +:case -:stri ng2j= ;j+;if(top-data=+|top-data=-|top-data=*|top-data=7)str
17、i ng2j=top-data;j+;top-data=a;break;else p=(struct node *)malloc(sizeof(struct no de);p-data=a;p-n ext=top;top=p;break;case ):stri ng2j= ;j+;if(top-data=)pri ntf(i nput error);break;while(top-data!=()stri ng2j=top-data;j+;p=top;top=top-n ext;free(p); p=top;top=top-n ext;free(p);break;while(top-data!
18、=)stri ng2j=top-data;j+;p=top;top=top-n ext;free(p);strin g2j=#;printf(后綴表達式為:);for(i=0;ij;i+)if(stri ng2i!=)prin tf(%c ,stri ng2i);return top;prin tf(n );/計算表達式的值struct node *calcolate(struct node *s)struct node *top,*p;char *q;int x,y,a;int i,n;top=s;/指向棧頂?shù)闹羔榝or(i=0;i=0&stri ng2iv=9)q=&stri ng2i;a
19、=atoi(q);for(n=i;stri ng2 n=0&stri ng2 nnum=a;p-n ext=top;top=p;i=n-1;elseif(string2i=#)/遇#號結束標志,輸出棧中的最后計算結果 elseprintf(計算結果為:%dn,top-num);if(stri ng2i= ) else y=top-nu m;p=top;top=top-n ext;free(p);x=top-nu m;p=top;top=top-n ext;free(p);switch(stri ng2i)case +:a=x+y;p=(struct node *)malloc(sizeof(struct no de);p-num=a;p-n ext=top;top=p;break;case -:a=x-y;p=(struct node *)malloc(sizeo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/T 45224-2025智慧城市城市交通基礎設施智能監(jiān)測技術要求
- 汽車租賃合同終止格式合同
- 公路貨物運輸合同風險防范與應對
- 戰(zhàn)略合作融資合同樣本
- 度畜牧產品購銷合同協(xié)議
- 12《祝?!方虒W設計2024-2025學年高一語文下學期(必修下冊)
- 養(yǎng)老院護理服務合同樣本
- 商業(yè)拓展合作合同轉讓合同
- 辦公用品年度采購合同范本
- 婚前合同關于子女撫養(yǎng)費的責任劃分
- (正式版)HG∕T 21633-2024 玻璃鋼管和管件選用規(guī)定
- 張祖慶祖父的園子教學課件
- 人教版《道德與法治》二年級下冊全冊優(yōu)秀課件
- 顱內動脈動脈瘤介入治療臨床路徑
- DB32∕T 2882-2016 城市軌道交通橋隧結構養(yǎng)護技術規(guī)程
- 氮化硅結構與性能
- 性病實驗室檢測與質量管理
- 高樁碼頭施工組織設計(福建)
- 這一封書信來得巧
- 監(jiān)獄服裝加工企業(yè)開展全面
- 標書密封條格式模版(共19頁)
評論
0/150
提交評論