📄 计算器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)
{
m_cLast='0'; //表示刚刚输入的数字或操作符
m_cLastOpr='='; //表示上次输入的操作符
m_nFraction=-1; //小数点后的数字位数,-1表示无小数点
m_nDigitLen=0; //所输入数的位数
m_dNumber=0; //正在输入的浮点数
m_nSign=1; //正负号
m_dOperands[0]=0; //操作数数组
m_nOprldx=-1; //操作符数组索引
m_result.Empty();
//{{AFX_DATA_INIT(CMyDlg)
m_result = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Text(pDX, IDC_EDIT1, 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, On7)
ON_BN_CLICKED(IDC_BUTTON2, On8)
ON_BN_CLICKED(IDC_BUTTON3, On9)
ON_BN_CLICKED(IDC_BUTTON4, Ondiv)
ON_BN_CLICKED(IDC_BUTTON5, On4)
ON_BN_CLICKED(IDC_BUTTON6, On5)
ON_BN_CLICKED(IDC_BUTTON7, On6)
ON_BN_CLICKED(IDC_BUTTON8, Onmul)
ON_BN_CLICKED(IDC_BUTTON9, On1)
ON_BN_CLICKED(IDC_BUTTON10, On2)
ON_BN_CLICKED(IDC_BUTTON11, On3)
ON_BN_CLICKED(IDC_BUTTON12, Onsub)
ON_BN_CLICKED(IDC_BUTTON13, On0)
ON_BN_CLICKED(IDC_BUTTON14, Onzf)
ON_BN_CLICKED(IDC_BUTTON15, Onpoint)
ON_BN_CLICKED(IDC_BUTTON16, Onadd)
ON_BN_CLICKED(IDC_BUTTON17, Onequal)
ON_BN_CLICKED(IDC_BUTTON18, OnBackspace)
ON_BN_CLICKED(IDC_BUTTON19, OnCE)
ON_BN_CLICKED(IDC_BUTTON20, Onsqrt)
ON_BN_CLICKED(IDC_BUTTON21, Onsquare)
ON_BN_CLICKED(IDC_BUTTON22, Oncb)
ON_BN_CLICKED(IDC_BUTTON23, Onpi)
ON_BN_CLICKED(IDC_BUTTON24, Onsin)
ON_BN_CLICKED(IDC_BUTTON25, Oncos)
ON_BN_CLICKED(IDC_BUTTON26, Ontan)
ON_BN_CLICKED(IDC_BUTTON27, Onln)
ON_BN_CLICKED(IDC_BUTTON28, Onlog)
ON_BN_CLICKED(IDC_BUTTON29, Onexp)
//}}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
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::Calculate()
{
ASSERT(m_nOprldx<=2&&m_nOprldx>=-1);
BYTE opr1,opr2;
while(m_nOprldx>0)
{
opr1=m_cOprs[m_nOprldx-1];
opr2=m_cOprs[m_nOprldx];
if(Priority(opr1)>=Priority(opr2))
{
Operate(m_dOperands[m_nOprldx-1],m_dOperands[m_nOprldx],opr1);
m_nOprldx--;
m_cOprs[m_nOprldx]=opr2;
}
else break;
}
}
int CMyDlg::Operate(double& d1,double d2,BYTE opr)
{ CString str;
switch(opr)
{
case '+':d1=d1+d2;break;
case '-':d1=d1-d2;break;
case '*':d1=d1*d2;break;
case '/':if(!d2) {printf("error!");return 0;}
else d1=d1/d2;
str.Format("%g",d1);
break;
default:return 0;
}
return 1;
}
int CMyDlg::Priority(BYTE opr)
{
switch(opr)
{
case '*':
case '/':return 2;
case '+':
case '-':return 1;
case '=':return 0;
default:return -1;
}
}
void CMyDlg::DealDigit(int n)
{
CString tempStr;
ASSERT(n>=-2&&n<=9);
if(m_nDigitLen>=16&&n!=-1) return;
if(n==-1) m_nSign*=-1;
else if(n==-2){
if(m_nFraction<0)
{
m_nFraction=0;
m_nDigitLen++;
m_result+='.';
}
}
else {
if(m_nFraction>=0) m_nFraction++;
if(!(m_nFraction<0&&n==0&&m_nDigitLen==0))
{
m_nDigitLen++;
if(m_nFraction>=0) m_dNumber+=n/pow(10,m_nFraction);
else m_dNumber=m_dNumber*10+n;
tempStr.Format("%d",n);
m_result+=tempStr;
}
m_cLast='0'+n;
}
if(m_nSign==-1) m_result='-'+m_result;
else if(m_result[0]=='-') m_result=m_result-'-';
UpdateData(FALSE); //显示结果
}
void CMyDlg::DealOpr(BYTE opr)
{
ASSERT(m_nOprldx>=-1&&m_nOprldx<2);
ASSERT(opr=='+'||opr=='-'||opr=='*'||opr=='/'||opr=='=');
if(m_nOprldx==0&&m_cOprs[0]=='=')
{
m_nOprldx=-1;
m_cLast='0';
m_dNumber=m_dOperands[0];
m_nSign=1;
}
m_nOprldx++;
m_cOprs[m_nOprldx]=opr;
m_dOperands[m_nOprldx]=m_dNumber*m_nSign;
m_dNumber=0;
m_nSign=1;
m_nDigitLen=0;
m_result.Empty();
Calculate();
CString strTmp,s;
strTmp.Format("%g",m_dOperands[m_nOprldx]);
s=m_result;
m_result=strTmp;
UpdateData(FALSE); //显示strTmp
m_result=s;
m_cLast=opr;
}
void CMyDlg::On7()
{
DealDigit(7);
}
void CMyDlg::On8()
{
DealDigit(8);
}
void CMyDlg::On9()
{
DealDigit(9);
}
void CMyDlg::Ondiv()
{
DealOpr('/');
}
void CMyDlg::On4()
{
DealDigit(4);
}
void CMyDlg::On5()
{
DealDigit(5);
}
void CMyDlg::On6()
{
DealDigit(6);
}
void CMyDlg::Onmul()
{
DealOpr('*');
}
void CMyDlg::On1()
{
DealDigit(1);
}
void CMyDlg::On2()
{
DealDigit(2);
}
void CMyDlg::On3()
{
DealDigit(3);
}
void CMyDlg::Onsub()
{
DealOpr('-');
}
void CMyDlg::On0()
{
DealDigit(0);
}
void CMyDlg::Onzf()
{
DealDigit(-1);
}
void CMyDlg::Onpoint()
{
DealDigit(-2);
}
void CMyDlg::Onadd()
{
DealOpr('+');
}
void CMyDlg::Onequal()
{
DealOpr('=');
}
void CMyDlg::OnBackspace()
{
if(!(m_nFraction<0&&m_nDigitLen==0))
{
m_nDigitLen--;
if(m_nFraction>=0){
m_dNumber=(int)(m_dNumber*pow(10,m_nFraction))/10;
m_dNumber=m_dNumber/pow(10,m_nFraction-1);
}
else m_dNumber=(int)m_dNumber/10;
m_result.Format("%g",m_dNumber);
m_nFraction--;
}
UpdateData(FALSE);
}
void CMyDlg::OnCE()
{ m_cLast='0';
m_cLastOpr='=';
m_nFraction=-1;
m_nDigitLen=0;
m_dNumber=0;
m_nSign=1;
m_dOperands[0]=0;
m_nOprldx=-1;
m_result='0';
UpdateData(FALSE);
m_result.Empty();
}
void CMyDlg::Onsqrt()
{
if(m_nSign) {
if(m_nOprldx==-1) {m_dNumber=sqrt(m_dNumber);m_dOperands[++m_nOprldx]=m_dNumber;}
else m_dOperands[m_nOprldx]=sqrt(m_dOperands[m_nOprldx]);
m_result.Format("%g",m_dOperands[m_nOprldx]);
UpdateData(FALSE);
}
}
void CMyDlg::Onsquare()
{ m_nSign=1;
if(m_nOprldx==-1) {m_dNumber*=m_dNumber;m_dOperands[++m_nOprldx]=m_dNumber;}
else m_dOperands[m_nOprldx]*=m_dOperands[m_nOprldx];
m_result.Format("%g",m_dOperands[m_nOprldx]);
UpdateData(FALSE);
}
void CMyDlg::Oncb()
{ if(m_nOprldx==-1) {m_dNumber=1/m_dNumber;m_dOperands[++m_nOprldx]=m_dNumber;}
else m_dOperands[m_nOprldx]=1/m_dOperands[m_nOprldx];
m_result.Format("%g",m_dOperands[m_nOprldx]);
UpdateData(FALSE);
}
void CMyDlg::Onpi()
{ m_dOperands[++m_nOprldx]=m_dNumber=3.14159;
m_result.Format("%g",m_dOperands[m_nOprldx]);
UpdateData(FALSE);
}
void CMyDlg::Onsin()
{ if(m_nOprldx==-1) {m_dNumber=sin(m_dNumber*3.1415926/180);m_dOperands[++m_nOprldx]=m_dNumber;}
else m_dOperands[m_nOprldx]=sin(m_dOperands[m_nOprldx]*3.1415926/180);
m_result.Format("%g",m_dOperands[m_nOprldx]);
UpdateData(FALSE);
}
void CMyDlg::Oncos()
{ if(m_nOprldx==-1) {m_dNumber=cos(m_dNumber*3.1415926/180);m_dOperands[++m_nOprldx]=m_dNumber;}
else m_dOperands[m_nOprldx]=cos(m_dOperands[m_nOprldx]*3.1415926/180);
m_result.Format("%g",m_dOperands[m_nOprldx]);
UpdateData(FALSE);
}
void CMyDlg::Ontan()
{ if(m_nOprldx==-1) {m_dNumber=tan(m_dNumber*3.1415926/180);m_dOperands[++m_nOprldx]=m_dNumber;}
else m_dOperands[m_nOprldx]=tan(m_dOperands[m_nOprldx]*3.1415926/180);
m_result.Format("%g",m_dOperands[m_nOprldx]);
UpdateData(FALSE);
}
void CMyDlg::Onln()
{ if(m_nSign) {
if(m_nOprldx==-1) {m_dNumber=log(m_dNumber);m_dOperands[++m_nOprldx]=m_dNumber;}
else m_dOperands[m_nOprldx]=log(m_dOperands[m_nOprldx]);
m_result.Format("%g",m_dOperands[m_nOprldx]);
UpdateData(FALSE);
}
}
void CMyDlg::Onlog()
{ if(m_nSign) {
if(m_nOprldx==-1) {m_dNumber=log10(m_dNumber);m_dOperands[++m_nOprldx]=m_dNumber;}
else m_dOperands[m_nOprldx]=log10(m_dOperands[m_nOprldx]);
m_result.Format("%g",m_dOperands[m_nOprldx]);
UpdateData(FALSE);
}
}
void CMyDlg::Onexp()
{ if(m_nOprldx==-1) {m_dNumber=exp(m_dNumber);m_dOperands[++m_nOprldx]=m_dNumber;}
else m_dOperands[m_nOprldx]=exp(m_dOperands[m_nOprldx]);
m_result.Format("%g",m_dOperands[m_nOprldx]);
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -