《面向程序設(shè)計》MFC計算器_第1頁
《面向程序設(shè)計》MFC計算器_第2頁
《面向程序設(shè)計》MFC計算器_第3頁
《面向程序設(shè)計》MFC計算器_第4頁
《面向程序設(shè)計》MFC計算器_第5頁
已閱讀5頁,還剩20頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、 面向程序設(shè)計實訓(xùn)報告專 業(yè): 學(xué)生姓名: 學(xué) 號: 指導(dǎo)教師: 2011年6月 29日面向程序設(shè)計實訓(xùn)報告1、實訓(xùn)項目:設(shè)計一個基于對話框的計算器,實現(xiàn)常用計算器的基本功能。2、性能、指標、要求;1、基本要求(1) 按照題目要求獨立設(shè)計計算器所需的所有按鈕,并完成程序源碼編寫、調(diào)試及關(guān)鍵代碼注釋。(2)能夠?qū)崿F(xiàn)+、-、*、/計算。 (3)能夠?qū)崿F(xiàn)對數(shù)及冪函數(shù)計算。 (4)能夠?qū)崿F(xiàn)三角函數(shù)及反三角函數(shù)計算。 2、發(fā)揮部分 能夠?qū)崿F(xiàn)8,16,10,2進制之間相互轉(zhuǎn)換。 3、方案設(shè)計: 方案論證 通過運行VC6.0,用MFC制作windows風(fēng)格的界面的計算器 工作原理 1、啟動VC+6.0, 從

2、File菜單中選擇New選項, 選擇Projects標簽,然后單擊MFCAppWizard(exe)。2、在Location域中輸入一個合適的路徑名或單擊Browse按鈕來選擇一個。在Project name中輸入項目名稱(比如:0952100407)。點OK。3、打開MFC AppWizard-Step1對話框后。選擇基本對話。點下一個 。4、在MFC AppWizard-Step2 of 4對話框。 輸入標題對話,如:計算器。點下一個。5、在MFC AppWizard-Step3 of 4對話框。不做任何更改,直接點下一個。6、在MFC AppWizard-Step4 of 4對話框??梢?/p>

3、修改分類名等信息,在這里不做任何更改,直接點完成。至此進入了界面設(shè)計過程。4、軟件設(shè)計 進入界面設(shè)計界面后,利用右側(cè)的控件欄添加按鈕,在按鈕屬性中修改按鍵的顯示,雙擊按鍵進入按鍵的程序設(shè)計。制作完成后的界面如下所示:5、制作與調(diào)試過程; 雙擊按鈕后進入程序設(shè)計過程,每個按鈕的程序如下所示:/ 0952100407Dlg.cpp : implementation file/#include stdafx.h#include 0952100407.h#include 0952100407Dlg.h#include cmath#ifdef _DEBUG#define new DEBUG_NEW#un

4、def 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 function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void

5、 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 CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pD

6、X);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMy0952100407Dlg dialogCMy0952100407Dlg:CMy0952100407Dlg(CWnd* pParent /*=NULL*/): CDialog(CMy0952100407Dlg:IDD, pParent)/AFX_DATA_INIT(CMy095210040

7、7Dlg)m_EDIT = _T();/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);void CMy0952100407Dlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CMy0952100407Dlg)DDX_Text(pDX, IDC_EDIT1, m_EDIT);/A

8、FX_DATA_MAPBEGIN_MESSAGE_MAP(CMy0952100407Dlg, CDialog)/AFX_MSG_MAP(CMy0952100407Dlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)ON_BN_CLICKED(IDC_BUTTON2, OnButton2)ON_BN_CLICKED(IDC_BUTTON3, OnButton3)ON_BN_CLICKED(IDC_BUTTON4, OnButton4)ON_BN_CLICKED(

9、IDC_BUTTON5, OnButton5)ON_BN_CLICKED(IDC_BUTTON6, OnButton6)ON_BN_CLICKED(IDC_BUTTON7, OnButton7)ON_BN_CLICKED(IDC_BUTTON8, OnButton8)ON_BN_CLICKED(IDC_BUTTON9, OnButton9)ON_BN_CLICKED(IDC_BUTTON10, OnButton10)ON_BN_CLICKED(IDC_BUTTON11, OnButton11)ON_BN_CLICKED(IDC_BUTTON12, OnButton12)ON_BN_CLICKE

