📄 calculatordlg.cpp
字号:
// CalculatorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorDlg.h"
#include "math.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_result = _T("");
m_isDegree = -1;
m_sentific = -1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_hAccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDD));
DtoR=1;
m_coff=0.1;
m_bCoff=0;
m_errorState = ErrNone;
m_bOperandAvail=FALSE;
m_operator=OpNone;
}
void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCalculatorDlg)
DDX_Text(pDX, IDC_DISPLAY, m_result);
DDX_Radio(pDX, IDC_DEGREE, m_isDegree);
DDX_Radio(pDX, IDC_SENTIFIC, m_sentific);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
//{{AFX_MSG_MAP(CCalculatorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND_RANGE(IDC_0, IDC_9, OnOperandInput)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_MINUS, OnMinus)
ON_BN_CLICKED(IDC_DIVID, OnDivid)
ON_BN_CLICKED(IDC_MULTIPLY, OnMultiply)
ON_BN_CLICKED(IDC_EQUAL, OnEqual)
ON_BN_CLICKED(IDC_SQRT, OnSqrt)
ON_BN_CLICKED(IDC_SIGN, OnSign)
ON_BN_CLICKED(IDC_RECIPROCAL, OnReciprocal)
ON_BN_CLICKED(IDC_POINT, OnPoint)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_LOG, OnLog)
ON_BN_CLICKED(IDC_LN, OnLn)
ON_BN_CLICKED(IDC_FACTORIAL, OnFactorial)
ON_BN_CLICKED(IDC_EXP, OnExp)
ON_BN_CLICKED(IDC_SENTIFIC, OnSentific)
ON_BN_CLICKED(IDC_SIN, OnSin)
ON_BN_CLICKED(IDC_SQUAR, OnSquar)
ON_BN_CLICKED(IDC_TAN, OnTan)
ON_BN_CLICKED(IDC_STANDARD, OnStandard)
ON_BN_CLICKED(IDC_DEGREE, OnDegree)
ON_BN_CLICKED(IDC_RAD, OnRad)
ON_BN_CLICKED(IDC_COS, OnCos)
//}}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
GetWindowRect(rcSentific);
CWnd* m_pMark=GetDlgItem(IDC_DISPLAY);
ASSERT(m_pMark);
CRect rect;
m_pMark->GetWindowRect(rect);
rcStandard=rcSentific;
rcStandard.right=rect.right+5;
m_bIsExpand=0;
m_sentific=1;
OnStandard();
m_result="0.";
m_isDegree=1;
UpdateData(FALSE);
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::OnOperandInput(UINT nID)
{
ASSERT(nID >= IDC_0 && nID <= IDC_9);
if (m_errorState != ErrNone)
return;
if(m_func!=FuncNone)
{
m_func=FuncNone;
Calculate();
}
if (!m_bOperandAvail)
m_operand = 0;
if(!m_bCoff)
m_operand=m_operand*10+(nID-IDC_0);
else
{
m_operand=m_operand+(nID-IDC_0)*m_coff;
m_coff*=0.1;
}
m_bOperandAvail=TRUE;
UpdateDisplay();
}
void CCalculatorDlg::Calculate()
{
if (m_errorState != ErrNone)
return;
if (m_bOperandAvail)
{
if (m_operator == OpNone)
m_accum = m_operand;
else if (m_operator == OpMultiply)
m_accum *= m_operand;
else if (m_operator == OpDivide)
{
if (m_operand == 0)
m_errorState = ErrDivideByZero;
else
m_accum /= m_operand;
}
else if (m_operator == OpAdd)
m_accum += m_operand;
else if (m_operator == OpSubtract)
m_accum -= m_operand;
}
m_bOperandAvail = FALSE;
m_bCoff=0;
m_coff=0.1;
UpdateDisplay();
}
void CCalculatorDlg::UpdateDisplay()
{
if (GetSafeHwnd() == NULL)
return;
if (m_errorState != ErrNone)
m_result="除数不能为零";
else
{
float lVal = (m_bOperandAvail) ? m_operand : m_accum;
m_result.Format(_T("%f"), lVal);
int i=m_result.GetLength();
while(m_result.GetAt(i-1)=='0')
{
m_result.Delete(i-1,1);
i-=1;
}
}
UpdateData(FALSE);
}
BOOL CCalculatorDlg::Keyboard(LPCTSTR szButton)
{
switch (szButton[0])
{
case 'c':
case 'C':
OnClear();
break;
case '/':
OnDivid();
break;
case '+':
OnAdd();
break;
case '-':
OnMinus();
break;
case '*':
OnMultiply();
break;
case '=':
OnEqual();
break;
default:
if (szButton[0] >= '0' && szButton[0] <= '9')
{
if (m_errorState != ErrNone)
return FALSE;
if (!m_bOperandAvail)
m_operand = 0;
m_operand=m_operand*10+szButton[0] - '0';
m_bOperandAvail=TRUE;
UpdateDisplay();
}
else
return FALSE;
break;
}
return TRUE;
}
void CCalculatorDlg::OnAdd()
{
// TODO: Add your control notification handler code here
Calculate();
m_operator = OpAdd;
}
void CCalculatorDlg::OnMinus()
{
// TODO: Add your control notification handler code here
Calculate();
m_operator = OpSubtract;
}
void CCalculatorDlg::OnDivid()
{
// TODO: Add your control notification handler code here
Calculate();
m_operator = OpDivide;
}
void CCalculatorDlg::OnMultiply()
{
// TODO: Add your control notification handler code here
Calculate();
m_operator = OpMultiply;
}
void CCalculatorDlg::OnEqual()
{
// TODO: Add your control notification handler code here
Calculate();
m_operator = OpNone;
}
void CCalculatorDlg::OnSign()
{
// TODO: Add your control notification handler code here
m_operand*=-1;
UpdateDisplay();
}
void CCalculatorDlg::OnSqrt()
{
// TODO: Add your control notification handler code here
m_func=FuncSqrt;
Run_Func();
}
void CCalculatorDlg::OnReciprocal()
{
// TODO: Add your control notification handler code here
m_func=FuncRec;
Run_Func();
}
void CCalculatorDlg::OnPoint()
{
// TODO: Add your control notification handler code here
m_bCoff=1;
UpdateDisplay();
}
void CCalculatorDlg::OnClear()
{
// TODO: Add your control notification handler code here
m_operator = OpNone;
m_operand = 0;
m_accum = 0;
m_bOperandAvail = FALSE;
m_errorState = ErrNone;
m_coff=0.1;
UpdateDisplay();
}
BOOL CCalculatorDlg::PreTranslateMessage(MSG* pMsg)
{
if (m_hAccel != NULL && TranslateAccelerator(m_hWnd, m_hAccel, pMsg))
return TRUE;
return CDialog::PreTranslateMessage(pMsg);
}
void CCalculatorDlg::OnLog()
{
// TODO: Add your control notification handler code here
m_func=FuncLog;
Run_Func();
}
void CCalculatorDlg::OnLn()
{
// TODO: Add your control notification handler code here
m_func=FuncLn;
Run_Func();
}
void CCalculatorDlg::OnFactorial()
{
// TODO: Add your control notification handler code here
m_func=FuncN;
Run_Func();
}
void CCalculatorDlg::OnExp()
{
// TODO: Add your control notification handler code here
m_func=FuncExp;
Run_Func();
}
void CCalculatorDlg::OnSentific()
{
// TODO: Add your control notification handler code here
m_bIsExpand=1;
m_sentific=0;
ExpandToSentific();
}
void CCalculatorDlg::OnSin()
{
// TODO: Add your control notification handler code here
m_func=FuncSin;
Run_Func();
}
void CCalculatorDlg::OnSquar()
{
// TODO: Add your control notification handler code here
m_func=FuncSqre;
Run_Func();
}
void CCalculatorDlg::OnTan()
{
// TODO: Add your control notification handler code here
m_func=FuncTan;
Run_Func();
}
void CCalculatorDlg::OnStandard()
{
// TODO: Add your control notification handler code here
m_bIsExpand=0;
m_sentific=1;
ExpandToSentific();
}
void CCalculatorDlg::Run_Func()
{
if (m_errorState != ErrNone)
return;
if (m_bOperandAvail)
{
if(m_func==FuncExp)
m_operand=exp(m_operand);
if(m_func==FuncLn)
m_operand=log(m_operand);
if(m_func==FuncLog)
m_operand=log10(m_operand);
if(m_func==FuncSqrt)
m_operand=sqrt(m_operand);
if(m_func==FuncSqre)
m_operand=pow(10,m_operand);
if(m_func==FuncSin)
m_operand=sin(m_operand*DtoR);
if(m_func==FuncCos)
m_operand=cos(m_operand*DtoR);
if(m_func==FuncTan)
m_operand=tan(m_operand*DtoR);
if(m_func==FuncRec)
m_operand=1/m_operand;
if(m_func=FuncN)
{
int i;
for(i=m_operand-1;i>=1;i--)
m_operand*=i;
}
}
UpdateDisplay();
// m_func=FuncNone;
}
void CCalculatorDlg::OnDegree()
{
// TODO: Add your control notification handler code here
m_isDegree=0;
DtoR=(2*PI)/360;
UpdateData(FALSE);
}
void CCalculatorDlg::OnRad()
{
// TODO: Add your control notification handler code here
m_isDegree=1;
DtoR=1;
UpdateData(TRUE);
}
void CCalculatorDlg::OnCos()
{
// TODO: Add your control notification handler code here
m_func=FuncCos;
Run_Func();
}
void CCalculatorDlg::ExpandToSentific()
{
if(m_bIsExpand)
{
SetWindowPos(NULL,0,0,rcSentific.Width(),rcSentific.Height(),
SWP_NOMOVE|SWP_NOZORDER);
SetVisibleCtrl();
}
else
{
SetWindowPos(NULL,0,0,rcStandard.Width(),rcSentific.Height(),
SWP_NOMOVE|SWP_NOZORDER);
SetVisibleCtrl();
}
UpdateData(FALSE);
}
void CCalculatorDlg::SetVisibleCtrl()
{
CWnd* pCtrl=GetWindow(GW_CHILD);
CRect rcTest;
CRect rcControl;
CRect rcShow;
GetWindowRect(rcShow);
while(pCtrl!=NULL)
{
pCtrl->GetWindowRect(rcControl);
if(rcTest.IntersectRect(rcShow,rcControl))
pCtrl->EnableWindow(TRUE);
else
pCtrl->EnableWindow(FALSE);
pCtrl=pCtrl->GetWindow(GW_HWNDNEXT);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -