VC++ MFC基于对话框简单计算器设计
简单计算器设计
1 实现计算器功能如图:
用VC++建立MFC基于对话框的简单计算器。
简易计算器设计说明:
1.三个编辑窗口:
(1)其中两个为输入操作窗口:数1、数2,一个结果输出窗口。
(2)只有一个操作数时在第一操作窗口输入。
(3)键盘输入数据,运算符按钮输出结果。
2.多个运算操作按钮:
(1)实现加减乘除的基本计算器功能。
(2)实现数值三角函数、对数、阶乘等的基本运算。
3.计算器设计过程:
(1)首先建立MFC的基于对话框程序,命名为“TEST1”,然后在对话框中加入三个编辑框,并添加对应的3个double型关联变量m_EDIT1_NUM、m_EDIT2_NUM、m_EDIT3_NUM,在后双击编辑框添加函数:UpdateData(TRUE);其他编辑框均进行此操作点击确定。
(2)在对话框中的适当位置添加20个按钮控件实现运算操作:
a.操作说明按钮:
改变按钮属性标题为“操作说明”,然后双击控件添加函数代码:
void CTEST1Dlg::OnINTR() //计算器操作说明
{
// TODO: Add your control notification handler code here
- 1 -
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);
}
其他的两个数运算的同理。
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);
}
- 2 -
e.三角函数的弧度制与角度制的切换:
在对话框中添加两个选择控件:Degree和Rad,第一个关联变量为m_CDR,并初始化为m_CDR = 0; 为零时选择框被选中。两个选择控件是互斥的,当选中Rad时m_CDR = 1,从而判断是执行角度制还是弧度制,以sin(x)为例添加代码:
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); //在只有一个操作数时,隐藏第二个编辑框
UpdateData(FALSE);
}
(3)在BOOL CTEST1Dlg::OnInitDialog()函数中加入SetWindowText(_T("计算器by 090411322 曾庆恭")); 改窗口标题为“计算器by 090411322 曾庆恭”。
(4)完成程序编写后,编译执行。
2 程序清单:
#include "math.h" //
数学
数学高考答题卡模板高考数学答题卡模板三年级数学混合运算测试卷数学作业设计案例新人教版八年级上数学教学计划
函数运算头文件
#define PI 3.1415926535897932384626433832795 //宏定义
在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);
- 3 -
}
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.
// 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
{
- 4 -
// 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
{
// 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);
- 5 -
}
void CTEST1Dlg::OnDS() //求倒数运算:1/x
{
// TODO: Add your control notification handler code here
if(m_EDIT1_NUM == 0)
{
MessageBox("错误~请输入不为零的数~"); //除数为零时,弹出警告
}
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
{
- 6 -
// 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)
{
// 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);
}
- 7 -
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);
}
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单选框关联变量,角度运算
- 8 -
}
else
{
m_FDR=(2*PI)/360; //弧度运算
}
m_EDIT3_NUM=sin(m_EDIT1_NUM*m_FDR);
GetDlgItem(IDC_EDIT2)->ShowWindow(SW_HIDE); //在只有一个操作数时,隐藏第二个编辑框
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); //在只有一个操作数时,隐藏第二
- 9 -
个编辑框
UpdateData(FALSE);
}
void CTEST1Dlg::OnCot() //余切函数运算:cot(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=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()
- 10 -
// 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);
}
- 11 -