計算器制作步驟_第1頁
計算器制作步驟_第2頁
計算器制作步驟_第3頁
計算器制作步驟_第4頁
計算器制作步驟_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、簡易計算器制作步驟:1、創(chuàng)建基于對話框的MFC(EXE)應(yīng)用程序Calculator;2、在對話框窗體上順序創(chuàng)建0到9十個數(shù)字按鈕,并設(shè)置其標識符分別為IDC_0到IDC_9,其它按鈕按下表設(shè)置屬性:3、按表2添加各運算按鈕的消息處理函數(shù)4、為使0到9十個數(shù)字按鈕響應(yīng)相同的消息處理函數(shù),定義宏ON_COMMAND_RANGE(1) / CalculatorDlg.h/AFX_MSG(CCalculatorDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void

2、OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnAdd();afx_msg void OnMinus();afx_msg void OnMutiply();afx_msg void OnDivid();afx_msg void OnClear();afx_msg void OnSign();afx_msg void OnPoint();afx_msg void OnEqual();afx_msg void OnSqrt();afx_msg void OnRecip();afx_msg void OnOperandInput(

3、UINT iID);/注意先向類中添加protected 型成員函數(shù)OnOperandInput,然后再屏蔽掉類中的該函數(shù)聲明,在此位置添加此說明/AFX_MSG(2)/CalculatorDlg.cppBEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)/AFX_MSG_MAP(CCalculatorDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_ADD, OnAdd)ON_BN_CLICKED(IDC_MINUS, OnMinus)ON_BN_CLICKED(

4、IDC_MUTIPLY, OnMutiply)ON_BN_CLICKED(IDC_DIVID, OnDivid)ON_BN_CLICKED(IDC_CLEAR, OnClear)ON_BN_CLICKED(IDC_SIGN, OnSign)ON_BN_CLICKED(IDC_POINT, OnPoint)ON_BN_CLICKED(IDC_EQUAL, OnEqual)ON_BN_CLICKED(IDC_SQRT, OnSqrt)ON_BN_CLICKED(IDC_RECIP, OnRecip)ON_COMMAND_RANGE(IDC_0,IDC_9,OnOperandInput)/使用一個消

5、息函數(shù)來處理對某個ID范圍內(nèi)所有控件的命令響應(yīng)/AFX_MSG_MAPEND_MESSAGE_MAP()5、在頭文件CalculatorDlg.h中添加類型、成員變量及成員函數(shù)(1)自定義類型(定義在類外)enum Operator OpNone,OpAdd,OpSubtract,OpMultiply,OpDivide;enum CalcError ErrNone,ErrDivideByZero;enum Func FuncSin, FuncTan, FuncCos, FuncSqrt, FuncSqre, FuncLn, FuncLog, FuncN, FuncRec, FuncExp, F

6、uncNone;(2)成員變量及函數(shù)(此處應(yīng)定義為類CalculatorDlg的公有成員)float m_operand; /存儲當前輸入的操作數(shù)float m_accum; /存儲當前的計算結(jié)果BOOL m_bCoff; /標識當前輸入是否是小數(shù)float m_coff; /小數(shù)輸入時的系數(shù)Operator m_operator; /enum型變量用以標識當前運算符CalcError m_errorState; /enum型變量用以標識當前運算狀態(tài)Func m_func; /enum型變量用以標識當前運算函數(shù)類型BOOL m_bOperandAvail; /標識當前輸入是否為新輸入數(shù)字voi

7、d Calculate();/處理普通計算(用類向?qū)?,公有成員)void UpdateDisplay();/處理顯示(用類向?qū)?,公有成員)void Run_Func();/處理函數(shù)運算(用類向?qū)В谐蓡T)(3)為編輯框添加Cstring 變量m_result6、部分變量的初始化CCalculatorDlg:CCalculatorDlg(CWnd* pParent /*=NULL*/): CDialog(CCalculatorDlg:IDD, pParent)/AFX_DATA_INIT(CCalculatorDlg)m_result = _T();/AFX_DATA_INIT/ Note t

8、hat LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);m_coff=0.1;m_bCoff=0;m_errorState = ErrNone;m_bOperandAvail=FALSE;m_operator=OpNone;另:在文件CalculatorDlg.cpp中添加#include math.h7、添加各功能代碼(1)數(shù)字輸入消息處理函數(shù)void CCalculatorDlg:OnOperandInput(UINT iID)A

9、SSERT(iID = IDC_0 & iID = IDC_9);if(m_errorState!=ErrNone)return;if(!m_bOperandAvail)m_operand=0;if(!m_bCoff)m_operand=m_operand*10+(iID-IDC_0);elsem_operand=m_operand+(iID-IDC_0)*m_coff;m_coff*=0.1;m_bOperandAvail=TRUE;UpdateDisplay();(2)運算符消息處理函數(shù)void CCalculatorDlg:OnAdd() /加/ TODO: Add your contr

10、ol notification handler code hereCalculate();m_operator=OpAdd;void CCalculatorDlg:OnMinus()/減/ TODO: Add your control notification handler code hereCalculate();m_operator=OpSubtract;void CCalculatorDlg:OnMutiply()/乘/ TODO: Add your control notification handler code hereCalculate();m_operator=OpMulti

11、ply;void CCalculatorDlg:OnDivid()/除/ TODO: Add your control notification handler code hereCalculate();m_operator=OpDivide;void CCalculatorDlg:OnSign() /處理正負號/ TODO: Add your control notification handler code herem_operand*=-1;UpdateDisplay();void CCalculatorDlg:OnEqual()/處理等號/ TODO: Add your control

12、 notification handler code hereCalculate();m_operator=OpNone;void CCalculatorDlg:OnSqrt()/處理開根號/ TODO: Add your control notification handler code herem_func=FuncSqrt;Run_Func();void CCalculatorDlg:OnRecip() /求倒數(shù)1/x/ TODO: Add your control notification handler code herem_func=FuncRec;Run_Func();void

13、CCalculatorDlg:OnPoint()/處理小數(shù)點/ TODO: Add your control notification handler code herem_bCoff=1;UpdateDisplay();void CCalculatorDlg:Calculate()/處理計算,注意用類向?qū)忍砑哟顺蓡T函數(shù)if(m_errorState!=ErrNone)return;if(m_bOperandAvail)if(m_operator=OpNone)m_accum=m_operand;else if(m_operator=OpMultiply)m_accum*=m_operand

14、;else if(m_operator=OpDivide)if(m_operand=0)m_errorState=ErrDivideByZero;elsem_accum/=m_operand;else if(m_operator=OpAdd)m_accum+=m_operand;else if(m_operator=OpSubtract)m_accum-=m_operand;m_bOperandAvail=FALSE;m_bCoff=0;m_coff=0.1;UpdateDisplay();void CCalculatorDlg:Run_Func()/處理求根和求倒if (m_errorSta

15、te != ErrNone)return;if (m_bOperandAvail)if(m_func=FuncSqrt)m_operand=sqrt(m_operand);if(m_func=FuncRec)m_operand=1/m_operand;UpdateDisplay();void CCalculatorDlg:UpdateDisplay()/處理顯示if(GetSafeHwnd()=NULL)return;if(m_errorState!=ErrNone)m_result=除數(shù)不能為零;elsefloat lval=(m_bOperandAvail)?m_operand:m_accum;m_result.Format(_T(%f),lval);int i=m_result.GetLength();while(m_result.GetAt(i-1)=0)m_result.Delete(i-1,1);i-=1;UpdateData(FALSE);void CCalculatorDlg:OnClear() /處理清空/ TODO: Add your control notification handler code herem_operator = OpNone;m_operand = 0;m_accum = 0;m_bOperandAvail = FALSE;m

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論