📄 我的计算器 dlg.cpp
字号:
// 我的计算器 Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "我的计算器 .h"
#include "我的计算器 Dlg.h"
#include "math.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()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
m_Result = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
memset(m_str1,0,sizeof(m_str1));
memset(m_str2,0,sizeof(m_str2));//用来将两个存放数据的字符数组置为空
Fflag=TRUE;
m_data1=0;
m_data2=0;
m_result=0;//将存放两个数据和结果的变量分别初始化为零
m_symbol='@';//运算符
m_TableFlag=FALSE;//当前活动的输入框
m_DeleteFlag=TRUE;//删除方式标志
m_CommonCalculate=TRUE;//将几个标志变量分别初始化
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Control(pDX, IDC_EDIT8, m_DelType);
DDX_Control(pDX, IDC_EDIT7, m_CalType);
DDX_Control(pDX, IDC_EDIT6, m_DataType);
DDX_Control(pDX, IDC_EDIT5, m_GetDate);
DDX_Control(pDX, IDC_EDIT3, m_Number2);
DDX_Control(pDX, IDC_EDIT2, m_Symbol);
DDX_Control(pDX, IDC_EDIT1, m_Number1);
DDX_Text(pDX, IDC_EDIT4, m_Result);
//}}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_BUTTON1, On0)
ON_BN_CLICKED(IDC_BUTTON2, On1)
ON_BN_CLICKED(IDC_BUTTON3, On2)
ON_BN_CLICKED(IDC_BUTTON4, On3)
ON_BN_CLICKED(IDC_BUTTON5, On4)
ON_BN_CLICKED(IDC_BUTTON6, On5)
ON_BN_CLICKED(IDC_BUTTON7, On6)
ON_BN_CLICKED(IDC_BUTTON8, On7)
ON_BN_CLICKED(IDC_BUTTON9, On8)
ON_BN_CLICKED(IDC_BUTTON10, On9)
ON_BN_CLICKED(IDC_BUTTON11, OnDot)
ON_BN_CLICKED(IDC_BUTTON12, OnProcess)
ON_BN_CLICKED(IDC_BUTTON13, OnPlus)
ON_BN_CLICKED(IDC_BUTTON14, OnMini)
ON_BN_CLICKED(IDC_BUTTON15, OnMulti)
ON_BN_CLICKED(IDC_BUTTON16, OnDivid)
ON_BN_CLICKED(IDCANCEL, OnClear)
ON_BN_CLICKED(IDC_BUTTON17, OnDelete)
ON_COMMAND(ID_MENUITEM32778, OnDeleteSet)
ON_COMMAND(ID_MENUITEM32779, OnDeleteChange)
ON_BN_CLICKED(IDC_BUTTON18, OnContinue)
ON_COMMAND(ID_MENUITEM32780, OnCalInit)
ON_COMMAND(ID_MENUITEM32781, OnCalChange)
ON_BN_CLICKED(IDC_BUTTON19, Onln)
ON_COMMAND(ID_MENUITEM32775, OnFflagSet)
ON_COMMAND(ID_MENUITEM32776, OnFflagChange)
ON_BN_CLICKED(IDC_BUTTON20, Onlog)
ON_BN_CLICKED(IDC_BUTTON21, OnSin)
ON_BN_CLICKED(IDC_BUTTON22, OnCos)
ON_BN_CLICKED(IDC_BUTTON23, OnTan)
ON_BN_CLICKED(IDC_BUTTON24, OnSqrt)
ON_COMMAND(ID_MENUITEM32783, OnNumberIntro)
ON_COMMAND(ID_MENUITEM32785, OnDailSymbolItro)
ON_COMMAND(ID_MENUITEM32786, OnComSymbolItro)
ON_COMMAND(ID_MENUITEM32787, OnProcessItro)
ON_COMMAND(ID_MENUITEM32789, OnContinueKeyItro)
ON_COMMAND(ID_MENUITEM32790, OnClearKeyItro)
ON_COMMAND(ID_MENUITEM32791, OnDeleKeyItro)
ON_COMMAND(ID_MENUITEM32792, OnQuitKeyItro)
ON_COMMAND(ID_MENUITEM32793, OnInputItro)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::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);
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
CTime tNow;
tNow=CTime::GetCurrentTime();
CString sNow;
sNow=tNow.Format ("20%y年%m月%d日");
m_GetDate.SetSel(0,-1);
m_GetDate.ReplaceSel(sNow);
m_DataType.SetSel(0,-1);
m_DataType.ReplaceSel("浮点数据");
m_DelType.SetSel(0,-1);
m_DelType.ReplaceSel("每次删去一个数字");
m_CalType.SetSel(0,-1);
m_CalType.ReplaceSel("日常运算");
UpdateData(FALSE); //用来初始化状态栏中的三个四个框中的显示
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//从这里开始用来设计设个数字按钮的映射函数
void CMyDlg::On0()
{
// TODO: Add your control notification handler code here
if(!m_TableFlag)
{
strcat(m_str1,"0");
m_Number1.SetWindowText(m_str1);
UpdateData(FALSE);
}
else
{
strcat(m_str2,"0");
m_Number2.SetWindowText(m_str2);
UpdateData(FALSE);
}
}
void CMyDlg::On1()
{
// TODO: Add your control notification handler code here
if(!m_TableFlag)
{
strcat(m_str1,"1");
m_Number1.SetWindowText(m_str1);
UpdateData(FALSE);
}
else
{
strcat(m_str2,"1");
m_Number2.SetWindowText(m_str2);
UpdateData(FALSE);
}
}
void CMyDlg::On2()
{
// TODO: Add your control notification handler code here
if(!m_TableFlag)
{
strcat(m_str1,"2");
m_Number1.SetWindowText(m_str1);
UpdateData(FALSE);
}
else
{
strcat(m_str2,"2");
m_Number2.SetWindowText(m_str2);
UpdateData(FALSE);
}
}
void CMyDlg::On3()
{
// TODO: Add your control notification handler code here
if(!m_TableFlag)
{
strcat(m_str1,"3");
m_Number1.SetWindowText(m_str1);
UpdateData(FALSE);
}
else
{
strcat(m_str2,"3");
m_Number2.SetWindowText(m_str2);
UpdateData(FALSE);
}
}
void CMyDlg::On4()
{
// TODO: Add your control notification handler code here
if(!m_TableFlag)
{
strcat(m_str1,"4");
m_Number1.SetWindowText(m_str1);
UpdateData(FALSE);
}
else
{
strcat(m_str2,"4");
m_Number2.SetWindowText(m_str2);
UpdateData(FALSE);
}
}
void CMyDlg::On5()
{
// TODO: Add your control notification handler code here
if(!m_TableFlag)
{
strcat(m_str1,"5");
m_Number1.SetWindowText(m_str1);
UpdateData(FALSE);
}
else
{
strcat(m_str2,"5");
m_Number2.SetWindowText(m_str2);
UpdateData(FALSE);
}
}
void CMyDlg::On6()
{
// TODO: Add your control notification handler code here
if(!m_TableFlag)
{
strcat(m_str1,"6");
m_Number1.SetWindowText(m_str1);
UpdateData(FALSE);
}
else
{
strcat(m_str2,"6");
m_Number2.SetWindowText(m_str2);
UpdateData(FALSE);
}
}
void CMyDlg::On7()
{
// TODO: Add your control notification handler code here
if(!m_TableFlag)
{
strcat(m_str1,"7");
m_Number1.SetWindowText(m_str1);
UpdateData(FALSE);
}
else
{
strcat(m_str2,"7");
m_Number2.SetWindowText(m_str2);
UpdateData(FALSE);
}
}
void CMyDlg::On8()
{
// TODO: Add your control notification handler code here
if(!m_TableFlag)
{
strcat(m_str1,"8");
m_Number1.SetWindowText(m_str1);
UpdateData(FALSE);
}
else
{
strcat(m_str2,"8");
m_Number2.SetWindowText(m_str2);
UpdateData(FALSE);
}
}
void CMyDlg::On9()
{
// TODO: Add your control notification handler code here
if(!m_TableFlag)
{
strcat(m_str1,"9");
m_Number1.SetWindowText(m_str1);
UpdateData(FALSE);
}
else
{
strcat(m_str2,"9");
m_Number2.SetWindowText(m_str2);
UpdateData(FALSE);
}
}
/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
////设置对小数点的映射函数
void CMyDlg::OnDot()
{
// TODO: Add your control notification handler code here
if(!strcmp(m_str1,""))
{
MessageBox("一个数以小数点开头是不正确的!请重新键入你的数据!\t","错误提示", MB_ICONWARNING|MB_OK|MB_DEFBUTTON1);
return;
}
if(!Fflag)
{
MessageBox("在整形运算中出现小数点是错误的!请重新键入你的数据!\t","错误提示", MB_ICONWARNING|MB_OK|MB_DEFBUTTON1);
goto end;
}
if(!m_TableFlag)
{
strcat(m_str1,".");
m_Number1.SetWindowText(m_str1);
UpdateData(FALSE);
}
else
{
strcat(m_str2,".");
m_Number2.SetWindowText(m_str2);
UpdateData(FALSE);
}
end:;
}
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//设计运算的等号键的处理函数
void CMyDlg::OnProcess()
{
// TODO: Add your control notification handler code here
m_Number1.GetWindowText(m_str1,20);
m_data1=atof((LPCTSTR)m_str1);
m_Number2.GetWindowText(m_str2,20);
int i=0;
while(m_str2[i])
{
if(m_str2[i]<'0'||m_str2[i]>'9')
{
if(!Fflag)
{
MessageBox("你输入的整数中可能出现了非0——9的非法数据,请重新键入你的数据!\t","错误提示", MB_ICONWARNING|MB_OK|MB_DEFBUTTON1);
m_TableFlag=FALSE;
return;
}
else if(m_str2[i]!='.')
{
MessageBox("你输入的数据中有非法数据,请重新键入你的数据!\t","错误提示", MB_ICONWARNING|MB_OK|MB_DEFBUTTON1);
m_TableFlag=FALSE;
return;
}
}
i++;
}
m_data2=atof((LPCTSTR)m_str2);
switch(m_symbol)
{
case '+': m_result=m_data1+m_data2;break;
case '-': m_result=m_data1-m_data2;break;
case '*': m_result=m_data1*m_data2;break;
case '/':
{
if(fabs(m_data2)<0.000000001)
{
MessageBox("除法运算中除数为零,为非法数据,请重新键入你的数据!\t","错误提示", MB_ICONWARNING|MB_OK|MB_DEFBUTTON1);
m_Number2.SetSel (0,-1);
m_Number2.ReplaceSel ("");
memset(m_str2,0,sizeof(m_str2));
UpdateData(FALSE);
return;
}
m_result=m_data1/m_data2;
break;
}
default : m_result=m_data1;
}
sprintf(m_str1,"%g",m_result);
i=0;
while(m_str1[i])
i++;
if(!Fflag)
m_str1[i-1]=NULL;
m_Result=(LPCTSTR)m_str1;
UpdateData(FALSE);
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//设置日常运算符的映射处理函数
void CMyDlg::OnPlus()
{
// TODO: Add your control notification handler code here
m_TableFlag=TRUE;
m_symbol='+';
GetDlgItem(IDC_EDIT3)->EnableWindow(SW_SHOW);
m_Number2.ShowWindow(SW_SHOW);
m_Number1.GetWindowText (m_str1,20);
int i=0;
while(m_str1[i])
{
if(m_str1[i]<'0'||m_str1[i]>'9')
{
if(!Fflag)
{
MessageBox("你输入的整数中可能出现了非0——9的非法数据,请重新键入你的数据!\t","错误提示", MB_ICONWARNING|MB_OK|MB_DEFBUTTON1);
m_TableFlag=FALSE;
return;
}
else if(m_str1[i]!='.')
{
MessageBox("你输入的数据中有非法数据,请重新键入你的数据!\t","错误提示", MB_ICONWARNING|MB_OK|MB_DEFBUTTON1);
m_TableFlag=FALSE;
return;
}
}
i++;
}
m_data1=atof((LPCTSTR)m_str1);
m_Symbol.SetWindowText("+");
UpdateData(FALSE);
}
void CMyDlg::OnMini()
{
// TODO: Add your control notification handler code here
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -