C++的计算器.docx
《C++的计算器.docx》由会员分享,可在线阅读,更多相关《C++的计算器.docx(15页珍藏版)》请在冰豆网上搜索。
C++的计算器
//计算器Dlg.cpp:
implementationfile
#include"Math.h"
#include"stdafx.h"
#include"计算器.h"
#include"计算器Dlg.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()
/////////////////////////////////////////////////////////////////////////////
//CMyDlgdialog
CMyDlg:
:
CMyDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CMyDlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
m_edit1=_T("0.0");
m_first=(0.0);
m_second=(0.0);
m_coff=(1.0);
m_operator=_T("+");
m_shizhong=_T("");
//}}AFX_DATA_INIT
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
voidCMyDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Text(pDX,IDC_EDIT1,m_edit1);
DDX_Text(pDX,IDC_EDIT2,m_shizhong);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg,CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_0,On0)
ON_BN_CLICKED(IDC_1,On1)
ON_BN_CLICKED(IDC_2,On2)
ON_BN_CLICKED(IDC_3,On3)
ON_BN_CLICKED(IDC_4,On4)
ON_BN_CLICKED(IDC_5,On5)
ON_BN_CLICKED(IDC_6,On6)
ON_BN_CLICKED(IDC_7,On7)
ON_BN_CLICKED(IDC_8,On8)
ON_BN_CLICKED(IDC_9,On9)
ON_BN_CLICKED(IDC_dian,Ondian)
ON_BN_CLICKED(IDC_jia,Onjia)
ON_BN_CLICKED(IDC_jian,Onjian)
ON_BN_CLICKED(IDC_cheng,Oncheng)
ON_BN_CLICKED(IDC_chu,Onchu)
ON_BN_CLICKED(IDC_C,OnC)
ON_BN_CLICKED(IDC_sqrt,Onsqrt)
ON_BN_CLICKED(IDC_1chux,On1chux)
ON_BN_CLICKED(IDC_dengyu,Ondengyu)
ON_BN_CLICKED(IDC_jiajian,Onjiajian)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_shitoer,Onshitoer)
ON_BN_CLICKED(IDC_shitoshiliu,Onshitoshiliu)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CMyDlgmessagehandlers
BOOLCMyDlg:
:
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
AfxMessageBox("欢迎体验陈信编写的简易计算器!
");
SetTimer(1,1000,NULL);//初始化打开定时器
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCMyDlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCMyDlg:
:
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.
HCURSORCMyDlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
voidCMyDlg:
:
On0()
{
//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);
}
voidCMyDlg:
:
On1()
{
//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);
}
voidCMyDlg:
:
On2()
{
//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);
}
voidCMyDlg:
:
On3()
{
//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);
}
voidCMyDlg:
:
On4()
{
//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);
}
voidCMyDlg:
:
On5()
{
//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);
}
voidCMyDlg:
:
On6()
{
//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);
}
voidCMyDlg:
:
On7()
{
//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);
}
voidCMyDlg:
:
On8()
{
//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);
}
voidCMyDlg:
:
On9()
{
//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);
}
voidCMyDlg:
:
Ondian()//按键.
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_coff=0.1;
}
voidCMyDlg:
:
Onjia()//按键+
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_operator="+";
}
voidCMyDlg:
:
Onjian()//按键-
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_operator="-";
}
voidCMyDlg:
:
Oncheng()//按键*
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_operator="*";
}
voidCMyDlg:
:
Onchu()//按键/
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_operator="/";
}
voidCMyDlg:
:
OnC()//按键C
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_second=0.0;
m_operator="+";
m_coff=1.0;
UpdateDisplay(0.0);
}
voidCMyDlg:
:
Onsqrt()//按键sqrt
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_second=sqrt(m_second);
UpdateDisplay(m_second);
}
voidCMyDlg:
:
On1chux()//按键1/X
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(fabs(m_second)<=0.000001)
{
m_edit1="除数不能等于零";
UpdateData(0);
return;
}
m_second=1.0/m_second;
UpdateDisplay(m_second);
}
voidCMyDlg:
:
Ondengyu()//按键=
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_first=0.0;
m_operator="+";
}
voidCMyDlg:
:
Onjiajian()//按键+/-
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_second=-m_second;
UpdateDisplay(m_second);
}
voidCMyDlg:
:
Calculate()//计算
{
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_edit1="除数不能等于零";
UpdateData(0);
return;
}
m_first/=m_second;break;
}
m_second=0.0;
m_coff=1.0;
UpdateDisplay(m_first);
}
voidCMyDlg:
:
UpdateDisplay(doublelVal)//更新显示
{
m_edit1.Format(_T("%f"),lVal);
inti=m_edit1.GetLength();
while(m_edit1.GetAt(i-1)=='0')
{
m_edit1.Delete(i-1,1);
i--;
}
UpdateData(0);
}
voidCMyDlg:
:
OnOk()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
SetTimer(1,1000,NULL);
}
voidCMyDlg:
:
OnTimer(UINTnIDEvent)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
//if(nIDEvent==1)
//{
CTimet=CTime:
:
GetCurrentTime();
m_shizhong.Format("时间:
%04d:
%02d:
%02d--%02d:
%02d:
%02d",t.GetYear(),t.GetMonth(),t.GetDay(),t.GetHour(),t.GetMinute(),t.GetSecond());
UpdateData(0);
//}
CDialog:
:
OnTimer(nIDEvent);
}
voidCMyDlg:
:
Onshitoer()
{
longsecond=0;
doublenum=0,sum=0,i=0.0,aaa;
second=(int)m_second;//转换为整形,使用函数pow的需要
while(second>=1)
{
if((second%2)==0)//十进制转换为二进制,除2取余
num=0;
else
num=1;
aaa=pow(10.0,i);//调用函数