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

📄 计算器dlg.cpp

📁 一个包含十进制和十六进制的计算器
💻 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)
{
	base=10;
	//{{AFX_DATA_INIT(CMyDlg)
	//}}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_Control(pDX, IDC_divide, m_divide);
	DDX_Control(pDX, IDC_tan, m_tan);
	DDX_Control(pDX, IDC_sin, m_sin);
	DDX_Control(pDX, IDC_log, m_log);
	DDX_Control(pDX, IDC_exp, m_exp);
	DDX_Control(pDX, IDC_cos, m_cos);
	DDX_Control(pDX, IDC_point, m_point);
	DDX_Control(pDX, IDC_EDIT1, m_edit);
	DDX_Control(pDX, IDC_F, m_F);
	DDX_Control(pDX, IDC_E, m_E);
	DDX_Control(pDX, IDC_D, m_D);
	DDX_Control(pDX, IDC_C, m_C);
	DDX_Control(pDX, IDC_B, m_B);
	DDX_Control(pDX, IDC_A, m_A);
	DDX_Control(pDX, IDC_9, m_9);
	DDX_Control(pDX, IDC_8, m_8);
	DDX_Control(pDX, IDC_7, m_7);
	DDX_Control(pDX, IDC_6, m_6);
	DDX_Control(pDX, IDC_5, m_5);
	DDX_Control(pDX, IDC_4, m_4);
	DDX_Control(pDX, IDC_3, m_3);
	DDX_Control(pDX, IDC_2, m_2);
	DDX_Control(pDX, IDC_1, m_1);
	DDX_Control(pDX, IDC_0, m_0);
	//}}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_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	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_A, OnA)
	ON_BN_CLICKED(IDC_B, OnB)
	ON_BN_CLICKED(IDC_C, OnC)
	ON_BN_CLICKED(IDC_D, OnD)
	ON_BN_CLICKED(IDC_E, OnE)
	ON_BN_CLICKED(IDC_F, OnF)
	ON_BN_CLICKED(IDC_point, Onpoint)
	ON_BN_CLICKED(IDC_sign, Onsign)
	ON_BN_CLICKED(IDC_plus, Onplus)
	ON_BN_CLICKED(IDC_minus, Onminus)
	ON_BN_CLICKED(IDC_multiply, Onmultiply)
	ON_BN_CLICKED(IDC_divide, Ondivide)
	ON_BN_CLICKED(IDC_log, Onlog)
	ON_BN_CLICKED(IDC_xy, Onxy)
	ON_BN_CLICKED(IDC_backspace, Onbackspace)
	ON_BN_CLICKED(IDC_CE, OnCe)
	ON_BN_CLICKED(IDC_equal, Onequal)
	ON_BN_CLICKED(IDC_sin, Onsin)
	ON_BN_CLICKED(IDC_cos, Oncos)
	ON_BN_CLICKED(IDC_tan, Ontan)
	ON_BN_CLICKED(IDC_exp, Onexp)
	ON_BN_CLICKED(IDC_factorial, Onfactorial)
	ON_BN_CLICKED(IDC_0, On0)
	//}}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::OnRadio1() 
{
	// TODO: Add your control notification handler code here
	base=10;
	m_1.EnableWindow(TRUE);
	m_2.EnableWindow(TRUE);
	m_3.EnableWindow(TRUE);
	m_4.EnableWindow(TRUE);
	m_5.EnableWindow(TRUE);
	m_6.EnableWindow(TRUE);
	m_7.EnableWindow(TRUE);
	m_8.EnableWindow(TRUE);
	m_9.EnableWindow(TRUE);
	m_0.EnableWindow(TRUE);
	m_A.EnableWindow(FALSE);
	m_B.EnableWindow(FALSE);
	m_C.EnableWindow(FALSE);
	m_D.EnableWindow(FALSE);
	m_E.EnableWindow(FALSE);
	m_F.EnableWindow(FALSE);
	m_point.EnableWindow(TRUE);
	m_divide.EnableWindow(TRUE);
	m_sin.EnableWindow(TRUE);
	m_cos.EnableWindow(TRUE);
    m_tan.EnableWindow(TRUE);
    m_log.EnableWindow(TRUE);
	m_exp.EnableWindow(TRUE);
	

}

void CMyDlg::OnRadio2() 
{
	// TODO: Add your control notification handler code here
	base=16;
	m_1.EnableWindow(TRUE);
	m_2.EnableWindow(TRUE);
	m_3.EnableWindow(TRUE);
	m_4.EnableWindow(TRUE);
	m_5.EnableWindow(TRUE);
	m_6.EnableWindow(TRUE);
	m_7.EnableWindow(TRUE);
	m_8.EnableWindow(TRUE);
	m_9.EnableWindow(TRUE);
	m_0.EnableWindow(TRUE);
	m_A.EnableWindow(FALSE);
	m_B.EnableWindow(FALSE);
	m_C.EnableWindow(FALSE);
	m_D.EnableWindow(FALSE);
	m_E.EnableWindow(FALSE);
	m_F.EnableWindow(FALSE);
	m_point.EnableWindow(FALSE);
	m_divide.EnableWindow(FALSE);
	m_sin.EnableWindow(FALSE);
	m_cos.EnableWindow(FALSE);
    m_tan.EnableWindow(FALSE);
    m_log.EnableWindow(FALSE);
	m_exp.EnableWindow(FALSE);

	
}

void CMyDlg::On1() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "1";
	m_edit.SetWindowText(m_string);
	currentnum = atof(m_string);
	
}

