![]() |
#2
小海豚2010-11-17 10:43
///////这是在计算器.h的头文件中
// 计算机.h : main header file for the 计算机 application // #if !defined(AFX__H__89A59E9C_4F19_4A90_8E5F_0BD797971356__INCLUDED_) #define AFX__H__89A59E9C_4F19_4A90_8E5F_0BD797971356__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols ///////////////////////////////////////////////////////////////////////////// // CMyApp: // See 计算机.cpp for the implementation of this class // class CMyApp : public CWinApp { public: CMyApp(); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMyApp) public: virtual BOOL InitInstance(); //}}AFX_VIRTUAL // Implementation //{{AFX_MSG(CMyApp) // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX__H__89A59E9C_4F19_4A90_8E5F_0BD797971356__INCLUDED_) /////////这是在计算器Dlg.h的头文件中 // 计算机Dlg.h : header file // #if !defined(AFX_DLG_H__903B19D8_E035_477A_AC56_3E449141C8B4__INCLUDED_) #define AFX_DLG_H__903B19D8_E035_477A_AC56_3E449141C8B4__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 ///////////////////////////////////////////////////////////////////////////// // CMyDlg dialog class CMyDlg : public CDialog { // Construction public: CMyDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CMyDlg) enum { IDD = IDD_MY_DIALOG }; CString m_result; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMyDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: HICON m_hIcon; //新增变量 CString num1,num2; BOOL isresult; //判断第一个数据是否输入完成,用于区分第一个与第二个数据的标记 int witch; //用来标记运算符号 double result2; // Generated message map functions //{{AFX_MSG(CMyDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); 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 OnButton0(); afx_msg void OnBUTTONAdd(); afx_msg void OnBUTTONMinus(); afx_msg void OnBUTTONMultiply(); afx_msg void OnBUTTONDivide(); afx_msg void OnBUTTONequal(); afx_msg void OnBUTTONClear(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_DLG_H__903B19D8_E035_477A_AC56_3E449141C8B4__INCLUDED_) ///////////////这是在计算器Dlg.cpp的C++源文件中 // 计算机Dlg.cpp : implementation file // #include "stdafx.h" #include "计算机.h" #include "计算机Dlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // 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() ///////////////////////////////////////////////////////////////////////////// // CMyDlg dialog CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/) : CDialog(CMyDlg::IDD, pParent) { //{{AFX_DATA_INIT(CMyDlg) m_result = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); //初始化变量 num1 = ""; num2 = ""; isresult = FALSE; witch = 0; result2 = 0.0; } void CMyDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMyDlg) DDX_Text(pDX, IDC_EDIT1, m_result); DDV_MaxChars(pDX, m_result, 10); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CMyDlg, CDialog) //{{AFX_MSG_MAP(CMyDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() 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_BUTTON0, OnButton0) ON_BN_CLICKED(IDC_BUTTON_Add, OnBUTTONAdd) ON_BN_CLICKED(IDC_BUTTON_Minus, OnBUTTONMinus) ON_BN_CLICKED(IDC_BUTTON_Multiply, OnBUTTONMultiply) ON_BN_CLICKED(IDC_BUTTON_Divide, OnBUTTONDivide) ON_BN_CLICKED(IDC_BUTTON_equal, OnBUTTONequal) ON_BN_CLICKED(IDC_BUTTON_Clear, OnBUTTONClear) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMyDlg message handlers BOOL CMyDlg::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 dialog 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 CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CMyDlg::OnButton1() //设置显示数字1 { // TODO: Add your control notification handler code here if( isresult == false ) { num1 += "1"; //"+"是字符串连接符 m_result = num1; UpdateData(FALSE); //拷贝变量值到控件显示 } if( isresult == true ) { num2 += "1"; m_result = num2; UpdateData(FALSE); } } void CMyDlg::OnButton2() //设置显示数字2 { // TODO: Add your control notification handler code here if( isresult == false ) { num1 += "2"; m_result = num1; UpdateData(FALSE); } if( isresult == true ) { num2 += "2"; m_result = num2; UpdateData(FALSE); } } void CMyDlg::OnButton3() //设置显示数字3 { // TODO: Add your control notification handler code here if( isresult == false ) { num1 += "3"; m_result = num1; UpdateData(FALSE); } if( isresult == true ) { num2 += "3"; m_result = num2; UpdateData(FALSE); } } void CMyDlg::OnButton4() //设置显示数字4 { // TODO: Add your control notification handler code here if( isresult == false ) { num1 += "4"; m_result = num1; UpdateData(FALSE); } if( isresult == true ) { num2 += "4"; m_result = num2; UpdateData(FALSE); } } void CMyDlg::OnButton5() //设置显示数字5 { // TODO: Add your control notification handler code here if( isresult == false ) { num1 += "5"; m_result = num1; UpdateData(FALSE); } if( isresult == true ) { num2 += "5"; m_result = num2; UpdateData(FALSE); } } void CMyDlg::OnButton6() //设置显示数字6 { // TODO: Add your control notification handler code here if( isresult == false ) { num1 += "6"; m_result = num1; UpdateData(FALSE); } if( isresult == true ) { num2 += "6"; m_result = num2; UpdateData(FALSE); } } void CMyDlg::OnButton7() //设置显示数字7 { // TODO: Add your control notification handler code here if( isresult == false ) { num1 += "7"; m_result = num1; UpdateData(FALSE); } if( isresult == true ) { num2 += "7"; m_result = num2; UpdateData(FALSE); } } void CMyDlg::OnButton8() //设置显示数字8 { // TODO: Add your control notification handler code here if( isresult == false ) { num1 += "8"; m_result = num1; UpdateData(FALSE); } if( isresult == true ) { num2 += "8"; m_result = num2; UpdateData(FALSE); } } void CMyDlg::OnButton9() //设置显示数字9 { // TODO: Add your control notification handler code here if( isresult == false ) { num1 += "9"; m_result = num1; UpdateData(FALSE); } if( isresult == true ) { num2 += "9"; m_result = num2; UpdateData(FALSE); } } void CMyDlg::OnButton0() //设置显示数字0 { // TODO: Add your control notification handler code here if( isresult == false ) { num1 += "0"; m_result = num1; UpdateData(FALSE); } if( isresult == true ) { num2 += "0"; m_result = num2; UpdateData(FALSE); } } void CMyDlg::OnBUTTONAdd() //设置加法运算 { // TODO: Add your control notification handler code here isresult = TRUE; witch = 1; //加减乘除依次为1,2,3,4 } void CMyDlg::OnBUTTONMinus() //设置减法运算 { // TODO: Add your control notification handler code here isresult = TRUE; witch = 2; } void CMyDlg::OnBUTTONMultiply() //设置乘法运算 { // TODO: Add your control notification handler code here isresult = TRUE; witch = 3; } void CMyDlg::OnBUTTONDivide() //设置除法运算 { // TODO: Add your control notification handler code here isresult = TRUE; witch = 4; } void CMyDlg::OnBUTTONequal() //设置等号运算 { // TODO: Add your control notification handler code here double result = 0.0; switch(witch) //判断加减乘除并对它做相应的处理 { case 1: result = atof(num1) + atof(num2); //atof是将CString类型的串强制转换为实型变量 break; case 2: result = atof(num1) - atof(num2); break; case 3: result = atof(num1) * atof(num2); break; case 4: result = atof(num1) / atof(num2); break; default:AfxMessageBox("程序出错!"); break; } m_result.Format("%f",result); //format是格式化函数,把小数点前后没有意义的0去掉 UpdateData(false); result2 = result; num2 = ""; } void CMyDlg::OnBUTTONClear() //设置清零运算 { // TODO: Add your control notification handler code here num1 = ""; num2 = ""; isresult = FALSE; result2 = 0.0; m_result = ""; UpdateData(FALSE); } /////////////////////这是在计算器.cpp的C++源文件中 // 计算机.cpp : Defines the class behaviors for the application. // #include "stdafx.h" #include "计算机.h" #include "计算机Dlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMyApp BEGIN_MESSAGE_MAP(CMyApp, CWinApp) //{{AFX_MSG_MAP(CMyApp) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMyApp construction CMyApp::CMyApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CMyApp object CMyApp theApp; ///////////////////////////////////////////////////////////////////////////// // CMyApp initialization BOOL CMyApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif CMyDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; } |
能进行多则运算,如1+2*3/6=2.
本人不太会优先级的编写!