中綴表達(dá)式(c++).doc_第1頁
中綴表達(dá)式(c++).doc_第2頁
中綴表達(dá)式(c++).doc_第3頁
中綴表達(dá)式(c++).doc_第4頁
中綴表達(dá)式(c++).doc_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

/* 模擬小學(xué)生 在線算術(shù)考試系統(tǒng)(C+實(shí)現(xiàn)) *實(shí)現(xiàn)的要求: *1.操作數(shù)個(gè)數(shù)隨機(jī)指定 *2.操作數(shù)隨機(jī)生成 *3.操作符隨機(jī)選擇 *4.利用中綴表達(dá)式堆棧求值法 求出正確答案 *5.判斷用戶輸入答案 并計(jì)算分值 *- *程序是幫別人寫的課程設(shè)計(jì) 時(shí)間比較緊迫 就沒有用鏈表實(shí)現(xiàn)堆棧 *也沒有將中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式后再求值 *- *程序邏輯還算清楚,有詳細(xì)的注釋。 *在vs2010,vs2008下可以正常運(yùn)行(vc6.0 就不用試了 不能運(yùn)行的,不是程序有錯(cuò)誤,而是 *vc6.0對(duì)局部變量的要求不一樣,所以不能運(yùn)行) *- * 2013-1-9 By:LiWei*/#include#include#include#includeusing namespace std;#define Max_Size 50 /定義最多保存的題目數(shù)量template class Op_Stackpublic:Op_Stack();void Push(T a); /進(jìn)棧操作T POP(); /出棧操作bool isNull();T Top();private:int top;T Data19;template Op_Stack:Op_Stack()top = -1;template void Op_Stack:Push(T a)top+;Datatop = a;template T Op_Stack:POP()T temp;temp = Datatop;top-;return temp;template bool Op_Stack:isNull()if(-1 = top )return false;template T Op_Stack:Top()if(-1 = top)return -1;elsereturn Datatop;class OP_Questionspublic:OP_Questions();int *Produce_Rand(int begin, int end, int count); /產(chǎn)生指定范圍內(nèi)的隨機(jī)數(shù),返回指向數(shù)組的指針void Produce_Questions(); /生成題目void print(); /打印題目int score(); /計(jì)算題目void show_score(); /輸出用戶成績void menu(); /private:int questions30,num; /保存生成的題目,操作數(shù)的個(gè)數(shù)Op_Stack number; /保存操作數(shù),用于中綴表達(dá)式求值Op_Stack op; /保存操作符,用于中綴表達(dá)式求值int finalscore;/總成績;OP_Questions:OP_Questions() /構(gòu)造函數(shù)num = 0;finalscore = 0; /初始化分?jǐn)?shù)和操作數(shù)個(gè)數(shù)int *OP_Questions:Produce_Rand(int begin, int end, int count) /生成所需的隨機(jī)數(shù)int *p = new int10;srand(unsigned)time(0);for(int i=0; i=count-1; i+)pi= rand()%(end - begin + 1) + begin; /取得 begin 到end 范圍內(nèi)的隨機(jī)數(shù)return p; /返回?cái)?shù)組首地址void OP_Questions:Produce_Questions()/*生成隨機(jī)題目 思路: *1,操作數(shù)個(gè)數(shù)隨機(jī)生成 *2,操作符隨機(jī)選擇 *3,操作數(shù)隨機(jī)生成*/int *p,index;index = 0;p = Produce_Rand(2,5,1); /隨機(jī)確定需要的操作數(shù)的個(gè)數(shù)num = p0; /記錄需要幾個(gè)操作數(shù)p = Produce_Rand(1,10,num); /取得操作數(shù),為方便計(jì)算 操作數(shù)范圍在110之間for(int i=1; i=2*num-1; i+=2) /將操作數(shù)保存在數(shù)組中(中綴表達(dá)式),奇數(shù)位置放操作數(shù)questionsi = *(p + index);index+;/記錄位置p = Produce_Rand(1,4,num-1); /*取得操作符*/index = 0; /*返回的數(shù)字表示操作符 代號(hào)*/for(int i=2; i=2*num-1; i+=2) /*1001,1002表示+ -,1011,1012表示* /, */ /*方便在中綴表達(dá)式求值時(shí) 判斷運(yùn)算符優(yōu)先級(jí)*/switch (*(p + index)case 1:questionsi = 1001; /表示+break;case 2:questionsi = 1002; /表示-break;case 3: questionsi = 1011; /表示* break;case 4: questionsi = 1012; /表示/ break;index+;delete p; /釋放內(nèi)存void OP_Questions:print() /顯示題目int ch;for(int i=1; i=2*num-1; i+) /循環(huán)2*num-1次 依次顯示取得的數(shù)字打印ch = questionsi;if(ch = 100)coutch ;elseswitch(ch)case 1001:cout+ ;break;case 1002:cout- ;break;case 1011:cout* ;break;case 1012:cout/ ;break;cout= endl;int OP_Questions:score() /中綴表達(dá)式求值int ch = 0; /記錄從questions數(shù)組中取得的 數(shù)字int temp = 0; /臨時(shí)變量 用于取得操作符 棧頂元素 和 當(dāng)前ch變量 比較運(yùn)算符優(yōu)先級(jí)int Left = 0; /表示 退棧時(shí)的 左操作數(shù)intRight = 0; /表示 退棧時(shí)的 右操作數(shù)int OP = 0; /表示操作符int i=1; while(i = 2*num-1) /逐一掃描questions數(shù)組ch = questionsi;if(ch temp) /優(yōu)先級(jí)高 進(jìn)棧op.Push(ch);i+;else /優(yōu)先級(jí)相同或低于 Right = number.POP();/從操作數(shù)棧退出兩個(gè)操作數(shù)Left = number.POP();OP = op.POP(); /并取出一個(gè)運(yùn)算符switch(OP)case 1001: /然后 計(jì)算 這三個(gè)數(shù)字表示的 數(shù)學(xué)含義,再將結(jié)果放入操作數(shù)棧number.Push(Left + Right);break;case 1002:number.Push(Left - Right);break;case 1011:number.Push(Left * Right);break;case 1012:number.Push(Left / Right);break; /*執(zhí)行完上面的while循環(huán)后 *操作數(shù)和操作符 棧里邊還是有數(shù)字 并沒有得到最終結(jié)果 *但是 此時(shí) 再求結(jié)果已經(jīng) 不用考慮運(yùn)算符的優(yōu)先級(jí)了 *所以利用下面的while循環(huán) 每次從操作數(shù)棧取出兩個(gè)數(shù),從操作符棧取出一個(gè)操作符 *計(jì)算 取得的 數(shù)學(xué)表達(dá)式 并將結(jié)果 再次 放回 操作數(shù)棧 *直至 操作符棧為空,此時(shí)操作數(shù)棧 還剩下一個(gè)數(shù)字 *即所求 結(jié)果*/while(false!= op.isNull()Right = number.POP();Left = number.POP();OP = op.POP();switch(OP) case 1001: number.Push(Left + Right); break;case 1002:number.Push(Left - Right);break;case 1011:number.Push(Left * Right);break;case 1012:number.Push(Left / Right);break;return number.POP();void OP_Questions:menu()int count = 0; /題目數(shù)量int user_answer; /用戶答案int system_answer; /系統(tǒng)答案coutcount;for(int i=0; icount; i+)Produce_Questions();print(); system_answer = score();/取得系統(tǒng)的正確答案coutuser_answer;if(system_answer = user_answer) finalscore += 5; /每答對(duì)一題5分void OP_Questions:show_score()cout*endl;cout* *endl;cout* *endl;cout* *endl;cout* finalscore分 *endl;cout* *endl;cout* *endl;cout*endl;int main()OP_Questions que;int select;for(;)couttt*endl;couttt* *endl;couttt* *endl;coutttt隨機(jī)題目生成endl;couttt* *endl;couttt* *endl;couttt*endl;cout1.開始做題:endlendl;cout2.顯示分?jǐn)?shù):endlendl;cout3.退出系統(tǒng):end

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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ǔ)空間,僅對(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)論