vc6.0中用MFC编写计算器代码.doc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- vc6 中用 MFC 编写 计算器 代码
- 资源描述:
-
// 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_)展开阅读全文
咨信网温馨提示:1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。




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



实名认证













自信AI助手
















微信客服
客服QQ
发送邮件
意见反馈



链接地址:https://www.zixin.com.cn/doc/7438472.html