⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculatordlg.cpp

📁 计算器程序
💻 CPP
字号:
// CalculatorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg dialog

CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)   
	: CDialog(CCalculatorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCalculatorDlg)
	m_one = _T("");   
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);//初始化下面程序用到的变量
   two = 0;
	one = 0;
	xz = 0;
	append = 0;
	dian = 0;
	base = 10;
	str= _T("");
}

void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalculatorDlg)
     DDX_Text(pDX, IDC_EDIT1, m_one);
   	DDX_Control(pDX, IDC_LIST1, m_list);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)//BUTTON的定义
	//{{AFX_MSG_MAP(CCalculatorDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnOff)
	ON_BN_CLICKED(IDC_BUTTON2, OnCe)
	ON_BN_CLICKED(IDC_BUTTON3, OnBackspace)
	ON_BN_CLICKED(IDC_BUTTON4, On7)
	ON_BN_CLICKED(IDC_BUTTON5, On8)
	ON_BN_CLICKED(IDC_BUTTON6, On9)
	ON_BN_CLICKED(IDC_BUTTON7, On4)
	ON_BN_CLICKED(IDC_BUTTON8, On5)
	ON_BN_CLICKED(IDC_BUTTON9, On6)
	ON_BN_CLICKED(IDC_BUTTON10, On1)
	ON_BN_CLICKED(IDC_BUTTON11, On2)
	ON_BN_CLICKED(IDC_BUTTON12, On3)
	ON_BN_CLICKED(IDC_BUTTON13, On0)
	ON_BN_CLICKED(IDC_BUTTON14, OnDot)
	ON_BN_CLICKED(IDC_BUTTON16, OnAdd)
	ON_BN_CLICKED(IDC_BUTTON18, OnMin)
	ON_BN_CLICKED(IDC_BUTTON20, OnMul)
	ON_BN_CLICKED(IDC_BUTTON23, OnDiv)
	ON_BN_CLICKED(IDC_BUTTON24, OnOut)
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg message handlers

BOOL CCalculatorDlg::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 CCalculatorDlg::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 CCalculatorDlg::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 CCalculatorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCalculatorDlg::OnNum(int j)// 输入数处理
{  

    double a=1;
	int i=0;
	if(append==1) one = 0;
	if(dian >= 1)       //输入数为小数
	{
	    for(i=0;i<dian;i++) a=a/base;
		one = j*a+one;
		dian++;
	}
	else                 //输入数为整数
	one = one*base+j;
	UpdateData(FALSE);
append=0;
}

void CCalculatorDlg::OnOff() //退出程序键
{
	// TODO: Add your control notification handler code here
	AfxMessageBox("确定退出计算器?");
	OnOK();
}

void CCalculatorDlg::OnCe() //CLEAR清0键
{
	// TODO: Add your control notification handler code here
    str=_T("");
	mo=_T("");
	one=0;
	two = 0;
	dian = 0;
	append = 0;
	m_one="0";
	UpdateData(FALSE);	
}

void CCalculatorDlg::OnBackspace() //退格键
{
	// TODO: Add your control notification handler code here
   	double a=1,b=10;
	int i;
	if(append==1) one = 0;
	if(dian >= 1)           
	{
		for(i=0;i<dian-1;i++) {a*=10;b*=0.1;}
		one = one*a;
        one = (int)one/10;
        one = one*b;
		dian--;
	}
	else 
	one = (int)one/10;     
	m_one=m_one.Left(m_one.GetLength()-1);
	append = 0;
	UpdateData(FALSE);
}

void CCalculatorDlg::On7() //输入7键
{
	// TODO: Add your control notification handler code here
if(m_one=="0")m_one="7";
	else m_one=m_one+"7";
	OnNum(7);
	append = 0;
	UpdateData(FALSE);	
}

void CCalculatorDlg::On8() //输入8键
{
if(m_one=="0")m_one="8";
	else m_one=m_one+"8";
	OnNum(8);
	append = 0;
	UpdateData(FALSE);
	
}

void CCalculatorDlg::On9() //输入9键
{
	// TODO: Add your control notification handler code here
if(m_one=="0")m_one="9";
	else m_one=m_one+"9";
	OnNum(9);
	append = 0;
	UpdateData(FALSE);	
}

