2023年7月17日发(作者:)
VC++MFC基于对话框简单计算器设计
1 实现计算器功能如图:
用VC++建立MFC基于对话框的简单计算器。
简易计算器设计说明:
1.三个编辑窗口:
(1)其中两个为输入操作窗口:数1、数2,一个结果输出窗口。
(2)只有一个操作数时在第一操作窗口输入。
(3)键盘输入数据,运算符按钮输出结果。
2.多个运算操作按钮:
(1)实现加减乘除的基本计算器功能。
(2)实现三角函数、对数、阶乘等的数值函数运算。
3.计算器设计过程:
(1)首先建立MFC的基于对话框程序,命名为“TEST1”
- 1 -
- 2 -
然后在对话框中加入三个编辑框,并用快捷键CTRL+W组合键打开MFC ClassWizard切换到Member Variables中其中找到IDC_EDIT1、IDC_EDIT2、IDC_EDIT3分别添加对应的3个double型关联变量m_EDIT1_NUM、m_EDIT2_NUM、m_EDIT3_NUM。
关联变量:- 3 -
在后双击编辑框改变编辑框函数名并添加函数:UpdateData(TRUE);其他两个编辑框均进行此操作。
(2)在对话框中的适当位置添加20个按钮控件实现运算操作:
a.操作说明按钮:
鼠标放在按钮控件上,右击鼠标点击属性,改变按钮属性标题为“操作说明”,然后双击控件改变函数名并点击“确定”添加函数代码:
void CTEST1Dlg::OnINTR() //计算器操作说明
{
// TODO: Add your control notification handler code here
- 4 -
MessageBox("例如M+N=?,在第一个编辑框输入M,第二个编辑框输入N,然后再点击运算符输出结果,减法、乘法、除法、取余等两个数运算的同理。只有一个数进行运算的时候, 在第一个编辑框输入数,然后点击运算符输出结果!");
}
b.两个数运算的按钮:
与添加“操作说明”按钮类似,比如加法, 改变按钮属性标题为“+”,然后双击控件添加函数代码:
void CTEST1Dlg::OnADD() //加法运算:m+n
{
// TODO: Add your control notification handler code here
m_EDIT3_NUM=m_EDIT1_NUM+m_EDIT2_NUM; //m_EDIT3_NUM、m_EDIT1_NUM、m_EDIT2_NUM为三个编辑框的对应关联变量
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_SHOW); //当执行双数运算时,重新显示第二个编辑框
UpdateData(FALSE);
}
其他的两个数运算的同理。
注意:
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_SHOW);我设计想法是当只有一个操作数运算时隐藏第二个编辑框,此函数就是隐藏与显示的功能。IDC_EDIT2是编辑框的关联地址;SW_SHOW便是执行显示编辑框,此外SW_HIDE是执行隐藏功能后面会用到。
- 5 -
c.只有一个数运算的按钮:
操作与上面一样,比如x^2, 改变按钮属性标题为“x^2”,然后双击控件添加函数代码:
void CTEST1Dlg::OnPF() //平方运算:x^2
{
// TODO: Add your control notification handler code here
m_EDIT3_NUM=m_EDIT1_NUM*m_EDIT1_NUM;
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
其他的一个数运算的同理。
d.第二个编辑框被隐藏后重新显示函数:
void CTEST1Dlg::OnUpdateEdit1Show()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function to send the EM_SETEVENTMASK message to the control
// with the ENM_UPDATE flag ORed into the lParam mask.
// TODO: Add your control notification handler code here
//GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE); //激活编辑框
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_SHOW); //当第一个编辑框重新输入数据时,重新显示第二个编辑框
UpdateData(TRUE);
}
注意:
这个函数要执行的功能是,当第二个编辑框被隐藏时,在第一个编辑框重新输入数据的同时,重新激活第二个编辑框。如果想简单设计的话,之前的所有编辑框隐藏和显示函数就不用添加。
e.三角函数的弧度制与角度制的切换:
在对话框中添加两个选择控件:Degree和Rad,第一个选择控件关联变量为m_CDR,添加方法和上面添加编辑框关联变量一样(注意:要关联变量必须把选择控件属性中的组勾选,不然找不到选择控件的地址),并初始化为m_CDR = 0;为零时选择框默认被选中。两个选择控件是互斥的,当选中Rad时m_CDR = 0,从而判断是执行角度制还是弧度制,然后双击选择控件,添加代码UpdateData(TRUE);以sin(x)为例添加代码:
void CTEST1Dlg::OnSin() //正弦函数运算:sin(x)
{
// TODO: Add your control notification handler code here
if(m_CDR == 1)
{
m_FDR=1; //m_CDR单选框关联变量,角度运算
- 6 -
}
else
{
m_FDR=(2*PI)/360; //弧度运算
}
m_EDIT3_NUM=sin(m_EDIT1_NUM*m_FDR);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
- 7 -
变量m_FDR添加方法:在右侧ClassView中的CAboutDlg鼠标右击选择Add Member
Variable….变量类型为:double,变量名为:m_FDR
同时记得添加头文件math.h
(3)在BOOL CTEST1Dlg::OnInitDialog()函数中加入SetWindowText(_T("计算器")); 改窗口标题为“计算器”。- 8 -
(4)完成程序编写后,编译执行。
2 计算界面演示:
加法:
倒数:
- 9 -
除法:
- 10 -
3 程序清单:
#include "math.h" //数学函数运算头文件
#define PI 3.97932384626433832795 //宏定义
在BOOL CTEST1Dlg::OnInitDialog()函数中加入SetWindowText(_T("计算器")); 改窗口标题为“计算器”;
void CTEST1Dlg::OnNO1() //第一个数字输入框
{
// 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
UpdateData(TRUE);
}
void CTEST1Dlg::OnNO2() //第二个数字输入框
{
// 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.
- 11 -
// TODO: Add your control notification handler code here
UpdateData(TRUE);
}
void CTEST1Dlg::OnNO3() //运算结果显示框
{
// 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
UpdateData(TRUE);
}
void CTEST1Dlg::OnINTR() //计算器操作说明
{
// TODO: Add your control notification handler code here
MessageBox("例如M+N=?,在第一个编辑框输入M,第二个编辑框输入N,然后再点击运算符输出结果,减法、乘法、除法、取余等两个数运算的同理。只有一个数进行运算的在第一个编辑框输入数,然后点击运算符输出结果!");
}
void CTEST1Dlg::OnADD() //加法运算:m+n
{
// TODO: Add your control notification handler code here
m_EDIT3_NUM=m_EDIT1_NUM+m_EDIT2_NUM; //m_EDIT3_NUM、m_EDIT1_NUM、m_EDIT2_NUM为三个编辑框的对应关联变量
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_SHOW); //当执行双数运算时,重新显示第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnSUB() //减法运算:m-n
{
// TODO: Add your control notification handler code here
m_EDIT3_NUM=m_EDIT1_NUM-m_EDIT2_NUM;
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_SHOW); //当执行双数运算时,重新显示第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnMUL() //乘法运算:x*y
- 12 -
{
// TODO: Add your control notification handler code here
m_EDIT3_NUM=m_EDIT1_NUM*m_EDIT2_NUM;
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_SHOW); //当执行双数运算时,重新显示第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnDIV() //除法运算:x/y
{
// TODO: Add your control notification handler code here
if(m_EDIT2_NUM == 0)
{
MessageBox("错误!除数不能为零,请检查……"); //除数为零时,弹出警告
}
else
{
m_EDIT3_NUM=m_EDIT1_NUM/m_EDIT2_NUM;
}
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_SHOW); //当执行双数运算时,重新显示第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnMod() //取余运算:x%y
{
// TODO: Add your control notification handler code here
if(m_EDIT2_NUM == 0)
{
MessageBox("错误!除数不能为零!"); //除数为零时,弹出警告
}
else
m_EDIT3_NUM=fmod(m_EDIT1_NUM,m_EDIT2_NUM);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_SHOW); //当执行双数运算时,重新显示第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnDS() //求倒数运算:1/x
{
// TODO: Add your control notification handler code here
if(m_EDIT1_NUM == 0)
{
MessageBox("错误!请输入不为零的数!"); //除数为零时,弹出警告
- 13 -
}
else
m_EDIT3_NUM=1/m_EDIT1_NUM;
//GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE); //激活编辑框
//GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE); //失效编辑框
//GetDlgItem(IDC_EDIT2)->ShowWindow(SW_SHOW); //显示编辑框
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //隐藏编辑框
//GetDlgItem(IDC_STATIC2)->ShowWindow(SW_HIDE);
UpdateData(FALSE);
}
void CTEST1Dlg::OnSqrt() //开平方运算:sqrt(x)
{
// TODO: Add your control notification handler code here
if(m_EDIT1_NUM < 0)
{
MessageBox("错误!请输入大于零的数!"); //被开方数小于零时,弹出警告
}
else
m_EDIT3_NUM=sqrt(m_EDIT1_NUM);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnPF() //平方运算:x^2
{
// TODO: Add your control notification handler code here
m_EDIT3_NUM=m_EDIT1_NUM*m_EDIT1_NUM;
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnLF() //立方运算:x^3
{
// TODO: Add your control notification handler code here
m_EDIT3_NUM=m_EDIT1_NUM*m_EDIT1_NUM*m_EDIT1_NUM;
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnLOG() //以10为底的对数运算:log(x)
- 14 -
{
// TODO: Add your control notification handler code here
if(m_EDIT1_NUM <= 0)
{
MessageBox("错误!请输入大于零的数!"); //真数小于零时,弹出警告
}
else
m_EDIT3_NUM=log10(m_EDIT1_NUM);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnLN() //以e为底的对数运算:ln(x)
{
// TODO: Add your control notification handler code here
if(m_EDIT1_NUM <= 0)
{
MessageBox("错误!请输入大于零的数!"); //真数小于零时,弹出警告
}
else
m_EDIT3_NUM=log(m_EDIT1_NUM);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnEXP() //以e为底的指数运算:e^x
{
// TODO: Add your control notification handler code here
m_EDIT3_NUM=exp(m_EDIT1_NUM);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnTENx() //以10为底的指数运算:10^x
{
// TODO: Add your control notification handler code here
m_EDIT3_NUM=pow(10,m_EDIT1_NUM);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
- 15 -
}
void CTEST1Dlg::OnNN() //n的阶乘运算:n!
{
// TODO: Add your control notification handler code here
int i,j=1;
if(m_EDIT1_NUM <= 0)
{
MessageBox("错误!请输入大于零的数!");
}
else
{
for(i=m_EDIT1_NUM;i>=1;i--)
j*=i;
}
m_EDIT3_NUM=j;
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnXY() //x的y次方运算:x^y
{
// TODO: Add your control notification handler code here
m_EDIT3_NUM=pow(m_EDIT1_NUM,m_EDIT2_NUM);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_SHOW); //当执行双数运算时,重新显示第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnSin() //正弦函数运算:sin(x)
{
// TODO: Add your control notification handler code here
if(m_CDR == 1)
{
m_FDR=1; //m_FDR单选框关联变量,角度运算
}
else
{
m_FDR=(2*PI)/360; //弧度运算
}
m_EDIT3_NUM=sin(m_EDIT1_NUM*m_FDR);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二- 16 -
个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnCos() //余弦函数运算:cos(x)
{
// TODO: Add your control notification handler code here
if(m_CDR == 1)
{
m_FDR=1;
}
else
{
m_FDR=(2*PI)/360;
}
m_EDIT3_NUM=cos(m_EDIT1_NUM*m_FDR);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnTan() //正切函数运算:tan(x)
{
// TODO: Add your control notification handler code here
if(m_CDR == 1)
{
m_FDR=1;
}
else
{
m_FDR=(2*PI)/360;
}
m_EDIT3_NUM=tan(m_EDIT1_NUM*m_FDR);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnCot() //余切函数运算:cot(x)
{
// TODO: Add your control notification handler code here
if(m_CDR == 1)
- 17 -
{
m_FDR=1;
}
else
{
m_FDR=(2*PI)/360;
}
m_EDIT3_NUM=1/tan(m_EDIT1_NUM*m_FDR);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnDegree() //选择角度单制选框函数
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
}
void CTEST1Dlg::OnRad() //选择弧度制单选框函数
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
}
void CTEST1Dlg::OnStatic2()
{
// TODO: Add your control notification handler code here
UpdateData(FALSE);
}
void CTEST1Dlg::OnUpdateEdit1Show()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function to send the EM_SETEVENTMASK message to the control
// with the ENM_UPDATE flag ORed into the lParam mask.
// TODO: Add your control notification handler code here
//GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE); //激活编辑框
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_SHOW); //当第一个编辑框重新输入数据时,重新显示第二个编辑框
UpdateData(TRUE);
- 18 -
}
- 19 -
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689607453a270145.html
评论列表(0条)