简易计算器实现加减乘除等功能,可以使用小数点
1.首先添加计算器按钮界面
2.设置好各个变量,注意添加变量时要选择value
3.双击各个button按键依次添加功能
话不多说直接上代码
// McountDlg.h : 头文件
#pragma once
// CMcountDlg 对话框
class CMcountDlg : public CDialogEx
{
// 构造
public:
CMcountDlg(CWnd* pParent = NULL); // 标准构造函数
// 对话框数据
enum { IDD = IDD_MCOUNT_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButtonone();
double m_showvalue;
afx_msg void OnBnClickedButtontwo();
afx_msg void OnBnClickedButtonthree();
afx_msg void OnBnClickedButton4();
afx_msg void OnBnClickedButton5();
afx_msg void OnBnClickedButton6();
afx_msg void OnBnClickedButton7();
afx_msg void OnBnClickedButton8();
afx_msg void OnBnClickedButton9();
afx_msg void OnBnClickedButton10();
static int i,sub,del,mup,chu,point;
static double j,pointlater;
static float valuenumber;
afx_msg void OnBnClickedButtonadd();
afx_msg void OnBnClickedButtonequel();
afx_msg void OnBnClickedButtonclean();
afx_msg void OnBnClickedButtonsub();
afx_msg void OnBnClickedButtonmplit();
afx_msg void OnBnClickedButtondelete();
afx_msg void OnBnClickedButtonpoint();
afx_msg void OnBnClickedButtonone2();
afx_msg void OnBnClickedButtonone3();
afx_msg void OnBnClickedButtonone4();
afx_msg void OnBnClickedButtonone5();
afx_msg void OnBnClickedButtondelete2();
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};
//
#include "stdafx.h"
#include "Mcount.h"
#include "McountDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
int CMcountDlg::i = 1;
double CMcountDlg::j = 0.1;
int CMcountDlg::sub = 0;
int CMcountDlg::del = 0;
int CMcountDlg::mup = 0;
int CMcountDlg::chu = 0;
int CMcountDlg::point = 0;
double CMcountDlg::pointlater = 1;
float CMcountDlg::valuenumber = 0;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CMcountDlg 对话框
CMcountDlg::CMcountDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CMcountDlg::IDD, pParent)
, m_showvalue(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMcountDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_showvalue);
}
BEGIN_MESSAGE_MAP(CMcountDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTONone, &CMcountDlg::OnBnClickedButtonone)
ON_BN_CLICKED(IDC_BUTTONtwo, &CMcountDlg::OnBnClickedButtontwo)
ON_BN_CLICKED(IDC_BUTTONthree, &CMcountDlg::OnBnClickedButtonthree)
ON_BN_CLICKED(IDC_BUTTON4, &CMcountDlg::OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON5, &CMcountDlg::OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON6, &CMcountDlg::OnBnClickedButton6)
ON_BN_CLICKED(IDC_BUTTON7, &CMcountDlg::OnBnClickedButton7)
ON_BN_CLICKED(IDC_BUTTON8, &CMcountDlg::OnBnClickedButton8)
ON_BN_CLICKED(IDC_BUTTON9, &CMcountDlg::OnBnClickedButton9)
ON_BN_CLICKED(IDC_BUTTON10, &CMcountDlg::OnBnClickedButton10)
ON_BN_CLICKED(IDC_BUTTONadd, &CMcountDlg::OnBnClickedButtonadd)
ON_BN_CLICKED(IDC_BUTTONequel, &CMcountDlg::OnBnClickedButtonequel)
ON_BN_CLICKED(IDC_BUTTONclean, &CMcountDlg::OnBnClickedButtonclean)
ON_BN_CLICKED(IDC_BUTTONsub, &CMcountDlg::OnBnClickedButtonsub)
ON_BN_CLICKED(IDC_BUTTONmplit, &CMcountDlg::OnBnClickedButtonmplit)
ON_BN_CLICKED(IDC_BUTTONdelete, &CMcountDlg::OnBnClickedButtondelete)
ON_BN_CLICKED(IDC_BUTTONpoint, &CMcountDlg::OnBnClickedButtonpoint)
ON_BN_CLICKED(IDC_BUTTONone2, &CMcountDlg::OnBnClickedButtonone2)
ON_BN_CLICKED(IDC_BUTTONone3, &CMcountDlg::OnBnClickedButtonone3)
ON_BN_CLICKED(IDC_BUTTONone4, &CMcountDlg::OnBnClickedButtonone4)
ON_BN_CLICKED(IDC_BUTTONone5, &CMcountDlg::OnBnClickedButtonone5)
ON_BN_CLICKED(IDC_BUTTONdelete2, &CMcountDlg::OnBnClickedButtondelete2)
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
// CMcountDlg 消息处理程序
BOOL CMcountDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
//改字体: 首先要把静态控件ID名称改一下IDC_STATIC1
CFont font;
font.CreatePointFont(240, _T("Time New Roman"));
GetDlgItem(IDC_EDIT1)->SetFont(&font);
font.DeleteObject();
font.CreatePointFont(320, _T("Time New Roman"));
GetDlgItem(IDC_BUTTONone)->SetFont(&font);
GetDlgItem(IDC_BUTTONtwo)->SetFont(&font);
GetDlgItem(IDC_BUTTONthree)->SetFont(&font);
GetDlgItem(IDC_BUTTON4)->SetFont(&font);
GetDlgItem(IDC_BUTTON5)->SetFont(&font);
GetDlgItem(IDC_BUTTON6)->SetFont(&font);
GetDlgItem(IDC_BUTTON7)->SetFont(&font);
GetDlgItem(IDC_BUTTON8)->SetFont(&font);
GetDlgItem(IDC_BUTTON9)->SetFont(&font);
//改颜色:
//窗体增加WM_CTLCOLOR消息响应
//在OnCtlColor()中:
//if (pWnd == GetDlgItem(IDC_STATIC1))
//{
// pDC->SetTextColor(RGB(255, 0, 0));
//}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CMcountDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CMcountDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMcountDlg::OnQueryDragIcon()
{
return static_cast(m_hIcon);
}
void CMcountDlg::OnBnClickedButtonone()
{
// TODO: 在此添加控件通知处理程序代码
int one = 1;
int onevalue;
if (point!=1)
{
if (i > 1)
{
m_showvalue = i * m_showvalue + one;
}
else
m_showvalue = one;
i = 10;
}
if (point==1)
{
m_showvalue = j *one+ m_showvalue;
j = j*0.1;
pointlater*=0.1;
}
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtontwo()
{
// TODO: 在此添加控件通知处理程序代码
int one = 2;
int onevalue;
if (point != 1)
{
if (i > 1)
{
m_showvalue = i * m_showvalue + one;
}
else
m_showvalue = one;
i = 10;
}
if (point == 1)
{
m_showvalue = j *one + m_showvalue;
j = j*0.1;
pointlater *= 0.1;
}
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtonthree()
{
// TODO: 在此添加控件通知处理程序代码
int one = 3;
int onevalue;
if (point != 1)
{
if (i > 1)
{
m_showvalue = i * m_showvalue + one;
}
else
m_showvalue = one;
i = 10;
}
if (point == 1)
{
m_showvalue = j *one + m_showvalue;
j = j*0.1;
pointlater *= 0.1;
}
UpdateData(false);
}
void CMcountDlg::OnBnClickedButton4()
{
// TODO: 在此添加控件通知处理程序代码
int one = 4;
int onevalue;
if (point != 1)
{
if (i > 1)
{
m_showvalue = i * m_showvalue + one;
}
else
m_showvalue = one;
i = 10;
}
if (point == 1)
{
m_showvalue = j *one + m_showvalue;
j = j*0.1;
pointlater *= 0.1;
}
UpdateData(false);
}
void CMcountDlg::OnBnClickedButton5()
{
// TODO: 在此添加控件通知处理程序代码
int one = 5;
int onevalue;
if (point != 1)
{
if (i > 1)
{
m_showvalue = i * m_showvalue + one;
}
else
m_showvalue = one;
i = 10;
}
if (point == 1)
{
m_showvalue = j *one + m_showvalue;
j = j*0.1;
pointlater *= 0.1;
}
UpdateData(false);
}
void CMcountDlg::OnBnClickedButton6()
{
// TODO: 在此添加控件通知处理程序代码
int one = 6;
int onevalue;
if (point != 1)
{
if (i > 1)
{
m_showvalue = i * m_showvalue + one;
}
else
m_showvalue = one;
i = 10;
}
if (point == 1)
{
m_showvalue = j *one + m_showvalue;
j = j*0.1;
pointlater *= 0.1;
}
UpdateData(false);
}
void CMcountDlg::OnBnClickedButton7()
{
// TODO: 在此添加控件通知处理程序代码
int one = 7;
int onevalue;
if (point != 1)
{
if (i > 1)
{
m_showvalue = i * m_showvalue + one;
}
else
m_showvalue = one;
i = 10;
}
if (point == 1)
{
m_showvalue = j *one + m_showvalue;
j = j*0.1;
pointlater *= 0.1;
}
UpdateData(false);
}
void CMcountDlg::OnBnClickedButton8()
{
// TODO: 在此添加控件通知处理程序代码
int one = 8;
int onevalue;
if (point != 1)
{
if (i > 1)
{
m_showvalue = i * m_showvalue + one;
}
else
m_showvalue = one;
i = 10;
}
if (point == 1)
{
m_showvalue = j *one + m_showvalue;
j = j*0.1;
pointlater *= 0.1;
}
UpdateData(false);
}
void CMcountDlg::OnBnClickedButton9()
{
// TODO: 在此添加控件通知处理程序代码
int one = 9;
int onevalue;
if (point != 1)
{
if (i > 1)
{
m_showvalue = i * m_showvalue + one;
}
else
m_showvalue = one;
i = 10;
}
if (point == 1)
{
m_showvalue = j *one + m_showvalue;
j = j*0.1;
pointlater *= 0.1;
}
UpdateData(false);
}
void CMcountDlg::OnBnClickedButton10()
{
// TODO: 在此添加控件通知处理程序代码
int one = 0;
int onevalue;
if (point != 1)
{
if (i > 1)
{
m_showvalue = i * m_showvalue + one;
}
else
m_showvalue = one;
i = 10;
}
if (point == 1)
{
m_showvalue = j *one + m_showvalue;
j = j*0.1;
pointlater *= 0.1;
}
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtonadd()
{
// TODO: 在此添加控件通知处理程序代码
valuenumber = m_showvalue;
m_showvalue = 0;
sub++;
point = 0;
j = 0.1;
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtonequel()
{
// TODO: 在此添加控件通知处理程序代码
if (sub!=0)
{
m_showvalue = m_showvalue + valuenumber;
sub--;
}
if (del!=0)
{
m_showvalue = valuenumber-m_showvalue;
del--;
}
if (mup != 0)
{
m_showvalue = valuenumber * m_showvalue;
mup--;
}
if (chu != 0)
{
m_showvalue = valuenumber / m_showvalue;
chu--;
}
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtonclean()
{
// TODO: 在此添加控件通知处理程序代码
m_showvalue = 0;
point = 0;
j = 0.1;
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtonsub()
{
// TODO: 在此添加控件通知处理程序代码
valuenumber = m_showvalue;
m_showvalue = 0;
del++;
point = 0;
j = 0.1;
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtonmplit()
{
// TODO: 在此添加控件通知处理程序代码
valuenumber = m_showvalue;
m_showvalue = 0;
mup++;
point = 0;
j = 0.1;
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtondelete()
{
// TODO: 在此添加控件通知处理程序代码
valuenumber = m_showvalue;
m_showvalue = 0;
chu++;
point = 0;
j = 0.1;
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtonpoint()
{
// TODO: 在此添加控件通知处理程序代码
point=1;
}
void CMcountDlg::OnBnClickedButtonone2()
{
// TODO: 在此添加控件通知处理程序代码
m_showvalue = m_showvalue*m_showvalue;
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtonone3()
{
// TODO: 在此添加控件通知处理程序代码
m_showvalue = sqrt(m_showvalue);
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtonone4()
{
// TODO: 在此添加控件通知处理程序代码
if (point==0)
{
int m_value=int(m_showvalue)%10;
m_showvalue = m_showvalue - m_value;
m_showvalue = m_showvalue / 10;
}
if (point==1)
{
int myvalue = m_showvalue * 100000;
while(myvalue%10==0)
{
myvalue = myvalue / 10;
}
myvalue = myvalue-myvalue % 10;
m_showvalue = myvalue*pointlater;
}
pointlater *= 10;
//point = 0;
j = 0.1;
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtonone5()
{
// TODO: 在此添加控件通知处理程序代码
if (m_showvalue==0)
{
m_showvalue = 1;
}
if (m_showvalue<0)
{
m_showvalue = -1;
}
for (int i = m_showvalue; i >1; i--)
{
m_showvalue = m_showvalue*(i - 1);
}
UpdateData(false);
}
void CMcountDlg::OnBnClickedButtondelete2()
{
// TODO: 在此添加控件通知处理程序代码
m_showvalue = abs(m_showvalue);
UpdateData(false);
}
HBRUSH CMcountDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何特性
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
if (pWnd->GetDlgCtrlID() == IDC_EDIT1)// IDC_Display为所选文本框ID
{
pDC->SetTextColor(RGB(255, 0, 0));//设置字体颜色
// pDC->SetBkColor(RGB(0, 255, 0));//设置背景颜色
//pDC->SetBkMode(TRANSPARENT);//设置背景透明
}
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return hbr;
}