📄 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
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg dialog
CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCalculatorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCalculatorDlg)
m_NowValue = _T("0");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_LastValue = 0;
m_ButtonID = 0;
m_LastButtonID= 0;
}
void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCalculatorDlg)
DDX_Control(pDX, IDC_REDUCE, m_Reduce);
DDX_Control(pDX, IDC_MUTIPLY, m_Mutiply);
DDX_Control(pDX, IDC_ADD, m_Add);
DDX_Control(pDX, IDC_DIVIDE, m_Divide);
DDX_Control(pDX, IDC_EQUAL, m_Equal);
DDX_Control(pDX, IDC_POWER, m_Power);
DDX_Control(pDX, IDC_POINT, m_Point);
DDX_Control(pDX, IDC_NUM9, m_Num9);
DDX_Control(pDX, IDC_NUM8, m_Num8);
DDX_Control(pDX, IDC_NUM7, m_Num7);
DDX_Control(pDX, IDC_NUM6, m_Num6);
DDX_Control(pDX, IDC_NUM5, m_Num5);
DDX_Control(pDX, IDC_NUM4, m_Num4);
DDX_Control(pDX, IDC_NUM3, m_Num3);
DDX_Control(pDX, IDC_NUM2, m_Num2);
DDX_Control(pDX, IDC_NUM1, m_Num1);
DDX_Control(pDX, IDC_NUM0, m_Num0);
DDX_Control(pDX, IDC_EVOLUTION, m_Evolution);
DDX_Control(pDX, IDC_BACKSPACE, m_BackSpace);
DDX_Control(pDX, IDC_CLEAR, m_Clear);
DDX_Text(pDX, IDC_EDIT, m_NowValue);
DDV_MaxChars(pDX, m_NowValue, 25);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
//{{AFX_MSG_MAP(CCalculatorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BACKSPACE, OnBackspace)
ON_EN_SETFOCUS(IDC_EDIT, OnSetfocusEdit)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_NUM1, OnNum1)
ON_BN_CLICKED(IDC_NUM2, OnNum2)
ON_BN_CLICKED(IDC_NUM3, OnNum3)
ON_BN_CLICKED(IDC_NUM4, OnNum4)
ON_BN_CLICKED(IDC_NUM5, OnNum5)
ON_BN_CLICKED(IDC_NUM6, OnNum6)
ON_BN_CLICKED(IDC_NUM7, OnNum7)
ON_BN_CLICKED(IDC_NUM8, OnNum8)
ON_BN_CLICKED(IDC_NUM9, OnNum9)
ON_BN_CLICKED(IDC_NUM0, OnNum0)
ON_BN_CLICKED(IDC_POINT, OnPoint)
ON_BN_CLICKED(IDC_POWER, OnPower)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_REDUCE, OnReduce)
ON_BN_CLICKED(IDC_MUTIPLY, OnMutiply)
ON_BN_CLICKED(IDC_DIVIDE, OnDivide)
ON_BN_CLICKED(IDC_EQUAL, OnEqual)
ON_BN_CLICKED(IDC_EVOLUTION, OnEvolution)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg message handlers
BOOL CCalculatorDlg::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 CCalculatorDlg::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 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::OnBackspace()
{
// TODO: Add your control notification handler code here
UpdateData();
m_NowValue = m_NowValue.Left(m_NowValue.GetLength() - 1);
UpdateData(FALSE);
}
void CCalculatorDlg::OnSetfocusEdit()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_BACKSPACE)->SetFocus();
}
void CCalculatorDlg::OnClear()
{
// TODO: Add your control notification handler code here
m_NowValue = "0";
m_ButtonID = 0;
m_LastButtonID = IDC_CLEAR;
UpdateData(FALSE);
}
void CCalculatorDlg::OnNum1()
{
// TODO: Add your control notification handler code here
UpdateData();
if ( IDC_NUM1 <= m_LastButtonID && IDC_POINT >= m_LastButtonID)
{
if ( 1 == m_NowValue.GetLength() && "0" == m_NowValue )
m_NowValue = "1";
else m_NowValue += "1";
}
else m_NowValue = "1";
m_LastButtonID = IDC_NUM1;
UpdateData(FALSE);
}
void CCalculatorDlg::OnNum2()
{
// TODO: Add your control notification handler code here
UpdateData();
if ( IDC_NUM1 <= m_LastButtonID && IDC_POINT >= m_LastButtonID)
{
if ( 1 == m_NowValue.GetLength() && "0" == m_NowValue )
m_NowValue = "2";
else m_NowValue += "2";
}
else m_NowValue = "2";
m_LastButtonID = IDC_NUM2;
UpdateData(FALSE);
}
void CCalculatorDlg::OnNum3()
{
// TODO: Add your control notification handler code here
UpdateData();
if ( IDC_NUM1 <= m_LastButtonID && IDC_POINT >= m_LastButtonID)
{
if ( 1 == m_NowValue.GetLength() && "0" == m_NowValue )
m_NowValue = "3";
else m_NowValue += "3";
}
else m_NowValue = "3";
m_LastButtonID = IDC_NUM3;
UpdateData(FALSE);
}
void CCalculatorDlg::OnNum4()
{
// TODO: Add your control notification handler code here
UpdateData();
if ( IDC_NUM1 <= m_LastButtonID && IDC_POINT >= m_LastButtonID)
{
if ( 1 == m_NowValue.GetLength() && "0" == m_NowValue )
m_NowValue = "4";
else m_NowValue += "4";
}
else m_NowValue = "4";
m_LastButtonID = IDC_NUM4;
UpdateData(FALSE);
}
void CCalculatorDlg::OnNum5()
{
// TODO: Add your control notification handler code here
UpdateData();
if ( IDC_NUM1 <= m_LastButtonID && IDC_POINT >= m_LastButtonID)
{
if ( 1 == m_NowValue.GetLength() && "0" == m_NowValue )
m_NowValue = "5";
else m_NowValue += "5";
}
else m_NowValue = "5";
m_LastButtonID = IDC_NUM5;
UpdateData(FALSE);
}
void CCalculatorDlg::OnNum6()
{
// TODO: Add your control notification handler code here
UpdateData();
if ( IDC_NUM1 <= m_LastButtonID && IDC_POINT >= m_LastButtonID)
{
if ( 1 == m_NowValue.GetLength() && "0" == m_NowValue )
m_NowValue = "6";
else m_NowValue += "6";
}
else m_NowValue = "6";
m_LastButtonID = IDC_NUM6;
UpdateData(FALSE);
}
void CCalculatorDlg::OnNum7()
{
// TODO: Add your control notification handler code here
UpdateData();
if ( IDC_NUM1 <= m_LastButtonID && IDC_POINT >= m_LastButtonID)
{
if ( 1 == m_NowValue.GetLength() && "0" == m_NowValue )
m_NowValue = "7";
else m_NowValue += "7";
}
else m_NowValue = "7";
m_LastButtonID = IDC_NUM7;
UpdateData(FALSE);
}
void CCalculatorDlg::OnNum8()
{
// TODO: Add your control notification handler code here
UpdateData();
if ( IDC_NUM1 <= m_LastButtonID && IDC_POINT >= m_LastButtonID)
{
if ( 1 == m_NowValue.GetLength() && "0" == m_NowValue )
m_NowValue = "8";
else m_NowValue += "8";
}
else m_NowValue = "8";
m_LastButtonID = IDC_NUM8;
UpdateData(FALSE);
}
void CCalculatorDlg::OnNum9()
{
// TODO: Add your control notification handler code here
UpdateData();
if ( IDC_NUM1 <= m_LastButtonID && IDC_POINT >= m_LastButtonID)
{
if ( 1 == m_NowValue.GetLength() && "0" == m_NowValue )
m_NowValue = "9";
else m_NowValue += "9";
}
else m_NowValue = "9";
m_LastButtonID = IDC_NUM9;
UpdateData(FALSE);
}
void CCalculatorDlg::OnNum0()
{
// TODO: Add your control notification handler code here
UpdateData();
if ( IDC_NUM1 <= m_LastButtonID && IDC_POINT >= m_LastButtonID)
{
if ( 1 == m_NowValue.GetLength() && "0" == m_NowValue )
m_NowValue = "0";
else m_NowValue += "0";
}
else m_NowValue = "0";
m_LastButtonID = IDC_NUM0;
UpdateData(FALSE);
}
void CCalculatorDlg::OnPoint()
{
// TODO: Add your control notification handler code here
UpdateData();
for (int i=0;i<m_NowValue.GetLength();i++)
if ( '.' == m_NowValue[i] ) return;
m_NowValue += '.';
m_LastButtonID = IDC_POINT;
UpdateData(FALSE);
}
long double CCalculatorDlg::atold(const CString &value) const
{
int PointPos = value.Find('.',0);
int length = value.GetLength();
if ( -1 == PointPos ) PointPos = length;
long double resultInt=0;
for ( int i=0;i<PointPos;i++) resultInt = resultInt*10 + value[i]-'0';
long double resultFloat=0;
for ( i=length-1;i>PointPos;i--)
resultFloat = resultFloat*0.1 + (value[i]-'0')*0.1;
return resultInt+resultFloat;
}
CString CCalculatorDlg::ldtoa(const long double &value) const
{
CString result;
result.Format("%f",value);
if( -1 != result.Find('.',0) )
while ( '0' == result[result.GetLength()-1] )
result.Delete(result.GetLength()-1);
if ( "." == result.Right(1) ) result.Remove('.');
return result;
}
long double CCalculatorDlg::compute(const CString &value)
{
long double NowValue = atold(value);
long double result;
long i;
switch (m_ButtonID)
{
case 0:
result = m_LastValue = NowValue;
break;
case IDC_POWER:
result = m_LastValue;
for(i=1;i<=(long)NowValue-1;i++)
result = result*m_LastValue;
break;
case IDC_EVOLUTION:
result = GetEvolution(m_LastValue,NowValue);
break;
case IDC_DIVIDE:
result = m_LastValue / NowValue;
break;
case IDC_MUTIPLY:
result = m_LastValue * NowValue;
break;
case IDC_REDUCE:
result = m_LastValue - NowValue;
break;
case IDC_ADD:
result = m_LastValue + NowValue;
break;
case IDC_EQUAL:
result = m_LastValue = NowValue;
break;
}
return result;
}
long double CCalculatorDlg::GetEvolution(const long double &value1,
const long double &value2) const
{
long double tempResult,smallone,bigone,temp;
if ( 1 == value1 ) return 1;
if ( value1 > 1 )
{
smallone = 1;
bigone = value1;
temp = ( smallone + bigone ) / 2;
do
{
tempResult = temp;
for (int i =1;i<=value2-1;i++)
tempResult *= temp;
if ( tempResult - value1 > 0.00000001 )
bigone = temp;
else {if ( tempResult - value1 < -0.00000001 )
smallone = temp;
else return temp;}
temp = ( bigone + smallone ) / 2;
}while(1);
}
smallone = 0;
bigone = 1;
temp = ( smallone + bigone ) / 2;
do
{
tempResult = temp;
for( int i=1;i<=value2-1;i++)
tempResult *= temp;
if ( tempResult - value1 > 0.00000001 )
bigone = temp;
else if ( tempResult - value1 < -0.00000001 )
smallone = temp;
else return temp;
temp = ( bigone + smallone ) / 2;
}while(1);
}
void CCalculatorDlg::OnPower()
{
// TODO: Add your control notification handler code here
UpdateData();
m_LastValue = compute(m_NowValue);
m_NowValue = ldtoa(m_LastValue);
UpdateData(FALSE);
m_ButtonID = IDC_POWER;
m_LastButtonID = IDC_POWER;
}
void CCalculatorDlg::OnAdd()
{
// TODO: Add your control notification handler code here
UpdateData();
m_LastValue = compute(m_NowValue);
m_NowValue = ldtoa(m_LastValue);
UpdateData(FALSE);
m_ButtonID = IDC_ADD;
m_LastButtonID = IDC_ADD;
}
void CCalculatorDlg::OnReduce()
{
// TODO: Add your control notification handler code here
UpdateData();
m_LastValue = compute(m_NowValue);
m_NowValue = ldtoa(m_LastValue);
UpdateData(FALSE);
m_ButtonID = IDC_REDUCE;
m_LastButtonID = IDC_REDUCE;
}
void CCalculatorDlg::OnMutiply()
{
// TODO: Add your control notification handler code here
UpdateData();
m_LastValue = compute(m_NowValue);
m_NowValue = ldtoa(m_LastValue);
UpdateData(FALSE);
m_ButtonID = IDC_MUTIPLY;
m_LastButtonID = IDC_MUTIPLY;
}
void CCalculatorDlg::OnDivide()
{
// TODO: Add your control notification handler code here
UpdateData();
m_LastValue = compute(m_NowValue);
m_NowValue = ldtoa(m_LastValue);
UpdateData(FALSE);
m_ButtonID = IDC_DIVIDE;
m_LastButtonID = IDC_DIVIDE;
}
void CCalculatorDlg::OnEqual()
{
// TODO: Add your control notification handler code here
UpdateData();
m_LastValue = compute(m_NowValue);
m_NowValue = ldtoa(m_LastValue);
UpdateData(FALSE);
m_ButtonID = IDC_EQUAL;
m_LastButtonID = IDC_EQUAL;
}
void CCalculatorDlg::OnEvolution()
{
// TODO: Add your control notification handler code here
UpdateData();
m_LastValue = compute(m_NowValue);
m_NowValue = ldtoa(m_LastValue);
UpdateData(FALSE);
m_ButtonID = IDC_EVOLUTION;
m_LastButtonID = IDC_EVOLUTION;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -