📄 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)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_Menu = NULL;
m_IsExtend = FALSE;
//初始化变量
m_IsCheckPoint = false;
m_StrBegin = "";
m_begin = 0.0;
m_end = 0.0;
m_bit = 0;
}
void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCalculatorDlg)
DDX_Control(pDX, BTN_27, m_AdvButtonLog);
DDX_Control(pDX, BTN_26, m_AdvButtonLn);
DDX_Control(pDX, BTN_25, m_AdvButtonExp);
DDX_Control(pDX, BTN_24, m_AdvButtonPow);
DDX_Control(pDX, BTN_23, m_AdvButtonCub);
DDX_Control(pDX, BTN_22, m_AdvButtonSqu);
DDX_Control(pDX, BTN_21, m_AdvButtonTan);
DDX_Control(pDX, BTN_20, m_AdvButtonCos);
DDX_Control(pDX, BTN_19, m_AdvButtonSin);
DDX_Control(pDX, BTN_18, m_AdvButtonReverse);
DDX_Control(pDX, BTN_17, m_AdvButtonSqrt);
DDX_Control(pDX, BTN_16, m_AdvButtonClear);
DDX_Control(pDX, BTN_15, m_AdvButtonDiv);
DDX_Control(pDX, BTN_14, m_AdvButtonMul);
DDX_Control(pDX, BTN_13, m_AdvButtonSub);
DDX_Control(pDX, BTN_12, m_AdvButtonAdd);
DDX_Control(pDX, BTN_11, m_AdvButtonEqu);
DDX_Control(pDX, BTN_10, m_AdvButtonPoint);
DDX_Control(pDX, IDC_DSPSTATIC, m_DspStatic);
DDX_Control(pDX, IDC_DSPEDIT, m_DspEdit);
DDX_Control(pDX, BTN_0, m_AdvButton0);
DDX_Control(pDX, BTN_1, m_AdvButton1);
DDX_Control(pDX, BTN_2, m_AdvButton2);
DDX_Control(pDX, BTN_3, m_AdvButton3);
DDX_Control(pDX, BTN_4, m_AdvButton4);
DDX_Control(pDX, BTN_5, m_AdvButton5);
DDX_Control(pDX, BTN_6, m_AdvButton6);
DDX_Control(pDX, BTN_7, m_AdvButton7);
DDX_Control(pDX, BTN_8, m_AdvButton8);
DDX_Control(pDX, BTN_9, m_AdvButton9);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
//{{AFX_MSG_MAP(CCalculatorDlg)
ON_COMMAND(ID_EXTEND,OnExtend)
ON_BN_CLICKED(BTN_0, AdvButton0)
ON_BN_CLICKED(BTN_1, AdvButton1)
ON_BN_CLICKED(BTN_2, AdvButton2)
ON_BN_CLICKED(BTN_3, AdvButton3)
ON_BN_CLICKED(BTN_4, AdvButton4)
ON_BN_CLICKED(BTN_5, AdvButton5)
ON_BN_CLICKED(BTN_6, AdvButton6)
ON_BN_CLICKED(BTN_7, AdvButton7)
ON_BN_CLICKED(BTN_8, AdvButton8)
ON_BN_CLICKED(BTN_9, AdvButton9)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CREATE()
ON_BN_CLICKED(BTN_10, AdvButtonPoint)
ON_BN_CLICKED(BTN_11, AdvButtonEqu)
ON_BN_CLICKED(BTN_12, AdvButtonAdd)
ON_BN_CLICKED(BTN_13, AdvButtonSub)
ON_BN_CLICKED(BTN_14, AdvButtonMul)
ON_BN_CLICKED(BTN_15, AdvButtonDiv)
ON_BN_CLICKED(BTN_16, AdvButtonClear)
ON_BN_CLICKED(BTN_17, AdvButtonSqrt)
ON_BN_CLICKED(BTN_18, AdvButtonReverse)
ON_BN_CLICKED(BTN_19, AdvButtonSin)
ON_BN_CLICKED(BTN_24, AdvButtonPow)
ON_BN_CLICKED(BTN_22, AdvButtonSqu)
ON_BN_CLICKED(BTN_23, AdvButtonCub)
ON_BN_CLICKED(BTN_25, AdvButtonExp)
ON_BN_CLICKED(BTN_26, AdvButtonLn)
ON_BN_CLICKED(BTN_27, AdvButtonLog)
ON_BN_CLICKED(BTN_20, AdvButtonCos)
ON_BN_CLICKED(BTN_21, AdvButtonTan)
//}}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);
m_Menu = new CMenu;
m_Menu->LoadMenu(IDR_MENU);
SetMenu(m_Menu);
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
// m_AdvButton0.Create("0",WS_CHILD|WS_VISIBLE,CRect(10 ,10,40 ,40),this,BTN_0);
// m_AdvButton1.Create("1",WS_CHILD|WS_VISIBLE,CRect(50 ,10,80 ,40),this,BTN_1);
// m_AdvButton2.Create("2",WS_CHILD|WS_VISIBLE,CRect(90 ,10,120,40),this,BTN_2);
// m_AdvButton3.Create("3",WS_CHILD|WS_VISIBLE,CRect(130,10,160,40),this,BTN_3);
m_DspEdit.OnDisplay("0.");
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::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
}
void CCalculatorDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CCalculatorDlg::OnExtend()
{
CRect rect;
GetWindowRect(&rect);
if(m_IsExtend)
{
SetWindowPos(&wndTop,0,0,232,rect.Height(),SWP_NOMOVE|SWP_NOZORDER);
m_DspEdit.GetWindowRect(&rect);
m_DspEdit.SetWindowPos(NULL,rect.left,rect.top,182,rect.Height(),SWP_NOMOVE|SWP_NOZORDER);
m_DspStatic.GetWindowRect(&rect);
m_DspStatic.SetWindowPos(NULL,rect.left,rect.top,202,rect.Height(),SWP_NOMOVE|SWP_NOZORDER);
m_Menu ->ModifyMenu(ID_EXTEND,MF_BYCOMMAND|MF_STRING,ID_EXTEND,"扩展");
m_IsExtend = FALSE;
}
else
{
SetWindowPos(&wndTop,0,0,409,rect.Height(),SWP_NOMOVE|SWP_NOZORDER);
m_DspEdit.GetWindowRect(&rect);
m_DspEdit.SetWindowPos(NULL,rect.left,rect.top,361,rect.Height(),SWP_NOMOVE|SWP_NOZORDER);
m_DspStatic.GetWindowRect(&rect);
m_DspStatic.SetWindowPos(NULL,rect.left,rect.top,381,rect.Height(),SWP_NOMOVE|SWP_NOZORDER);
m_Menu ->ModifyMenu(ID_EXTEND,MF_BYCOMMAND|MF_STRING,ID_EXTEND,"标准");
m_IsExtend = TRUE;
}
}
int CCalculatorDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
CRect rect;
GetWindowRect(&rect);
SetWindowPos(&wndTop,0,0,232,rect.Height(),SWP_NOMOVE|SWP_NOZORDER);
// TODO: Add your specialized creation code here
return 0;
}
void CCalculatorDlg::AdvButton0()//按下0键时的处理函数
{
m_StrBegin = m_StrBegin+"0";
if (!m_IsCheckPoint)
m_DspEdit.OnDisplay(m_StrBegin+".");
else
m_DspEdit.OnDisplay(m_StrBegin);
}
void CCalculatorDlg::AdvButton1()
{
m_StrBegin = m_StrBegin+"1";
if (!m_IsCheckPoint)
m_DspEdit.OnDisplay(m_StrBegin+".");
else
m_DspEdit.OnDisplay(m_StrBegin);
}
void CCalculatorDlg::AdvButton2()
{
m_StrBegin = m_StrBegin+"2";
if (!m_IsCheckPoint)
m_DspEdit.OnDisplay(m_StrBegin+".");
else
m_DspEdit.OnDisplay(m_StrBegin);
}
void CCalculatorDlg::AdvButton3()
{
m_StrBegin = m_StrBegin+"3";
if (!m_IsCheckPoint)
m_DspEdit.OnDisplay(m_StrBegin+".");
else
m_DspEdit.OnDisplay(m_StrBegin);
}
void CCalculatorDlg::AdvButton4()
{
m_StrBegin = m_StrBegin+"4";
if (!m_IsCheckPoint)
m_DspEdit.OnDisplay(m_StrBegin+".");
else
m_DspEdit.OnDisplay(m_StrBegin);
}
void CCalculatorDlg::AdvButton5()
{
m_StrBegin = m_StrBegin+"5";
if (!m_IsCheckPoint)
m_DspEdit.OnDisplay(m_StrBegin+".");
else
m_DspEdit.OnDisplay(m_StrBegin);
}
void CCalculatorDlg::AdvButton6()
{
m_StrBegin = m_StrBegin+"6";
if (!m_IsCheckPoint)
m_DspEdit.OnDisplay(m_StrBegin+".");
else
m_DspEdit.OnDisplay(m_StrBegin);
}
void CCalculatorDlg::AdvButton7()
{
m_StrBegin = m_StrBegin+"7";
if (!m_IsCheckPoint)
m_DspEdit.OnDisplay(m_StrBegin+".");
else
m_DspEdit.OnDisplay(m_StrBegin);
}
void CCalculatorDlg::AdvButton8()
{
m_StrBegin = m_StrBegin+"8";
if (!m_IsCheckPoint)
m_DspEdit.OnDisplay(m_StrBegin+".");
else
m_DspEdit.OnDisplay(m_StrBegin);
}
void CCalculatorDlg::AdvButton9()
{
m_StrBegin = m_StrBegin+"9";
if (!m_IsCheckPoint)
m_DspEdit.OnDisplay(m_StrBegin+".");
else
m_DspEdit.OnDisplay(m_StrBegin);
}
void CCalculatorDlg::AdvButtonPoint() //按下.键时的处理函数
{
// TODO: Add your control notification handler code here
if (!m_IsCheckPoint && m_StrBegin != "")
{
m_StrBegin = m_StrBegin+".";
m_DspEdit.OnDisplay(m_StrBegin);
m_IsCheckPoint = true;
}
}
void CCalculatorDlg::AdvButtonAdd()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 1;
}
void CCalculatorDlg::AdvButtonSub()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 2;
}
void CCalculatorDlg::AdvButtonMul()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 3;
}
void CCalculatorDlg::AdvButtonDiv()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 4;
}
void CCalculatorDlg::AdvButtonClear()
{
// TODO: Add your control notification handler code here
m_IsCheckPoint = false;
m_StrBegin = "";
m_begin = 0.0;
m_end = 0.0;
m_bit = 0;
m_DspEdit.OnDisplay("0.");
}
void CCalculatorDlg::AdvButtonSqrt()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 5;
}
void CCalculatorDlg::AdvButtonReverse()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 6;
}
void CCalculatorDlg::AdvButtonEqu() //按下=键时的处理函数
{
// TODO: Add your control notification handler code here
Result();
m_bit = 7;
}
void CCalculatorDlg::AdvButtonSin()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 8;
}
void CCalculatorDlg::AdvButtonCos()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 9;
}
void CCalculatorDlg::AdvButtonTan()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 10;
}
void CCalculatorDlg::AdvButtonPow()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 13;
}
void CCalculatorDlg::AdvButtonSqu()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 11;
}
void CCalculatorDlg::AdvButtonCub()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 12;
}
void CCalculatorDlg::AdvButtonExp()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 14;
}
void CCalculatorDlg::AdvButtonLn()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 15;
}
void CCalculatorDlg::AdvButtonLog()
{
// TODO: Add your control notification handler code here
Result();
m_bit = 16;
}
void CCalculatorDlg::Result()//用来处理按下符号键时的函数,是计算的核心函数
{
m_IsCheckPoint = false;
m_begin = atof(m_StrBegin);
switch(m_bit)
{
case 1: //处理+号
m_end += m_begin;
break;
case 2: //处理-号
m_end -= m_begin;
break;
case 3: //处理*号,即乘法
m_end *= m_begin;
break;
case 4: //处理/号,即除法
if (m_begin==0)
{
m_DspEdit.OnDisplay("除数不能为零!");
return;
}
m_end /= m_begin;
break;
case 5: //处理sqrt号,即开方
if (m_end<0)
{
m_DspEdit.OnDisplay("函数输入无效!");
return;
}
m_end = sqrt(m_end);
break;
case 6: //处理1/x号,即求倒数
m_end = 1/m_end;
case 7: //处理=号
break;
case 8: //处理sin号
m_end = sin(m_end);
break;
case 9: //处理cos号
m_end = cos(m_end);
break;
case 10: //处理tan号
m_end = tan(m_end);
break;
case 11: //处理x^2号,即平方
m_end = m_end*m_end;
break;
case 12: //处理x^3号,即立方
m_end = m_end*m_end*m_end;
break;
case 13: //处理x^y号,即求x的y次方
m_end = pow(m_end,m_begin);
break;
case 14: //处理Exp号,即求e的x次方
m_end = exp(m_end);
break;
case 15: //处理ln号
if (m_end<=0)
{
m_DspEdit.OnDisplay("函数输入无效!");
return;
}
m_end = log(m_end);
break;
case 16: //处理log号
if (m_end<=0)
{
m_DspEdit.OnDisplay("函数输入无效!");
return;
}
m_end = log10(m_end);
break;
default: //
m_end = m_begin;
break;
}
m_begin = 0.0;
m_bit = 0;
m_StrBegin = "";
int i = sprintf(m_buffer,"%10.12f",m_end);//将结果转换为字符串
TrimZero(m_buffer,i);//去掉字符串尾部多余的零
m_DspEdit.OnDisplay(m_buffer);
}
void CCalculatorDlg::TrimZero(char *pStr,int iLen)//用于去掉字符串尾部多余的零。
{
char * p = &pStr[iLen-1];
while (1) {
if( *p != '0') break;
* p -- = '\0';
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -