《面向程序设计》MFC计算器.doc_第1页
《面向程序设计》MFC计算器.doc_第2页
《面向程序设计》MFC计算器.doc_第3页
《面向程序设计》MFC计算器.doc_第4页
《面向程序设计》MFC计算器.doc_第5页
免费预览已结束,剩余20页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

面向程序设计实训报告专 业: 学生姓名: 学 号: 指导教师: 2011年6月 29日面向程序设计实训报告1、实训项目:设计一个基于对话框的计算器,实现常用计算器的基本功能。2、性能、指标、要求;1、基本要求(1) 按照题目要求独立设计计算器所需的所有按钮,并完成程序源码编写、调试及关键代码注释。(2)能够实现+、-、*、/计算。 (3)能够实现对数及幂函数计算。 (4)能够实现三角函数及反三角函数计算。 2、发挥部分 能够实现8,16,10,2进制之间相互转换。 3、方案设计: 方案论证 通过运行VC6.0,用MFC制作windows风格的界面的计算器 工作原理 1、启动VC+6.0, 从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对话框。可以修改分类名等信息,在这里不做任何更改,直接点完成。至此进入了界面设计过程。4、软件设计 进入界面设计界面后,利用右侧的控件栏添加按钮,在按钮属性中修改按键的显示,双击按键进入按键的程序设计。制作完成后的界面如下所示:5、制作与调试过程; 双击按钮后进入程序设计过程,每个按钮的程序如下所示:/ 0952100407Dlg.cpp : implementation file/#include stdafx.h#include 0952100407.h#include 0952100407Dlg.h#include cmath#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 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 CAboutDlg: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()/ CMy0952100407Dlg dialogCMy0952100407Dlg:CMy0952100407Dlg(CWnd* pParent /*=NULL*/): CDialog(CMy0952100407Dlg:IDD, pParent)/AFX_DATA_INIT(CMy0952100407Dlg)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);/AFX_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(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_CLICKED(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)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, 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_RADIO3, 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.ASSERT(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 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(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 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;/ 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.HCURSOR CMy0952100407Dlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CMy0952100407Dlg: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 notification handler code herevoid CMy0952100407Dlg:OnButton2() / TODO: Add your control 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);UpdateData(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: 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_EDIT += _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:OnButton12() / 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 handler 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);void 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 control notification handler code heretemp=m_EDIT;op=+; /标志,用于等号算法时选择执行m_EDIT=_T();UpdateData(FALSE);void CMy0952100407Dlg:OnButton20() / TODO: Add your control notification handler code heretemp=m_EDIT;op=-; /标志,用于等号算法时选择执行m_EDIT=_T();UpdateData(FALSE);void CMy0952100407Dlg:OnButton21() / TODO: Add your control notification handler code heretemp=m_EDIT;op=*; /标志,用于等号算法时选择执行m_EDIT=_T();UpdateData(FALSE);void CMy0952100407Dlg:OnButton22() / TODO: Add your control notification handler code heretemp=m_EDIT;op=/; /标志,用于等号算法时选择执行m_EDIT=_T();UpdateData(FALSE);void CMy0952100407Dlg:OnButton23() /正负号/ TODO: 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, 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 *: result=first*second; m_EDIT.Format(%X,result); break; case /: if(second=0) MessageBox(除数不能为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型,可以进行小数的操作 switch (op) /判断op标志的值,不同值进行不同的运算 case : r=pow(a,b); m_EDIT.Format(%f,r); break; case %: if(second=0) MessageBox(第二个操作数不能为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 *: r=a*b; m_EDIT.Format(%f,r); break; case /:if(b=0) MessageBox(除数不能为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 : result=(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(除数不能为0!,ERROR!); /弹出错误对话框 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_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(除数不能为0!,ERROR!); /弹出错误对话框 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=; /标志,用于等号算法时选择执行m_EDIT=_T(); UpdateData(FALSE);void CMy0952100407Dlg:OnButton30() /对数运算/ 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(操作数应大于零,ERROR!); /弹出错误对话框 m_EDIT=_T(); UpdateData(false); break;case D: logtemp=atof(m_EDIT);if(logtemp0) m_EDIT.Format(%f,log10(logtemp); UpdateData(false);else MessageBox(操作数应大于零,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(false);else MessageBox(操作数应大于零,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(操作数应大于零,ERROR!); /弹出错误对话框 m_EDIT=_T(); UpdateData(false); break; default: return;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:OnButton26() /正弦计算/ TODO: Add your control notification handler code here double sintemp; sintemp=atof(m_EDIT); m_EDIT.Format(%f,sin(sintemp * 3.141592653 / 180); /进行弧度与角度的转换 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); /进行弧度与角度的转换UpdateData(false);void CMy0952100407Dlg:OnButton28() /正切/ TODO: Add your control no

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论