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

📄 tempdlg.cpp

📁 mfc做的计算器 界面简单 功能基本 初级作品
💻 CPP
字号:
// tempDlg.cpp : implementation file
//

#include "stdafx.h"
#include "temp.h"
#include "tempDlg.h"
#include <math.h>
#include <iostream.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()

/////////////////////////////////////////////////////////////////////////////
// CTempDlg dialog

CTempDlg::CTempDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTempDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTempDlg)
	m_display = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTempDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTempDlg)
	DDX_Control(pDX, IDC_sqrt, m_sqrt);
	DDX_Control(pDX, IDC_right, m_r);
	DDX_Control(pDX, IDC_percent, m_per);
	DDX_Control(pDX, IDC_mul, m_mul);
	DDX_Control(pDX, IDC_left, m_l);
	DDX_Control(pDX, IDC_equal, m_equal);
	DDX_Control(pDX, IDC_dot, m_dot);
	DDX_Control(pDX, IDC_div, m_div);
	DDX_Control(pDX, IDC_dec, m_dec);
	DDX_Control(pDX, IDC_ce, m_ce);
	DDX_Control(pDX, IDC_back, m_back);
	DDX_Control(pDX, IDC_anybyone, m_any);
	DDX_Control(pDX, IDC_add, m_add);
	DDX_Control(pDX, IDOK, m_IDOK);
	DDX_Control(pDX, IDC_9, m_B_9);
	DDX_Control(pDX, IDC_8, m_B_8);
	DDX_Control(pDX, IDC_7, m_B_7);
	DDX_Control(pDX, IDC_6, m_B_6);
	DDX_Control(pDX, IDC_0, m_B_0);
	DDX_Control(pDX, IDC_5, m_B_5);
	DDX_Control(pDX, IDC_4, m_B_4);
	DDX_Control(pDX, IDC_3, m_B_3);
	DDX_Control(pDX, IDC_2, m_B_2);
	DDX_Control(pDX, IDC_1, m_B_1);
	DDX_Text(pDX, IDC_EDIT, m_display);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTempDlg, CDialog)
	//{{AFX_MSG_MAP(CTempDlg)
	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_dec, Ondec)
	ON_BN_CLICKED(IDC_mul, Onmul)
	ON_BN_CLICKED(IDC_div, Ondiv)
	ON_BN_CLICKED(IDC_ce, Once)
	ON_BN_CLICKED(IDC_equal, Onequal)
	ON_WM_MOUSEMOVE()
	ON_BN_CLICKED(IDC_back, Onback)
	ON_BN_CLICKED(IDC_sqrt, Onsqrt)
	ON_BN_CLICKED(IDC_anybyone, Onanybyone)
	ON_BN_CLICKED(IDC_dot, Ondot)
	ON_BN_CLICKED(IDC_right, Onright)
	ON_BN_CLICKED(IDC_left, Onleft)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTempDlg message handlers

BOOL CTempDlg::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
	m_IDOK.SetBkColor(RGB(238,253,202));
	m_B_1.SetBkColor( RGB(238,253,202) );
	m_B_1.SetForeColor( RGB(255,0,0) );
	m_B_1.SetTextFont( 200, _T("leh") );
	m_B_2.SetBkColor( RGB(238,253,202) );
	m_B_2.SetForeColor( RGB(255,0,0) );
	m_B_2.SetTextFont( 200, _T("leh") );
	m_B_3.SetBkColor( RGB(238,253,202) );
	m_B_3.SetForeColor( RGB(255,0,0) );
	m_B_3.SetTextFont( 200, _T("leh") );
	m_B_4.SetBkColor( RGB(238,253,202) );
	m_B_4.SetForeColor( RGB(255,0,0) );
	m_B_4.SetTextFont( 200, _T("leh") );
	m_B_5.SetBkColor( RGB(238,253,202) );
	m_B_5.SetForeColor( RGB(255,0,0) );
	m_B_5.SetTextFont( 200, _T("leh") );
	m_B_6.SetBkColor( RGB(238,253,202) );
	m_B_6.SetForeColor( RGB(255,0,0) );
	m_B_6.SetTextFont( 200, _T("leh") );
	m_B_7.SetBkColor( RGB(238,253,202) );
	m_B_7.SetForeColor( RGB(255,0,0) );
	m_B_7.SetTextFont( 200, _T("leh") );
	m_B_8.SetBkColor( RGB(238,253,202) );
	m_B_8.SetForeColor( RGB(255,0,0) );
	m_B_8.SetTextFont( 200, _T("leh") );
	m_B_9.SetBkColor( RGB(238,253,202) );
	m_B_9.SetForeColor( RGB(255,0,0) );
	m_B_9.SetTextFont( 200, _T("leh") );
	m_B_0.SetBkColor( RGB(238,253,202) );
	m_B_0.SetForeColor( RGB(255,0,0) );
	m_B_0.SetTextFont( 200, _T("leh") );

	m_sqrt.SetBkColor( RGB(238,253,202) );
//	m_r.SetBkColor( RGB(238,253,202) );
	m_per.SetBkColor( RGB(238,253,202) );
	m_mul.SetBkColor( RGB(238,253,202) );
//	m_l.SetBkColor( RGB(238,253,202) );
	m_equal.SetBkColor( RGB(238,253,202) );
	m_dot.SetBkColor( RGB(238,253,202) );
	m_dot.SetForeColor( RGB(255,0,0) );
	m_dot.SetTextFont( 200, _T("leh") );
	m_div.SetBkColor( RGB(238,253,202) );
	m_dec.SetBkColor( RGB(238,253,202) );
	m_ce.SetBkColor( RGB(238,253,202) );
	m_back.SetBkColor( RGB(238,253,202) );
	m_any.SetBkColor( RGB(238,253,202) );
	m_add.SetBkColor( RGB(238,253,202) );

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTempDlg::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 CTempDlg::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();
	}
}
//	------------------ Compuer with +-*/ st -------------------------------
static char *expr;
static int pos;

double CTempDlg::addsubt(){
	
	double rtn = CTempDlg::multdiv();
	while (expr[pos] == '+' || expr[pos] == '-')
	{
		char op = expr[pos++];
		double opr2 = multdiv();
		if (op == '+')
			rtn += opr2;
		else
			rtn -= opr2;
	}
	return rtn;
}
//
double CTempDlg::multdiv(){
	
	double rtn = number();
	while (expr[pos] == '*' || expr[pos] == '/')
	{
		char op = expr[pos++];
		double opr2 = number();
		if (op == '*')
			rtn *= opr2;
		else{
			if (opr2 == 0)
				CTempDlg::ShowError();
			else
				rtn /= opr2;
		}
	}
	return rtn;
}
//
double CTempDlg::number(){
	
	double rtn;
	
	if (expr[pos] == '(')
	{
		// temp expression
		pos++;
		rtn = addsubt();			// return to top
		if (expr[pos++] != ')')		// must have ")"
			CTempDlg::ShowError();
		return rtn;
	}
	if (!isdigit(expr[pos]))
		CTempDlg::ShowError();
	rtn = atof(expr + pos);
	while (isdigit(expr[pos]))
		pos++;
	return rtn;
}
///////////////////////////////
// syntax error
///////////////////////////////
void CTempDlg::ShowError(){
	m_display = "Syntax error!";
	UpdateData(FALSE);
	m_display = "";
}
//	------------------ Compuer with +-*/ ed -------------------------------
int mode = 1;	//	1: commom compute
				//	2: formula compute

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTempDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTempDlg::On0() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display +"0";
	UpdateData(FALSE);
}

void CTempDlg::On1() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display +"1";
	UpdateData(FALSE);
}

void CTempDlg::On2() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display +"2";
	UpdateData(FALSE);
}

void CTempDlg::On3() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display +"3";
	UpdateData(FALSE);
}

void CTempDlg::On4() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display +"4";
	UpdateData(FALSE);
}

void CTempDlg::On5() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display +"5";
	UpdateData(FALSE);
}

void CTempDlg::On6() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display+"6";
	UpdateData(FALSE);
}

void CTempDlg::On7() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display +"7";
	UpdateData(FALSE);
}

void CTempDlg::On8() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display +"8";
	UpdateData(FALSE);
}

void CTempDlg::On9() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display +"9";
	UpdateData(FALSE);
}

void CTempDlg::Onadd() 
{
	// TODO: Add your control notification handler code here
	if (mode == 2){
		m_display=m_display + "+";
	}else{
		operater = atof(m_display);
		key = 1;
		m_display = "";
	}
	UpdateData(FALSE);
}

void CTempDlg::Ondec() 
{
	// TODO: Add your control notification handler code here
	if (mode == 2){
		m_display=m_display + "-";
	}else{
		operater = atof(m_display);
		key = 2;
		m_display = "";
	}
	UpdateData(FALSE);
}

