日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MFC制作计算器

發布時間:2024/7/23 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MFC制作计算器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、代碼如下:

// zsjsqDlg.cpp : implementation file //#include "stdafx.h" #include "zsjsq.h" #include "zsjsqDlg.h"#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif/ // CAboutDlg dialog used for App Aboutclass 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_MSGDECLARE_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()/ // CZsjsqDlg dialogCZsjsqDlg::CZsjsqDlg(CWnd* pParent /*=NULL*/): CDialog(CZsjsqDlg::IDD, pParent) {//{{AFX_DATA_INIT(CZsjsqDlg)m_value = 0;m_nDec = -1;//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);m_num1=0;m_num2=0;m_operator=-1;m_bOperator=FALSE;m_nDec=0; }void CZsjsqDlg::DoDataExchange(CDataExchange* pDX) {CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CZsjsqDlg)DDX_Text(pDX, IDC_VALUE, m_value);DDX_Radio(pDX, IDC_DEC, m_nDec);//}}AFX_DATA_MAP }BEGIN_MESSAGE_MAP(CZsjsqDlg, CDialog)//{{AFX_MSG_MAP(CZsjsqDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_B1, OnB1)ON_BN_CLICKED(IDC_B0, OnB0)ON_BN_CLICKED(IDC_B2, OnB2)ON_BN_CLICKED(IDC_B3, OnB3)ON_BN_CLICKED(IDC_B4, OnB4)ON_BN_CLICKED(IDC_B5, OnB5)ON_BN_CLICKED(IDC_B6, OnB6)ON_BN_CLICKED(IDC_B7, OnB7)ON_BN_CLICKED(IDC_B9, OnB9)ON_BN_CLICKED(IDC_BC, OnBc)ON_BN_CLICKED(IDC_DIV, OnDiv)ON_BN_CLICKED(IDC_EQUAL, OnEqual)ON_BN_CLICKED(IDC_MUL, OnMul)ON_BN_CLICKED(IDC_SUB, OnSub)ON_BN_CLICKED(IDC_B8, OnB8)ON_BN_CLICKED(IDC_ADD, OnAdd)ON_BN_CLICKED(IDC_DEC, OnDec)ON_BN_CLICKED(IDC_OCT, OnOct)ON_BN_CLICKED(IDC_CHECK_HEX, OnCheckHex)//}}AFX_MSG_MAP END_MESSAGE_MAP()/ // CZsjsqDlg message handlersBOOL CZsjsqDlg::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 dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization hereCButton *pDec = ((CButton*)GetDlgItem(IDC_DEC));pDec->EnableWindow(FALSE);CButton *pOct = ((CButton*)GetDlgItem(IDC_OCT));pOct->EnableWindow(FALSE);return TRUE; // return TRUE unless you set the focus to a control }void CZsjsqDlg::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 CZsjsqDlg::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);}else{CDialog::OnPaint();} }// The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CZsjsqDlg::OnQueryDragIcon() {return (HCURSOR) m_hIcon; }void CZsjsqDlg::OnB1() {// TODO: Add your control notification handler code herem_value=m_value*10+1;UpdateData(FALSE);}void CZsjsqDlg::OnB0() {// TODO: Add your control notification handler code herem_value=m_value*10+0;UpdateData(FALSE);}void CZsjsqDlg::OnB2() {// TODO: Add your control notification handler code herem_value=m_value*10+2;UpdateData(FALSE);}void CZsjsqDlg::OnB3() {// TODO: Add your control notification handler code herem_value=m_value*10+3;UpdateData(FALSE);}void CZsjsqDlg::OnB4() {// TODO: Add your control notification handler code herem_value=m_value*10+4;UpdateData(FALSE);}void CZsjsqDlg::OnB5() {// TODO: Add your control notification handler code herem_value=m_value*10+5;UpdateData(FALSE);}void CZsjsqDlg::OnB6() {// TODO: Add your control notification handler code herem_value=m_value*10+6;UpdateData(FALSE);}void CZsjsqDlg::OnB7() {// TODO: Add your control notification handler code herem_value=m_value*10+7;UpdateData(FALSE);}void CZsjsqDlg::OnB9() {// TODO: Add your control notification handler code herem_value=m_value*10+9;UpdateData(FALSE);}void CZsjsqDlg::OnBc() {// TODO: Add your control notification handler code herem_value=0;m_num1=0;m_num2=0;m_bOperator=FALSE;m_operator=-1;UpdateData(FALSE);}void CZsjsqDlg::OnDiv() {// TODO: Add your control notification handler code herem_operator=3;m_bOperator=TRUE;m_num1=m_value;m_value=0;}void CZsjsqDlg::OnEqual() {// TODO: Add your control notification handler code hereif(m_bOperator==FALSE){MessageBox("輸入錯誤,請單擊運算符");return;}m_num2=m_value;if(m_nDec == 1) //如果是8進制{m_num1 = octToDec(m_num1); //將運算數轉換為10進制m_num2 = octToDec(m_num2);}switch(m_operator){case 0:m_value=m_num1+m_num2;break;case 1:m_value=m_num1-m_num2;break;case 2:m_value=m_num1*m_num2;break;case 3:{if(m_num2==0){AfxMessageBox("輸入錯誤,除數不能為0");OnBc() ;}else{m_value=m_num1/m_num2;}break;}}if(m_nDec == 1) //如果是8進制{char s8[30];itoa(m_value,s8,8); //將計算結果以8進制顯示m_value = atoi(s8);}UpdateData(FALSE);m_value=0;}void CZsjsqDlg::OnMul() {// TODO: Add your control notification handler code herem_operator=2;m_bOperator=TRUE;m_num1=m_value;m_value=0;}void CZsjsqDlg::OnSub() {// TODO: Add your control notification handler code herem_operator=1;m_bOperator=TRUE;m_num1=m_value;m_value=0;}void CZsjsqDlg::OnB8() {// TODO: Add your control notification handler code herem_value=m_value*10+8;UpdateData(FALSE);}void CZsjsqDlg::OnAdd() {// TODO: Add your control notification handler code herem_operator=0;m_bOperator=TRUE;m_num1=m_value;m_value=0;}void CZsjsqDlg::OnDec() {// TODO: Add your control notification handler code hereCButton *p8 = ((CButton*)GetDlgItem(IDC_B8)); p8->EnableWindow(TRUE); CButton *p9 = ((CButton*)GetDlgItem(IDC_B9));p9->EnableWindow(TRUE);transform(); }void CZsjsqDlg::OnOct() {// TODO: Add your control notification handler code hereCButton *p8 = (CButton*)GetDlgItem(IDC_B8); p8->EnableWindow(FALSE); CButton *p9 = ((CButton*)GetDlgItem(IDC_B9));p9->EnableWindow(FALSE);transform(); }void CZsjsqDlg::transform() {UpdateData(TRUE); if(m_nDec == 0) //10進制單選按鈕被選中{m_value = octToDec(m_value);//將8進制轉換為10進制}else //8進制單選按鈕被選中{char s8[30];itoa(m_value,s8,8); //將10進制轉換為8進制m_value = atoi(s8);}UpdateData(FALSE); }int CZsjsqDlg::octToDec(int value) {int result=0; //結果變量int num = 0; //次冪變量while (value != 0) {int low = value%10; //取個位數int mul = 1;for(int i=0;i<num;i++) //該位數為8的num次冪{mul *= 8;}result = result + low*mul; //累加結果num++;value = value/10; //去掉個位數}return result; }void CZsjsqDlg::OnCheckHex() {// TODO: Add your control notification handler code hereCButton *pHex = ((CButton*)GetDlgItem(IDC_CHECK_HEX));//如果復選框選中if(BST_CHECKED==pHex->GetCheck()){CButton *pDec = ((CButton*)GetDlgItem(IDC_DEC));pDec->EnableWindow(TRUE);CButton *pOct = ((CButton*)GetDlgItem(IDC_OCT));pOct->EnableWindow(TRUE);}else{CButton *pDec = ((CButton*)GetDlgItem(IDC_DEC));pDec->EnableWindow(FALSE);CButton *pOct = ((CButton*)GetDlgItem(IDC_OCT));pOct->EnableWindow(FALSE);}}


