caculatordlg.cpp
来自「我自己写的一个基于对话框的计算器」· C++ 代码 · 共 976 行 · 第 1/2 页
CPP
976 行
if(s==1)m_fun=1;
else if(s==2)m_fun=2;
else if(s==3)m_fun=3;
else if(s==4)m_fun=4;
else if(s==5)m_fun=5;
else if(s==5)m_fun=5;
}
else
{
if(s==1)m_fun=1;
if(s==2)m_fun=2;
if(s==3)m_fun=3;
if(s==4)m_fun=4;
if(s==5)m_fun=5;
change=TRUE;
UpdateData(FALSE);
}
m_firstdata=0.0;
m_seconddata=0.0;
m_firstdata1=0.0;
m_seconddata1=0.0;
m_firxw=0;
m_secxw=0;
ishavedot=FALSE;
m_showtime.SetFocus();
sign=1;
m_signxw=0;
}
void CCaculatorDlg::OnBack() //修改
{
// TODO: Add your control notification handler code here
if(change==FALSE){
if(ishavedot==FALSE)
{m_firstdata=int((m_firstdata)/10);
m_result=sign*m_firstdata;
}
if(ishavedot==TRUE)
{
if(m_firxw){int temp=(int)((m_seconddata*pow(10,m_firxw))/10);
m_firxw--;
m_seconddata=temp*(pow(10,(-1)*m_firxw));
m_result=sign*(m_firstdata+m_seconddata);}
else{ishavedot=FALSE;
}
}
}
else if(change==TRUE){
if(ishavedot==FALSE)
{m_firstdata1=int((m_firstdata1)/10);
m_result=sign*m_firstdata1;
}
else if(ishavedot==TRUE)
{
if(m_secxw){int temp=(int)((m_seconddata1*pow(10,m_secxw))/10);
m_secxw--;
m_seconddata1=temp*(pow(10,(-1)*m_secxw));
m_result=sign*(m_firstdata1+m_seconddata1);}
else{ishavedot=FALSE;
}
}
}
UpdateData(FALSE);
m_showtime.SetFocus();
}
void CCaculatorDlg::OnSelect() //正负
{
m_signxw++;
// TODO: Add your control notification handler code here
sign=(int)(pow((-1),m_signxw));
//m_result*=sign; ???
if(change=FALSE){
m_temp=sign*(m_firstdata+m_seconddata);
//m_result=sign*(m_firstdata+m_seconddata);
//m_temp=sign*m_temp;
m_result=m_temp;
//m_temp=m_result;
}
else if(change=TRUE){
//m_result=sign*m_result;
//m_result=sign*(m_firstdata1+m_seconddata1);
m_temp1=sign*(m_firstdata1+m_seconddata1);
m_result=m_temp1;
//m_temp1=m_result;
}
UpdateData(FALSE);
m_showtime.SetFocus();
}
void CCaculatorDlg::OnAdd() //+
{
// TODO: Add your control notification handler code here
AddFun(1);
}
void CCaculatorDlg::OnMinus() //-
{
// TODO: Add your control notification handler code here
AddFun(2);
}
void CCaculatorDlg::OnMuti() //*
{
// TODO: Add your control notification handler code here
AddFun(3);
}
void CCaculatorDlg::OnDivide() // /
{
// TODO: Add your control notification handler code here
AddFun(4);
}
void CCaculatorDlg::OnRecipal() //倒数
{
m_result=1/m_result;
m_temp=m_result;
UpdateData(FALSE);
m_showtime.SetFocus();
// TODO: Add your control notification handler code here
//AddFun(5);
}
void CCaculatorDlg::OnDoub() //平方
{
m_result*=m_result;
m_temp=m_result;
UpdateData(FALSE);
m_showtime.SetFocus();
// TODO: Add your control notification handler code here
//AddFun(6);
}
void CCaculatorDlg::OnPoint() // .
{
// TODO: Add your control notification handler code here
ishavedot=TRUE;
m_showtime.SetFocus();
}
void CCaculatorDlg::OnSqurt() //平方
{m_result=sqrt(m_result);
m_temp=m_result;
UpdateData(FALSE);
m_showtime.SetFocus();
// TODO: Add your control notification handler code here
//AddFun(7);
}
void CCaculatorDlg::OnEqual() // =
{
// TODO: Add your control notification handler code here
if(m_fun==1)
{
m_x=m_temp+m_temp1;
m_result=m_x;
UpdateData(FALSE);
//m_temp=m_x;
change=TRUE;
}
else if(m_fun==2)
{
m_x=m_temp-m_temp1;
m_result=m_x;
UpdateData(FALSE);
//m_temp=m_x;
change=TRUE;
}
else if(m_fun==3)
{
m_x=m_temp*m_temp1;
m_result=m_x;
UpdateData(FALSE);
//m_temp=m_x;
change=TRUE;
}
else if(m_fun==4)
{
m_x=m_temp/m_temp1;
m_result=m_x;
UpdateData(FALSE);
//m_temp=m_x;
change=TRUE;
}
else if(m_fun==5)
{
m_x=pow(m_temp,m_temp1);
m_result=m_x;
UpdateData(FALSE);
//m_temp=m_x;
change=TRUE;
}
m_firstdata=0.0;
m_seconddata=0.0;
m_firstdata1=0.0;
m_seconddata1=0.0;
m_firxw=0;
m_secxw=0;
ishavedot=FALSE;
sign=1;
m_signxw=0;
m_showtime.SetFocus();
}
int CCaculatorDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
CSplashWnd::ShowSplashScreen(this);
return 0;
// TODO: Add your specialized creation code here
}
void CCaculatorDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
{
CTime tm;
CString tr;
tm=CTime::GetCurrentTime();
tr=tm.Format("%H:%M:%S");
m_showtime.SetWindowText(tr);
}
m_showtime.SetFocus();
CDialog::OnTimer(nIDEvent);
}
void CCaculatorDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
KillTimer(1);
CDialog::OnClose();
}
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//CDC *pDC=(CDC*)GetClientDc();//
// pDC->SetBkColor(RGB(0,0,255));
// TODO: Add extra initialization here
m_OK.LoadBitmap(IDB_OK1);
CButton* bt=(CButton*)GetDlgItem(IDOK);
HBITMAP hBitmap=(HBITMAP)m_OK.GetSafeHandle();
bt->SetBitmap(hBitmap);
CTime tm;
CString tr;
tm=CTime::GetCurrentTime();
CEdit *m_showtime=(CEdit*)GetDlgItem(IDC_SHOWTIME);
//m_showtime=tm.Format("%Y年%m月%d日");
tr=tm.Format("%Y年%m月%d日");
m_showtime->SetWindowText(tr);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CCaculatorDlg::OnAbout()
{
// TODO: Add your command handler code here
CAboutDlg dlg;
dlg.DoModal();
}
void CCaculatorDlg::OnRadian() //弧度
{
// TODO: Add your control notification handler code here
m_change=1.0;
m_showtime.SetFocus();
}
void CCaculatorDlg::OnAngle() //角度
{
// TODO: Add your control notification handler code here
double pi = 3.1415926535897932384626433832795;
// TODO: Add your control notification handler code here
m_change=pi/180;
m_showtime.SetFocus();
}
void CCaculatorDlg::OnSin() //sin
{
// TODO: Add your control notification handler code here
m_result=sin(m_change*m_result);
UpdateData(FALSE);
m_showtime.SetFocus();
m_firstdata=0;
m_seconddata=0.0;
}
void CCaculatorDlg::OnCos() //cos
{
// TODO: Add your control notification handler code here
m_result=cos(m_change*m_result);
UpdateData(FALSE);
m_showtime.SetFocus();
m_firstdata=0;
m_seconddata=0.0;
}
void CCaculatorDlg::OnLog() //ln
{
// TODO: Add your control notification handler code here
m_result=log(m_result);
UpdateData(FALSE);
m_showtime.SetFocus();
m_firstdata=0;
m_seconddata=0.0;
}
void CCaculatorDlg::OnTan() //tan
{
// TODO: Add your control notification handler code here
m_result=tan(m_change*m_result);
UpdateData(FALSE);
m_showtime.SetFocus();
m_firstdata=0;
m_seconddata=0.0;
}
void CCaculatorDlg::OnXy() //x^y
{
// TODO: Add your control notification handler code here
AddFun(5);
}
void CCaculatorDlg::OnFactorial() //n!
{int i;
// TODO: Add your control notification handler code here
for(i=m_result-1;i>0;i--)
{
m_result*=i;
}
UpdateData(FALSE);
m_showtime.SetFocus();
m_temp=0.0;
m_firstdata=0;
m_seconddata=0.0;
}
void CCaculatorDlg::OnX3() //x^3
{
m_result=pow(m_result,3);
UpdateData(FALSE);
m_showtime.SetFocus(); // TODO: Add your control notification handler code here
}
void CCaculatorDlg::OnResid()
{
// TODO: Add your control notification handler code here
m_result=exp(m_result);
UpdateData(FALSE);
m_showtime.SetFocus();
m_temp=m_result;
m_firstdata=0;
m_seconddata=0.0;
}
void CCaculatorDlg::ToggleSize()
{
CRect rect;
CString str;
if(m_bToggleSize)
{
rect=m_rectFull;}
else
{
rect=m_rectHalf;
}
SetWindowPos(NULL,0,0,rect.Width(),rect.Height(),SWP_NOZORDER|SWP_NOMOVE);
m_bToggleSize=!m_bToggleSize;
}
void CCaculatorDlg::OnStandard()
{
// TODO: Add your command handler code here
ToggleSize();
}
BOOL CCaculatorDlg::PreTranslateMessage(MSG* pMsg) //键盘输入
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message == WM_KEYDOWN)
{
if(pMsg->wParam=='1'||pMsg->wParam==VK_NUMPAD1)//1
OnNumber1();
else if(pMsg->wParam=='2'||pMsg->wParam==VK_NUMPAD2)//2
OnNumber2();
else if(pMsg->wParam=='3'||pMsg->wParam==VK_NUMPAD3)//3
OnNumber3();
else if(pMsg->wParam=='4'||pMsg->wParam==VK_NUMPAD4)//4
OnNumber4();
else if(pMsg->wParam=='5'||pMsg->wParam==VK_NUMPAD5)//5
OnNumber5();
else if(pMsg->wParam=='6'||pMsg->wParam==VK_NUMPAD6)//6
OnNumber6();
else if(pMsg->wParam=='7'||pMsg->wParam==VK_NUMPAD7)//7
OnNumber7();
else if(pMsg->wParam=='8'||pMsg->wParam==VK_NUMPAD8)//8
OnNumber8();
else if(pMsg->wParam=='9'||pMsg->wParam==VK_NUMPAD9)//9
OnNumber9();
else if(pMsg->wParam=='0'||pMsg->wParam==VK_NUMPAD0)//0
OnNumber0();
else if(pMsg->wParam==VK_ADD)//+
OnAdd();
else if(pMsg->wParam==VK_DECIMAL)//.
OnPoint();
else if(pMsg->wParam==VK_SUBTRACT )//-
OnMinus();
else if(pMsg->wParam==VK_MULTIPLY)//*
OnMuti();
else if(pMsg->wParam==VK_DIVIDE)// /
OnDivide();
else if(pMsg->wParam==VK_DELETE)//清零
OnAllzero() ;
else if(pMsg->wParam==VK_RETURN)//=
OnEqual();
else if(pMsg->wParam==VK_BACK)//back
OnBack();
}
UpdateData(FALSE);
return CDialog::PreTranslateMessage(pMsg);
}
void CCaculatorDlg::OnOK()
{
// TODO: Add extra validation here
// CDialog::OnOK();
}
BOOL CCaculatorDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
// if(pWnd->GetDlgCtrlID() == IDC_SHOW)
//OnQueryDragIcon();
return TRUE;
//::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
//return CDialog::OnSetCursor(pWnd, nHitTest, message);
}
BOOL CCaculatorDlg::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized code here and/or call the base class
// if (cs.lpszClass == NULL)
cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS);
return TRUE;
//return CDialog::PreCreateWindow(cs);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?