void CCalculatorDlg::On4() //输入4键
{
	// TODO: Add your control notification handler code here
    if(m_one=="0")m_one="4";
	else m_one=m_one+"4";
	OnNum(4);
	append = 0;
	UpdateData(FALSE);	
}

void CCalculatorDlg::On5() //输入5键
{
	// TODO: Add your control notification handler code here
if(m_one=="0")m_one="5";
	else m_one=m_one+"5";
	OnNum(5);
	append = 0;
	UpdateData(FALSE);	
}

void CCalculatorDlg::On6() //输入6键
{
	// TODO: Add your control notification handler code here
if(m_one=="0")m_one="6";
	else m_one=m_one+"6";
	OnNum(6);
	append = 0;
	UpdateData(FALSE);	
}

void CCalculatorDlg::On1() //输入1健
{
	// TODO: Add your control notification handler code here
    if(m_one=="0")m_one="1";
	else m_one=m_one+"1";
	OnNum(1);
	append = 0;
	UpdateData(FALSE);	
}

void CCalculatorDlg::On2() //输入2键
{
	// TODO: Add your control notification handler code here
if(m_one=="0")m_one="2";
	else m_one=m_one+"2";
	OnNum(2);
	append = 0;
	UpdateData(FALSE);	
}

void CCalculatorDlg::On3() //输入3健
{
	// TODO: Add your control notification handler code here
	if(m_one=="0")m_one="3";
	else m_one=m_one+"3";
	OnNum(3);
	append = 0;
	UpdateData(FALSE);
}

void CCalculatorDlg::On0() //输入0键
{
	// TODO: Add your control notification handler code here
    if(m_one=="0")m_one="0";
	else m_one=m_one+"0";
	OnNum(0);
	append = 0;
	UpdateData(FALSE);	
}

void CCalculatorDlg::OnDot() //输入小数点
{
	// TODO: Add your control notification handler code here
m_one=m_one+".";
	if(dian == 0)
	{
		dian = 1;
	UpdateData(FALSE);
	}	
}



void CCalculatorDlg::OnAdd() //加法运算
{
	// TODO: Add your control notification handler code here
    xz=1;
	two=one;
	s=m_one+"+";
	m_one ="0";
	append=1;
	dian=0;	
}

void CCalculatorDlg::OnMin() //减法运算
{
	// TODO: Add your control notification handler code here
   	xz=2;
	two=one;
	s=m_one+"-";
	m_one="0";
	append=1;
	dian=0;
	one=0;
}

void CCalculatorDlg::OnMul() //乘法运算
{
	// TODO: Add your control notification handler code here
	xz=3;
	two=one;
	s=m_one+"*";
	m_one="0";
	append=1;
	dian = 0;
}

void CCalculatorDlg::OnDiv() //除法运算
{
	// TODO: Add your control notification handler code here
	xz=4;
	two=one;
	s=m_one+"/";
	m_one="0";
	append=1;
	dian=0;
} 

void CCalculatorDlg::OnOut() //‘=’号输入
{
	// TODO: Add your control notification handler code here
	switch(xz)
	{
	case 1:one=one+two; break;
	case 2:one=two-one; break;
	case 3:one=one*two; break;
	case 4:if(one==0) {MessageBox("0不能作为除数!!");break;}
		   one=two/one; break;
	case 5:one=pow(two,one); break;
	case 6:one=sqrt(two); break;
	case 7:int a,b;               
	       a=(int)one;
	       b=(int)two;
	       one=(double)(b%a); break;
}
if(base==10)
	{
	if(xz>=1&&xz<=7)             //显示点击数值历史记录
	{
	str.Format(_T("%f"), one);
	s = s+m_one+"="+str;
	m_list.InsertString(0,s);
	s = _T("");
	}
    if(xz>=8&&xz<=19)
	{
	str.Format(_T("%f"), one);
	s = s+"="+str;
	m_list.InsertString(0,s);
	s = _T("");
	}
	m_one.Format(_T("%f"), one);
    UpdateData(FALSE);
	} 
}

void CCalculatorDlg::OnChangeEdit1() //显示输出点击的按钮数字函数 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CCalculatorDlg::OnSelchangeList1() //显示输出整个式子函数
{
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -