實(shí)驗(yàn)三算法表達(dá)式C語(yǔ)言實(shí)驗(yàn)報(bào)告.doc_第1頁(yè)
實(shí)驗(yàn)三算法表達(dá)式C語(yǔ)言實(shí)驗(yàn)報(bào)告.doc_第2頁(yè)
實(shí)驗(yàn)三算法表達(dá)式C語(yǔ)言實(shí)驗(yàn)報(bào)告.doc_第3頁(yè)
實(shí)驗(yàn)三算法表達(dá)式C語(yǔ)言實(shí)驗(yàn)報(bào)告.doc_第4頁(yè)
實(shí)驗(yàn)三算法表達(dá)式C語(yǔ)言實(shí)驗(yàn)報(bào)告.doc_第5頁(yè)
已閱讀5頁(yè),還剩12頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)實(shí)驗(yàn)項(xiàng)目三棧和隊(duì)列的應(yīng)用 計(jì)算機(jī)科學(xué)與技術(shù)系0902班 組 長(zhǎng): 雷耀陽(yáng) 組 員:柴瑞東、李志鵬、張玲春 日 期:實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)類型_綜合設(shè)計(jì)_ 實(shí)驗(yàn)室 軟件實(shí)驗(yàn)室1 1 實(shí)驗(yàn)題目求一個(gè)數(shù)學(xué)表達(dá)式的值:用戶輸入一個(gè)包含正整數(shù)、括號(hào)和四則運(yùn)算符(“+”、 “”、 “*”、 “/”)的算術(shù)表達(dá)式,計(jì)算其結(jié)果。2 需求分析首先置操作數(shù)棧為空棧,表達(dá)式起始符“#”為運(yùn)算符棧底元素;依次讀入表達(dá)式中每個(gè)字符,若是操數(shù)則進(jìn)操作數(shù)棧,若是操作符則和操作符棧頂?shù)倪\(yùn)算符進(jìn)行比較優(yōu)先權(quán)后作相應(yīng)的操作,直到整個(gè)表達(dá)式求值完畢(即操作符棧頂元素和當(dāng)前讀入的字符均為“#”)3 概要設(shè)計(jì)結(jié)點(diǎn)結(jié)構(gòu)類型描述如下 typedef structchar *base,*top;int stacksize;sqstack;函數(shù)關(guān)系:main()result()compute()face()op()4 詳細(xì)設(shè)計(jì)實(shí)現(xiàn)概要設(shè)計(jì)中定義的所有的數(shù)據(jù)類型,對(duì)每個(gè)操作給出偽碼算法。對(duì)主程序和其他模塊也都需要寫出偽碼算法。#include #include #include #include #include #define N 100 double numStackN=0; int numTop; char opStackN; int opTop; int op(char ch) if(ch=+|ch=-) return 2; if(ch=*|ch=/) return 3; if(ch=() return -1; return 0; double result(double num1,char op,double num2) if(op=+) return num1+num2; if(op=-) return num1-num2; if(op=*) return num1*num2; if(op=/) return num1/num2; return 0; int compute(char str) double num=0; int i=0,j=1,k=1; int Flag=0; numTop=opTop=0; while(stri!=0|opTop0) if(stri=0&stri0&!op(stri-1)&stri!=(&stri-1!=) numStacknumTop+=num*k; num=0; j=1; Flag=0; k=1; if(opTop=0|stri=() opStackopTop+=stri; else if(stri=) while(opTop0&opStack-opTop!=() numStacknumTop-2=result(numStacknumTop-2,opStackopTop,numStacknumTop-1); numTop-; if(opStackopTop!=() return 0; else if(stri=0&numTop=0) return 0; while(opTop0&op(stri)=0&ch=0&ch=9 ) numj+=ch; numj=0; else j=0; if(ch=S|ch=s) if(strlen(num) face(); printf(%s has been savedn,strcpy(save,num); printf(input an expression,press key E to computen); printf(%s,str); else face(); printf(there is no number to save!n); printf(input an expression,press key E to computen); printf(%s,str); if(ch=R|ch=r) if(strlen(save) face(); printf(input an expression,press key E to computen); printf(%s,strcat(str,save); i+=strlen(save); if(ch=C|ch=c) if(strlen(str) str-i=0; face(); printf(input an expression,press key E to computen); printf(%s,str); if(ch=E|ch=e) if(compute(str) printf(n=%gn,numStack0); j=0; temp=numStack0; if(temp=10) k*=10; while(k) numj+=temp2/k+0; numj=0; temp2=temp2%k; k/=10; temp=temp-(int)temp; if(temp!=0) numj+=.; numj=0; temp+=0.0000005; for(k=6;k0;k-) if(temp=0) break; temp*=10; numj+=(int)temp+0; numj=0; temp=temp-(int)temp; else face(); printf(input an expression,press key E to computen); printf(%s,str); printf(nwrong expression!); i=0; j=0; str0=0; if(ch=Q|ch=q) printf(nare you sure to quit?(Y/N)n); ch=getch(); if(ch=Y|ch=y) break; else face(); printf(input an expression,press key E to computen); printf(%s,str); ch=getch(); #include #include #include #include #include #define N 100 double numStackN=0; int numTop; char opStackN; int opTop; int op(char ch) if(ch=+|ch=-) return 2; if(ch=*|ch=/) return 3; if(ch=() return -1; return 0; double result(double num1,char op,double num2) if(op=+) return num1+num2; if(op=-) return num1-num2; if(op=*) return num1*num2; if(op=/) return num1/num2; return 0; int compute(char str) double num=0; int i=0,j=1,k=1; int Flag=0; numTop=opTop=0; while(stri!=0|opTop0) if(stri=0&stri0&!op(stri-1)&stri!=(&stri-1!=) numStacknumTop+=num*k; num=0; j=1; Flag=0; k=1; if(opTop=0|stri=() opStackopTop+=stri; else if(stri=) while(opTop0&opStack-opTop!=() numStacknumTop-2=result(numStacknumTop-2,opStackopTop,numStacknumTop-1); numTop-; if(opStackopTop!=() return 0; else if(stri=0&numTop=0) return 0; while(opTop0&op(stri)=0&ch=0&ch=9 ) numj+=ch; numj=0; else j=0; if(ch=S|ch=s) if(strlen(num) face(); printf(%s has been savedn,strcpy(save,num); printf(input an expression,press key E to computen); printf(%s,str); else face(); printf(there is no number to save!n); printf(input an expression,press key E to computen); printf(%s,str); if(ch=R|ch=r) if(strlen(save) face(); printf(input an expression,press key E to computen); printf(%s,strcat(str,save); i+=strlen(save); if(ch=C|ch=c) if(strlen(str) str-i=0; face(); printf(input an expression,press key E to computen); printf(%s,str); if(ch=E|ch=e) if(compute(str) printf(n=%gn,numStack0); j=0; temp=numStack0; if(temp=10) k*=10; while(k) numj+=temp2/k+0; numj=0; temp2=temp2%k; k/=10; temp=temp-(int)temp; if(temp!=0) numj+=.; numj=0; temp+=0.0000005; for(k=6;k0;k-) if(temp=0) break; temp*=10; numj+=(int)temp+0; numj=0; temp=temp-(int)temp; else face(); printf(input an expression,press key E to computen); printf(%s,str); printf(nwrong expression!); i=0; j=0; str0=0; if(ch=Q|ch=q) printf(nare you sure to quit?(Y/N)n); ch=getch(); if(ch=Y|ch=y) break; else face(); printf(input an expression,press key E to computen); printf(%s,str); ch=getch(); 5 調(diào)試分析1. 實(shí)驗(yàn)過程中,發(fā)現(xiàn)錯(cuò)誤后,經(jīng)過看書,不斷調(diào)試后,成功。2. 菜單是借鑒別人的,拿過來(lái)經(jīng)過改動(dòng)后,才用到本試驗(yàn)中中的。6 使用說明程序名為表達(dá)式求值.exe,運(yùn)行環(huán)境為DOS。程序執(zhí)行后顯示_ Save number(S) | Read number(R) | Clear(C) | Equal(E) | Quit(Q) -input an expression,press key E to compute 請(qǐng)選擇操作SRCE Q選擇:在選擇不同的字母后,將實(shí)現(xiàn)不同功能。選擇Q:退出程序顯示:are you sure to quit?(Y/N)輸入Y,推出輸入N,回到主界面選擇C:1.清屏 2.刪除上一次輸入 選擇E:計(jì)算輸入表達(dá)式的結(jié)果并顯示結(jié)果 選擇S:保存所輸入表達(dá)式的結(jié)果 如果沒有表達(dá)式的結(jié)果,輸出:there is no number

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論