C++语言计算器的实验报告.docx
《C++语言计算器的实验报告.docx》由会员分享,可在线阅读,更多相关《C++语言计算器的实验报告.docx(11页珍藏版)》请在冰点文库上搜索。
![C++语言计算器的实验报告.docx](https://file1.bingdoc.com/fileroot1/2023-5/4/0064b153-3665-4ce4-9e9c-347889c5e913/0064b153-3665-4ce4-9e9c-347889c5e9131.gif)
C++语言计算器的实验报告
1.题目:
简单计算器
2.实验目的:
模仿日常生活中所用的计算器,自行设计一个简单的计算器程序,实现简单的计算功能。
3.实验主要硬件软件环境:
Window7VisualC++
4.实验内容:
(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;
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()//按钮“C”
{
//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;
}
voidCCalculatorDlg:
:
OnRadio1()//按钮“Sin”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_edit1=sin(result);
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnRadio2()//按钮“Cos”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_edit1=cos(result);
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnRadio3()//按钮“Tan”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_edit1=tan(result);
UpdateData(FALSE);
}
voidCCalculatorDlg:
:
OnRadio4()//按钮“Log10”
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_edit1=log10(result);
UpdateData(FALSE);
}
(4)程序调试:
简单计算器:
测试与思考:
5.小结:
A.在做上面那个简单计算器的时候,遇到的问题很少,而且差不多都是些很小的问题,很容易就解决了
B.刚开始,在老师没讲课之前,不知道如何设置响应事件,经老师讲解后,很快就解决了
C.在运行的调试的时候,发现计算器的减法与除法的功能出错,原来是减数与被减数、除数与被除数的位置互换了;还有,连续点击“=”时显示结果会不停变动,解决方法是:
加“sort=0;”语句;另外,在点击“=”后,再点击数字键时,会发现点击的相应数字会显示在原显示结果的后面,解决方法是:
加“append=1;”语句
D.后来,在做后面的测试与思考时,遇到的问题就很多了。
其中最棘手的就是Backspace键和连续四则运算的实现,其次就是M系列按键(MS:
保存;MC:
清除;MR:
读数;M+:
数加)的理解和响应代码的编写,再者就是正负号和小数点的处理,其工程量非常大。
E.但是还有很多细节问题解决不了,例如小数后面的零因为是double型的而不是字符型,所以显示不出来,还有连续的四则运算实现不了,不过连续的单一运算还是可以运行的。
F.通过此次试验,我初次认识了MFC,了解到其功能的强大,及其实用性之强,只不过我还只是接触到其冰山一角而已,要想真正地掌握MFC还有很长的路要走,还要投入更多精力。