📄 dialog1.cpp
字号:
// Dialog1.cpp : implementation file
//
#include "stdafx.h"
#include "0510210204watch.h"
#include "Dialog1.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDialog1 dialog
CDialog1::CDialog1(CWnd* pParent /*=NULL*/)
: CDialog(CDialog1::IDD, pParent)
{
//{{AFX_DATA_INIT(CDialog1)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDialog1::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDialog1)
// NOTE: the ClassWizard will add DDX and DDV calls here
DDX_Control(pDX, IDC_PERCENT, m_percent);
DDX_Control(pDX, IDC_RESULT, m_result);
DDX_Control(pDX, IDC_SUB, m_sub);
DDX_Control(pDX, IDC_MUL, m_mul);
DDX_Control(pDX, IDC_EQUAL, m_equal);
DDX_Control(pDX, IDC_DIV, m_div);
DDX_Control(pDX, IDC_CE, m_ce);
DDX_Control(pDX, IDC_C, m_c);
DDX_Control(pDX, IDC_BACKSPACE, m_backspace);
DDX_Control(pDX, IDC_ADD, m_add);
DDX_Control(pDX, IDC_NOT, m_not);
DDX_Control(pDX, IDC_POINT, m_point);
DDX_Control(pDX, IDC_9, m_9);
DDX_Control(pDX, IDC_8, m_8);
DDX_Control(pDX, IDC_7, m_7);
DDX_Control(pDX, IDC_6, m_6);
DDX_Control(pDX, IDC_5, m_5);
DDX_Control(pDX, IDC_4, m_4);
DDX_Control(pDX, IDC_3, m_3);
DDX_Control(pDX, IDC_2, m_2);
DDX_Control(pDX, IDC_1, m_1);
DDX_Control(pDX, IDC_0, m_0);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDialog1, CDialog)
//{{AFX_MSG_MAP(CDialog1)
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_ADD, OnAdd)
ON_BN_CLICKED(IDC_BACKSPACE, OnBackspace)
ON_BN_CLICKED(IDC_C, OnC)
ON_BN_CLICKED(IDC_CE, OnCe)
ON_BN_CLICKED(IDC_DIV, OnDiv)
ON_BN_CLICKED(IDC_EQUAL, OnEqual)
ON_BN_CLICKED(IDC_MUL, OnMul)
ON_BN_CLICKED(IDC_NOT, OnNot)
ON_BN_CLICKED(IDC_POINT, OnPoint)
ON_BN_CLICKED(IDC_SQRT, OnSqrt)
ON_BN_CLICKED(IDC_SUB, OnSub)
ON_BN_CLICKED(IDC_PERCENT, OnPercent)
ON_BN_CLICKED(IDC_RECI, OnReci)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDialog1 message handlers
BOOL CDialog1::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_result.SetWindowText(" 0.");
num2=0;
num1=0; //第一个操作数
sign=0; //运算符号
flag=0; //是否已按运算符的标志,为1就是已按了运算符,为0表示正在输入数据
fpoint=0; //小数点的标志
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
/*HCURSOR CDialog1::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}*/
void CDialog1::On0()
{
// TODO: Add your control notification handler code here
m_result.GetWindowText(text);
length=text.GetLength();
if(flag)
{ flag=0;
num2=0;
text="0.";
fpoint=0;
}
else if(length<30)
{
if(fpoint) text+="0";
else{
if(num2)
{text.Remove('.');text+="0.";}
else text="0.";
}
}
num2=atof(text);
m_result.SetWindowText(text);
}
void CDialog1::On1()
{
// TODO: Add your control notification handler code here
m_result.GetWindowText(text);
length=text.GetLength();
if(flag)
{ flag=0;
num2=1;
text=" 1.";
fpoint=0;
}
else if(length<30)
{
if(fpoint) text+="1";
else{
if(num2)
{text.Remove('.');text+="1.";}
else text="1.";
}
}
num2=atof(text);
m_result.SetWindowText(text);
}
void CDialog1::On2()
{
// TODO: Add your control notification handler code here
m_result.GetWindowText(text);
length=text.GetLength();
if(flag)
{ flag=0;
num2=2;
text=" 2.";
fpoint=0;
}
else if(length<30)
{
if(fpoint) text+="2";
else{
if(num2)
{text.Remove('.');text+="2.";}
else text="2.";
}
}
num2=atof(text);
m_result.SetWindowText(text);
}
void CDialog1::On3()
{
// TODO: Add your control notification handler code here
m_result.GetWindowText(text);
length=text.GetLength();
if(flag)
{ flag=0;
num2=3;
text=" 3.";
fpoint=0;
}
else if(length<30)
{
if(fpoint) text+="3";
else{
if(num2)
{text.Remove('.');text+="3.";}
else text="3.";
}
}
num2=atof(text);
m_result.SetWindowText(text);
}
void CDialog1::On4()
{
// TODO: Add your control notification handler code here
m_result.GetWindowText(text);
length=text.GetLength();
if(flag)
{ flag=0;
num2=4;
text=" 4.";
fpoint=0;
}
else if(length<30)
{
if(fpoint) text+="4";
else{
if(num2)
{text.Remove('.');text+="4.";}
else text="4.";
}
}
num2=atof(text);
m_result.SetWindowText(text);
}
void CDialog1::On5()
{
// TODO: Add your control notification handler code here
m_result.GetWindowText(text);
length=text.GetLength();
if(flag)
{ flag=0;
num2=5;
text=" 5.";
fpoint=0;
}
else if(length<30)
{
if(fpoint) text+="5";
else{
if(num2)
{text.Remove('.');text+="5.";}
else text="5.";
}
}
num2=atof(text);
m_result.SetWindowText(text);
}
void CDialog1::On6()
{
// TODO: Add your control notification handler code here
m_result.GetWindowText(text);
length=text.GetLength();
if(flag)
{ flag=0;
num2=6;
text=" 6.";
fpoint=0;
}
else if(length<30)
{
if(fpoint) text+="6";
else{
if(num2)
{text.Remove('.');text+="6.";}
else text="6.";
}
}
num2=atof(text);
m_result.SetWindowText(text);
}
void CDialog1::On7()
{
// TODO: Add your control notification handler code here
m_result.GetWindowText(text);
length=text.GetLength();
if(flag)
{ flag=0;
num2=7;
text=" 7.";
fpoint=0;
}
else if(length<30)
{
if(fpoint) text+="7";
else{
if(num2)
{text.Remove('.');text+="7.";}
else text="7.";
}
}
num2=atof(text);
m_result.SetWindowText(text);
}
void CDialog1::On8()
{
// TODO: Add your control notification handler code here
m_result.GetWindowText(text);
length=text.GetLength();
if(flag)
{ flag=0;
num2=8;
text=" 8.";
fpoint=0;
}
else if(length<30)
{
if(fpoint) text+="8";
else{
if(num2)
{text.Remove('.');text+="8.";}
else text="8.";
}
}
num2=atof(text);
m_result.SetWindowText(text);
}
void CDialog1::On9()
{
// TODO: Add your control notification handler code here
m_result.GetWindowText(text);
length=text.GetLength();
if(flag)
{ flag=0;
num2=9;
text=" 9.";
fpoint=0;
}
else if(length<30)
{
if(fpoint) text+="9";
else{
if(num2)
{text.Remove('.');text+="9.";}
else text="9.";
}
}
num2=atof(text);
m_result.SetWindowText(text);
}
void CDialog1::OnAdd()
{
// TODO: Add your control notification handler code here
OnEqual();
sign='+';
num1=num2;
}
void CDialog1::OnBackspace()
{
// TODO: Add your control notification handler code here
if(flag)return;
m_result.GetWindowText(text);
length=text.GetLength();
if(text.GetAt(length-1)=='.')
{
fpoint=0;
if(length==2)
{ num2=0;
text=" 0.";
}
else{
text.SetAt(length-2,'.');
text.SetAt(length-1,'\0');
}
}
else{
fpoint=1;
text.SetAt(length-1,'\0');
}
length--;
m_result.SetWindowText(text);
num2=atof(text);
}
//clear
void CDialog1::OnC()
{
// TODO: Add your control notification handler code here
m_result.SetWindowText("0.");
num1=num2=0;
length=0;
fpoint=0;
sign=0;
flag=0;
}
//clean
void CDialog1::OnCe()
{
// TODO: Add your control notification handler code here
m_result.SetWindowText("0.");
num2=0;
fpoint=0;
}
void CDialog1::OnDiv()
{
// TODO: Add your control notification handler code here
OnEqual();
sign='/';
num1=num2;
}
void CDialog1::OnEqual()
{
// TODO: Add your control notification handler code here
flag=1;
if(sign)
{
switch(sign)
{
case'+':num2=num1+num2;break;
case'-':num2=num1-num2;break;
case'*':num2=num1*num2;break;
case'/':
if(!num2)
{
MessageBox("除数不能为零!");
sign=0;
return;
}
num2=1.0*num1/num2;break;
}
}
sign=0;
DisplayResult(num2);
m_result.SetWindowText(text);
}
void CDialog1::OnMul()
{
// TODO: Add your control notification handler code here
OnEqual();
sign='*';
num1=num2;
}
void CDialog1::OnNot()
{
// TODO: Add your control notification handler code here
m_result.GetWindowText(text);
num2=atof(text);
num2=-num2;
length=text.GetLength ();
if(num2<0)text="-"+text;
else
text.Delete(0,1);
length++;
m_result.SetWindowText(text);
}
void CDialog1::OnPoint()
{
// TODO: Add your control notification handler code here
fpoint=1;
if(flag){m_result.SetWindowText("0.");flag=0;return;}
m_result.GetWindowText(text);
length=text.GetLength ();
if(text.GetAt(length-1)=='.')return;
return;
}
void CDialog1::OnSqrt()
{
// TODO: Add your control notification handler code here
flag=1;
if(num2<0)
{MessageBox("不能为负数!");return;}
num2=sqrt(num2);
DisplayResult(num2);
m_result.SetWindowText(text);
}
void CDialog1::OnSub()
{
// TODO: Add your control notification handler code here
OnEqual();
sign='-';
num1=num2;
}
void CDialog1::OnPercent()
{
// TODO: Add your control notification handler code here
OnEqual();
num2=num2*0.01;
DisplayResult(num2);
m_result.SetWindowText(text);
}
void CDialog1::DisplayResult(double display)
{
if(!display)
{
text="0.";
return;
}
double i=display,j=1;
//判断是否使用科学计数exp30
if(i<0)i=-i;
while(i>=10)
{
i=i/10;j++;
}
if(j>30)
{
text.Format("%e",display); return;
}
// normal output
text.Format("%.10f",display);
text.TrimRight('0');
}
void CDialog1::OnReci()
{
// TODO: Add your control notification handler code here
flag=1;
if(!num2)
{MessageBox("不能为0!");return;}
num2=1.0/num2;
this->DisplayResult(num2);
m_result.SetWindowText(text);
}
BOOL CDialog1::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_CHAR)
{
switch(pMsg->wParam)
{
case '0':
Key='0';
m_0.SendMessage(WM_LBUTTONDOWN);
break;
case '1':
Key='1';
m_1.SendMessage(WM_LBUTTONDOWN);
break;
case '2':
Key='2';
m_2.SendMessage(WM_LBUTTONDOWN);
break;
case '3':
Key='3';
m_3.SendMessage(WM_LBUTTONDOWN);
break;
case '4':
Key='4';
m_4.SendMessage(WM_LBUTTONDOWN);
break;
case '5':
Key='5';
m_5.SendMessage(WM_LBUTTONDOWN);
break;
case '6':
Key='6';
m_6.SendMessage(WM_LBUTTONDOWN);
break;
case '7':
Key='7';
m_7.SendMessage(WM_LBUTTONDOWN);
break;
case '8':
Key='8';
m_8.SendMessage(WM_LBUTTONDOWN);
break;
case '9':
Key='9';
m_9.SendMessage(WM_LBUTTONDOWN);
break;
case '+':
Key='+';
m_add.SendMessage(WM_LBUTTONDOWN);
break;
case '-':
Key='-';
m_sub.SendMessage(WM_LBUTTONDOWN);
break;
case '*':
Key='*';
m_mul.SendMessage(WM_LBUTTONDOWN);
break;
case '/':
Key='/';
m_div.SendMessage(WM_LBUTTONDOWN);
break;
case '.':
Key='.';
m_point.SendMessage(WM_LBUTTONDOWN);
break;
case '%':
Key='%';
m_percent.SendMessage(WM_LBUTTONDOWN);
break;
case '=':
Key='=';
m_equal.SendMessage(WM_LBUTTONDOWN);
break;
case VK_BACK:
Key='b';
m_backspace.SendMessage(WM_LBUTTONDOWN);
break;
}
return FALSE;
}
if(pMsg->message==WM_KEYUP)
{
switch(Key)
{
case '0':
m_0.SendMessage(WM_LBUTTONUP);
break;
case '1':
m_1.SendMessage(WM_LBUTTONUP);
break;
case '2':
m_2.SendMessage(WM_LBUTTONUP);
break;
case '3':
m_3.SendMessage(WM_LBUTTONUP);
break;
case '4':
m_4.SendMessage(WM_LBUTTONUP);
break;
case '5':
m_5.SendMessage(WM_LBUTTONUP);
break;
case '6':
m_6.SendMessage(WM_LBUTTONUP);
break;
case '7':
m_7.SendMessage(WM_LBUTTONUP);
break;
case '8':
m_8.SendMessage(WM_LBUTTONUP);
break;
case '9':
m_9.SendMessage(WM_LBUTTONUP);
break;
case '+':
m_add.SendMessage(WM_LBUTTONUP);
break;
case '-':
m_sub.SendMessage(WM_LBUTTONUP);
break;
case '*':
m_mul.SendMessage(WM_LBUTTONUP);
break;
case '/':
m_div.SendMessage(WM_LBUTTONUP);
break;
case '.':
m_point.SendMessage(WM_LBUTTONUP);
break;
case '%':
m_percent.SendMessage(WM_LBUTTONUP);
break;
case '=':
m_equal.SendMessage(WM_LBUTTONUP);
break;
case 'b':
m_backspace.SendMessage(WM_LBUTTONUP);
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -