MyCalculatorDlgcpp.docx
《MyCalculatorDlgcpp.docx》由会员分享,可在线阅读,更多相关《MyCalculatorDlgcpp.docx(16页珍藏版)》请在冰豆网上搜索。
![MyCalculatorDlgcpp.docx](https://file1.bdocx.com/fileroot1/2023-2/23/6f8344cd-eff9-4b13-96c9-aa3bca0b3697/6f8344cd-eff9-4b13-96c9-aa3bca0b36971.gif)
MyCalculatorDlgcpp
//MyCalculatorDlg.cpp:
implementationfile
//
#include"stdafx.h"
#include"MyCalculator.h"
#include"MyCalculatorDlg.h"
#include"Math.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
//DialogData
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
:
IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
voidCAboutDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//Nomessagehandlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
一、构造函数初始化CMyCalculatorDlg
//CMyCalculatorDlgdialog
CMyCalculatorDlg:
:
CMyCalculatorDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CMyCalculatorDlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CMyCalculatorDlg)
m_display=_T("0.0");
m_first=0.0;
m_second=0.0;
m_operator=_T("+");
m_coff=1.0;
//}}AFX_DATA_INIT
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
voidCMyCalculatorDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyCalculatorDlg)
DDX_Text(pDX,IDC_DISPLAY,m_display);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyCalculatorDlg,CDialog)
//{{AFX_MSG_MAP(CMyCalculatorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON0,OnButton0)
ON_BN_CLICKED(IDC_BUTTON1,OnButton1)
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_BUTTON_SQRT,OnButtonSqrt)
ON_BN_CLICKED(IDC_BUTTON_SIGN,OnButtonSign)
ON_BN_CLICKED(IDC_BUTTON_MUTIPLY,OnButtonMutiply)
ON_BN_CLICKED(IDC_BUTTON_MINUS,OnButtonMinus)
ON_BN_CLICKED(IDC_BUTTON_EQUAL,OnButtonEqual)
ON_BN_CLICKED(IDC_BUTTON_DIV,OnButtonDiv)
ON_BN_CLICKED(IDC_BUTTON_CLEAR,OnButtonClear)
ON_BN_CLICKED(IDC_BUTTON_ADD,OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_RECIPROCAL,OnButtonReciprocal)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CMyCalculatorDlgmessagehandlers
BOOLCMyCalculatorDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//Add"About..."menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);//Setbigicon
SetIcon(m_hIcon,FALSE);//Setsmallicon
//TODO:
Addextrainitializationhere
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCMyCalculatorDlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCMyCalculatorDlg:
:
OnPaint()
{
if(IsIconic())
{
CPaintDCdc(this);//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialog:
:
OnPaint();
}
}
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCMyCalculatorDlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
二、按钮函数
voidCMyCalculatorDlg:
:
OnButton0()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(m_coff==1.0)
m_second=m_second*10+0;
else
{m_second=m_second+0*m_coff;
m_coff*=0.1;
}
UpdateDisplay(m_second);
}
voidCMyCalculatorDlg:
:
OnButton1()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(m_coff==1.0)
m_second=m_second*10+1;
else
{m_second=m_second+1*m_coff;
m_coff*=0.1;
}
UpdateDisplay(m_second);
}
voidCMyCalculatorDlg:
:
OnButton2()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(m_coff==1.0)
m_second=m_second*10+2;
else
{m_second=m_second+2*m_coff;
m_coff*=0.1;
}
UpdateDisplay(m_second);
}
voidCMyCalculatorDlg:
:
OnButton3()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(m_coff==1.0)
m_second=m_second*10+3;
else
{m_second=m_second+3*m_coff;
m_coff*=0.1;
}
UpdateDisplay(m_second);
}
voidCMyCalculatorDlg:
:
OnButton4()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(m_coff==1.0)
m_second=m_second*10+4;
else
{m_second=m_second+4*m_coff;
m_coff*=0.1;
}
UpdateDisplay(m_second);
}
voidCMyCalculatorDlg:
:
OnButton5()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(m_coff==1.0)
m_second=m_second*10+5;
else
{m_second=m_second+5*m_coff;
m_coff*=0.1;
}
UpdateDisplay(m_second);
}
voidCMyCalculatorDlg:
:
OnButton6()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(m_coff==1.0)
m_second=m_second*10+6;
else
{m_second=m_second+6*m_coff;
m_coff*=0.1;
}
UpdateDisplay(m_second);
}
voidCMyCalculatorDlg:
:
OnButton7()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(m_coff==1.0)
m_second=m_second*10+7;
else
{m_second=m_second+7*m_coff;
m_coff*=0.1;
}
UpdateDisplay(m_second);
}
voidCMyCalculatorDlg:
:
OnButton8()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(m_coff==1.0)
m_second=m_second*10+8;
else
{m_second=m_second+8*m_coff;
m_coff*=0.1;
}
UpdateDisplay(m_second);
}
voidCMyCalculatorDlg:
:
OnButton9()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(m_coff==1.0)
m_second=m_second*10+9;
else
{m_second=m_second+9*m_coff;
m_coff*=0.1;
}
UpdateDisplay(m_second);
}
三、开平方函数
voidCMyCalculatorDlg:
:
OnButtonSqrt()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_second=sqrt(m_second);
UpdateDisplay(m_second);
}
四、符号函数
voidCMyCalculatorDlg:
:
OnButtonSign()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_second=-m_second;
UpdateDisplay(m_second);
}
五、加减乘除函数
voidCMyCalculatorDlg:
:
OnButtonAdd()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_operator="+";
}
voidCMyCalculatorDlg:
:
OnButtonMinus()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_operator="-";
}
voidCMyCalculatorDlg:
:
OnButtonMutiply()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_operator="*";
}
voidCMyCalculatorDlg:
:
OnButtonDiv()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_operator.Format("%s","/");
}
voidCMyCalculatorDlg:
:
OnButtonClear()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_first=0.0;
m_second=0.0;
m_operator="+";
m_coff=1.0;
UpdateDisplay(0.0);
}
六、计算函数
voidCMyCalculatorDlg:
:
Calculate(void)
{
switch(m_operator.GetAt(0))
{
case'+':
m_first+=m_second;break;
case'-':
m_first-=m_second;break;
case'*':
m_first*=m_second;break;
case'/':
if(fabs(m_second)<=0.000001)
{m_display="除数不能为零";
UpdateData(false);
return;
}
m_first/=m_second;break;
}
m_second=0.0;
m_coff=1.0;
UpdateDisplay(m_first);
}
七、“=”函数
voidCMyCalculatorDlg:
:
OnButtonEqual()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_first=0.0;
m_operator="+";
}
八、显示函数
voidCMyCalculatorDlg:
:
UpdateDisplay(doublelVal)
{
m_display.Format(_T("%f"),lVal);
inti=m_display.GetLength();
//格式化输出,将输出结果后的零全部截去
while(m_display.GetAt(i-1)=='0')
{m_display.Delete(i-1,1);i--;}
UpdateData(false);//更新显示编辑框变量m_display
}
九、倒数函数
voidCMyCalculatorDlg:
:
OnButtonReciprocal()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(fabs(m_second)<0.000001)
{m_display="除数不能为零";
UpdateData(false);
return;
}
m_second=1.0/m_second;
UpdateDisplay(m_second);
}