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 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()
/////////////////////////////////////////////////////////////////////////////
// CZsjsqDlg dialog
CZsjsqDlg::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 Win32
m_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 handlers
BOOL 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 dialog
SetIcon(m_hIcon, TRUE);// Set big icon
SetIcon(m_hIcon, FALSE);// Set small icon
// TODO: Add extra initialization here
CButton *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 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 CZsjsqDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CZsjsqDlg::OnB1()
{
// TODO: Add your control notification handler code here
m_value=m_value*10+1;
UpdateData(FALSE);
}
void CZsjsqDlg::OnB0()
{
// TODO: Add your control notification handler code here
m_value=m_value*10+0;
UpdateData(FALSE);
}
void CZsjsqDlg::OnB2()
{
// TODO: Add your control notification handler code here
m_value=m_value*10+2;
UpdateData(FALSE);
}
void CZsjsqDlg::OnB3()
{
// TODO: Add your control notification handler code here
m_value=m_value*10+3;
UpdateData(FALSE);
}
void CZsjsqDlg::OnB4()
{
// TODO: Add your control notification handler code here
m_value=m_value*10+4;
UpdateData(FALSE);
}
void CZsjsqDlg::OnB5()
{
// TODO: Add your control notification handler code here
m_value=m_value*10+5;
UpdateData(FALSE);
}
void CZsjsqDlg::OnB6()
{
// TODO: Add your control notification handler code here
m_value=m_value*10+6;
UpdateData(FALSE);
}
void CZsjsqDlg::OnB7()
{
// TODO: Add your control notification handler code here
m_value=m_value*10+7;
UpdateData(FALSE);
}
void CZsjsqDlg::OnB9()
{
// TODO: Add your control notification handler code here
m_value=m_value*10+9;
UpdateData(FALSE);
}
void CZsjsqDlg::OnBc()
{
// TODO: Add your control notification handler code here
m_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 here
m_operator=3;
m_bOperator=TRUE;
m_num1=m_value;
m_value=0;
}
void CZsjsqDlg::OnEqual()
{
// TODO: Add your control notification handler code here
if(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 here
m_operator=2;
m_bOperator=TRUE;
m_num1=m_value;
m_value=0;
}
void CZsjsqDlg::OnSub()
{
// TODO: Add your control notification handler code here
m_operator=1;
m_bOperator=TRUE;
m_num1=m_value;
m_value=0;
}
void CZsjsqDlg::OnB8()
{
// TODO: Add your control notification handler code here
m_value=m_value*10+8;
UpdateData(FALSE);
}
void CZsjsqDlg::OnAdd()
{
// TODO: Add your control notification handler code here
m_operator=0;
m_bOperator=TRUE;
m_num1=m_value;
m_value=0;
}
void CZsjsqDlg::OnDec()
{
// TODO: Add your control notification handler code here
CButton *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 here
CButton *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 here
CButton *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 dialog
class 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 };
intm_value;
intm_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_MSG
DECLARE_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_)