c++计算器实验报告.docx
《c++计算器实验报告.docx》由会员分享,可在线阅读,更多相关《c++计算器实验报告.docx(8页珍藏版)》请在冰豆网上搜索。
![c++计算器实验报告.docx](https://file1.bdocx.com/fileroot1/2023-2/3/2f3e8846-8683-4324-9ce5-d31a7439fe54/2f3e8846-8683-4324-9ce5-d31a7439fe541.gif)
c++计算器实验报告
简单计算器
姓名:
周吉祥
实验目的:
模仿日常生活中所用的计算器,自行设计一个简单的计算器程序,实现简单的计算功能。
实验内容:
(1)体系设计:
程序是一个简单的计算器,能正确输入数据,能实现加、减、乘、除等算术运算,运算结果能正确显示,可以清楚数据等。
(2)设计思路:
1)先在VisualC++6.0中建立一个MFC工程文件,名为calculator.
2)在对话框中添加适当的编辑框、按钮、静态文件、复选框和单选框
3)设计按钮,并修改其相应的ID与Caption.
4)选择和设置各控件的单击鼠标事件。
5)为编辑框添加double类型的关联变量m_edit1.
6)在calculatorDlg.h中添加math.h头文件,然后添加public成员。
7)打开calculatorDlg.cpp文件,在构造函数中,进行成员初始化和完善各控件的响应函数代码。
(3)程序清单:
添加的public成员:
doubletempvalue;//存储中间变量
doubleresult;//存储显示结果的值
intsort;//判断后面是何种运算:
1.加法2.减法3.乘法4.除法
intappend;//判断后面是否添加数字
成员初始化:
CCalculatorDlg:
:
CCalculatorDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CCalculatorDlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CCalculatorDlg)
m_edit1=0.0;
//}}AFX_DATA_INIT
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
tempvalue=0;
result=0;
sort=0;
append=0;
}
各控件响应函数代码:
voidCCalculatorDlg:
:
OnButton1()//按钮“1”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+1;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnButton2()//按钮“2”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+2;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnButton3()//按钮“3”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+3;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnButton4()//按钮“4”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+4;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnButton5()//按钮“5”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+5;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnButton6()//按钮“6”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+6;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnButton7()//按钮“7”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+7;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnButton8()//按钮“8”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+8;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnButton9()//按钮“9”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+9;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnBUTTONzero()//按钮“0”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+0;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnBUTTONequal()//按钮“=”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
switch(sort)
{
case1:
result=result+tempvalue;break;
case2:
result=tempvalue-result;break;
case3:
result=result*tempvalue;break;
case4:
result=tempvalue/result;break;
}
m_edit1=result;
sort=0;
append=1;
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnBUTTONclean()//按钮“退出”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
tempvalue=0;
result=0;
m_edit1=0.0;
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnBUTTONplus()//按钮“+”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
sort=1;
tempvalue=result;
m_edit1=0;
append=1;
}
voidCCalculatorDlg:
:
OnBUTTONminus()//按钮“-”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
sort=2;
tempvalue=result;
m_edit1=0;
append=1;
}
voidCCalculatorDlg:
:
OnBUTTONmulti()//按钮“*”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
sort=3;
tempvalue=result;
m_edit1=0;
append=1;
}
voidCCalculatorDlg:
:
OnBUTTONdiv()//按钮“/”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
sort=4;
tempvalue=result;
m_edit1=0;
append=1;
}
(4)程序调试:
简单计算器
测试与思考:
1.小结:
A.在做上面那个简单计算器的时候,遇到的问题很少,而且差不多都是些很小的问题,很容易就解决了
B.刚开始,在老师没讲课之前,不知道如何设置响应事件,经老师讲解后,很快就解决了
C.在运行的调试的时候,发现计算器的减法与除法的功能出错,原来是减数与被减数、除数与被除数的位置互换了;还有,连续点击“=”时显示结果会不停变动,解决方法是:
加“sort=0;”语句;另外,在点击“=”后,再点击数字键时,会发现点击的相应数字会显示在原显示结果的后面,解决方法是:
加“append=1;”语句