收藏 分销(赏)

vc6.0中用MFC编写计算器代码.doc

上传人:xrp****65 文档编号:7438472 上传时间:2025-01-04 格式:DOC 页数:14 大小:67.50KB 下载积分:10 金币
下载 相关 举报
vc6.0中用MFC编写计算器代码.doc_第1页
第1页 / 共14页
vc6.0中用MFC编写计算器代码.doc_第2页
第2页 / 共14页


点击查看更多>>
资源描述
// CKcalculateDlg.cpp : implementation file // #include "stdafx.h" #include "math.h" #include "CKcalculate.h" #include "CKcalculateDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static 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 CDialog { public: 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 // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCKcalculateDlg dialog CCKcalculateDlg::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 Win32 m_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_MAP } BEGIN_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_MAP END_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 < 0xF000); CMenu* 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 this automatically // when the application's main window is not a SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CCKcalculateDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::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 painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int 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 icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::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 here onButtonN(0); } void CCKcalculateDlg::OnButton1() { // TODO: Add your control notification handler code here onButtonN(1); } void CCKcalculateDlg::OnButton2() { // TODO: Add your control notification handler code here onButtonN(2); } void CCKcalculateDlg::OnButton3() { // TODO: Add your control notification handler code here onButtonN(3); } void CCKcalculateDlg::OnButton4() { // TODO: Add your control notification handler code here onButtonN(4); } void CCKcalculateDlg::OnButton5() { // TODO: Add your control notification handler code here onButtonN(5); } void CCKcalculateDlg::OnButton6() { // TODO: Add your control notification handler code here onButtonN(6); } void CCKcalculateDlg::OnButton7() { // TODO: Add your control notification handler code here onButtonN(7); } void CCKcalculateDlg::OnButton8() { // TODO: Add your control notification handler code here onButtonN(8); } void CCKcalculateDlg::OnButton9() { // TODO: Add your control notification handler code here onButtonN(9); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////计算按键代码 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void CCKcalculateDlg::OnButtonAdd() { // TODO: Add your control notification handler code here Calculate(); m_operator="+"; } void CCKcalculateDlg::OnButtonMinus() { // TODO: Add your control notification handler code here Calculate(); m_operator="-"; } void CCKcalculateDlg::OnButtonMutiply() { // TODO: Add your control notification handler code here Calculate(); m_operator="*"; } void CCKcalculateDlg::OnButtonDiv() { // TODO: Add your control notification handler code here Calculate(); m_operator="/"; } void CCKcalculateDlg::OnButtonSigh() { // TODO: Add your control notification handler code here m_second=-m_second; UpdateDisplay(m_second); } void CCKcalculateDlg::OnButtonPoint() { // TODO: Add your control notification handler code here m_coff =0.1; } void CCKcalculateDlg::OnButtonClear() { // TODO: Add your control notification handler code here m_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 here if( 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 here if( 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); } else { m_second=1.0/m_second; UpdateDisplay(m_second); } } void CCKcalculateDlg::OnButtonEqual() { // TODO: Add your control notification handler code here XdeY(); 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_display UpdateData(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 ) <= 0.000001) { m_display="除数不能为0"; UpdateData(false); return; } m_first/=m_second;break; } m_second=0.0; m_coff=1.0; m_operator=_T("+"); //更新编辑框显示内容 UpdateDisplay(m_first); } void CCKcalculateDlg::onButtonN(int n) { if(m_coff==1.0) //作为整数输入数字时 m_second=m_second*10+n; else { //作为小数输入数字 m_second=m_second+n*m_coff; m_coff*=0.1; } //更新编辑框的数字显示 UpdateDisplay(m_second); } void CCKcalculateDlg::OnButtonSin() { // TODO: Add your control notification handler code here m_second=m_second*m_PI/180; m_second=sin(m_second); UpdateDisplay(m_second); } void CCKcalculateDlg::OnButtonCos() { // TODO: Add your control notification handler code here m_second=m_second*m_PI/180; m_second=cos(m_second); UpdateDisplay(m_second); } void CCKcalculateDlg::OnButtonLog() { // TODO: Add your control notification handler code here m_second=log10(m_second); UpdateDisplay(m_second); } void CCKcalculateDlg::OnButtonXdeY() { // TODO: Add your control notification handler code here temp=m_second; m_second=0.0; UpdateDisplay(m_second); isXdeY=true; } void CCKcalculateDlg::XdeY() { if(isXdeY) { m_second=m_second; m_second=pow(temp,m_second); } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////以下是CKcalculateDlg.h代码 // /////////CKcalculateDlg.h : header file ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #if !defined(AFX_CKCALCULATEDLG_H__8DEB211E_CFCF_44C8_895C_BEF4E77EBE6C__INCLUDED_) #define AFX_CKCALCULATEDLG_H__8DEB211E_CFCF_44C8_895C_BEF4E77EBE6C__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 ///////////////////////////////////////////////////////////////////////////// // CCKcalculateDlg dialog class CCKcalculateDlg : public CDialog { // Construction public: 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 }; CString m_display; //}}AFX_DATA double 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 // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CCKcalculateDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnButton0(); afx_msg void OnButton1(); afx_msg void OnButton2(); afx_msg void OnButton3(); afx_msg void OnButton4(); afx_msg void OnButton5(); afx_msg void OnButton6(); afx_msg void OnButton7(); afx_msg void OnButton8(); afx_msg void OnButton9(); afx_msg void OnButtonAdd(); afx_msg void OnButtonMinus(); afx_msg void OnButtonMutiply(); afx_msg void OnButtonDiv(); afx_msg void OnButtonSigh(); afx_msg void OnButtonPoint(); afx_msg void OnButtonClear(); afx_msg void OnButtonSqrt(); afx_msg void OnButtonReci(); afx_msg void OnButtonEqual(); afx_msg void OnButtonSin(); afx_msg void OnButtonCos(); afx_msg void OnButtonLog(); afx_msg void OnButtonXdeY(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_CKCALCULATEDLG_H__8DEB211E_CFCF_44C8_895C_BEF4E77EBE6C__INCLUDED_)
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 应用文书 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服