void CMyDlg::On2() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "2";
	m_edit.SetWindowText(m_string);
	currentnum = atof(m_string);
	
}

void CMyDlg::On3() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "3";
	m_edit.SetWindowText(m_string);
	currentnum = atof(m_string);
	
}

void CMyDlg::On4() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "4";
	m_edit.SetWindowText(m_string);
	currentnum = atof(m_string);
	
}

void CMyDlg::On5() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "5";
	m_edit.SetWindowText(m_string);
	currentnum = atof(m_string);
	
}

void CMyDlg::On6() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "6";
	m_edit.SetWindowText(m_string);
	currentnum = atof(m_string);
	
}

void CMyDlg::On7() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "7";
	m_edit.SetWindowText(m_string);
	currentnum = atof(m_string);
	
}

void CMyDlg::On8() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "8";
	m_edit.SetWindowText(m_string);
	currentnum = atof(m_string);
	
}

void CMyDlg::On9() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "9";
	m_edit.SetWindowText(m_string);
	currentnum = atof(m_string);
	
}

void CMyDlg::OnA() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "A";
	m_edit.SetWindowText(m_string);
	m_string.Format("%lX", m_string);
	m_string.Format("%ld", m_string);
	currentnum = atof(m_string);
	
}

void CMyDlg::OnB() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "B";
	m_edit.SetWindowText(m_string);
	m_string.Format("%lX", m_string);
	m_string.Format("%ld", m_string);
	currentnum = atof(m_string);
	
	
}

void CMyDlg::OnC() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "C";
	m_edit.SetWindowText(m_string);
	m_string.Format("%lX", m_string);
	m_string.Format("%ld", m_string);
	currentnum = atof(m_string);
	
	
}

void CMyDlg::OnD() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "D";
	m_edit.SetWindowText(m_string);
	m_string.Format("%lX", m_string);
	m_string.Format("%ld", m_string);
	currentnum = atof(m_string);
	
	
}

void CMyDlg::OnE() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "E";
	m_edit.SetWindowText(m_string);
	m_string.Format("%lX", m_string);
	m_string.Format("%ld", m_string);
	currentnum = atof(m_string);
	
	
}

void CMyDlg::OnF() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "F";
	m_edit.SetWindowText(m_string);
	m_string.Format("%lX", m_string);
	m_string.Format("%ld", m_string);
	currentnum = atof(m_string);
	
	
}

void CMyDlg::Onpoint() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + ".";
	m_edit.SetWindowText(m_string);
	
}

void CMyDlg::Onsign() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "-";
	m_edit.SetWindowText(m_string);
	
}

void CMyDlg::Onplus() 
{
	// TODO: Add your control notification handler code here
	num1=currentnum;
    controlchar='+';
	m_string=_T("");
	currentnum=0;

	
}

