📄 calculatordlg.cpp
字号:
// calculatorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "calculator.h"
#include "calculatorDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg dialog
CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCalculatorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCalculatorDlg)
m_display = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_bit = m_bit2 = 0;
m_base = m_middle = m_end =0;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCalculatorDlg)
DDX_Text(pDX, IDC_EDIT1, m_display);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
//{{AFX_MSG_MAP(CCalculatorDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(ID_CE, OnCe)
ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
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_BUTTONADD, OnButtonadd)
ON_BN_CLICKED(IDC_BUTTONDIV, OnButtondiv)
ON_BN_CLICKED(IDC_BUTTONDOT, OnButtondot)
ON_BN_CLICKED(IDC_BUTTONEQU, OnButtonequ)
ON_BN_CLICKED(IDC_BUTTONMUT, OnButtonmut)
ON_BN_CLICKED(IDC_BUTTONSUB, OnButtonsub)
ON_EN_MAXTEXT(IDC_EDIT1, OnMaxtextEdit1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg message handlers
BOOL CCalculatorDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
}
// 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 CCalculatorDlg::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 CCalculatorDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCalculatorDlg::OnButton1() // 数字按钮"1"的单击处理函数
{
// TODO: Add your control notification handler code here
m_end = m_end*10 +1;
m_display = m_end;
UpdateData(FALSE);
}
void CCalculatorDlg::OnCe() // 清除按钮的单击处理函数
{
// TODO: Add your control notification handler code here
m_display = 0;
m_end = m_bit = m_base = m_middle = 0;
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton10() // 数字按钮"0"的单击处理函数
{
// TODO: Add your control notification handler code here
m_end = m_end*10 +0;
m_display = m_end;
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton2() // 数字按钮"2"的单击处理函数
{
// TODO: Add your control notification handler code here
m_end = m_end*10 +2;
m_display = m_end;
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton3() // 数字按钮"3"的单击处理函数
{
// TODO: Add your control notification handler code here
m_end = m_end*10 +3;
m_display = m_end;
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton4()
{
// TODO: Add your control notification handler code here
m_end = m_end*10 +4;
m_display = m_end;
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton5()
{
// TODO: Add your control notification handler code here
m_end = m_end*10 +5;
m_display = m_end;
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton6()
{
// TODO: Add your control notification handler code here
m_end = m_end*10 +6;
m_display = m_end;
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton7()
{
// TODO: Add your control notification handler code here
m_end = m_end*10 +7;
m_display = m_end;
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton8()
{
// TODO: Add your control notification handler code here
m_end = m_end*10 +8;
m_display = m_end;
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton9()
{
// TODO: Add your control notification handler code here
m_end = m_end*10 +9;
m_display = m_end;
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonadd() // 加号运算符
{
// TODO: Add your control notification handler code here
Result(); // 求值
m_bit = 1;
}
void CCalculatorDlg::OnButtonsub() // 减号运算符
{
// TODO: Add your control notification handler code here
Result(); // 求值
m_bit = 2;
}
void CCalculatorDlg::OnButtonmut() // 乘号运算符
{
// TODO: Add your control notification handler code here
Record(); // 记录
m_bit = 3;
}
void CCalculatorDlg::OnButtondiv() // 除号运算符
{
// TODO: Add your control notification handler code here
Record(); // 记录
m_bit = 4;
}
void CCalculatorDlg::OnButtonequ() // 等号运算符
{
// TODO: Add your control notification handler code here
Result(); // 求值
m_bit = 1;
m_bit2 = m_middle = m_end = 0; // 清零
}
void CCalculatorDlg::OnButtondot()
{
// TODO: Add your control notification handler code here
}
void CCalculatorDlg::OnMaxtextEdit1()
{
// TODO: Add your control notification handler code here
}
void CCalculatorDlg::Result()
{
switch(m_bit)
{
case 1://"+"
m_base += m_end;
break;
case 2://"-"
m_base -= m_end;
break;
case 3://"*"
if(m_bit2==0) m_base *= m_end;
else if(m_bit2==1) m_base += m_middle*m_end;
else if(m_bit2==2) m_base -= m_middle*m_end;
break;
case 4://"/"
if(m_end==0)
{
MessageBox("除数为0!");
OnCe();
return;
}
if(m_bit2==0) m_base /= m_end;
else if(m_bit2==1) m_base += m_middle/m_end;
else if(m_bit2==2) m_base -= m_middle/m_end;
break;
default:
m_base = m_end;
break;
}
m_bit = 0;
m_bit2 = 0;
m_end = 0;
m_display = m_base;
UpdateData(FALSE);
}
void CCalculatorDlg::Record()
{
switch(m_bit)
{
case 1://"+"
m_middle = m_end;
m_bit2 = 1;
break;
case 2://"-"
m_middle = m_end;
m_bit2 = 2;
break;
case 3://"*"
m_base *= m_end;
m_bit2 = 0;
break;
case 4://"/"
if(m_end==0)
{
MessageBox("除数为0!");
OnCe();
return;
}
m_base /= m_end;
m_bit2 = 0;
break;
default:
m_base = m_end;
break;
}
m_end = 0;
if(!m_bit2)
{
m_display = m_base;
UpdateData(FALSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -