小型計算器程序的編寫_第1頁
小型計算器程序的編寫_第2頁
小型計算器程序的編寫_第3頁
小型計算器程序的編寫_第4頁
小型計算器程序的編寫_第5頁
已閱讀5頁,還剩14頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、 目錄一設計題目:小型計算器程序的編寫2二設計目的和內容2三:基本功能描述3四:設計思路3五:軟件設計:設計步驟、界面設計、關鍵功能的實現31設計步驟:32界面設計43關鍵功能的實現6六:結論與心得體會8七 .參考文獻8八 附錄:8一設計題目:小型計算器程序的編寫二設計目的和內容【設計目的】1學習Visual C+的MFC開發程序的步驟。2綜合運用所學的類、繼承和多態的知識。3進一步掌握程序的調試方法。【設計內容】 1 利用MFC的向導,創建基于對話框的應用程序,添加按鈕、編輯框等控件;2 實現算術加、減、乘、除等運算;3 選做:三角函數的運算、對數運算、指數運算、進制轉換等。界面參考三:基本

2、功能描述 程序運行時,顯示一個MFC程序窗口,等待用戶輸入,用戶可以單擊鼠標輸入要計算的表達式,輸入的表達式在窗口中,用戶鍵入“=”符號后,窗口顯示出結果,該計算器實現了加減乘除以及求倒數運算。四:設計思路1Windows消息處理機制的理解 首先編寫程序需要對Windows程序的消息處理機制(Message Handle)有個比較清晰的了解。Windows的程序都是通過消息來傳送數據,有不需要用戶參與的系統消息,比如異常處理等。還有用戶消息,比如鼠標的單擊,雙擊,鍵盤的鍵入等。2.界面的設計,對需要的各種運算符在窗口中劃出,然后定義各個的屬性3成員函數及其定義,首先對編輯框的相關變量進行定義,

3、定義為m_edit,將消息框和函數聯系起來,然后進行編寫相應函數。4運行結果并測試五:軟件設計:設計步驟、界面設計、關鍵功能的實現1設計步驟:1). 添加頭文件將Calculate.cpp(見附錄)改為Calculate.h將其添加到計算器Dlg.cpp : implementation file中,如下:#include "Calculate.h"。2.):成員函數及其釋義virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint()

4、;afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnChangeEdit1();afx_msg void On1();afx_msg void On2();afx_msg void On3();afx_msg void On4();afx_msg void On5();afx_msg void On6();afx_msg void On7();afx_msg void On8();afx_msg void On9();afx_msg void On0();afx_msg void Onequal();afx_msg void Onadd();af

5、x_msg void Onmin();afx_msg void Onplus();afx_msg void Ondiv();afx_msg void Ondelete();afx_msg void Oncountdown();/AFX_MSGDECLARE_MESSAGE_MAP()CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INIT2界面設計1)界面的初步設計仿照Windows附件里面的計算器,在資源視圖中畫好界面。2)修改每個static的屬性IDCAPTIONIDD_ST

6、ATIC4簡易表達式計算器 IDC_STATIC1待求表達式IDC_STATIC2運算結果3)修改每個button的屬性IDC_BUTTON1等于(=)IDC_BUTTON2全清(CE)IDC_BUTTON3清除 (C)4)修改每個button的處理機制在類向導Classwizard窗口中進行,如下圖: 其button按鈕的修改類似5)修改每個edit的類型和名稱在類向導Classwizard窗口中進行:單擊Add Variable按鈕,在如下窗口中進行修改其他edit的修改類似最終結果如下:3關鍵功能的實現1).OnButton1()按鈕的處理函數void CMy1Dlg:On1() / T

7、ODO: Add your control notification handler code herem_jisuan=m_jisuan+"1"UpdateData(FALSE);其他數字按鈕類似2)運算符按鈕的處理函數void CMy1Dlg:Onadd() / TODO: Add your control notification handler code heresign=1;data=atof(m_jisuan);m_jisuan="+"UpdateData(FALSE);m_jisuan=""3)等號的按鈕的處理函數voi

