VC6.0的MFC简单计算器制作

在VC中的计算器制作,我们开始要考虑的就是简单的计算器的是由什么构成的,有按钮,有显示框。按钮有数字的0~9部分还有小数点,,外加4个运算符号加减乘除,还有就是显示结果的等于号。。可以的话可以加个清除显示结果框的按钮和倒退按钮。。

首先先电脑有vc6.0,,进入后点击文件,再新建~工程~MFC...【exe】输入工程名,

点击IDD_XX_DIALOG进入设置页面

用DEL除去软件给我们的,有这些工具在界面设置

设置后在右击按钮在属性~常规~标题中修改0~9的标记后的大概的样子

再右击显示结果的框架~~建立类向导 ,添加变量


设置完就再回到编辑 界面双击你设置的按钮的从0开始输入代码

void CMyDlg::OnButton0() 
{
m_result += _T("0");//定义宏
UpdateData(FALSE); //再清屏

};

下面的1~9包括小数点也是

设置完就对运算符先设置加号

void CMyDlg::OnButton4() 
{
n=0;//标记
data = atof(m_result);//输入的是字符串的存在要转换成数字在计算
m_result = _T(" ");

}

再设置减号,样式差不多就是其中的标记改成1,乘除也依次类推。。

 这是等号的代码

void CMyDlg::OnButton6() 
{if(n==0)
{
data += atof(m_result);
m_result.Format(_T("%lf"),data);
UpdateData(FALSE);
}
if(n==1)
{
data -= atof(m_result);
m_result.Format(_T("%lf"),data);
UpdateData(FALSE);
}
if(n==2)
{
data *= atof(m_result);
m_result.Format(_T("%lf"),data);
UpdateData(FALSE);
}
if(n==3)//辨别输入的标号
{
data /= atof(m_result);//数据进行转换后也等于data=data/m_result
m_result.Format(_T("%lf"),data);//进行输出的精确取值
UpdateData(FALSE);
}


}

删除的代码是

void CMyDlg::OnButton17() 
{
m_result = _T(" ");//返回
UpdateData(FALSE);

}


void CMyDlg::OnButton18() 
{
  m_result = m_result.Left(m_result.GetLength()-1);//取总长度,再把其中的字符串减一
  UpdateData(FALSE);

}


开始定义了看图片,,不解释


可以了,运行程序



总的源代码如下


// 计算器Dlg.cpp : implementation file
//


#include "stdafx.h"
#include "计算器.h"
#include "计算器Dlg.h"


#include<Math.h>
#include<wchar.h>
//#include<coino.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)
{
//{{AFX_DATA_INIT(CMyDlg)
m_result = _T("");
//}}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_Text(pDX, IDC_EDIT1, m_result);
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON13, OnButton13)
ON_BN_CLICKED(IDC_BUTTON15, OnButton15)
ON_BN_CLICKED(IDC_BUTTON14, OnButton14)
ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON16, OnButton16)
ON_BN_CLICKED(IDC_BUTTON12, OnButton12)
ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
ON_BN_CLICKED(IDC_BUTTON17, OnButton17)
ON_BN_CLICKED(IDC_BUTTON18, OnButton18)
//}}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::OnChangeEdit1() 
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.

// TODO: Add your control notification handler code here

}


void CMyDlg::OnButton1() 
{
m_result += _T("1");//unicode编译
UpdateData(FALSE);//清屏,可以重新输入


}


void CMyDlg::OnButton3() 
{
m_result += _T("2");
UpdateData(FALSE);

}


void CMyDlg::OnButton2() 
{
m_result += _T("3");
UpdateData(FALSE);

}


void CMyDlg::OnButton13() 
{
m_result += _T("4");
UpdateData(FALSE);

}


void CMyDlg::OnButton15() 
{
m_result += _T("5");
UpdateData(FALSE);

}


void CMyDlg::OnButton14() 
{
m_result += _T("6");
UpdateData(FALSE);

}


void CMyDlg::OnButton9() 
{
m_result += _T("7");
UpdateData(FALSE);

}


void CMyDlg::OnButton11() 
{
m_result += _T("8");
UpdateData(FALSE);
}


void CMyDlg::OnButton10() 
{
m_result += _T("9");
UpdateData(FALSE);
}


void CMyDlg::OnButton7() 
{
m_result += _T("0");
UpdateData(FALSE);

}


void CMyDlg::OnButton5() 
{
m_result = m_result + _T("."); 
UpdateData(FALSE);
}


void CMyDlg::OnButton6() 
{if(n==0)
{
data += atof(m_result);
m_result.Format(_T("%lf"),data);
UpdateData(FALSE);
}
if(n==1)
{
data -= atof(m_result);
m_result.Format(_T("%lf"),data);
UpdateData(FALSE);
}
if(n==2)
{
data *= atof(m_result);
m_result.Format(_T("%lf"),data);
UpdateData(FALSE);
}
if(n==3)
{
data /= atof(m_result);
m_result.Format(_T("%lf"),data);
UpdateData(FALSE);
}


}


void CMyDlg::OnButton4() 
{
n=0;
data = atof(m_result);
m_result = _T(" ");

}


void CMyDlg::OnButton16() 
{
n=1;
data = atof(m_result);
m_result = _T(" ");

}


void CMyDlg::OnButton12() 
{
n=2;
data = atof(m_result);
m_result = _T(" ");

}


void CMyDlg::OnButton8() 
{
n=3;
data = atof(m_result);
m_result = _T(" ");

}


void CMyDlg::OnButton17() 
{
m_result = _T(" ");
UpdateData(FALSE);

}


void CMyDlg::OnButton18() 
{
  m_result = m_result.Left(m_result.GetLength()-1);
  UpdateData(FALSE);

}

  • 50
    点赞
  • 225
    收藏
    觉得还不错? 一键收藏
  • 25
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值