




已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)題目: 使用鍵盤輸入表達(dá)式,計(jì)算表達(dá)式的值并輸出;將表達(dá)式轉(zhuǎn)化成后綴表達(dá)式輸出,利用后綴表達(dá)式求表達(dá)式的值并輸出。實(shí)驗(yàn)?zāi)康模菏褂脳5牟僮骶帉戧P(guān)于數(shù)據(jù)結(jié)構(gòu)的程序。實(shí)驗(yàn)內(nèi)容:寫出程序并上機(jī)調(diào)試、通過。一、需求分析1、演示程序以用戶和計(jì)算機(jī)的對話方式執(zhí)行,即在計(jì)算機(jī)終端上顯示“請輸入表達(dá)式”時(shí)輸入中綴表達(dá)式。然后計(jì)算機(jī)終端輸出轉(zhuǎn)換后的后綴表達(dá)式及計(jì)算后的結(jié)果。2、程序執(zhí)行的命令包括:(1)構(gòu)造鏈棧;(2)輸入數(shù)據(jù);(3)判斷輸入的表達(dá)式是否為非法表達(dá)式;(4)將中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式;(5)計(jì)算表達(dá)式的值;(6)輸出。(7)結(jié)束4、本程序能將中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式,并且能計(jì)算表達(dá)式的值。5、輸入及輸出示例:例1:請輸入表達(dá)式6+3*(6+5)后綴表達(dá)式:6 3 6 5 + * +計(jì)算結(jié)果為:39press any key to continue 例2:請輸入表達(dá)式6-3*(7+1error:表達(dá)式錯(cuò)誤press any key to continue 二 概要設(shè)計(jì)1基本操作(1)、struct node操作結(jié)果:創(chuàng)建結(jié)構(gòu)體(2)、int searchexpression(char string1)初始條件:表達(dá)式string1已經(jīng)存在。操作結(jié)果:判斷表達(dá)式是否非法(3)、struct node *initialization()操作結(jié)果:創(chuàng)建棧鏈。(4)、struct node *assort(struct node *s)初始條件:string1、string2已存在。操作結(jié)果:將中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式并存在string2中。(5)、struct node *calcolate(struct node *s)操作結(jié)果:求出表達(dá)式的值2、模塊調(diào)用圖主程序模塊創(chuàng)建結(jié)構(gòu)體判斷表達(dá)式是否非法將中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式表達(dá)式求值三 詳細(xì)設(shè)計(jì)1、每個(gè)模塊:(1) 定義結(jié)構(gòu)體struct node char data;int num;struct node *next; (2) 判斷表達(dá)式是否非法int searchexpression(char string1) int i1,b1,b2;int m;m=strlen(string1);if(string109)printf(error:表達(dá)式缺操作數(shù)!n);return(wrong);for(i1=0;i1=m;i1+)if(string1i1=()b1+;elseif(string1i1=)b2+;if(b1!=b2)printf(error:缺少括號n);return(wrong);for(i1=0;i1m;i1+)if(0=string1i1&string1i1=9&0=string1i1+1&string1i1+1=9) printf(error:表達(dá)式缺操作符!n);return(wrong);for(i1=0;i1=m;i1+)if(string1i1=+&(string1i1+1=+|string1i1+1=-|string1i1+1=*|string1i1+1=/) printf(error:表達(dá)式缺操作數(shù)!n);return(wrong);elseif(string1i1=-&(string1i1+1=+|string1i1+1=-|string1i1+1=*|string1i1+1=/) printf(error:表達(dá)式缺操作數(shù)!n);return(wrong);elseif(string1i1=*&(string1i1+1=+|string1i1+1=-|string1i1+1=*|string1i1+1=/)printf(error:表達(dá)式缺操作數(shù)!n);return(wrong);elseif(string1i1=/&(string1i1+1=+|string1i1+1=-|string1i1+1=*|string1i1+1=/)printf(error:表達(dá)式缺操作數(shù)!n);return(wrong);return(right); (3)、將中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式struct node *assort(struct node *s)/輸入字符串struct node *p,*top;int i;top=s;int m;char a;m=strlen(string1); for(i=0;i=m;i+)a=string1i;if(0=string1i&string1idata=a;p-next=top;top=p;break; case *:case /:string2j= ;j+;if(top-data=*)|(top-data=/)string2j=top-data;j+; /比其高,現(xiàn)將棧頂運(yùn)算符出棧,再進(jìn)棧。top-data=a;break;elsep=(struct node *)malloc(sizeof(struct node);/否,直接進(jìn)棧p-data=a;p-next=top;top=p;break;case +:case -:string2j= ;j+;if(top-data=+|top-data=-|top-data=*|top-data=/)string2j=top-data;j+;top-data=a;break;else p=(struct node *)malloc(sizeof(struct node);p-data=a;p-next=top;top=p;break; case ):string2j= ;j+;if(top-data=)printf(input error);break;while(top-data!=()string2j=top-data;j+;p=top;top=top-next;free(p);p=top;top=top-next;free(p);break;while(top-data!=)string2j=top-data;j+;p=top;top=top-next;free(p);string2j=#;printf(后綴表達(dá)式為:);for(i=0;ij;i+)if(string2i!= )printf(%c ,string2i);printf(n );return top; (4)表達(dá)式求值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&string2i=0&string2nnum=a;p-next=top;top=p;i=n-1;elseif(string2i=#) /遇#號結(jié)束標(biāo)志,輸出棧中的最后計(jì)算結(jié)果printf(計(jì)算結(jié)果為:%dn,top-num);elseif(string2i= )elsey=top-num;p=top;top=top-next;free(p);x=top-num;p=top;top=top-next;free(p);switch(string2i)case +:a=x+y;p=(struct node *)malloc(sizeof(struct node);p-num=a;p-next=top;top=p;break;case -:a=x-y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-next=top;top=p;break;case *:a=x*y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-next=top;top=p;break;case /:if(y=0) printf(error:除數(shù)為零!n);a=(float)x/y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-next=top;top=p;break;return 0;(5)、主函數(shù)void main()struct node *top,*head;top=initialization();/建立一個(gè)鏈棧,并返回棧頂指針printf(請輸入表達(dá)式:n);gets(string1);if(searchexpression(string1)head=assort(top);/中綴轉(zhuǎn)化為后綴表達(dá)式calcolate(head);2、完整函數(shù)#include#include#include#include#define max 60#define right 1#define wrong 0#define demax 15#define null 0char string1max;char string2max;int j=0;struct node /定義結(jié)構(gòu)體。char data;int num;struct node *next;int searchexpression(char string1) /判斷非法表達(dá)式int i1,b1,b2;int m;m=strlen(string1);if(string109)printf(error:表達(dá)式缺操作數(shù)!n);return(wrong);for(i1=0;i1=m;i1+)if(string1i1=()b1+;elseif(string1i1=)b2+;if(b1!=b2)printf(error:缺少括號n);return(wrong);for(i1=0;i1m;i1+)if(0=string1i1&string1i1=9&0=string1i1+1&string1i1+1=9) printf(error:表達(dá)式缺操作符!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=strlen(string1); for(i=0;i=m;i+)a=string1i;if(0=string1i&string1idata=a;p-next=top;top=p;break; case *:case /:string2j= ;j+;if(top-data=*)|(top-data=/)string2j=top-data;j+; /比其高,現(xiàn)將棧頂運(yùn)算符出棧,再進(jìn)棧。top-data=a;break;elsep=(struct node *)malloc(sizeof(struct node);/否,直接進(jìn)棧p-data=a;p-next=top;top=p;break;case +:case -:string2j= ;j+;if(top-data=+|top-data=-|top-data=*|top-data=/)string2j=top-data;j+;top-data=a;break;else p=(struct node *)malloc(sizeof(struct node);p-data=a;p-next=top;top=p;break; case ):string2j= ;j+;if(top-data=)printf(input error);break;while(top-data!=()string2j=top-data;j+;p=top;top=top-next;free(p);p=top;top=top-next;free(p);break;while(top-data!=)string2j=top-data;j+;p=top;top=top-next;free(p);string2j=#;printf(后綴表達(dá)式為:);for(i=0;ij;i+)if(string2i!= )printf(%c ,string2i);printf(n );return top;struct node *calcolate(struct node *s)/計(jì)算表達(dá)式的值struct node *top,*p;char *q;int x,y,a;int i,n;top=s;/指向棧頂?shù)闹羔榝or(i=0;i=0&string2i=0&string2nnum=a;p-next=top;top=p;i=n-1;elseif(string2i=#) /遇#號結(jié)束標(biāo)志,輸出棧中的最后計(jì)算結(jié)果printf(計(jì)算結(jié)果為:%dn,top-num);elseif(string2i= )elsey=top-num;p=top;top=top-next;free(p);x=top-num;p=top;top=top-next;free(p);switch(string2i)case +:a=x+y;p=(struct node *)malloc(sizeof(struct node);p-num=a;p-next=top;top=p;break;case -:a=x-y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-next=top;top=p;break;case *:a=x*y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-next=top;top=p;break;case /:if(y=0) printf(error:除數(shù)為零!n);a=(float)x/y;p=(struct node *)malloc(sizeof(struct node );p-num=a;p-next=top;top=p;break;return 0;void main()/主函數(shù)struct node *top,*head;top=initialization();/建立一個(gè)鏈棧,并返回棧頂指針printf(請輸入表達(dá)式:n);gets(string1);if(searchexpression(string1)head=assort(top);/中綴轉(zhuǎn)化為后綴表達(dá)式calcolate(head);四 使用說明、測試分析及結(jié)果1、運(yùn)行界面:microsoft visual c/c+2、測試結(jié)果例1:請輸入表達(dá)式6+3*(6+5)后綴表達(dá)式:6 3 6 5 + * +計(jì)算結(jié)果為:39press any key to continue 例2:請輸入表達(dá)式6-3*(7+1error:表達(dá)式錯(cuò)誤press any key to continue 五、實(shí)驗(yàn)總結(jié)教師評語:實(shí)驗(yàn)成績:指導(dǎo)教師簽名: 批閱日期: ut2apodfxxc02gybkskcww97mrqqwhoj5tl15zt6jipyytycummtarp3v1n5luizi3xh3bhwyreko8d9g7nmzqowpjetldrw08gvs8dsdqqygc3ce7moo2tlf0jf1gk74iuxybmtivr97ckrfvqult5fn2t6mpjr6rbzvpsortzvij5nb5ndvvsr4iwr1twlfkglspzuhrjq3cmzu98euouijdlszqpmvrw9zkupxf8wfug9l2g9277g2rtipa1ypczeuqxpkbhtvdcooqozxuz3vjrzmocijym62zchmeootyes8ebmm932tbz2yo09rtszeys8zrd2yktj8l6jeazvajnfbtrylvsm6ofbftoxvrffn7owiygjlamkunxjybz5rrb7r4vsur9zpfzfmfsjhcfca37lnw2vvlrkn7r8psz1bn6oric5hu5z6hcxayqynpog8duybawqsl20csg06dh2sm8hltgpkicskrgopdpuhbj1lmpk7lydvc6nnmwl3fwhzftfvyaary7lhssxj10v3ph3y19bxyr77ib7cpzsu2tijqe3hkqkkau9kskcphkxuikvvyjzpg2yijrkqfbggovyqkuxnwi9omnjtt6qilzxtyrf7d20fbmabcfiixrqkusvnxbppfuxyq1fjskfsubkgs2duvqc9sz4jkbgn4qqv66pyoarjurnfj3txyfclzieeptwfjthpheipdfnqnr2hjqkv2dzwtmpdjqkbcxmovdsjqctjagjmdlskpgad2s0h0vmzgaht36gyuez7umank1ndreubeqdgrx0venqgnsyib2ilq3siqrnl4m56t7z8y8da5k0kupn5nzg4jvjdtffhyt82aogqkxo4vblmleiy2p7hthbho07rcfttxodydppdtqso7wxd0j6fkklgm4wodzplhtrr2xgqn13hqy59zu1gegdyqnihntavsieuefqcyfucjwd3vk5i7ykmhundmiz ut2apodfxxc02gybkskcww97mrqqwhoj5tl15zt6jipyytycummtarp3v1n5luizi3xh3bhwyreko8d9g7nmzqowpjetldrw08gvs8dsdqqygc3ce7moo2tlf0jf1gk74iuxybmtivr97ckrfvqult5fn2t6mpjr6rbzvpsortzvij5nb5ndvvsr4
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 政府采購合同履約擔(dān)保行業(yè)深度調(diào)研及發(fā)展戰(zhàn)略咨詢報(bào)告
- 農(nóng)村生活垃圾分類與資源化利用行業(yè)跨境出海戰(zhàn)略研究報(bào)告
- 迪斯科表演AI應(yīng)用行業(yè)跨境出海戰(zhàn)略研究報(bào)告
- 互聯(lián)網(wǎng)證券交易行業(yè)跨境出海戰(zhàn)略研究報(bào)告
- 碳金融產(chǎn)品創(chuàng)新與市場推廣企業(yè)制定與實(shí)施新質(zhì)生產(chǎn)力戰(zhàn)略研究報(bào)告
- 承德醫(yī)學(xué)院附屬醫(yī)院招聘工作人員筆試真題2024
- 藝術(shù)繪畫與創(chuàng)意工坊行業(yè)深度調(diào)研及發(fā)展戰(zhàn)略咨詢報(bào)告
- 2025【攜手建屋合同】攜手建屋合同
- 2025企業(yè)餐廳承包合同食堂經(jīng)營合同范本
- 美術(shù)項(xiàng)目式學(xué)習(xí)實(shí)踐計(jì)劃
- 2025屆廣東省高三第一次調(diào)研考試語文試題講評課件
- 山東省名校聯(lián)盟2024-2025學(xué)年高一3月校際聯(lián)考英語試題(原卷版+解析版)
- 京劇頭飾美術(shù)課件
- 數(shù)據(jù)庫應(yīng)用技術(shù)-第三次形考作業(yè)(第10章~第11章)-國開-參考資料
- 垃圾分類我宣講(課件)三年級下冊勞動(dòng)人民版
- 重慶市2025屆高三3月適應(yīng)性月考語文試卷及參考答案
- T-CECS 10390-2024 建筑幕墻用背栓
- 國旗下講話第三周校長講話稿:以習(xí)慣鑄舟楫 以品格揚(yáng)云帆-讓成長在堅(jiān)守中綻放華章
- 三門峽水庫實(shí)習(xí)報(bào)告
- 文化產(chǎn)業(yè)項(xiàng)目風(fēng)險(xiǎn)管理及應(yīng)對措施
- 民用無人機(jī)操控員執(zhí)照(CAAC)考試復(fù)習(xí)重點(diǎn)題庫500題(含答案)
評論
0/150
提交評論