10、D(IDC_BUTTON13, OnButton13)ON_BN_CLICKED(IDC_BUTTON14, OnButton14)ON_BN_CLICKED(IDC_BUTTON15, OnButton15)ON_BN_CLICKED(IDC_BUTTON16, OnButton16)ON_BN_CLICKED(IDC_BUTTON17, OnButton17)ON_BN_CLICKED(IDC_BUTTON18, OnButton18)ON_BN_CLICKED(IDC_BUTTON19, OnButton19)ON_BN_CLICKED(IDC_BUTTON20, OnButton20)

11、ON_BN_CLICKED(IDC_BUTTON21, OnButton21)ON_BN_CLICKED(IDC_BUTTON22, OnButton22)ON_BN_CLICKED(IDC_BUTTON23, OnButton23)ON_BN_CLICKED(IDC_BUTTON24, OnButton24)ON_BN_CLICKED(IDC_BUTTON25, OnButton25)ON_BN_CLICKED(IDC_BUTTON30, OnButton30)ON_BN_CLICKED(IDC_BUTTON35, OnButton35)ON_BN_CLICKED(IDC_BUTTON36,

12、 OnButton36)ON_BN_CLICKED(IDC_BUTTON26, OnButton26)ON_BN_CLICKED(IDC_BUTTON27, OnButton27)ON_BN_CLICKED(IDC_BUTTON28, OnButton28)ON_BN_CLICKED(IDC_BUTTON31, OnButton31)ON_BN_CLICKED(IDC_BUTTON32, OnButton32)ON_BN_CLICKED(IDC_BUTTON33, OnButton33)ON_BN_CLICKED(IDC_RADIO1, OnRadio1)ON_BN_CLICKED(IDC_R

13、ADIO3, OnRadio3)ON_BN_CLICKED(IDC_RADIO2, OnRadio2)ON_BN_CLICKED(IDC_RADIO4, OnRadio4)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMy0952100407Dlg message handlersBOOL CMy0952100407Dlg:OnInitDialog()CDialog:OnInitDialog();/ Add About. menu item to system menu./ IDM_ABOUTBOX must be in the system command range.AS

14、SERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this automatically/ when the applications main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big

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

