testdlg.cpp

来自「本代码用C++实现简易计算器的功能」· C++ 代码 · 共 616 行

CPP
616
字号
// testDlg.cpp : implementation file
//

#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#include "math.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()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog

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

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDlg)
	DDX_Text(pDX, IDC_see, m_see);
	DDV_MaxChars(pDX, m_see, 1999999999);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON0, OnButton0)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON_pi, OnBUTTON_pi)
	ON_BN_CLICKED(IDC_BUTTON_e, OnBUTTON_e)
	ON_BN_CLICKED(IDC_BUTTON_add, OnBUTTON_add)
	ON_BN_CLICKED(IDC_BUTTON_mul, OnBUTTON_mul)
	ON_BN_CLICKED(IDC_BUTTON_pow, OnBUTTON_pow)
	ON_BN_CLICKED(IDC_BUTTON_float, OnBUTTON_float)
	ON_BN_CLICKED(IDC_BUTTON_sub, OnBUTTON_sub)
	ON_BN_CLICKED(IDC_BUTTON_div, OnBUTTON_div)
	ON_BN_CLICKED(IDC_BUTTON_mod, OnBUTTON_mod)
	ON_BN_CLICKED(IDC_BUTTON_is, OnBUTTON_is)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_BUTTON_n_p, OnBUTTON_n_p)
	ON_BN_CLICKED(IDC_BUTTON_reset, OnBUTTON_reset)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers

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

/******************************************************************
*****************以下为自行编写的代码*****************************
*******************************************************************/
double num=0,s=0;	//存放操作数和结果
int temp=0;			//进行何种运算
int float_flag=0;	//标志有没有小数点按下
int float_n=0;		//小数点后第几位
int n_p_flag=0;		//0正,1负
int f=0;

void CTestDlg::OnBUTTON_n_p() 
{
	if(n_p_flag==1)
	{
		n_p_flag=0;
	}
	else n_p_flag=1;
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}

}

void CTestDlg::OnButton0() 
{
	if(f==0)
	if(float_flag==0)
	{
		num=num*10;
	}
	else{ float_n--;}
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}
}

void CTestDlg::OnButton1() 
{
	if(f==0)
	if(float_flag==0)
	{
		num=num*10+1;
	}
	else 
	{
		float_n--;
		num=num+1*pow(10,float_n);
	}
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}
}


void CTestDlg::OnButton2() 
{
		if(f==0)
	if(float_flag==0)
	{
		num=num*10+2;
	}
	else 
	{
		float_n--;
		num=num+2*pow(10,float_n);
	}
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}	
}

void CTestDlg::OnButton3() 
{
		if(f==0)
	if(float_flag==0)
	{
		num=num*10+3;
	}
	else 
	{
		float_n--;
		num=num+3*pow(10,float_n);
	}
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}	
}

void CTestDlg::OnButton4() 
{
		if(f==0)
	if(float_flag==0)
	{
		num=num*10+4;
	}
	else 
	{
		float_n--;
		num=num+4*pow(10,float_n);
	}
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}
}
void CTestDlg::OnButton5() 
{
		if(f==0)
	if(float_flag==0)
	{
		num=num*10+5;
	}
	else 
	{
		float_n--;
		num=num+5*pow(10,float_n);
	}
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}
}


void CTestDlg::OnButton6() 
{
		if(f==0)
	if(float_flag==0)
	{
		num=num*10+6;
	}
	else 
	{
		float_n--;
		num=num+6*pow(10,float_n);
	}
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}
}

void CTestDlg::OnButton7() 
{
		if(f==0)
	if(float_flag==0)
	{
		num=num*10+7;
	}
	else 
	{
		float_n--;
		num=num+7*pow(10,float_n);
	}
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}
}

void CTestDlg::OnButton8() 
{
		if(f==0)
	if(float_flag==0)
	{
		num=num*10+8;
	}
	else 
	{
		float_n--;
		num=num+8*pow(10,float_n);
	}
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}
}

void CTestDlg::OnButton9() 
{
		if(f==0)
	if(float_flag==0)
	{
		num=num*10+9;
	}
	else 
	{
		float_n--;
		num=num+9*pow(10,float_n);
	}
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}
}

void CTestDlg::OnBUTTON_float() 
{
	if(float_flag==0)float_flag=1;
}

void CTestDlg::OnBUTTON_pi() 
{
	num=3.1415926535;
	float_flag=0;
	f=1;
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}
}

void CTestDlg::OnBUTTON_e() 
{
	num=2.71828;
	float_flag=0;
	f=1;
	if(n_p_flag==0)
	{
			m_see.Format("%6.8f",num);
			UpdateData(FALSE);
	}
	else
	{		m_see.Format("%6.8f",0-num);
			UpdateData(FALSE);
	}
}

void CTestDlg::OnBUTTON_add() 
{
	f=0;
	float_n=0;
	float_flag=0;
	if(n_p_flag==1)num=0-num;
	s=num;
	num=0;
	temp=1;//加
	n_p_flag=0;
}

void CTestDlg::OnBUTTON_sub() 
{
	float_n=0;
	float_flag=0;
	f=0;
	if(n_p_flag==1)num=0-num;
	s=num;
	num=0;
	temp=2;//减
	n_p_flag=0;	
}

void CTestDlg::OnBUTTON_mul() 
{
	float_n=0;
	float_flag=0;
	f=0;
	if(n_p_flag==1)num=0-num;
	s=num;
	num=0;
	temp=3;//乘	
	n_p_flag=0;
}

void CTestDlg::OnBUTTON_div() 
{
	float_n=0;
	float_flag=0;
	f=0;
	if(n_p_flag==1)num=0-num;
	s=num;
	num=0;
	temp=4;//除
	n_p_flag=0;
}


void CTestDlg::OnBUTTON_pow() 
{
	float_n=0;
	float_flag=0;
	f=0;
	if(n_p_flag==1)num=0-num;
	s=num;
	num=0;
	temp=5;//乘方
	n_p_flag=0;
}

void CTestDlg::OnBUTTON_mod() 
{
	float_n=0;
	float_flag=0;
	f=0;
	if(n_p_flag==1)num=0-num;
	s=num;
	num=0;
	temp=6;//求余
	n_p_flag=0;
}

void CTestDlg::OnBUTTON_is() 
{
	float_n=0;
	float_flag=0;
	f=1;
	if(n_p_flag==1)num=0-num;
	if(temp==1)
	{
		s=s+num;
	}
	else if(temp==2)
	{
		s=s-num;
	}
	else if(temp==3)
	{
		s=s*num;
	}
	else if(temp==4)
	{
		s=s/num;
	}
	else if(temp==5)
	{
		s=pow(s,num);
	}
	else if(temp==6)
	{
		s=(int)s%(int)num;
	}
	temp=0;
	num=s;
	n_p_flag=0;
	m_see.Format("%6.8f",num);
	UpdateData(FALSE);

}




void CTestDlg::OnBUTTON_reset() 
{
	num=0;
	s=0;	//存放操作数和结果
	temp=0;			//进行何种运算
	float_flag=0;	//标志有没有小数点按下
	float_n=0;		//小数点后第几位
	n_p_flag=0;		//0正,1负
	f=0;
	m_see.Format("%6.8f",num);
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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