📄 计算器dlg.cpp
字号:
// 计算器Dlg.cpp : implementation file
#include "stdafx.h"
#include "计算器.h"
#include "计算器Dlg.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)
{//构造函数---------------------------------------------------------------------------------------------------------------
//成员变量初始化
sum=0;
//{{AFX_DATA_INIT(CMyDlg)
str = _T("");
M = _T("");
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Text(pDX, IDC_EDIT1, str);
DDX_Text(pDX, IDC_EDIT2, M);
//}}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, 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_BUTTON10, OnButton10)
ON_BN_CLICKED(IDC_BUTTON14, OnButton14)
ON_BN_CLICKED(IDC_BUTTON15, OnButton15)
ON_BN_CLICKED(IDC_BUTTON16, OnButton16)
ON_BN_CLICKED(IDC_BUTTON17, OnButton17)
ON_BN_CLICKED(IDC_BUTTON13, OnButton13)
ON_BN_CLICKED(IDC_BUTTON12, OnButton12)
ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
ON_BN_CLICKED(IDC_BUTTON18, OnButton18)
ON_BN_CLICKED(IDC_BUTTON19, OnButton19)
ON_BN_CLICKED(IDC_BUTTON20, OnButton20)
ON_BN_CLICKED(IDC_BUTTON21, OnButton21)
ON_BN_CLICKED(IDC_BUTTON22, OnButton22)
ON_BN_CLICKED(IDC_BUTTON23, OnButton23)
ON_BN_CLICKED(IDC_BUTTON24, OnButton24)
ON_BN_CLICKED(IDC_BUTTON25, OnButton25)
ON_BN_CLICKED(IDC_BUTTON35, OnButton35)
ON_BN_CLICKED(IDC_BUTTON26, OnButton26)
ON_BN_CLICKED(IDC_BUTTON42, OnButton42)
ON_BN_CLICKED(IDC_BUTTON32, OnButton32)
ON_BN_CLICKED(IDC_BUTTON33, OnButton33)
ON_BN_CLICKED(IDC_BUTTON28, OnButton28)
ON_BN_CLICKED(IDC_BUTTON29, OnButton29)
ON_BN_CLICKED(IDC_BUTTON30, OnButton30)
ON_BN_CLICKED(IDC_BUTTON31, OnButton31)
ON_BN_CLICKED(IDC_BUTTON27, OnButton27)
ON_BN_CLICKED(IDC_BUTTON34, OnButton34)
ON_BN_CLICKED(IDC_BUTTON36, OnButton36)
ON_BN_CLICKED(IDC_BUTTON37, OnButton37)
ON_BN_CLICKED(IDC_BUTTON38, OnButton38)
ON_BN_CLICKED(IDC_BUTTON39, OnButton39)
ON_BN_CLICKED(IDC_BUTTON40, OnButton40)
ON_BN_CLICKED(IDC_BUTTON41, OnButton41)
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
//}}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);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
((CButton*)GetDlgItem(IDC_RADIO2))->SetCheck(1);//初始化-----------------------------------------------------------------------------------------------------
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();
}
}
HCURSOR CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//_________________________________________________________________________________________________________________________________
void CMyDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
void CMyDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CMyDlg::OnButton1()
{
str+="1";
UpdateData(false);
}
void CMyDlg::OnButton2()
{
str+="2";
UpdateData(false);
}
void CMyDlg::OnButton3()
{
str+="3";
UpdateData(false);
}
void CMyDlg::OnButton4()
{
str+="4";
UpdateData(false);
}
void CMyDlg::OnButton5()
{
str+="5";
UpdateData(false);
}
void CMyDlg::OnButton6()
{
str+="6";
UpdateData(false);
}
void CMyDlg::OnButton7()
{
str+="7";
UpdateData(false);
}
void CMyDlg::OnButton8()
{
str+="8";
UpdateData(false);
}
void CMyDlg::OnButton9()
{
str+="9";
UpdateData(false);
}
void CMyDlg::OnButton10()
{
str+="0";
UpdateData(false);
}
void CMyDlg::OnButton14()
{
str+="+";
UpdateData(false);
}
void CMyDlg::OnButton15()
{
str+="-";
UpdateData(false);
}
void CMyDlg::OnButton16()
{
str+="*";
UpdateData(false);
}
void CMyDlg::OnButton17()
{
str+="/";
UpdateData(false);
}
void CMyDlg::OnButton13() //清0
{
str="";
UpdateData(false);
}
void CMyDlg::OnButton12() //删除
{
str.Delete(str.GetLength()-1);
UpdateData(false);
}
void CMyDlg::OnButton18()
{
str+="(";
UpdateData(false);
}
void CMyDlg::OnButton19()
{
str+=")";
UpdateData(false);
}
void CMyDlg::OnButton20()
{
str+="E";
UpdateData(false);
}
void CMyDlg::OnButton25()
{
str+="Pi";
UpdateData(false);
}
void CMyDlg::OnButton35()
{
str+=".";
UpdateData(false);
}
void CMyDlg::OnButton26()
{
// str+='%';
// UpdateData(false);
}
void CMyDlg::OnButton42()
{
MessageBox("优先级为:“e”=“Pi”>“Ln”=“Log”=“Sin”=“Cos”=“Tan”=“Ctg”>“^”>“*”=“/”>“+”=“-”","优先级");
}
void CMyDlg::OnButton32()
{
str+="LN";
UpdateData(false);
}
void CMyDlg::OnButton33()
{
str+="LoG";
UpdateData(false);
}
void CMyDlg::OnButton28()
{
str+="Sin";
UpdateData(false);
}
void CMyDlg::OnButton29()
{
str+="Cos";
UpdateData(false);
}
void CMyDlg::OnButton30()
{
str+="Tag";
UpdateData(false);
}
void CMyDlg::OnButton31()
{
str+="Cot";
UpdateData(false);
}
void CMyDlg::OnButton27()
{
str+="^";
UpdateData(false);
}
//++++++++++++++++++++++++++++++++++++++++++++++++计算++++++++++++++++++++++++++++++++++++++++++++++++++
void CMyDlg::OnButton11() //计算
{
test=0;
CString st=str;//sum=strtod(str,&stop);
if(-1!=str.ReverseFind('='))
st=str.Right(str.GetLength()-str.ReverseFind('=')-1);
brack(st);//从括号开始计算
if(test==0){
False1(st);
str+="\r\n=";
str+=st;
sum=strtod(st,&stop);
}
UpdateData(false);
}
int CMyDlg::brack(CString & st)//判断和计算括号
{
if(st=="" || st=="."){
st="0";
return -1;}
int a=st.ReverseFind('(');
if(-1==a){
if(-1!=st.Find(')')){
False(st);
return -1;
}
count(st);
}
else{
int b=st.Find(')',a+1);
if(-1==b){
False(st);
return -1;
}
else{
CString st1=st.Mid(a+1,b-a-1);
if(st1=="" || st1=="."){
st1="0";
}
CString s=count(st1);
st.Replace(st.Mid(a,b-a+1),s);
brack(st);
}
}
return 0;
}
CString & CMyDlg::count(CString & s)//计算无括号表达式
{
countPi(s);//计算常数Pi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -