




已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
數(shù)據(jù)結(jié)構(gòu)實驗報告實驗題目: 使用鍵盤輸入表達式,計算表達式的值并輸出;將表達式轉(zhuǎn)化成后綴表達式輸出,利用后綴表達式求表達式的值并輸出。實驗目的:使用棧的操作編寫關于數(shù)據(jù)結(jié)構(gòu)的程序。實驗內(nèi)容:寫出程序并上機調(diào)試、通過。一、需求分析1、演示程序以用戶和計算機的對話方式執(zhí)行,即在計算機終端上顯示“請輸入表達式”時輸入中綴表達式。然后計算機終端輸出轉(zhuǎn)換后的后綴表達式及計算后的結(jié)果。2、程序執(zhí)行的命令包括:(1)構(gòu)造鏈棧;(2)輸入數(shù)據(jù);(3)判斷輸入的表達式是否為非法表達式;(4)將中綴表達式轉(zhuǎn)換為后綴表達式;(5)計算表達式的值;(6)輸出。(7)結(jié)束4、本程序能將中綴表達式轉(zhuǎn)換為后綴表達式,并且能計算表達式的值。5、輸入及輸出示例:例1:請輸入表達式6+3*(6+5)后綴表達式:6 3 6 5 + * +計算結(jié)果為:39press any key to continue 例2:請輸入表達式6-3*(7+1error:表達式錯誤press any key to continue 二 概要設計1基本操作(1)、struct node操作結(jié)果:創(chuàng)建結(jié)構(gòu)體(2)、int searchexpression(char string1)初始條件:表達式string1已經(jīng)存在。操作結(jié)果:判斷表達式是否非法(3)、struct node *initialization()操作結(jié)果:創(chuàng)建棧鏈。(4)、struct node *assort(struct node *s)初始條件:string1、string2已存在。操作結(jié)果:將中綴表達式轉(zhuǎn)換為后綴表達式并存在string2中。(5)、struct node *calcolate(struct node *s)操作結(jié)果:求出表達式的值2、模塊調(diào)用圖主程序模塊創(chuàng)建結(jié)構(gòu)體判斷表達式是否非法將中綴表達式轉(zhuǎn)換為后綴表達式表達式求值三 詳細設計1、每個模塊:(1) 定義結(jié)構(gòu)體struct node char data;int num;struct node *next; (2) 判斷表達式是否非法int searchexpression(char string1) int i1,b1,b2;int m;m=strlen(string1);if(string109)printf(error:表達式缺操作數(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:表達式缺操作符!n);return(wrong);for(i1=0;i1=m;i1+)if(string1i1=+&(string1i1+1=+|string1i1+1=-|string1i1+1=*|string1i1+1=/) printf(error:表達式缺操作數(shù)!n);return(wrong);elseif(string1i1=-&(string1i1+1=+|string1i1+1=-|string1i1+1=*|string1i1+1=/) printf(error:表達式缺操作數(shù)!n);return(wrong);elseif(string1i1=*&(string1i1+1=+|string1i1+1=-|string1i1+1=*|string1i1+1=/)printf(error:表達式缺操作數(shù)!n);return(wrong);elseif(string1i1=/&(string1i1+1=+|string1i1+1=-|string1i1+1=*|string1i1+1=/)printf(error:表達式缺操作數(shù)!n);return(wrong);return(right); (3)、將中綴表達式轉(zhuǎn)換為后綴表達式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)將棧頂運算符出棧,再進棧。top-data=a;break;elsep=(struct node *)malloc(sizeof(struct node);/否,直接進棧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(后綴表達式為:);for(i=0;ij;i+)if(string2i!= )printf(%c ,string2i);printf(n );return top; (4)表達式求值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é)束標志,輸出棧中的最后計算結(jié)果printf(計算結(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();/建立一個鏈棧,并返回棧頂指針printf(請輸入表達式:n);gets(string1);if(searchexpression(string1)head=assort(top);/中綴轉(zhuǎn)化為后綴表達式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) /判斷非法表達式int i1,b1,b2;int m;m=strlen(string1);if(string109)printf(error:表達式缺操作數(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:表達式缺操作符!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)將棧頂運算符出棧,再進棧。top-data=a;break;elsep=(struct node *)malloc(sizeof(struct node);/否,直接進棧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(后綴表達式為:);for(i=0;ij;i+)if(string2i!= )printf(%c ,string2i);printf(n );return top;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é)束標志,輸出棧中的最后計算結(jié)果printf(計算結(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();/建立一個鏈棧,并返回棧頂指針printf(請輸入表達式:n);gets(string1);if(searchexpression(string1)head=assort(top);/中綴轉(zhuǎn)化為后綴表達式calcolate(head);四 使用說明、測試分析及結(jié)果1、運行界面:microsoft visual c/c+2、測試結(jié)果例1:請輸入表達式6+3*(6+5)后綴表達式:6 3 6 5 + * +計算結(jié)果為:39press any key to continue 例2:請輸入表達式6-3*(7+1error:表達式錯誤press any key to continue 五、實驗總結(jié)教師評語:實驗成績:指導教師簽名: 批閱日期: ut2apodfxxc02gybkskcww97mrqqwhoj5tl15zt6jipyytycummtarp3v1n5luizi3xh3bhwyreko8d9g7nmzqowpjetldrw08gvs8dsdqqygc3ce7moo2tlf0jf1gk74iuxybmtivr97ckrfvqult5fn2t6mpjr6rbzvpsortzvij5nb5ndvvsr4iwr1twlfkglspzuhrjq3cmzu98euouijdlszqpmvrw9zkupxf8wfug9l2g9277g2rtipa1ypczeuqxpkbhtvdcooqozxuz3vjrzmocijym62zchmeootyes8ebmm932tbz2yo09rtszeys8zrd2yktj8l6jeazvajnfbtrylvsm6ofbftoxvrffn7owiygjlamkunxjybz5rrb7r4vsur9zpfzfmfsjhcfca37lnw2vvlrkn7r8psz1bn6oric5hu5z6hcxayqynpog8duybawqsl20csg06dh2sm8hltgpkicskrgopdpuhbj1lmpk7lydvc6nnmwl3fwhzftfvyaary7lhssxj10v3ph3y19bxyr77ib7cpzsu2tijqe3hkqkkau9kskcphkxuikvvyjzpg2yijrkqfbggovyqkuxnwi9omnjtt6qilzxtyrf7d20fbmabcfiixrqkusvnxbppfuxyq1fjskfsubkgs2duvqc9sz4jkbgn4qqv66pyoarjurnfj3txyfclzieeptwfjthpheipdfnqnr2hjqkv2dzwtmpdjqkbcxmovdsjqctjagjmdlskpgad2s0h0vmzgaht36gyuez7umank1ndreubeqdgrx0venqgnsyib2ilq3siqrnl4m56t7z8y8da5k0kupn5nzg4jvjdtffhyt82aogqkxo4vblmleiy2p7hthbho07rcfttxodydppdtqso7wxd0j6fkklgm4wodzplhtrr2xgqn13hqy59zu1gegdyqnihntavsieuefqcyfucjwd3vk5i7ykmhundmiz ut2apodfxxc02gybkskcww97mrqqwhoj5tl15zt6jipyytycummtarp3v1n5luizi3xh3bhwyreko8d9g7nmzqowpjetldrw08gvs8dsdqqygc3ce7moo2tlf0jf1gk74iuxybmtivr97ckrfvqult5fn2t6mpjr6rbzvpsortzvij5nb5ndvvsr4
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 教育之光殘疾教育者的創(chuàng)業(yè)故事
- 心理健康提升員工心理韌性新方法
- 廣告投放平臺選擇與運用
- 2025年中國漏電保護器行業(yè)市場深度分析及投資戰(zhàn)略規(guī)劃報告
- 微博營銷化妝品品牌的傳播之道
- 建筑結(jié)構(gòu)動力學及動應力計算
- 提升殘疾人才資源利用率的政策與措施研究
- 家校協(xié)同在學生學習習慣養(yǎng)成中的核心作用
- 休閑裝本土化創(chuàng)新創(chuàng)業(yè)項目商業(yè)計劃書
- 自助售貨機數(shù)據(jù)分析平臺企業(yè)制定與實施新質(zhì)生產(chǎn)力項目商業(yè)計劃書
- 求職心理調(diào)適專家講座
- GB/T 6344-2008軟質(zhì)泡沫聚合材料拉伸強度和斷裂伸長率的測定
- GB/T 3532-1995日用瓷器
- 學術(shù)論文寫作規(guī)范與技巧課件
- 生物高中-基于大數(shù)據(jù)分析的精準教學課件
- 工程結(jié)算審計實施方案(共8篇)
- 樂東221氣田投產(chǎn)專家驗收匯報
- 信任五環(huán)(用友營銷技巧)課件
- 2022年廣東省深圳市中考化學真題試卷
- 危險貨物道路運輸安全生產(chǎn)管理制度
- GB∕T 8110-2020 熔化極氣體保護電弧焊用非合金鋼及細晶粒鋼實心焊絲
評論
0/150
提交評論