计算器实验报告.docx
- 文档编号:1699076
- 上传时间:2022-10-23
- 格式:DOCX
- 页数:7
- 大小:15.51KB
计算器实验报告.docx
《计算器实验报告.docx》由会员分享,可在线阅读,更多相关《计算器实验报告.docx(7页珍藏版)》请在冰豆网上搜索。
计算器实验报告
简单计算器
姓名:
周吉祥
实验目的:
模仿日常生活中所用的计算器,自行设计一个简单的计算器程序,实现简单的计算功能。
实验内容:
(1)体系设计:
程序是一个简单的计算器,能正确输入数据,能实现加、减、乘、除等算术运算,运算结果能正确显示,可以清楚数据等。
(2)设计思路:
1)先在VisualC++中建立一个MFC工程文件,名为calculator.
2)在对话框中添加适当的编辑框、按钮、静态文件、复选框和单选框
3)设计按钮,并修改其相应的ID与Caption.
4)选择和设置各控件的单击鼠标事件。
5)为编辑框添加double类型的关联变量m_edit1.
6)在中添加头文件,然后添加public成员。
7)打开文件,在构造函数中,进行成员初始化和完善各控件的响应函数代码。
(3)程序清单:
添加的public成员:
doubletempvalue;法2.减法3.乘法4.除法
intappend;//判断后面是否添加数字
成员初始化:
CCalculatorDlg:
:
CCalculatorDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CCalculatorDlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CCalculatorDlg)
m_edit1=;
//}}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=;
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;”语句
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算器 实验 报告