由于之前QT里面做过计算器,所以这次在MFC里面制作,除了函数库不同之外,逻辑都是照上一次的,而且最近特别忙,所以不会讲解太多,详情可以参考Qt开发——计算器制作
第一步:界面设计
建立基本对话框项目
设计界面如下
第二步,功能实现
头文件里面
private:
CString m_str; //显示内容
int m_num1; //储存数字1
int m_num2; //储存数字2
int m_result; //储存答案
int m_opera; //储存加减乘除,1代表+,2代表-,3代表*,4代表/
bool m_opera_has_click; //判断加减乘除是否已经按下
bool m_num1_has_click;//判断num1已经按下
构造函数
m_str="";
m_num1=0;
m_num2=0;
m_result=0;
m_opera_has_click=false;
m_num1_has_click=false;
按钮功能实现
//////////////////////0~9按钮的实现/////////////////////////////
void CCalculatorDlg::OnButton1() //按下1
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false)//判断是否按下了运算符,没按下输入数字1,按下了则输入数字2
{
m_num1=m_num1*10+1;
m_str+="1";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_num1_has_click=true;
}
else
{
m_num2=m_num2*10+1;
m_str+="1";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
}
void CCalculatorDlg::OnButton2()
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false)
{
m_num1=m_num1*10+2;
m_str+="2";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_num1_has_click=true;
}
else
{
m_num2=m_num2*10+2;
m_str+="2";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
}
void CCalculatorDlg::OnButton3()
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false)//判断是否按下了运算符,没按下输入数字1,按下了则输入数字2
{
m_num1=m_num1*10+3;
m_str+="3";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_num1_has_click=true;
}
else
{
m_num2=m_num2*10+3;
m_str+="3";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
}
void CCalculatorDlg::OnButton4()
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false)//判断是否按下了运算符,没按下输入数字1,按下了则输入数字2
{
m_num1=m_num1*10+4;
m_str+="4";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_num1_has_click=true;
}
else
{
m_num2=m_num2*10+4;
m_str+="4";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
}
void CCalculatorDlg::OnButton5()
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false)//判断是否按下了运算符,没按下输入数字1,按下了则输入数字2
{
m_num1=m_num1*10+5;
m_str+="5";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_num1_has_click=true;
}
else
{
m_num2=m_num2*10+5;
m_str+="5";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
}
void CCalculatorDlg::OnButton6()
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false)//判断是否按下了运算符,没按下输入数字1,按下了则输入数字2
{
m_num1=m_num1*10+6;
m_str+="6";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_num1_has_click=true;
}
else
{
m_num2=m_num2*10+6;
m_str+="6";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
}
void CCalculatorDlg::OnButton7()
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false)//判断是否按下了运算符,没按下输入数字1,按下了则输入数字2
{
m_num1=m_num1*10+7;
m_str+="7";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_num1_has_click=true;
}
else
{
m_num2=m_num2*10+7;
m_str+="7";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
}
void CCalculatorDlg::OnButton8()
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false)//判断是否按下了运算符,没按下输入数字1,按下了则输入数字2
{
m_num1=m_num1*10+8;
m_str+="8";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_num1_has_click=true;
}
else
{
m_num2=m_num2*10+8;
m_str+="8";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
}
void CCalculatorDlg::OnButton9()
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false)//判断是否按下了运算符,没按下输入数字1,按下了则输入数字2
{
m_num1=m_num1*10+9;
m_str+="9";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_num1_has_click=true;
}
else
{
m_num2=m_num2*10+9;
m_str+="9";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
}
void CCalculatorDlg::OnButton0()
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false)//判断是否按下了运算符,没按下输入数字1,按下了则输入数字2
{
m_num1=m_num1*10+0;
m_str+="0";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_num1_has_click=true;
}
else
{
m_num2=m_num2*10+0;
m_str+="0";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
}
/////////////////////////运算符的实现/////////////////////////////
void CCalculatorDlg::OnButtonAdd() //按下加号
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false&&m_num1_has_click==true)//判断是否按下了运算符与数字1,防止按下多次运算符与开始按下运算符
{
m_str+="+";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_opera_has_click=true;
m_opera=1;
}
}
void CCalculatorDlg::OnButtonDel() //按下减号
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false&&m_num1_has_click==true)//判断是否按下了运算符与数字1,防止按下多次运算符与开始按下运算符
{
m_str+="-";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_opera_has_click=true;
m_opera=2;
}
}
void CCalculatorDlg::OnButtonMul() //按下乘号
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false&&m_num1_has_click==true)//判断是否按下了运算符与数字1,防止按下多次运算符与开始按下运算符
{
m_str+="*";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_opera_has_click=true;
m_opera=3;
}
}
void CCalculatorDlg::OnButtonDiv() //按下除号
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==false&&m_num1_has_click==true)//判断是否按下了运算符与数字1,防止按下多次运算符与开始按下运算符
{
m_str+="/";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
m_opera_has_click=true;
m_opera=4;
}
}
void CCalculatorDlg::OnButtonEqual() //按下等号
{
// TODO: Add your control notification handler code here
if(m_opera_has_click==true&&m_num1_has_click==true)//防止开始按下等号
{
switch(m_opera)
{
case 1:
m_result=m_num1+m_num2;
break;
case 2:
m_result=m_num1-m_num2;
break;
case 3:
m_result=m_num1*m_num2;
break;
case 4:
if(m_num2==0)//防止除数为0
{
MessageBox("错误:除数为0");
//重置数据
m_opera_has_click=false;
m_num1_has_click=false;
m_str="";
m_num1=0;
m_num2=0;
m_result=0;
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
break;
}
else
{
m_result=m_num1/m_num2;
break;
}
}
//计算显示结果
m_str.Format("%d",m_result);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
//重置数据
m_opera_has_click=false;
m_num1_has_click=false;
m_str="";
m_num1=0;
m_num2=0;
m_result=0;
}
}
void CCalculatorDlg::OnButtonClear() //按下清除键
{
// TODO: Add your control notification handler code here
//重置数据
m_str="";
m_num1=0;
m_num2=0;
m_result=0;
m_opera_has_click=false;
m_num1_has_click=false;
//清空屏幕
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
最后效果如图