


版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、簡(jiǎn)易計(jì)算器c+程序設(shè)計(jì)(mfc ) 課程設(shè)計(jì)報(bào)告設(shè)計(jì)題目:簡(jiǎn)易計(jì)算器班級(jí):1. 設(shè)計(jì)目標(biāo)2. 設(shè)計(jì)內(nèi)容3. 設(shè)計(jì)思想4. 設(shè)計(jì)說(shuō)明5. 設(shè)計(jì)步驟姓名:學(xué)號(hào): 指導(dǎo)教師:時(shí)間:2010 年 6 月 30 日目錄6. 主要成員函數(shù)關(guān)系圖7. 程序主要代碼8. 難點(diǎn)分析9. 總結(jié)1. 設(shè)計(jì)目標(biāo)簡(jiǎn)易計(jì)算器 (mfc)了解 windows 應(yīng)用程序的結(jié)構(gòu)與dos 程序的不同,掌握應(yīng)用mfc 類(lèi)庫(kù)編寫(xiě) windows 應(yīng)用程序的基本模式。2. 設(shè)計(jì)內(nèi)容以簡(jiǎn)易計(jì)算器為例,通過(guò)對(duì)簡(jiǎn)單應(yīng)用軟件計(jì)算器的設(shè)計(jì),編制、調(diào)試,實(shí)現(xiàn) 簡(jiǎn)單的加,減,乘,除等運(yùn)算,以學(xué)習(xí)應(yīng)用mfc 庫(kù)類(lèi)編寫(xiě)對(duì)話(huà)框的原理,加深對(duì) c+類(lèi)的學(xué)習(xí)
2、及應(yīng)用。3. 設(shè)計(jì)思想基于 mfc 庫(kù)類(lèi)對(duì)對(duì)話(huà)框界面的設(shè)計(jì),通過(guò)創(chuàng)建類(lèi)成員函數(shù)成員變量,編輯控件創(chuàng)建消息映射,調(diào)用消息函數(shù)完成數(shù)據(jù)的輸入輸出,實(shí)現(xiàn)計(jì)算功能。生成簡(jiǎn)單的應(yīng)用軟件。4. 設(shè)計(jì)說(shuō)明(1) 包含的功能有:加、減、乘、除運(yùn)算,開(kāi)方、平方等功能。(2) 計(jì)算器上數(shù)字0 9 為一個(gè)控件數(shù)組,加、減、乘、除為一個(gè)控件數(shù)組, 其余為單一的控件。(3) 輸入的原始數(shù)據(jù)、運(yùn)算中間數(shù)據(jù)和結(jié)果都顯示在窗口頂部的同一個(gè)標(biāo)簽中。(4) 計(jì)算功能基本上是用系統(tǒng)內(nèi)部函數(shù)。(5) 程序可以能自動(dòng)判斷輸入數(shù)據(jù)的正確性,保證不出現(xiàn)多于一個(gè)小數(shù)點(diǎn)、以 0 開(kāi)頭等不正?,F(xiàn)象。(6) “ ce按”鈕可以清除所有已輸入的數(shù)據(jù)
3、從頭計(jì)算5.設(shè)計(jì)步驟(1)選擇 filenewlproject 命令,選擇 mfc為 caa,按確定。在彈出界面選擇創(chuàng)建對(duì)話(huà)框,單擊appwizard(exe)創(chuàng)建 project 名finish 按鈕(2)在生成的設(shè)計(jì)界面中加入控件,得到計(jì)算器應(yīng)用界面。如圖:( 3)為控件編輯框編輯按鈕名稱(chēng)id ,如圖:id用途0idc_0按鍵01idc_1按鍵12idc_2按鍵23idc_3按鍵34idc_4按鍵45idc_5按鍵56idc_6按鍵67idc_7按鍵78idc_8按鍵89idc_9按鍵9.+/-idc_dianidc_zhengfu按鍵正負(fù).+idc_jia加-idc_jian減*idc_
4、cheng乘/idc_chu除ceidc_qinglin清零sqrtidc_sqrt開(kāi)方x*xidc_pingfang平方=idc_dengyu等于編輯框idc_e1顯示數(shù)據(jù)輸入輸出(4)進(jìn)入 classwizard 界面選擇 message maps為控件插入消息映射,插入消息函數(shù),實(shí)現(xiàn)控件命令功能。如圖:(5) 進(jìn)入 classwizard界面 message varibles為編輯框添加成員變量m_result用以顯示結(jié)果。如圖:(6)在 caadlg.h 中加入在主要成員函數(shù)和成員變量的申明在caadlg.cpp中加入主要成員函數(shù)實(shí)現(xiàn)過(guò)程和成員變量的初始化(代碼片段將于后面介紹)。6.
5、 主要成員函數(shù)關(guān)系圖按鈕09.函數(shù)如: on0( ).on1( ).on2( ).處理數(shù)字輸入onoperandinput( )計(jì)算 calculate( )按 鈕+-*/.+/-ce=函數(shù)如:onjia(). onjian( ).oncheng( ) .按鈕 sqrtx*x函數(shù):onsqrt( ).onpingfang( )顯示輸出 updatedisplay( )7. 程序主要代碼/ 在頭文件中手動(dòng)添加成員函數(shù)與變量聲明/ caadlg.h : header file/ 定義兩個(gè) euum型結(jié)構(gòu)enum operator opnone, opadd, opsubtract, opmulti
6、ply, opdivide ;enum calcerror errnone, errdividebyzero ;float m_operand;/存儲(chǔ)當(dāng)前輸入的操作數(shù)float m_accum;/存儲(chǔ)當(dāng)前的計(jì)算處理結(jié)束bool m_bcoff;/標(biāo)識(shí)當(dāng)前輸入是否是小數(shù)float m_coff;/小數(shù)輸入時(shí)的系數(shù)operator m_operator;/enum型變量用以標(biāo)識(shí)當(dāng)前運(yùn)算符calcerror m_errorstate;bool m_boperandavail;/enum/型變量用以標(biāo)識(shí)當(dāng)前運(yùn)算狀態(tài)標(biāo)識(shí)當(dāng)前輸入是否是新輸入數(shù)字void updatedisplay();/處理顯示void
7、 calculate();/處理計(jì)算void onoperandinput(int a);/處理數(shù)字輸入/ 在執(zhí)行文件中添加成員函數(shù)實(shí)現(xiàn)部分與變量初始/ caadlg.cpp : implementation file/以下是對(duì)聲明變量進(jìn)行初始ccaadlg:ccaadlg(cwnd* pparent /*=null*/): cdialog(ccaadlg:idd, pparent)/afx_data_init(ccaadlg)m_result = _t("");/默認(rèn)為 m_result變量初始/afx_data_init/ note that loadicon does
8、 not require a subsequent destroyicon inwin32/ 以下是消息處理函數(shù),用以響應(yīng)消息映射void ccaadlg:on0()/處理“ 0 ”按鈕/ todo: add your control notification handler code hereonoperandinput(0);void ccaadlg:on1()/處理“ 1 ”按鈕/ todo: add your control notification handler code hereonoperandinput(1);void ccaadlg:on2()/處理“ 2 ”按鈕/ tod
9、o: add your control notification handler code hereonoperandinput(2);m_hicon = afxgetapp()->loadicon(idr_mainframe);m_coff=0.1;m_bcoff=0; m_errorstate = errnone;/為變量進(jìn)行初始為變量進(jìn)行初始為變量進(jìn)行初始m_boperandavail=false;m_operator=opnone;/為變量進(jìn)行初始為變量進(jìn)行初始void ccaadlg:on3()/處理“ 3 ”按鈕/ todo: add your control notific
10、ation handler code here onoperandinput(3);void ccaadlg:on4()/處理“ 4 ”按鈕/ todo: add your control notification handler code here onoperandinput(4);void ccaadlg:on5()/處理“ 5 ”按鈕/ todo: add your control notification handler code here onoperandinput(5);void ccaadlg:on6()/處理“ 6 ”按鈕/ todo: add your control n
11、otification handler code here onoperandinput(6);void ccaadlg:on7()/處理“ 7 ”按鈕/ todo: add your control notification handler code here onoperandinput(7);void ccaadlg:on8()/處理“ 8 ”按鈕/ todo: add your control notification handler code here onoperandinput(8);void ccaadlg:on9()/處理“ 9 ”按鈕/ todo: add your con
12、trol notification handler code here onoperandinput(9);void ccaadlg:oncheng()/處理“ * ”按鈕/ todo: add your control notification handler code here calculate();m_operator = opmultiply;void ccaadlg:onchu()/處理“ / ”按鈕/ todo: add your control notification handler code here calculate();m_operator = opdivide;vo
13、id ccaadlg:ondengyu()/處理“ = ”按鈕/ todo: add your control notification handler code here calculate();m_operator = opnone;void ccaadlg:ondian()/處理“ . ”按鈕/ todo: add your control notification handler code here m_bcoff=1;updatedisplay();void ccaadlg:onjia()/處理“ + ”按鈕/ todo: add your control notification
14、handler code here calculate();m_operator = opadd;void ccaadlg:onjian()/處理“ - ”按鈕/ todo: add your control notification handler code here calculate();m_operator = opsubtract;void ccaadlg:onpingfang()/處理“ x*x ”按鈕/ todo: add your control notification handler code here m_operand*=m_operand;updatedisplay(
15、);void ccaadlg:onqinglin()/處理“ ce”按鈕/ todo: add your control notification handler code here m_operator = opnone;m_operand = 0;m_accum = 0; m_boperandavail = false; m_errorstate = errnone; m_coff=0.1;m_bcoff=0;updatedisplay();void ccaadlg:onsqrt()/處理“ sqrt”按鈕/ todo: add your control notification hand
16、ler code here m_operand=sqrt(m_operand);updatedisplay();void ccaadlg:onzhengfu()/處理“ +/-”按鈕/ todo: add your control notification handler code here m_operand*=-1;updatedisplay();/ 以下是自定義函數(shù),用于實(shí)現(xiàn)計(jì)算功能void ccaadlg:onoperandinput(int a)/處理 0-9數(shù)字的輸入函數(shù)if (m_errorstate != errnone)/判斷當(dāng)前運(yùn)算是否有誤,若有則返回return;if (
17、!m_boperandavail)/判斷是否輸入新的數(shù)字,1- 是 0- 否m_operand = 0;if(!m_bcoff)/判斷是否是小數(shù)輸入, 1- 是 0- 否m_operand=m_operand*10+(a);elsem_operand=m_operand+(a)*m_coff; m_coff*=0.1;m_boperandavail=true;updatedisplay();/更新顯示void ccaadlg:calculate()/處理計(jì)算函數(shù)if (m_errorstate != errnone) return;if (m_boperandavail)/ 如果沒(méi)有計(jì)算符就將
18、m_operand值賦予 m_accum if (m_operator = opnone)m_accum = m_operand;else if (m_operator = opmultiply)/否則按計(jì)算符進(jìn)行計(jì)算m_accum *= m_operand; else if (m_operator = opdivide)if (m_operand = 0)elsem_errorstate = errdividebyzero; m_accum /= m_operand;else if (m_operator = opadd) m_accum += m_operand;else if (m_op
19、erator = opsubtract) m_accum -= m_operand;m_boperandavail = false; m_bcoff=0;m_coff=0.1;updatedisplay();void ccaadlg:updatedisplay()/處理顯示函數(shù)if (getsafehwnd() = null) return;if (m_errorstate != errnone) m_result="除數(shù)不能為零 "else/如果當(dāng)前計(jì)算無(wú)錯(cuò)誤那么進(jìn)行顯示/ 如果是輸入計(jì)算數(shù),那么顯示輸入情況,如果按下計(jì)算符,則顯示結(jié)果float lval = (m_bo
20、perandavail) ? m_operand : m_accum; m_result.format(_t("%f"), lval); /將 float型轉(zhuǎn)化為 cstring型int i=m_result.getlength();while(m_result.getat(i-1)='0')m_result.delete(i-1,1); i-=1;/ 用編輯控件變量m_result改變編輯控件的值, 更新顯示updatedata(false);8. 難點(diǎn)分析在利用 mfc 編寫(xiě)計(jì)算器過(guò)程中,遇到的技術(shù)上的問(wèn)題,及解決方法。例如:計(jì)算數(shù)輸入的處理在程序中使用
21、 m_operand來(lái)存儲(chǔ)當(dāng)前的輸入。 而程序只使用一個(gè)函數(shù)來(lái)處理所有數(shù)字輸入, 也就是說(shuō)每次單擊數(shù)字按鈕就會(huì)調(diào)用該函數(shù)。由于計(jì)算數(shù)不僅僅是個(gè)位數(shù), 因此用戶(hù)可能需要單擊幾個(gè)數(shù)字按鈕來(lái)完成一個(gè)計(jì)算數(shù)的輸入,這樣在每次輸入時(shí)都必須將輸入的數(shù)字累加入m_operand中。而且對(duì)于小數(shù)輸入和整數(shù)輸入也必須采取不同的累加方式。如對(duì)整數(shù)的輸入,累加方式為: m_operand=m_operand*10+n;其中 n 為輸入的數(shù)字。而對(duì)于小數(shù)輸入,累加方式為: m_operand=m_operand+n*10i ;其中 n 為數(shù)字, i 為小數(shù)位數(shù)。因此在程序中還需設(shè)立布爾型變量m_coff的值來(lái)標(biāo)識(shí)當(dāng)按下按鈕小數(shù)點(diǎn)的時(shí)候來(lái)確定調(diào)用哪個(gè)累加方式。自定義消息處理函數(shù)、成員函數(shù)、變量在添加自定義函數(shù)、變量時(shí)需注意函數(shù)及變量的聲明在頭文件中如:caa
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 代購(gòu)代加工合同范本
- 臨時(shí)借車(chē)合同范例
- 《格薩爾》史詩(shī)中的古代法律文化研究
- 冰箱購(gòu)貨合同范例
- 出納托管合同范例
- CIP貿(mào)易術(shù)語(yǔ)合同范例
- 中標(biāo)制作合同范例
- 乒乓球桌子購(gòu)買(mǎi)合同范例
- 全案設(shè)計(jì)物料采購(gòu)合同范本
- 人才輸送合作合同范本
- 2025年中考百日誓師大會(huì)校長(zhǎng)發(fā)言稿:激揚(yáng)青春志 決勝中考時(shí)
- 封條模板A4直接打印版
- 石油產(chǎn)品化驗(yàn)室分析規(guī)程
- 同一直線(xiàn)上的二力合成導(dǎo)學(xué)案
- 2022年度八年級(jí)美術(shù)抽測(cè)題庫(kù)
- 義務(wù)教育《地理》課程標(biāo)準(zhǔn)(2022年版)
- 鍋爐改造方案(省煤器)講解-共18頁(yè)
- 單細(xì)胞蛋白論文
- 021[學(xué)士]某六層框架宿舍樓畢業(yè)設(shè)計(jì)(含計(jì)算書(shū)、圖紙)
- (完整版)高層鋼結(jié)構(gòu)住宅施工方案(非常詳細(xì))
- 云管理系統(tǒng)CRM系統(tǒng)操作手冊(cè)
評(píng)論
0/150
提交評(píng)論