8、d CMy1Dlg:Onequal() / TODO: Add your control notification handler code hereswitch(sign)case 1:data=data+atof(m_jisuan);break;case 2:data=data-atof(m_jisuan);break;case 3: data=data*atof(m_jisuan);break;case 4: if(atof(m_jisuan)=0) MessageBox("ERROR");break;elsedata=data/atof(m_jisuan);brea

9、k; char Drray50;memset(Drray,0,50);sprintf(Drray,"%f",data);m_jisuan=Drray;UpdateData(FALSE);4)編輯框的按鈕的處理函數void CMy1Dlg:OnChangeEdit1() / TODO: If this is a RICHEDIT control, the control will not/ send this notification unless you override the CDialog:OnInitDialog()/ function and call CRich

10、EditCtrl().SetEventMask()/ with the ENM_CHANGE flag ORed into the mask./ TODO: Add your control notification handler code here六:結論與心得體會 這次課程設計用MFC的相關知識編寫一個小型計算器,剛開整個始感覺這個題目挺新鮮,很想好好嘗試下,平時只知道用計算器,其中的原理卻不甚了解。此次課程設計沒有像上次那樣尷尬,不知從何做起,可能是有了上次的經驗。老師開始的講解感覺作用挺大的,知道了從那塊著手,編寫的過程中不斷出錯,寫的源代碼總是找不到頭文件,這是最讓人郁悶的了。和同

11、學的交流,探討過程,是我對這學期學的類有了更深刻的了解。身邊有很多高手的存在,通過他們的講解很容易對整個過程熟悉,比上次好多了。七 .參考文獻VisualC+7.0 實戰入口八 附錄:調試及測試結果如上,關鍵源代碼如下:#if !defined(AFX_1DLG_H_B53FAC72_C8D2_473B_BF3F_DF4E3D42EB50_INCLUDED_)#define AFX_1DLG_H_B53FAC72_C8D2_473B_BF3F_DF4E3D42EB50_INCLUDED_#if _MSC_VER > 1000#pragma once#endif / _MSC_VER &g

12、t; 1000/ CMy1Dlg dialogclass CMy1Dlg : public CDialog/ Constructionpublic:double data;int sign;CMy1Dlg(CWnd* pParent = NULL);/ standard constructor/ Dialog Data/AFX_DATA(CMy1Dlg)enum IDD = IDD_MY1_DIALOG ;CStringm_jisuan;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CMy1Dlg

13、)protected:virtual void DoDataExchange(CDataExchange* pDX);/ DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:HICON m_hIcon;/ Generated message map functions/AFX_MSG(CMy1Dlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQu

14、eryDragIcon();afx_msg void OnChangeEdit1();afx_msg void On1();afx_msg void On2();afx_msg void On3();afx_msg void On4();afx_msg void On5();afx_msg void On6();afx_msg void On7();afx_msg void On8();afx_msg void On9();afx_msg void On0();afx_msg void Onequal();afx_msg void Onadd();afx_msg void Onmin();af

15、x_msg void Onplus();afx_msg void Ondiv();afx_msg void Ondelete();afx_msg void Oncountdown();/AFX_MSGDECLARE_MESSAGE_MAP();/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line.#endif / !defined(AFX_1DLG_H_B53FAC72_C8D2_473B_BF3F_DF4E3D42EB

16、50_INCLUDED_)void CMy1Dlg:OnChangeEdit1() / TODO: If this is a RICHEDIT control, the control will not/ send this notification unless you override the CDialog:OnInitDialog()/ function and call CRichEditCtrl().SetEventMask()/ with the ENM_CHANGE flag ORed into the mask./ TODO: Add your control notific

17、ation handler code hereCPP文件中關鍵代碼:/ 計算器1Dlg.cpp : implementation file/#include "stdafx.h"#include "計算器1.h"#include "計算器1Dlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : pu

18、blic CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAbout

19、Dlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No me

20、ssage handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMy1Dlg dialogCMy1Dlg:CMy1Dlg(CWnd* pParent /*=NULL*/): CDialog(CMy1Dlg:IDD, pParent)/AFX_DATA_INIT(CMy1Dlg)m_jisuan = _T("");/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon

21、(IDR_MAINFRAME);void CMy1Dlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CMy1Dlg)DDX_Text(pDX, IDC_EDIT1, m_jisuan);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CMy1Dlg, CDialog)/AFX_MSG_MAP(CMy1Dlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_EN_CHANGE(IDC_EDIT1, OnChang

22、eEdit1)ON_BN_CLICKED(IDC_1, On1)ON_BN_CLICKED(IDC_2, On2)ON_BN_CLICKED(IDC_3, On3)ON_BN_CLICKED(IDC_4, On4)ON_BN_CLICKED(IDC_5, On5)ON_BN_CLICKED(IDC_6, On6)ON_BN_CLICKED(IDC_7, On7)ON_BN_CLICKED(IDC_8, On8)ON_BN_CLICKED(IDC_9, On9)ON_BN_CLICKED(IDC_0, On0)ON_BN_CLICKED(IDC_equal, Onequal)ON_BN_CLIC

23、KED(IDC_add, Onadd)ON_BN_CLICKED(IDC_min, Onmin)ON_BN_CLICKED(IDC_plus, Onplus)ON_BN_CLICKED(IDC_div, Ondiv)ON_BN_CLICKED(IDC_delete, Ondelete)ON_BN_CLICKED(IDC_countdown, Oncountdown)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMy1Dlg message handlersBOOL CMy1Dlg:OnInitDialog()CDialog:OnInitDialog();/ Add "

24、;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);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL)CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!str

25、AboutMenu.IsEmpty()pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this automatically/ when the application's main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, F

26、ALSE);/ Set small icon/ TODO: Add extra initialization herereturn TRUE; / return TRUE unless you set the focus to a controlvoid CMy1Dlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog:OnSysCommand(nID, lParam);/ If you add a

27、 minimize button to your dialog, you will need the code below/ to draw the icon. For MFC applications using the document/view model,/ this is automatically done for you by the framework.void CMy1Dlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND

28、, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(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_hI

29、con);elseCDialog:OnPaint();/ The system calls this to obtain the cursor to display while the user drags/ the minimized window.HCURSOR CMy1Dlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CMy1Dlg:On1() / TODO: Add your control notification handler code herem_jisuan=m_jisuan+"1"UpdateData(

30、FALSE);void CMy1Dlg:On2() / TODO: Add your control notification handler code herem_jisuan=m_jisuan+"2"UpdateData(FALSE);void CMy1Dlg:On3() / TODO: Add your control notification handler code herem_jisuan=m_jisuan+"3"UpdateData(FALSE);void CMy1Dlg:On4() / TODO: Add your control not

31、ification handler code herem_jisuan=m_jisuan+"4"UpdateData(FALSE);void CMy1Dlg:On5() / TODO: Add your control notification handler code herem_jisuan=m_jisuan+"5"UpdateData(FALSE);void CMy1Dlg:On6() / TODO: Add your control notification handler code herem_jisuan=m_jisuan+"6&q

32、uot;UpdateData(FALSE);void CMy1Dlg:On7() / TODO: Add your control notification handler code herem_jisuan=m_jisuan+"7"UpdateData(FALSE);void CMy1Dlg:On8() / TODO: Add your control notification handler code herem_jisuan=m_jisuan+"8"UpdateData(FALSE);void CMy1Dlg:On9() / TODO: Add y

33、our control notification handler code herem_jisuan=m_jisuan+"9"UpdateData(FALSE);void CMy1Dlg:On0() / TODO: Add your control notification handler code herem_jisuan=m_jisuan+"0"UpdateData(FALSE);void CMy1Dlg:Onequal() / TODO: Add your control notification handler code hereswitch(sign)case 1:data=data+atof(m_jisuan);break;case 2:data=data-atof(m_jisuan);break;case 3: data=data*atof(m_jisuan);break;case 4: if(atof(m_jisuan)=0)MessageBox("ERROR");break;else data=data/atof(m

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論