16、(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,/ this is automatically done for you by the framework.void CMy0952100407Dlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for

17、paintingSendMessage(WM_ICONERASEBKGND, (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;/ Dr

18、aw 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.HCURSOR CMy0952100407Dlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CMy0952100407Dlg:OnChangeEdit1() / TODO: If this is a RICHEDIT contro

19、l, 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 notification handler code herevoid CMy0952100407Dlg:OnButton2() / TODO: Add your cont

20、rol notification handler code herem_EDIT += _T(1);UpdateData(FALSE);void CMy0952100407Dlg:OnButton3() / TODO: Add your control notification handler code herem_EDIT += _T(2);UpdateData(FALSE);void CMy0952100407Dlg:OnButton4() / TODO: Add your control notification handler code herem_EDIT += _T(3);Upda

21、teData(FALSE);void CMy0952100407Dlg:OnButton5() / TODO: Add your control notification handler code herem_EDIT += _T(4);UpdateData(FALSE);void CMy0952100407Dlg:OnButton6() / TODO: Add your control notification handler code herem_EDIT += _T(5);UpdateData(FALSE);void CMy0952100407Dlg:OnButton7() / TODO

22、: Add your control notification handler code herem_EDIT += _T(6);UpdateData(FALSE);void CMy0952100407Dlg:OnButton8() / TODO: Add your control notification handler code herem_EDIT += _T(7);UpdateData(FALSE);void CMy0952100407Dlg:OnButton9() / TODO: Add your control notification handler code herem_EDI

23、T += _T(8);UpdateData(FALSE);void CMy0952100407Dlg:OnButton10() / TODO: Add your control notification handler code herem_EDIT += _T(9);UpdateData(FALSE);void CMy0952100407Dlg:OnButton11() / TODO: Add your control notification handler code herem_EDIT += _T(0);UpdateData(FALSE);void CMy0952100407Dlg:O

24、nButton12() / TODO: Add your control notification handler code herem_EDIT += _T(A);UpdateData(FALSE);void CMy0952100407Dlg:OnButton13() / TODO: Add your control notification handler code herem_EDIT += _T(B);UpdateData(FALSE);void CMy0952100407Dlg:OnButton14() / TODO: Add your control notification ha

25、ndler code herem_EDIT += _T(C);UpdateData(FALSE);void CMy0952100407Dlg:OnButton15() / TODO: Add your control notification handler code herem_EDIT += _T(D);UpdateData(FALSE);void CMy0952100407Dlg:OnButton16() / TODO: Add your control notification handler code herem_EDIT += _T(E);UpdateData(FALSE);voi

26、d CMy0952100407Dlg:OnButton17() / TODO: Add your control notification handler code herem_EDIT += _T(F);UpdateData(FALSE);void CMy0952100407Dlg:OnButton18() / TODO: Add your control notification handler code herem_EDIT += _T(.);UpdateData(FALSE);void CMy0952100407Dlg:OnButton19() / TODO: Add your con

27、trol notification handler code heretemp=m_EDIT;op=+; /標志,用于等號算法時選擇執(zhí)行m_EDIT=_T();UpdateData(FALSE);void CMy0952100407Dlg:OnButton20() / TODO: Add your control notification handler code heretemp=m_EDIT;op=-; /標志,用于等號算法時選擇執(zhí)行m_EDIT=_T();UpdateData(FALSE);void CMy0952100407Dlg:OnButton21() / TODO: Add yo

28、ur control notification handler code heretemp=m_EDIT;op=*; /標志,用于等號算法時選擇執(zhí)行m_EDIT=_T();UpdateData(FALSE);void CMy0952100407Dlg:OnButton22() / TODO: Add your control notification handler code heretemp=m_EDIT;op=/; /標志,用于等號算法時選擇執(zhí)行m_EDIT=_T();UpdateData(FALSE);void CMy0952100407Dlg:OnButton23() /正負號/ TO

29、DO: Add your control notification handler code heredouble tmp;tmp=(double)atof(m_EDIT);tmp*=-1;m_EDIT.Format(%g,tmp);UpdateData(FALSE);void CMy0952100407Dlg:OnButton24() / TODO: Add your control notification handler code herechar c50;switch(form) /判斷form的值,不同進制的輸出控制格式不一樣case H:first=_tcstoul(temp, 0

30、, 16); /十六進制下的運算,輸出控制我十六進制%x second=_tcstoul(m_EDIT, 0, 16); switch (op) /判斷op標志的值,不同值進行不同的運算 case : result=(int)pow(first,second); m_EDIT.Format(%X,result); break; case +: result=first+second; m_EDIT.Format(%X,result); break; case -: result=first-second; m_EDIT.Format(%X,result); break; case *: res

31、ult=first*second; m_EDIT.Format(%X,result); break; case /: if(second=0) MessageBox(除數(shù)不能為0!,ERROR!); m_EDIT=_T(); else result=first/second; m_EDIT.Format(%X,result); break; default: return; UpdateData(FALSE); break;case D: a = atof(temp); /十進制下的運算,輸出十進制格式%f b = atof(m_EDIT); /a、b、r定義成double型,可以進行小數(shù)的操

32、作 switch (op) /判斷op標志的值,不同值進行不同的運算 case : r=pow(a,b); m_EDIT.Format(%f,r); break; case %: if(second=0) MessageBox(第二個操作數(shù)不能為0!,ERROR!); m_EDIT=_T(); else result=first%second; m_EDIT.Format(%d,result); break; case +: r=a+b; m_EDIT.Format(%f,r); break; case -: r=a-b; m_EDIT.Format(%f,r); break; case *:

33、 r=a*b; m_EDIT.Format(%f,r); break; case /:if(b=0) MessageBox(除數(shù)不能為0!,ERROR!); m_EDIT=_T(); else r=a/b; m_EDIT.Format(%f,r); break; default: return; UpdateData(FALSE); break;case O:first=_tcstoul(temp, 0, 8); /八進制下的運算,輸出為八進制%o second=_tcstoul(m_EDIT, 0, 8); switch (op) /判斷op標志的值,不同值進行不同的運算 case : re

34、sult=(int)pow(first,second); m_EDIT.Format(%o,result); break; case +: result=first+second; m_EDIT.Format(%o,result); break; case -: result=first-second; m_EDIT.Format(%o,result); break; case *: result=first*second; m_EDIT.Format(%o,result); break; case /: if(second=0) MessageBox(除數(shù)不能為0!,ERROR!); /彈出

35、錯誤對話框 m_EDIT=_T(); else result=first/second; m_EDIT.Format(%o,result); break; default: return; UpdateData(FALSE); break;case B: first=_tcstoul(temp, 0, 2); /二進制下的運算,輸出控制格式為字符串%s second=_tcstoul(m_EDIT, 0, 2); switch (op) /判斷op標志的值,不同值進行不同的運算 case : result=(int)pow(first,second); itoa(result,c,2); m_

36、EDIT.Format(%s,c); break; case +: result=first+second; itoa(result,c,2); m_EDIT.Format(%s,c); break; case -: result=first-second; itoa(result,c,2); m_EDIT.Format(%s,c); break; case *: result=first*second; itoa(result,c,2); m_EDIT.Format(%s,c); break; case /:if(second=0) MessageBox(除數(shù)不能為0!,ERROR!); /

37、彈出錯誤對話框 m_EDIT=_T(); else result=first/second; itoa(result,c,2); m_EDIT.Format(%s,c); break; default: return; UpdateData(FALSE); break;default:return;void CMy0952100407Dlg:OnButton25() / TODO: Add your control notification handler code heretemp=m_EDIT;op=; /標志,用于等號算法時選擇執(zhí)行m_EDIT=_T(); UpdateData(FALS

38、E);void CMy0952100407Dlg:OnButton30() /對數(shù)運算/ TODO: Add your control notification handler code heredouble logtemp;int logt;char logch50;switch(form) /進制case H: logt=_tcstoul(m_EDIT, 0, 16);if(logt0) m_EDIT.Format(%X,(int)log10(logt); UpdateData(false);else MessageBox(操作數(shù)應(yīng)大于零,ERROR!); /彈出錯誤對話框 m_EDIT=

39、_T(); UpdateData(false); break;case D: logtemp=atof(m_EDIT);if(logtemp0) m_EDIT.Format(%f,log10(logtemp); UpdateData(false);else MessageBox(操作數(shù)應(yīng)大于零,ERROR!); /彈出錯誤對話框 m_EDIT=_T(); UpdateData(false); break; case O: logt=_tcstoul(m_EDIT, 0, 8);if(logt0) m_EDIT.Format(%o,(int)log10(logt); UpdateData(fal

40、se);else MessageBox(操作數(shù)應(yīng)大于零,ERROR!); /彈出錯誤對話框 m_EDIT=_T(); UpdateData(false); break;case B: logt=_tcstoul(m_EDIT, 0, 2);if(logt0) itoa(int)log10(logt),logch,2); m_EDIT.Format(%s,logch); UpdateData(false);else MessageBox(操作數(shù)應(yīng)大于零,ERROR!); /彈出錯誤對話框 m_EDIT=_T(); UpdateData(false); break; default: return

41、;void CMy0952100407Dlg:OnButton35() /退格/ TODO: Add your control notification handler code here m_EDIT=m_EDIT.Left(m_EDIT.GetLength()-1);UpdateData(false);void CMy0952100407Dlg:OnButton36() /清空/ TODO: Add your control notification handler code herem_EDIT=_T();UpdateData(false);void CMy0952100407Dlg:O

42、nButton26() /正弦計算/ TODO: Add your control notification handler code here double sintemp; sintemp=atof(m_EDIT); m_EDIT.Format(%f,sin(sintemp * 3.141592653 / 180); /進行弧度與角度的轉(zhuǎn)換 UpdateData(false);void CMy0952100407Dlg:OnButton27() /余弦/ TODO: Add your control notification handler code here double costemp; costemp=atof(m_EDIT); m_EDIT.Format(%f,cos(costemp * 3.141592653 / 180); /進行弧度與角度的轉(zhuǎn)換UpdateData(false);void CMy0952100407Dlg:OnButton28() /正切/ TODO: Add your control no

溫馨提示

  • 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)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論