VC简易计算器实现

时间:2023-01-09 21:23:37

首先,新建对话框应用程序,并且在界面上添加相应的控件

VC简易计算器实现


添加相关变量和控件的处理

头文件:

class CJisuanqiDlg : public CDialog
{
// Construction
public:
	double m_dbnum;
	bool m_bFlag;
	double m_dbZ;
	double m_dbY;
	int m_nType;
	double m_dbX;
	CJisuanqiDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	//{{AFX_DATA(CJisuanqiDlg)
	enum { IDD = IDD_JISUANQI_DIALOG };
	CButton	m_c;
	CButton	m_back;
	CButton	m_9;
	CButton	m_8;
	CButton	m_7;
	CButton	m_6;
	CButton	m_5;
	CButton	m_4;
	CButton	m_3;
	CButton	m_2;
	CButton	m_1;
	CButton	m_0;
	CButton	m_jian;
	CButton	m_jia;
	CButton	m_dian;
	CButton	m_deng;
	CButton	m_chu;
	CButton	m_cheng;
	CString	m_number;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CJisuanqiDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	//{{AFX_MSG(CJisuanqiDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnBDeng();
	afx_msg void OnBJia();
	afx_msg void OnBJian();
	afx_msg void OnBCheng();
	afx_msg void OnBChu();
	afx_msg void OnBDian();
	afx_msg void OnB0();
	afx_msg void OnB1();
	afx_msg void OnB2();
	afx_msg void OnB3();
	afx_msg void OnB4();
	afx_msg void OnB5();
	afx_msg void OnB6();
	afx_msg void OnB7();
	afx_msg void OnB8();
	afx_msg void OnB9();
	afx_msg void OnBack();
	afx_msg void OnC();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

源文件:

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

void CJisuanqiDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CJisuanqiDlg)
	DDX_Control(pDX, IDC_C, m_c);
	DDX_Control(pDX, IDC_BACK, m_back);
	DDX_Control(pDX, IDC_B9, m_9);
	DDX_Control(pDX, IDC_B8, m_8);
	DDX_Control(pDX, IDC_B7, m_7);
	DDX_Control(pDX, IDC_B6, m_6);
	DDX_Control(pDX, IDC_B5, m_5);
	DDX_Control(pDX, IDC_B4, m_4);
	DDX_Control(pDX, IDC_B3, m_3);
	DDX_Control(pDX, IDC_B2, m_2);
	DDX_Control(pDX, IDC_B1, m_1);
	DDX_Control(pDX, IDC_B0, m_0);
	DDX_Control(pDX, IDC_B_JIAN, m_jian);
	DDX_Control(pDX, IDC_B_JIA, m_jia);
	DDX_Control(pDX, IDC_B_DIAN, m_dian);
	DDX_Control(pDX, IDC_B_DENG, m_deng);
	DDX_Control(pDX, IDC_B_CHU, m_chu);
	DDX_Control(pDX, IDC_B_CHENG, m_cheng);
	DDX_Text(pDX, IDC_STATIC_number, m_number);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CJisuanqiDlg, CDialog)
	//{{AFX_MSG_MAP(CJisuanqiDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_B_DENG, OnBDeng)
	ON_BN_CLICKED(IDC_B_JIA, OnBJia)
	ON_BN_CLICKED(IDC_B_JIAN, OnBJian)
	ON_BN_CLICKED(IDC_B_CHENG, OnBCheng)
	ON_BN_CLICKED(IDC_B_CHU, OnBChu)
	ON_BN_CLICKED(IDC_B_DIAN, OnBDian)
	ON_BN_CLICKED(IDC_B0, OnB0)
	ON_BN_CLICKED(IDC_B1, OnB1)
	ON_BN_CLICKED(IDC_B2, OnB2)
	ON_BN_CLICKED(IDC_B3, OnB3)
	ON_BN_CLICKED(IDC_B4, OnB4)
	ON_BN_CLICKED(IDC_B5, OnB5)
	ON_BN_CLICKED(IDC_B6, OnB6)
	ON_BN_CLICKED(IDC_B7, OnB7)
	ON_BN_CLICKED(IDC_B8, OnB8)
	ON_BN_CLICKED(IDC_B9, OnB9)
	ON_BN_CLICKED(IDC_BACK, OnBack)
	ON_BN_CLICKED(IDC_C, OnC)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJisuanqiDlg message handlers

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

void CJisuanqiDlg::OnBDeng() 
{
	UpdateData(TRUE);
	m_dbY = strtod(m_number,NULL);//整型。             strtoul(m_number,NULL,10)浮点

	if(0 == m_nType)
	{
		m_dbZ = m_dbX + m_dbY;
		
	}
	else if(1 == m_nType)
	{
		m_dbZ = m_dbX - m_dbY;
	}
	else if(2 == m_nType)
	{
		m_dbZ = m_dbX * m_dbY;
	}
	else if(3 == m_nType)
	{
		m_dbZ = m_dbX / m_dbY;
	}

	CString str = "";
	str.Format("%f",m_dbZ);
	str.TrimRight("0");
	str.TrimRight(".");
	m_number = str;
	UpdateData(FALSE);

	m_bFlag = true;
}

