// CalculatorDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CCalculatorDlg 对话框
CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CCalculatorDlg::IDD, pParent)
, m_res(_T(""))
, m_iSign(0)
, m_fNumber1(0)
, m_fNumber2(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, &CCalculatorDlg::OnBnClickedOk)
ON_BN_CLICKED(IDCANCEL, &CCalculatorDlg::OnBnClickedCancel)
ON_BN_CLICKED(IDC_BTN0, &CCalculatorDlg::OnBnClickedBtn0)
ON_BN_CLICKED(IDC_BTN1, &CCalculatorDlg::OnBnClickedBtn1)
ON_BN_CLICKED(IDC_BTN2, &CCalculatorDlg::OnBnClickedBtn2)
ON_BN_CLICKED(IDC_BTN3, &CCalculatorDlg::OnBnClickedBtn3)
ON_BN_CLICKED(IDC_BTN4, &CCalculatorDlg::OnBnClickedBtn4)
ON_BN_CLICKED(IDC_BTN5, &CCalculatorDlg::OnBnClickedBtn5)
ON_BN_CLICKED(IDC_BTN6, &CCalculatorDlg::OnBnClickedBtn6)
ON_BN_CLICKED(IDC_BTN7, &CCalculatorDlg::OnBnClickedBtn7)
ON_BN_CLICKED(IDC_BTN8, &CCalculatorDlg::OnBnClickedBtn8)
ON_BN_CLICKED(IDC_BTN9, &CCalculatorDlg::OnBnClickedBtn9)
ON_BN_CLICKED(IDC_BTNPOINT, &CCalculatorDlg::OnBnClickedBtnpoint)
ON_BN_CLICKED(IDC_BTNPLUS, &CCalculatorDlg::OnBnClickedBtnplus)
ON_BN_CLICKED(IDC_BTNEQUIT, &CCalculatorDlg::OnBnClickedBtnequit)
ON_BN_CLICKED(IDC_BTNJIAN, &CCalculatorDlg::OnBnClickedBtnjian)
ON_BN_CLICKED(IDC_BTNCHEN, &CCalculatorDlg::OnBnClickedBtnchen)
ON_BN_CLICKED(IDC_BTNDIV, &CCalculatorDlg::OnBnClickedBtndiv)
ON_BN_CLICKED(IDC_BTNRESET, &CCalculatorDlg::OnBnClickedBtnreset)
ON_BN_CLICKED(IDC_BTNDAO, &CCalculatorDlg::OnBnClickedBtndao)
ON_BN_CLICKED(IDC_2X, &CCalculatorDlg::OnBnClicked2x)
ON_BN_CLICKED(IDC_SQRT, &CCalculatorDlg::OnBnClickedSqrt)
ON_BN_CLICKED(IDC_DELETE, &CCalculatorDlg::OnBnClickedDelete)
ON_BN_CLICKED(IDC_INTTOO, &CCalculatorDlg::OnBnClickedInttoo)
ON_BN_CLICKED(IDC_INTTOASCII, &CCalculatorDlg::OnBnClickedInttoascii)
ON_BN_CLICKED(IDC_BTNMOD, &CCalculatorDlg::OnBnClickedBtnmod)
END_MESSAGE_MAP()
// CCalculatorDlg 消息处理程序
BOOL CCalculatorDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CCalculatorDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast(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(m_hIcon);
}
void CCalculatorDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
if (GetDlgItem(IDOK) == GetFocus())
{
CDialogEx::OnOK();
}
}
void CCalculatorDlg::OnBnClickedCancel()
{
// TODO: 在此添加控件通知处理程序代码
/*CDialogEx::OnCancel();*/
EndDialog(IDCANCEL);
}
void CCalculatorDlg::OnBnClickedBtn0()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_res += "0";
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtn1()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_res += "1";
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtn2()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_res += "2";
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtn3()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_res += "3";
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtn4()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_res += "4";
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtn5()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_res += "5";
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtn6()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_res += "6";
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtn7()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_res += "7";
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtn8()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_res += "8";
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtn9()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_res += "9";
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtnpoint()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_res += ".";
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtnplus()
{
// TODO: 在此添加控件通知处理程序代码
SaveValue();
m_iSign = 0;
}
void CCalculatorDlg::SaveValue()
{
UpdateData(TRUE);
m_fNumber1 = atof(csTocc(m_res));
m_res = _T("");
play(m_res);
UpdateData(FALSE);
}
const char * CCalculatorDlg::csTocc(CString str)
{
const size_t strsize = (str.GetLength() + 1) * 2;
char * pstr = new char[strsize];
size_t sz = 0;
wcstombs_s(&sz, pstr, strsize, str, _TRUNCATE);
return (const char*)pstr;
}
void CCalculatorDlg::OnBnClickedBtnequit()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_fNumber2 = atof(csTocc(m_res));
double f = 0.0f;
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.0f){
f = m_fNumber1;
}
else{
f = m_fNumber1 / m_fNumber2;
}
break;
case 4:
if ((m_fNumber2 - int(m_fNumber2)) != 0.0f)
{
AfxMessageBox(_T("次方必须是整数"));
return;
}
f = m_fNumber1;
for (int i = 0; i < int(m_fNumber2)-1;i++)
{
f = f * m_fNumber1;
}
break;
default:
break;
}
if (f - int(f) < 1e-5)
{
m_res.Format(_T("%d"),int(f));
}
else
{
m_res.Format(_T("%f"), f);
}
play(m_res);
UpdateData(FALSE);
m_fNumber1 = f;
m_fNumber2 = 0.0f;
}
void CCalculatorDlg::OnBnClickedBtnjian()
{
// TODO: 在此添加控件通知处理程序代码
SaveValue();
m_iSign = 1;
}
void CCalculatorDlg::OnBnClickedBtnchen()
{
// TODO: 在此添加控件通知处理程序代码
SaveValue();
m_iSign = 2;
}
void CCalculatorDlg::OnBnClickedBtndiv()
{
// TODO: 在此添加控件通知处理程序代码
SaveValue();
m_iSign = 3;
}
void CCalculatorDlg::play(CString str)
{
SetDlgItemText(IDC_RESULT, str);
}
void CCalculatorDlg::OnBnClickedBtnreset()
{
// TODO: 在此添加控件通知处理程序代码
m_res = _T("");
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtndao()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_fNumber1 = atof(csTocc(m_res));
if (m_fNumber1 == 0.0f)
{
AfxMessageBox(_T("sorry,0不能做分母!"));
}
double f = 1 / m_fNumber1;
if (f - int(f) < 1e-5)
{
m_res.Format(_T("%d"), int(f));
}
else
{
m_res.Format(_T("%f"), f);
}
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClicked2x()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_fNumber1 = atof(csTocc(m_res));
double f = m_fNumber1 * m_fNumber1;
if (f - int(f) < 1e-5)
{
m_res.Format(_T("%d"), int(f));
}
else
{
m_res.Format(_T("%f"), f);
}
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedSqrt()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
m_fNumber1 = atof(csTocc(m_res));
double f = sqrt(m_fNumber1);
if (f - int(f) < 1e-5)
{
m_res.Format(_T("%d"), int(f));
}
else
{
m_res.Format(_T("%f"), f);
}
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedDelete()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
if (!m_res.IsEmpty())
{
m_res = m_res.Left(m_res.GetLength() - 1);
}
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedInttoo()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
if (m_res.GetLength() != 3)
{
AfxMessageBox(_T("八进制必须是3位"));
}
else{
CString a = m_res.Mid(0, 1);
CString b = m_res.Mid(1, 1);
CString c = m_res.Mid(2, 1);
int aa = atoi(csTocc(a)) * 64;
int bb = atoi(csTocc(b)) * 8;
int cc = atoi(csTocc(c)) * 1;
int res = aa + bb + cc;
m_res.Format(_T("%d"),res);
}
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedInttoascii()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
if (m_res.GetLength() != 3)
{
AfxMessageBox(_T("八进制必须是3位"));
}
else{
CString a = m_res.Mid(0, 1);
CString b = m_res.Mid(1, 1);
CString c = m_res.Mid(2, 1);
int aa = atoi(csTocc(a)) * 64;
int bb = atoi(csTocc(b)) * 8;
int cc = atoi(csTocc(c)) * 1;
int res = aa + bb + cc;
m_res.Format(_T("%c"), res);
}
play(m_res);
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtnmod()
{
// TODO: 在此添加控件通知处理程序代码
SaveValue();
m_iSign = 4;
}