void CMyDlg::Onminus() 
{
	// TODO: Add your control notification handler code here
	num1=currentnum;
    controlchar='-';
	m_string=_T("");
	currentnum=0;
	
}

void CMyDlg::Onmultiply() 
{
	// TODO: Add your control notification handler code here
	num1=currentnum;
    controlchar='*';
	m_string=_T("");
	currentnum=0;
	
}

void CMyDlg::Ondivide() 
{
	// TODO: Add your control notification handler code here
	num1=currentnum;
    controlchar='/';
	m_string=_T("");
	currentnum=0;
	
}

void CMyDlg::Onlog() 
{
	// TODO: Add your control notification handler code here
	num1=currentnum;
    controlchar='l';
	m_string=_T("");
	currentnum=0;
	
}

void CMyDlg::Onxy() 
{
	// TODO: Add your control notification handler code here
	num1=currentnum;
    controlchar='^';
	m_string=_T("");
	currentnum=0;
	
}

void CMyDlg::Onbackspace() 
{
	// TODO: Add your control notification handler code here
	m_string=m_string.Left(m_string.GetLength()-1);
	m_edit.SetWindowText(m_string);

	
}

void CMyDlg::OnCe() 
{
	// TODO: Add your control notification handler code here
	m_string=_T("");
	currentnum=0;
	controlchar='0';
	m_edit.SetWindowText(m_string);
	
}

void CMyDlg::Onequal() 
{
	// TODO: Add your control notification handler code here
	num2=currentnum;
	switch(controlchar)
	{
	case '0':
		num1=currentnum;
		break;
	case '+':
		num1=num1+num2;
		break;
	case '-':
		num1=num1-num2;
		break;
	case '*':
		num1=num1*num2;
		break;
	case '/':
		num1=num1/num2;
		break;
	case 'l':
		num1=log(num1)/log(num2);
		break;
	case '^':
		num1=pow(num1, num2);
		break;
	}

	if(base==10)
	{
		m_string.Format("%f", num1);
	    m_edit.SetWindowText(m_string);
	}

	if(base==16)
	{
		m_string.Format("%lX", (long)num1);
	    m_edit.SetWindowText(m_string);
	}

	currentnum =num1;
	controlchar='0';
	m_string = _T("");

}

void CMyDlg::Onsin() 
{
	// TODO: Add your control notification handler code here
	currentnum=sin(currentnum);
	m_string.Format("%f", currentnum);
	m_edit.SetWindowText(m_string);
	m_string=_T("");

	
}

void CMyDlg::Oncos() 
{
	// TODO: Add your control notification handler code here
	currentnum=cos(currentnum);
	m_string.Format("%f", currentnum);
	m_edit.SetWindowText(m_string);
	m_string=_T("");
	
}

void CMyDlg::Ontan() 
{
	// TODO: Add your control notification handler code here
	currentnum=tan(currentnum);
	m_string.Format("%f", currentnum);
	m_edit.SetWindowText(m_string);
	m_string=_T("");
	
}

void CMyDlg::Onexp() 
{
	// TODO: Add your control notification handler code here
	currentnum=exp(currentnum);
	m_string.Format("%f", currentnum);
	m_edit.SetWindowText(m_string);
	m_string=_T("");
	
}

long CMyDlg::factorial(long num)
{
	if(num<=1)
		return 1;
	else
		return (num * factorial(num-1));
}

void CMyDlg::Onfactorial() 
{
	// TODO: Add your control notification handler code here
	currentnum=(double)factorial((long)currentnum);
	if(base==10)
	{
		m_string.Format("%f", currentnum);
	    m_edit.SetWindowText(m_string);
	}
	if(base==16)
	{
		m_string.Format("%lX", (long)currentnum);
	    m_edit.SetWindowText(m_string);
	}
	 m_string=_T("");
}

void CMyDlg::On0() 
{
	// TODO: Add your control notification handler code here
	m_string = m_string + "0";
	m_edit.SetWindowText(m_string);
	currentnum = atof(m_string);
	
	
}

⌨️ 快捷键说明

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