// zsjsqDlg.h : header file //#if !defined(AFX_ZSJSQDLG_H__31E05884_F98D_4BA6_A7F5_7C7B80C8E559__INCLUDED_) #define AFX_ZSJSQDLG_H__31E05884_F98D_4BA6_A7F5_7C7B80C8E559__INCLUDED_#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000/ // CZsjsqDlg dialogclass CZsjsqDlg : public CDialog { // Construction public:int octToDec(int value);void transform();BOOL m_bOperator;int m_operator;int m_num2;int m_num1;CZsjsqDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data//{{AFX_DATA(CZsjsqDlg)enum { IDD = IDD_ZSJSQ_DIALOG };int m_value;int m_nDec;//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CZsjsqDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementation protected:HICON m_hIcon;// Generated message map functions//{{AFX_MSG(CZsjsqDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnB1();afx_msg void OnB0();afx_msg void OnB2();afx_msg void OnB3();afx_msg void OnB4();afx_msg void OnB5();afx_msg void OnB6();afx_msg void OnB7();afx_msg void OnB9();afx_msg void OnBc();afx_msg void OnDiv();afx_msg void OnEqual();afx_msg void OnMul();afx_msg void OnSub();afx_msg void OnB8();afx_msg void OnAdd();afx_msg void OnDec();afx_msg void OnOct();afx_msg void OnCheckHex();//}}AFX_MSGDECLARE_MESSAGE_MAP() };//{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_ZSJSQDLG_H__31E05884_F98D_4BA6_A7F5_7C7B80C8E559__INCLUDED_)


2、運行效果如下:


總結

以上是生活随笔為你收集整理的MFC制作计算器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。