
Vc++6.0 mfc做一个简单计算器 能加减乘除开方平方就可以, 最主要的是工程名改为p0206 5
1个回答
展开全部
MFC深入浅出
demo:
头文件:
// CalculatorDlg.h : 头文件#pragma once// CCalculatorDlg 对话框class CCalculatorDlg : public CDialogEx{// 构造public:CCalculatorDlg(CWnd* pParent = NULL); // 标准构造函数void SaveValue1();// 对话框数据enum { IDD = IDD_CALCULATOR_DIALOG };protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持// 实现protected:HICON m_hIcon;// 生成的消息映射函数virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();DECLARE_MESSAGE_MAP()public:afx_msg void OnBnClickedButton0();afx_msg void OnBnClickedButton1();afx_msg void OnBnClickedButton2();afx_msg void OnBnClickedButton3();afx_msg void OnBnClickedButton4();afx_msg void OnBnClickedButton5();afx_msg void OnBnClickedButton6();afx_msg void OnBnClickedButton7();afx_msg void OnBnClickedButton8();afx_msg void OnBnClickedButton9();afx_msg void OnBnClickedPoint();afx_msg void OnBnClickedButtonJia();afx_msg void OnBnClickedButtonJian();afx_msg void OnBnClickedButtonCheng();afx_msg void OnBnClickedButtonChu();afx_msg void OnBnClickedButtonAc();afx_msg void OnBnClickedButtonGenhao();afx_msg void OnBnClickedButtonCifang();afx_msg void OnBnClickedButtonBack();afx_msg void OnBnClickedButtondDeng();afx_msg void OnEnChangeEdit1(); double m_fNumber1; double m_fNumber2;int m_iSign; //计算 void Calculator(); void CCalculatorDlg::OnButtonKaiFang() ;void CCalculatorDlg::OnButtonPingFang();void CCalculatorDlg::OnButtonClear();CString m_str;afx_msg void OnBnClickedButton10();afx_msg void OnBnClickedButton12();afx_msg void OnBnClickedButton11();afx_msg void OnBnClickedButton13();};源文件:
// CalculatorDlg.cpp : 实现文件#include "stdafx.h"#include "Calculator.h"#include "CalculatorDlg.h"#include "afxdialogex.h"#include <stdlib.h>#include <math.h>#include<stdio.h>#ifdef _DEBUG#define new DEBUG_NEW#endif// 用于应用程序“关于”菜单项的 CAboutDlg 对话框class CAboutDlg : public CDialogEx{public:CAboutDlg();// 对话框数据enum { IDD = IDD_ABOUTBOX };protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持// 实现protected:DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD){}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialogEx::DoDataExchange(pDX);} BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)END_MESSAGE_MAP() // CCalculatorDlg 对话框CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/): CDialogEx(CCalculatorDlg::IDD, pParent), m_str(_T("")){m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CCalculatorDlg::DoDataExchange(CDataExchange* pDX){CDialogEx::DoDataExchange(pDX);DDX_Text(pDX, IDC_EDIT1, m_str);DDX_Text(pDX, IDC_EDIT1, m_str);}BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON_0, &CCalculatorDlg::OnBnClickedButton0)ON_BN_CLICKED(IDC_BUTTON_6, &CCalculatorDlg::OnBnClickedButton6)ON_BN_CLICKED(IDC_BUTTON_1, &CCalculatorDlg::OnBnClickedButton1)ON_BN_CLICKED(IDC_BUTTON_7, &CCalculatorDlg::OnBnClickedButton7)ON_BN_CLICKED(IDC_BUTTON_JIA, &CCalculatorDlg::OnBnClickedButtonJia)ON_BN_CLICKED(IDC_BUTTON_AC, &CCalculatorDlg::OnBnClickedButtonAc)ON_BN_CLICKED(IDC_BUTTON_GENHAO, &CCalculatorDlg::OnBnClickedButtonGenhao)ON_BN_CLICKED(IDC_BUTTON_CIFANG, &CCalculatorDlg::OnBnClickedButtonCifang)ON_BN_CLICKED(IDC_BUTTON_CHU, &CCalculatorDlg::OnBnClickedButtonChu)ON_BN_CLICKED(IDC_BUTTON_BAIFEN, &CCalculatorDlg::OnBnClickedButtonBack)ON_BN_CLICKED(IDC_BUTTOND_DENG, &CCalculatorDlg::OnBnClickedButtondDeng)ON_BN_CLICKED(IDC_POINT, &CCalculatorDlg::OnBnClickedPoint)ON_BN_CLICKED(IDC_BUTTON_JIAN, &CCalculatorDlg::OnBnClickedButtonJian)ON_BN_CLICKED(IDC_BUTTON_8, &CCalculatorDlg::OnBnClickedButton8)ON_BN_CLICKED(IDC_BUTTON_9, &CCalculatorDlg::OnBnClickedButton9)ON_BN_CLICKED(IDC_BUTTON_CHENG, &CCalculatorDlg::OnBnClickedButtonCheng)ON_EN_CHANGE(IDC_EDIT1, &CCalculatorDlg::OnEnChangeEdit1)ON_BN_CLICKED(IDC_BUTTON_10, &CCalculatorDlg::OnBnClickedButton10)ON_BN_CLICKED(IDC_BUTTON_12, &CCalculatorDlg::OnBnClickedButton12)ON_BN_CLICKED(IDC_BUTTON_11, &CCalculatorDlg::OnBnClickedButton11)ON_BN_CLICKED(IDC_BUTTON_13, &CCalculatorDlg::OnBnClickedButton13)END_MESSAGE_MAP()// CCalculatorDlg 消息处理程序BOOL CCalculatorDlg::OnInitDialog(){CDialogEx::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。// IDM_ABOUTBOX 必须在系统命令范围内。ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){BOOL bNameValid;CString strAboutMenu;bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);ASSERT(bNameValid);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}} // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动// 执行此操作SetIcon(m_hIcon, TRUE); // 设置大图标SetIcon(m_hIcon, FALSE); // 设置小图标// TODO: 在此添加额外的初始化代码return TRUE; // 除非将焦点设置到控件,否则返回 TRUE}void CCalculatorDlg::OnSysCommand(UINT nID, LPARAM lParam){if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialogEx::OnSysCommand(nID, lParam);}} // 如果向对话框添加最小化按钮,则需要下面的代码// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,// 这将由框架自动完成。 \\void CCalculatorDlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // 用于绘制的设备上下文SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);// 使图标在工作区矩形中居中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;// 绘制图标dc.DrawIcon(x, y, m_hIcon);}else{CDialogEx::OnPaint();}} //当用户拖动最小化窗口时系统调用此函数取得光标//显示。HCURSOR CCalculatorDlg::OnQueryDragIcon(){return static_cast<HCURSOR>(m_hIcon);}void CCalculatorDlg::OnBnClickedButton2(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); m_str = m_str + '2'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButton3(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); m_str = m_str + '3'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButton5(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); m_str = m_str + '5'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButton0(){// TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); m_str = m_str + '0'; UpdateData(FALSE); }void CCalculatorDlg::OnBnClickedButton6(){// TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); m_str = m_str + '6'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButton1(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); m_str = m_str + '1'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButton7(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); m_str= m_str + '7'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButtonJia(){// TODO: 在此添加控件通知处理程序代码SaveValue1(); m_iSign = 0; }void CCalculatorDlg::OnBnClickedButtonAc(){// TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); m_str = ""; m_fNumber1 = 0.0; m_fNumber2 = 0.0; m_iSign = 0; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButtonGenhao(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); m_fNumber1 = _ttof (m_str); double f = sqrt(m_fNumber1); //如果浮点数其实是个整数 if (f - int(f) <= 1e-5){m_str.Format("%d", (int)f);}else{m_str.Format("%f", f);} UpdateData(FALSE); m_fNumber1 = f; m_fNumber2 = 0.0; m_iSign = 5; }void CCalculatorDlg::OnBnClickedButtonCifang(){// TODO: 在此添加控件通知处理程序代码 // TODO: Add your control notification handler code here UpdateData(TRUE); m_fNumber1 = _ttof (m_str); double f = m_fNumber1 * m_fNumber1; //如果浮点数其实是个整数 if (f - int(f) <= 1e-5){m_str.Format("%d", (int)f);}else{m_str.Format("%f", f);} UpdateData(FALSE); m_fNumber1 = f; m_fNumber2 = 0.0; m_iSign = 4; }void CCalculatorDlg::OnBnClickedButtonChu(){// TODO: 在此添加控件通知处理程序代码SaveValue1(); m_iSign = 3; }void CCalculatorDlg::OnBnClickedButtonBaifen(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); //移除最右边一个字符 if (!m_str.IsEmpty()) { m_str = m_str.Left(m_str.GetLength()-1); } UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButtondDeng(){// TODO: 在此添加控件通知处理程序代码Calculator(); }void CCalculatorDlg::OnBnClickedPoint(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); //如果没有小数点,则加上一个小数点,如果已有小数点就忽略此次的小数点,保证最多只有1个 if (-1 == m_str.Find('.')) { m_str = m_str + '.'; } UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButtonJian(){// TODO: 在此添加控件通知处理程序代码SaveValue1(); m_iSign = 1; }void CCalculatorDlg::OnBnClickedButton8(){// TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); m_str = m_str + '8'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButton9(){// TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); m_str = m_str + '9'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButtonCheng(){// TODO: 在此添加控件通知处理程序代码SaveValue1(); m_iSign = 2;}void CCalculatorDlg::SaveValue1() { UpdateData(TRUE); m_fNumber1 = _ttof (m_str); m_str = ""; UpdateData(FALSE); } void CCalculatorDlg::Calculator(){ UpdateData(TRUE);m_fNumber2 = _ttof(m_str);double f = 0.0;switch (m_iSign){//加 case 0:f = m_fNumber1 + m_fNumber2;break;//减 case 1:f = m_fNumber1 - m_fNumber2;break;//乘 case 2:f = m_fNumber1 * m_fNumber2;break;//除 case 3:if (m_fNumber2 == 0.0){f = m_fNumber1;}else{f = m_fNumber1 / m_fNumber2;}break;//平方 default:break;}//如果浮点数其实是个整数 if (f - int(f) <= 1e-5){m_str.Format("%d", (int)f);}else{m_str.Format("%f", f);}UpdateData(FALSE); m_fNumber1 = f;m_fNumber2 = 0.0;} void CCalculatorDlg::OnEnChangeEdit1(){// TODO: 如果该控件是 RICHEDIT 控件,它将不// 发送此通知,除非重写 CDialogEx::OnInitDialog()// 函数并调用 CRichEditCtrl().SetEventMask(),// 同时将 ENM_CHANGE 标志“或”运算到掩码中。// TODO: 在此添加控件通知处理程序代码}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |