端午节这两天没什么事,就用MFC写了一个简单的计算器,界面做的也比较简单,如下图1,可以进行简单的加、减、乘和除功能,小数点显示这块做的不是很好,比如输入1.2,不会一个个的显示,而是先显示“1”,后同时显示“.2”,还有就是遇到0.00时,显示的也不够人性化,哎,就这样吧。。。
图1
1、建立工程:New -> Projects,选择Win32 MFC
AppWizard(exe),并输入工程(counter)名字及设置好路径,点击OK,选择“Dialog based”,基于对话框,直接点finish。
2、将对话框进行简单的处理,把无关的按钮删掉,添加自己的按钮及Edit Box,如图2。
图2
3、对按钮及Edit Box进行属性设置,比如按钮1属性设置为“IDC_BUTTON_1”,2设置为IDC_BUTTON_2,一直类推。Edit
Box不仅要设置ID(IDC_DISPLAY),还要设置相关连的成员变量,右键,选择ClasssWizard..,在点击Add
Variable..,这里设置为double m_display。
4、对每个按钮添加消息处理函数,只需双击该按钮,按照提示点OK,就进入到了该函数入口处。
1设置void CCounterDlg::OnButton1();
2设置void CCounterDlg::OnButton2();
....
其他类推。
加 void CCounterDlg::OnButtonAdd();
减void CCounterDlg::OnButtonSub();
乘void CCounterDlg::OnButtonMult();
除void CCounterDlg::OnButtonDiv();
= void CCounterDlg::OnButtonEqual();
5、定义成员变量
int count; //计数
BOOL
dot_flg; //小数点标志
BOOL
continue_flg;//没按运算符,是否一直连续按数字按钮标志
BOOL
str_flag;//有没按运算符标志
CString str;//记录当前按的运算符
double result;
//num1和num2运算的结果
double num1; //按运算符之前的第一个数
double num2; //按运算符之后的第二个数
6、定义成员函数,该函数主要是对按下的数字进行叠加,跟踪num1和num2:
double CCounterDlg::buttonDownNum(int numSize);
7、当按下之后数字要及时显示出来,需要用到:
UpdateData(FALSE); //该函数是对Edit
Box的内容进行更新,FALSE是有内部数成员的数据值更新到界面上,TRUE是从界面上更新到内部的数据成员上。
GetDlgItem(IDC_DISPLAY)->SetWindowText("小数点");//可以利用这个语句直接在Edit
Box上显示内容“小数点”。只是提示自己也可以用这个来显示,程序中注释掉了。
8、代码实现:
//贴出自己添加的代码,其他代码默认有的
#include "math.h"// CCounterDlg dialog
CCounterDlg::CCounterDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCounterDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCounterDlg)
//初始化
m_display = 0.0;
num1 = 0;
num2 = 0;
result = 0.0;
str = "";
str_flag = FALSE;
continue_flg = FALSE;
downDot = FALSE;
count = 1;//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}double CCounterDlg::buttonDownNum(int numSize)
{
if (!str_flag) //运算符没有被按下,那就是num1
{
if (!continue_flg) //按下一次
{
num1 = numSize;
continue_flg =TRUE;
}
else
{
num1 = num1*pow(10,count) + numSize;
if (downDot)
{
num1 = num1/(pow(10,count));
count++;
}
}return num1;
}
else //有运算符被按下
{
if (!continue_flg) //是否连续按下
{
num2 = numSize;
continue_flg =TRUE;
}
else
{
num2 = num2*pow(10,count) + numSize;
if (downDot)
{
num2 = num2/(pow(10,count));
count++;
}
}return num2;
}
}void CCounterDlg::OnButton0()
{
// TODO: Add your control notification handler code herem_display = buttonDownNum(0);
UpdateData(FALSE);
}void CCounterDlg::OnButton1()
{
// TODO: Add your control notification handler code here
m_display = buttonDownNum(1);
UpdateData(FALSE);
}void CCounterDlg::OnButton2()
{
// TODO: Add your control notification handler code herem_display = buttonDownNum(2);
UpdateData(FALSE);
}void CCounterDlg::OnButton3()
{
// TODO: Add your control notification handler code here
m_display = buttonDownNum(3);
UpdateData(FALSE);
}void CCounterDlg::OnButton4()
{
// TODO: Add your control notification handler code here
m_display = buttonDownNum(4);
UpdateData(FALSE);
}void CCounterDlg::OnButton5()
{
// TODO: Add your control notification handler code here
m_display = buttonDownNum(5);
UpdateData(FALSE);
}void CCounterDlg::OnButton6()
{
// TODO: Add your control notification handler code here
m_display = buttonDownNum(6);
UpdateData(FALSE);
}void CCounterDlg::OnButton7()
{
// TODO: Add your control notification handler code here
m_display = buttonDownNum(7);
UpdateData(FALSE);
}void CCounterDlg::OnButton8()
{
// TODO: Add your control notification handler code here
m_display = buttonDownNum(8);
UpdateData(FALSE);
}void CCounterDlg::OnButton9()
{
// TODO: Add your control notification handler code here
m_display = buttonDownNum(9);
UpdateData(FALSE);
}void CCounterDlg::OnButtonDot() //小数点
{
// TODO: Add your control notification handler code here
downDot = TRUE;
// GetDlgItem(IDC_DISPLAY)->SetWindowText("小数点"); //可以利用该函数在编辑框中显示信息
}void CCounterDlg::OnButtonAdd() //加
{
// TODO: Add your control notification handler code here
str = "+";
str_flag = TRUE;
continue_flg = FALSE;
downDot = FALSE;
count = 1;
UpdateData(FALSE);
}void CCounterDlg::OnButtonSub() //减
{
// TODO: Add your control notification handler code here
str = "-";
str_flag = TRUE;
continue_flg = FALSE;
downDot = FALSE;
count = 1;
UpdateData(FALSE);
}void CCounterDlg::OnButtonMult() //乘
{
// TODO: Add your control notification handler code here
str = "*";
str_flag = TRUE;
continue_flg = FALSE;
downDot = FALSE;
count = 1;
UpdateData(FALSE);
}void CCounterDlg::OnButtonDiv() //除
{
// TODO: Add your control notification handler code here
str = "/";
str_flag = TRUE;
continue_flg = FALSE;
downDot = FALSE;
count = 1;
UpdateData(FALSE);
}void CCounterDlg::OnButtonEqual()
{
// TODO: Add your control notification handler code here
if(str == "+")
result = num1 + num2;
else if(str == "-")
result = num1 - num2;
else if(str == "*")
result = num1 * num2;
else if(str == "/")
{
result = double(num1) / double(num2);
}num1 = result;
m_display = result;
str_flag = FALSE;
continue_flg = FALSE;
downDot = FALSE;
count = 1;
UpdateData(FALSE);
}void CCounterDlg::OnButtonClear()
{
// TODO: Add your control notification handler code herenum1 = 0;
num2 = 0;
m_display = 0;
continue_flg = FALSE;
str_flag = FALSE;
downDot = FALSE;
count = 1;
UpdateData(FALSE);
}
利用MFC编写计算器