void CTempDlg::Onmul() 
{
	// TODO: Add your control notification handler code here
	if (mode == 2){
		m_display=m_display +"*";
	}else{
		operater = atof(m_display);
		key = 3;
		m_display = "";
	}
	UpdateData(FALSE);
}

void CTempDlg::Ondiv() 
{
	// TODO: Add your control notification handler code here
	if (mode == 2){
		m_display=m_display +"/";
	}else{
		operater = atof(m_display);
		key = 4;
		m_display = "";
	}
	UpdateData(FALSE);
}

void CTempDlg::Once() 
{
	// TODO: Add your control notification handler code here
	m_display="0.";  
	UpdateData(FALSE);
	m_display="";
}

void CTempDlg::Onequal() 
{
	double result=0;
	CString strError;
	// TODO: Add your control notification handler code here
	if (mode == 2) {
		MessageBox("Compute with formula!","Information");
		pos = 0;
		expr = m_display.GetBuffer(m_display.GetLength());
		result = addsubt();
		if ( ( pos) != m_display.GetLength()){
			strError = "Syntax error!";
		}
		else{
			//result = addsubt();
			cout << result << endl;		//	compute result
		}
		m_display.ReleaseBuffer();
	}else{
		switch(key)  
		{
		case 1:
			result = operater + atof(m_display);
			break;
		case 2:
			result = operater - atof(m_display);
			break;
		case 3:
			result = operater * atof(m_display);
			break;
		case 4:
			if (atof(m_display) != 0)
			{
				result = operater / atof(m_display);
				break;
			}else{
				MessageBox("Div can't use zero!","Error message");
			}
			//return;
		}
	}
	if (strError == "Syntax error!") {
		m_display = strError;
	}else{
		char ch[50];
		memset(ch,0,50);
		sprintf(ch,"%10.2f",result);	//	wide is 10, behind . is 2
		m_display = ch;
	}
	UpdateData(FALSE);
	m_display = "";
}

void CTempDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//CRect rc(0,0,300,300);//帺屓巜掕嬫堟
	//ScreenToClient(&rc);
	//if(rc.PtInRect(point))
	//{
	//	CDC   *dc=GetDC();
	//	dc->TextOut(rc.left,rc.top,"採帵");
	//}
	CDialog::OnMouseMove(nFlags, point);
}

void CTempDlg::Onback() 
{
	// TODO: Add your control notification handler code here
	m_display="0.";  
	UpdateData(FALSE);
	m_display="";
}

void CTempDlg::Onsqrt()
{
	// TODO: Add your control notification handler code here
	double result;
	result = sqrt(atof(m_display));
	char ch[50];
	memset(ch,0,50);
	sprintf(ch,"%10.9f",result);	//	wide is 10, behind . is 2
	m_display = ch;
	UpdateData(FALSE);
//	m_display = "";
}

void CTempDlg::Onanybyone() 
{
	// TODO: Add your control notification handler code here
	double result;
	if (atof(m_display) != 0.0) {
		result = 1 / atof(m_display);
		char ch[50];
		memset(ch,0,50);
		sprintf(ch,"%10.2f",result);	//	wide is 10, behind . is 2
		m_display = ch;
		UpdateData(FALSE);
	}else{
		MessageBox("Div can't use zero!","Error message");
	}
}

void CTempDlg::Ondot() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display + ".";
	UpdateData(FALSE);
}
void CTempDlg::OnOK() 
{
	// TODO: Add extra validation here
	if (mode == 1) {
		CButton *pButton=(CButton*)GetDlgItem(IDC_right);
		pButton->EnableWindow(TRUE);
		CButton *pButtonL=(CButton*)GetDlgItem(IDC_left);
		pButtonL->EnableWindow(TRUE);
		m_l.SetBkColor( RGB(238,253,202) );
		m_r.SetBkColor( RGB(238,253,202) );
		mode = 2;
		GetDlgItem(IDOK)->SetWindowText("Common");
	}else{
		CButton *pButton=(CButton*)GetDlgItem(IDC_right);
		pButton->EnableWindow(FALSE);
		CButton *pButtonL=(CButton*)GetDlgItem(IDC_left);
		pButtonL->EnableWindow(FALSE);
		m_l.SetBkColor( RGB(153,153,153) );
		m_r.SetBkColor( RGB(153,153,153) );
		mode = 1;
		GetDlgItem(IDOK)->SetWindowText("Advance");
	}
	//CDialog::OnOK();
}

void CTempDlg::Onright() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display +"(";
	UpdateData(FALSE);
}

void CTempDlg::Onleft() 
{
	// TODO: Add your control notification handler code here
	m_display=m_display +")";
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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