




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、MFC實(shí)現(xiàn)簡易計(jì)算器©Free Copy操作簡便,還有幫助提示,支持double類型的數(shù)據(jù)進(jìn)行運(yùn)算,顯示清除無效0.By: 杜運(yùn)福 計(jì)算器類結(jié)構(gòu)如下:class CCalculatorDlg : public CDialog/ Constructionpublic:CCalculatorDlg(CWnd* pParent = NULL);/ standard constructor/ Dialog Data/AFX_DATA(CCalculatorDlg)enum IDD = IDD_CALCULATOR_DIALOG ;doublem_Num;/AFX_DATAdouble num
2、1,num2; /第一、二操作數(shù)char cal; /運(yùn)算符號BYTE dotflag; /小數(shù)點(diǎn)標(biāo)志BYTE numflag; /鍵入數(shù)字標(biāo)志long quan2; /小數(shù)部分權(quán)值/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CCalculatorDlg)protected:virtual void DoDataExchange(CDataExchange* pDX);/ DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:HICON m_hIcon;/ Ge
3、nerated message map functions/AFX_MSG(CCalculatorDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnNum0();afx_msg void OnNum1();afx_msg void OnNum2();afx_msg void OnNum3();afx_msg void OnNum4();a
4、fx_msg void OnNum5();afx_msg void OnNum6();afx_msg void OnNum7();afx_msg void OnNum8();afx_msg void OnNum9();afx_msg void OnAdd();afx_msg void OnSub();afx_msg void OnMul();afx_msg void OnDiv();afx_msg void OnEqual();afx_msg void OnSign();afx_msg void OnInv();afx_msg void OnMod();afx_msg void OnSqrt(
5、);afx_msg void OnDot();afx_msg void OnClear();afx_msg void OnBackspace();afx_msg void OnHelp();/AFX_MSGDECLARE_MESSAGE_MAP();完整實(shí)現(xiàn)代碼如下:/ CalculatorDlg.cpp : implementation file/* 簡易計(jì)算器* by 追夢 2011-4-18*/#include "stdafx.h"#include "Calculator.h"#include "CalculatorDlg.h"
6、#include "math.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual
7、 function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAbou
8、tDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CCalculatorDlg dialogCCalculatorDlg:CCalculatorDlg(CWnd* pParent /*=NULL*/): CDialog
9、(CCalculatorDlg:IDD, pParent)/AFX_DATA_INIT(CCalculatorDlg)m_Num = 0.0;/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);void CCalculatorDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MA
10、P(CCalculatorDlg)DDX_Text(pDX, IDC_Num, m_Num);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)/AFX_MSG_MAP(CCalculatorDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_Num0, OnNum0)ON_BN_CLICKED(IDC_Num1, OnNum1)ON_BN_CLICKED(IDC_Num2, OnNum2)ON_BN_CLICKED(IDC_Num3, O
11、nNum3)ON_BN_CLICKED(IDC_Num4, OnNum4)ON_BN_CLICKED(IDC_Num5, OnNum5)ON_BN_CLICKED(IDC_Num6, OnNum6)ON_BN_CLICKED(IDC_Num7, OnNum7)ON_BN_CLICKED(IDC_Num8, OnNum8)ON_BN_CLICKED(IDC_Num9, OnNum9)ON_BN_CLICKED(IDC_Add, OnAdd)ON_BN_CLICKED(IDC_Sub, OnSub)ON_BN_CLICKED(IDC_Mul, OnMul)ON_BN_CLICKED(IDC_Div
12、, OnDiv)ON_BN_CLICKED(IDC_Equal, OnEqual)ON_BN_CLICKED(IDC_Sign, OnSign)ON_BN_CLICKED(IDC_Inv, OnInv)ON_BN_CLICKED(IDC_Mod, OnMod)ON_BN_CLICKED(IDC_Sqrt, OnSqrt)ON_BN_CLICKED(IDC_Dot, OnDot)ON_BN_CLICKED(IDC_Clear, OnClear)ON_BN_CLICKED(IDC_Backspace, OnBackspace)ON_BN_CLICKED(IDC_Help, OnHelp)/AFX_
13、MSG_MAPEND_MESSAGE_MAP()/ CCalculatorDlg message handlers/BOOL CCalculatorDlg:OnInitDialog()CDialog:OnInitDialog();/ Add "About." menu item to system menu./ IDM_ABOUTBOX must be in the system command range.ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);C
14、Menu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL)CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does thi
15、s automatically/ when the application's main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small icon/ TODO: Add extra initialization herequan2 = 1;dotflag = 0;numflag = 0;return TRUE; / return TRUE unless you set the focus to a controlvoid CCalculatorD
16、lg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog:OnSysCommand(nID, lParam);/ If you add a minimize button to your dialog, you will need the code below/ to draw the icon. For MFC applications using the document/view model,/
17、 this is automatically done for you by the framework.void CCalculatorDlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSyste
18、mMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;/ Draw the icondc.DrawIcon(x, y, m_hIcon);elseCDialog:OnPaint();/ The system calls this to obtain the cursor to display while the user drags/ the minimized window.
19、HCURSOR CCalculatorDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;/鍵入 0void CCalculatorDlg:OnNum0() UpdateData();if(dotflag)quan2 *= 10;m_Num += 0 / (double)quan2;else if(numflag)m_Num = m_Num * 10 + 0;else m_Num = 0;numflag = 1;UpdateData(FALSE);/鍵入 1void CCalculatorDlg:OnNum1() UpdateData();if(dotf
20、lag)quan2 *= 10;m_Num += 1 / (double)quan2;else if(numflag)m_Num = m_Num * 10 + 1;else m_Num = 1;numflag = 1;UpdateData(FALSE);/鍵入 2void CCalculatorDlg:OnNum2() UpdateData();if(dotflag)quan2 *= 10;m_Num += 2 / (double)quan2;else if(numflag)m_Num = m_Num * 10 + 2;else m_Num = 2;numflag = 1;UpdateData
21、(FALSE);/鍵入 3void CCalculatorDlg:OnNum3() UpdateData();if(dotflag)quan2 *= 10;m_Num += 3 / (double)quan2;else if(numflag)m_Num = m_Num * 10 + 3;else m_Num = 3;numflag = 1;UpdateData(FALSE);/鍵入 4void CCalculatorDlg:OnNum4() UpdateData();if(dotflag)quan2 *= 10;m_Num += 4 / (double)quan2;else if(numfla
22、g)m_Num = m_Num * 10 + 4;else m_Num = 4;numflag = 1;UpdateData(FALSE);/鍵入 5void CCalculatorDlg:OnNum5() UpdateData();if(dotflag)quan2 *= 10;m_Num += 5 / (double)quan2;else if(numflag)m_Num = m_Num * 10 + 5;else m_Num = 5;numflag = 1;UpdateData(FALSE);/鍵入 6void CCalculatorDlg:OnNum6() UpdateData();if
23、(dotflag)quan2 *= 10;m_Num += 6 / (double)quan2;else if(numflag)m_Num = m_Num * 10 + 6;else m_Num = 6;numflag = 1;UpdateData(FALSE);/鍵入 7void CCalculatorDlg:OnNum7() UpdateData();if(dotflag)quan2 *= 10;m_Num += 7 / (double)quan2;else if(numflag)m_Num = m_Num * 10 + 7;else m_Num = 7;numflag = 1;Updat
24、eData(FALSE);/鍵入 8void CCalculatorDlg:OnNum8() UpdateData();if(dotflag)quan2 *= 10;m_Num += 8 / (double)quan2;else if(numflag)m_Num = m_Num * 10 + 8;else m_Num = 8;numflag = 1;UpdateData(FALSE);/鍵入 9void CCalculatorDlg:OnNum9() UpdateData();if(dotflag)quan2 *= 10;m_Num += 9 / (double)quan2;else if(n
25、umflag)m_Num = m_Num * 10 + 9;else m_Num = 9;numflag = 1;UpdateData(FALSE);/鍵入 +void CCalculatorDlg:OnAdd() UpdateData();num1 = m_Num;cal = '+'numflag = 0;dotflag = 0;quan2 = 1;/鍵入 -void CCalculatorDlg:OnSub() UpdateData();num1 = m_Num;cal = '-'numflag = 0;dotflag = 0;quan2 = 1;/鍵入 *
26、void CCalculatorDlg:OnMul() UpdateData();num1 = m_Num;cal = '*'numflag = 0;dotflag = 0;quan2 = 1;/鍵入 /void CCalculatorDlg:OnDiv() UpdateData();num1 = m_Num;cal = '/'numflag = 0;dotflag = 0;quan2 = 1;/鍵入 %void CCalculatorDlg:OnMod() UpdateData();num1 = m_Num;cal = '%'numflag =
27、 0;dotflag = 0;quan2 = 1;/鍵入 =void CCalculatorDlg:OnEqual() UpdateData();num2 = m_Num;switch(cal)case '+': m_Num = num1 + num2; break;case '-': m_Num = num1 - num2; break;case '*': m_Num = num1 * num2; break;case '/': m_Num = num1 / num2; break;case '%': m_Num
28、 = (int)num1 % (int)num2; break;numflag = 0;dotflag = 0;quan2 = 1;UpdateData(FALSE);/鍵入 +/-void CCalculatorDlg:OnSign() UpdateData();m_Num = 0 - m_Num;UpdateData(FALSE);/鍵入 1/xvoid CCalculatorDlg:OnInv() UpdateData();m_Num = 1 / m_Num;numflag = 0;dotflag = 0;quan2 = 1;UpdateData(FALSE);/鍵入 sqrtvoid CCalculatorDlg:OnSqrt() U
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 個人租房中介合同范例
- 入住民宿退房合同范例
- 云南土地流轉(zhuǎn)合同范例
- 星形膠質(zhì)細(xì)胞分化異常在低氧環(huán)境暴露所致神經(jīng)發(fā)育障礙的作用研究
- 農(nóng)戶購銷化肥合同范例
- 復(fù)雜艱險地區(qū)鐵路工程建設(shè)風(fēng)險識別與分析方法研究
- 義齒公司員工合同范例
- 2000施工合同范例
- 農(nóng)村拆屋合同范例
- 東西轉(zhuǎn)讓協(xié)議合同范例
- 5G通信技術(shù)在智慧城市中的應(yīng)用
- 小學(xué)信息科技三年級下冊 教案 第三單元《智能生活安全》單元教學(xué)設(shè)計(jì)
- 動物防疫知識講座
- 2024年高等教育法學(xué)類自考-00227公司法歷年考試高頻考點(diǎn)試題附帶答案
- 《乳腺癌的防治》課件
- 個人所得稅個人所得稅
- 孤獨(dú)癥兒童早期干預(yù)操作手冊
- 《小貓的九個命》
- 大班健康《愛是什么》課件
- 特種作業(yè)(鍋爐工)安全培訓(xùn)
- 鋼梁現(xiàn)場安裝檢驗(yàn)批質(zhì)量檢驗(yàn)記錄
評論
0/150
提交評論