void CJisuanqiDlg::OnBJia() 
{
	m_nType = 0;
	UpdateData(TRUE);
	m_dbX = strtod(m_number,NULL);
	m_bFlag = true;
}

void CJisuanqiDlg::OnBJian() 
{
	m_nType = 1;
	UpdateData(TRUE);
	m_dbX = strtod(m_number,NULL);
	m_bFlag = true;
}

void CJisuanqiDlg::OnBCheng() 
{
	m_nType = 2;
	UpdateData(TRUE);
	m_dbX = strtod(m_number,NULL);
	m_bFlag = true;
}

void CJisuanqiDlg::OnBChu() 
{
	m_nType = 3;
	UpdateData(TRUE);
	m_dbX = strtod(m_number,NULL);
	m_bFlag = true;
}

void CJisuanqiDlg::OnBDian() 
{
	if(m_bFlag)
	{
		m_number = "";
		UpdateData(FALSE);
		m_bFlag = false;
	}
	else
	{}
	UpdateData(TRUE);
	m_number = m_number + ".";
	UpdateData(FALSE);
}

void CJisuanqiDlg::OnB0() 
{
	if(m_bFlag)
	{
		m_number = "";
		UpdateData(FALSE);
		m_bFlag = false;
	}
	else
	{}
	UpdateData(TRUE);
	m_number = m_number + "0";
	UpdateData(FALSE);
}

void CJisuanqiDlg::OnB1() 
{
	if(m_bFlag)
	{
		m_number = "";
		UpdateData(FALSE);
		m_bFlag = false;
	}
	else
	{}
	UpdateData(TRUE);
	m_number = m_number + "1";
	UpdateData(FALSE);
}

void CJisuanqiDlg::OnB2() 
{
	if(m_bFlag)
	{
		m_number = "";
		UpdateData(FALSE);
		m_bFlag = false;
	}
	else
	{}
	UpdateData(TRUE);
	m_number = m_number + "2";
	UpdateData(FALSE);
}

void CJisuanqiDlg::OnB3() 
{
	if(m_bFlag)
	{
		m_number = "";
		UpdateData(FALSE);
		m_bFlag = false;
	}
	else
	{}
	UpdateData(TRUE);
	m_number = m_number + "3";
	UpdateData(FALSE);
}

void CJisuanqiDlg::OnB4() 
{
	if(m_bFlag)
	{
		m_number = "";
		UpdateData(FALSE);
		m_bFlag = false;
	}
	else
	{}
	UpdateData(TRUE);
	m_number = m_number + "4";
	UpdateData(FALSE);
}

void CJisuanqiDlg::OnB5() 
{
	if(m_bFlag)
	{
		m_number = "";
		UpdateData(FALSE);
		m_bFlag = false;
	}
	else
	{}
	UpdateData(TRUE);
	m_number = m_number + "5";
	UpdateData(FALSE);
}

void CJisuanqiDlg::OnB6() 
{
	if(m_bFlag)
	{
		m_number = "";
		UpdateData(FALSE);
		m_bFlag = false;
	}
	else
	{}
	UpdateData(TRUE);
	m_number = m_number + "6";
	UpdateData(FALSE);
}

void CJisuanqiDlg::OnB7() 
{
	if(m_bFlag)
	{
		m_number = "";
		UpdateData(FALSE);
		m_bFlag = false;
	}
	else
	{}
	UpdateData(TRUE);
	m_number = m_number + "7";
	UpdateData(FALSE);
}

void CJisuanqiDlg::OnB8() 
{
	if(m_bFlag)
	{
		m_number = "";
		UpdateData(FALSE);
		m_bFlag = false;
	}
	else
	{}
	UpdateData(TRUE);
	m_number = m_number + "8";
	UpdateData(FALSE);
}

void CJisuanqiDlg::OnB9() 
{
	if(m_bFlag)
	{
		m_number = "";
		UpdateData(FALSE);
		m_bFlag = false;
	}
	else
	{}
	UpdateData(TRUE);
	m_number = m_number + "9";
	UpdateData(FALSE);
}

void CJisuanqiDlg::OnBack() 
{
	UpdateData(TRUE);
//	m_number = m_number-"";
//	m_dbnum = strtoul(m_number,NULL,10);
//	m_dbnum = m_dbnum/10;

	int length = m_number.GetLength();
	if(length > 0)
	{
		m_number = m_number.Left(length-1);
	}
	length = m_number.GetLength();
	if(length == 0)
	{
		m_number = "0";
	}

//	CString str;
//	str.Format("%f",m_dbnum);
//	m_number = str;
	UpdateData(FALSE);
}

void CJisuanqiDlg::OnC() 
{
	UpdateData(TRUE);
//	CString str;
//	str.Format("%f",0);
//	m_bFlag = true;
	m_number = "0.";
	m_bFlag = true;
	UpdateData(FALSE);
}


运行效果:

VC简易计算器实现