vc6.0中用MFC编写计算器代码.doc_第1页
vc6.0中用MFC编写计算器代码.doc_第2页
vc6.0中用MFC编写计算器代码.doc_第3页
vc6.0中用MFC编写计算器代码.doc_第4页
vc6.0中用MFC编写计算器代码.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

/ CKcalculateDlg.cpp : implementation file/#include stdafx.h#include math.h#include CKcalculate.h#include CKcalculateDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/double m_first;/存储一次运算的第一个操作数及一次运算的结果/ double m_second; /存储一次运算的第二个操作数/CString m_operator;/存储运算符/double m_coff;/存储小数点的系数权值/ CAboutDlg dialog used for App About/class 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()/ CCKcalculateDlg dialogCCKcalculateDlg:CCKcalculateDlg(CWnd* pParent /*=NULL*/): CDialog(CCKcalculateDlg:IDD, pParent)/void UpdateDisplay(double);/void Calculate(void);/AFX_DATA_INIT(CCKcalculateDlg)/isXdeY=false;m_display = _T();m_PI=3.14159265358979323846;m_first=0.0;m_second=0.0;m_operator=_T(+);m_coff=0;m_display=_T(0.0);/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);void CCKcalculateDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CCKcalculateDlg)DDX_Text(pDX, IDC_DISPLAY, m_display);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CCKcalculateDlg, CDialog)/AFX_MSG_MAP(CCKcalculateDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON0, OnButton0)ON_BN_CLICKED(IDC_BUTTON1, OnButton1)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_BUTTON_ADD, OnButtonAdd)ON_BN_CLICKED(IDC_BUTTON_MINUS, OnButtonMinus)ON_BN_CLICKED(IDC_BUTTON_MUTIPLY, OnButtonMutiply)ON_BN_CLICKED(IDC_BUTTON_DIV, OnButtonDiv)ON_BN_CLICKED(IDC_BUTTON_SIGH, OnButtonSigh)ON_BN_CLICKED(IDC_BUTTON_POINT, OnButtonPoint)ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)ON_BN_CLICKED(IDC_BUTTON_SQRT, OnButtonSqrt)ON_BN_CLICKED(IDC_BUTTON_RECI, OnButtonReci)ON_BN_CLICKED(IDC_BUTTON_EQUAL, OnButtonEqual)ON_BN_CLICKED(IDC_BUTTON_SIN, OnButtonSin)ON_BN_CLICKED(IDC_BUTTON_COS, OnButtonCos)ON_BN_CLICKED(IDC_BUTTON_LOG, OnButtonLog)ON_BN_CLICKED(IDC_BUTTON_XDEY, OnButtonXdeY)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CCKcalculateDlg message handlers/BOOL CCKcalculateDlg: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 SetIcon(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 CCKcalculateDlg: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 CCKcalculateDlg: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 CCKcalculateDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;/按键代码/void CCKcalculateDlg:OnButton0() / TODO: Add your control notification handler code hereonButtonN(0);void CCKcalculateDlg:OnButton1() / TODO: Add your control notification handler code hereonButtonN(1);void CCKcalculateDlg:OnButton2() / TODO: Add your control notification handler code hereonButtonN(2);void CCKcalculateDlg:OnButton3() / TODO: Add your control notification handler code hereonButtonN(3);void CCKcalculateDlg:OnButton4() / TODO: Add your control notification handler code hereonButtonN(4);void CCKcalculateDlg:OnButton5() / TODO: Add your control notification handler code hereonButtonN(5);void CCKcalculateDlg:OnButton6() / TODO: Add your control notification handler code hereonButtonN(6);void CCKcalculateDlg:OnButton7() / TODO: Add your control notification handler code hereonButtonN(7);void CCKcalculateDlg:OnButton8() / TODO: Add your control notification handler code hereonButtonN(8);void CCKcalculateDlg:OnButton9() / TODO: Add your control notification handler code hereonButtonN(9);/计算按键代码/void CCKcalculateDlg:OnButtonAdd() / TODO: Add your control notification handler code hereCalculate();m_operator=+;void CCKcalculateDlg:OnButtonMinus() / TODO: Add your control notification handler code hereCalculate();m_operator=-;void CCKcalculateDlg:OnButtonMutiply() / TODO: Add your control notification handler code hereCalculate();m_operator=*;void CCKcalculateDlg:OnButtonDiv() / TODO: Add your control notification handler code hereCalculate();m_operator=/;void CCKcalculateDlg:OnButtonSigh() / TODO: Add your control notification handler code herem_second=-m_second; UpdateDisplay(m_second);void CCKcalculateDlg:OnButtonPoint() / TODO: Add your control notification handler code herem_coff =0.1;void CCKcalculateDlg:OnButtonClear() / TODO: Add your control notification handler code herem_first=0.0;m_second=0.0;m_operator=+;m_coff = 1.0;UpdateDisplay(0.0);void CCKcalculateDlg:OnButtonSqrt() / TODO: Add your control notification handler code hereif( m_second=0 )m_first=sqrt(m_first);UpdateDisplay(m_first);else m_second=sqrt(m_second);UpdateDisplay(m_second); void CCKcalculateDlg:OnButtonReci() / TODO: Add your control notification handler code hereif( fabs(m_second ) 0.000001 & fabs( m_first )0.000001)m_display=除数不能为零;UpdateDisplay(false);return;if( fabs(m_second ) 0.000001)m_first=1.0/m_first;UpdateDisplay(m_first);elsem_second=1.0/m_second;UpdateDisplay(m_second);void CCKcalculateDlg:OnButtonEqual() / TODO: Add your control notification handler code hereXdeY();Calculate();void CCKcalculateDlg:UpdateDisplay(double ck)m_display.Format(_T(%f),ck);int i=m_display.GetLength();/格式化输出,将输出结果后的零截去while(m_display.GetAt(i-1)=0) m_display.Delete(i-1,1);i-;/更新编辑框变量m_displayUpdateData(false); void CCKcalculateDlg:Calculate(void)/将前一次数据与当前数据进行运算,作为下次的第一操作数,并在编辑框显示switch(m_operator.GetAt(0)case +: m_first+=m_second;break;case -: m_first-=m_second;break;case *: m_first*=m_second;break;case /:if( fabs(m_second ) 1000#pragma once#endif / _MSC_VER 1000/ CCKcalculateDlg dialogclass CCKcalculateDlg : public CDialog / Constructionpublic:void XdeY(void);bool isXdeY;double temp;double m_PI;void onButtonN(int n);void UpdateDisplay(double ck);void Calculate();CCKcalculateDlg(CWnd* pParent = NULL);/ standard constructor/ Dialog Data/AFX_DATA(CCKcalculateDlg)enum IDD = IDD_CKCALCULATE_DIALOG ;CStringm_display;/AFX_DATAdouble m_first;/存储一次运算的第一个操作数及一次运算的结果double m_second;/存储一次运算的第二个操作数CString m_operator;/存储运算符double m_coff;/存储小数点的系数权值/ ClassWizard generated virtual function overrides/ AFX_VIRTUAL(CCKcalculateDlg)protected:virtual void DoDataExchange(CDataExchange* pDX);/ DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:HICON m_hIcon;/ Generated message map functions/AFX_MSG(CCKcalculateDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPai

温馨提示

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

评论

0/150

提交评论