版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、面向对象程序设计 -简单计算器的设计一、问题描述简单计算器的基本功能如下:1. 四则运算,例如加减乘除等;2. 除了整数的运算也可实现小数的各类运算;3. 判断非法操作,例如判定1/0为非法操作;4. 其他特性;这个简易计算器只实现以上所述功能,即实现简单计算。二、设计思路计算器起始运行在归零状态,此时只接受数字输入,当按下一个数字按钮时,计算器只显示用户输入第一个操作数,用户在该状态下可以继续输入数字。1当第一个数字输入完成后,可以接受第二个操作数。2在数字输入结束进行运算后,按下=按钮可以根据前面按下的操作符计算结果显示,表示计算完成。3在上述计算过程中都可以接受双击等号回到清零状态。三、
2、功能函数设计:1数字按钮这个是通过最基本的onbutton()将09十个数字按钮的单击消息处理函数用类向导全部关联到此,适文本框应该只是显示当前单击的数字;2小数点按钮函数ondecimal这个函数主要是实现小数点在计算器的相关功能3列表在算法的函数onselchangeoperatorlist()这一类按钮包括“+”“-”“*”“/”,它们的单击消息在同一个处理函数中进行处理,以同样的方法把这四个按钮的单击操作关联到此函数中,使四则运算法则在列表中通过选择去实现。4“=”符号函数onenquall()实现计算器中等号的功能,双击时也可使编辑框的数字归零。四、编码实现:int calculat
3、or_type_i;double count=10,value=0;double input_data_int=0,input_data_dec=0;double inputdata1=0,inputdata2=0;char datatostr_buffer10;bool decimal=false;以上代码是添加的全局变量,为正确获取键盘输入供所有按钮函数使用。/ todo: add extra initialization herem_operatorbox.addstring(-);m_operatorbox.addstring(*);m_operatorbox.addstring(/)
4、;m_operatorbox.addstring(+);以上代码是初始化列表框,并使其在程序运行时具有“加”“减”“乘”“除”可选择功能。void cmycalculatordlg:on1button() if(decimal)input_data_dec=input_data_dec+1/count;count=10*count;else input_data_int=10*input_data_int+1; value=input_data_int+input_data_dec; _gcvt(value,10,datatostr_buffer); m_showresultedit=(lpc
5、tstr)datatostr_buffer;/ todo: add your control notification handler code hereupdatedata(false);void cmycalculatordlg:on2button() if(decimal)input_data_dec=input_data_dec+2/count;count=10*count;else input_data_int=10*input_data_int+2; value=input_data_int+input_data_dec; _gcvt(value,10,datatostr_buff
6、er); m_showresultedit=(lpctstr)datatostr_buffer;/ todo: add your control notification handler code hereupdatedata(false);void cmycalculatordlg:on3button() if(decimal)input_data_dec=input_data_dec+3/count;count=10*count;else input_data_int=10*input_data_int+3; value=input_data_int+input_data_dec; _gc
7、vt(value,10,datatostr_buffer); m_showresultedit=(lpctstr)datatostr_buffer;/ todo: add your control notification handler code hereupdatedata(false);void cmycalculatordlg:on4button() if(decimal)input_data_dec=input_data_dec+4/count;count=10*count;else input_data_int=10*input_data_int+4; value=input_da
8、ta_int+input_data_dec; _gcvt(value,10,datatostr_buffer); m_showresultedit=(lpctstr)datatostr_buffer;/ todo: add your control notification handler code hereupdatedata(false);void cmycalculatordlg:on5button() if(decimal)input_data_dec=input_data_dec+5/count;count=10*count;else input_data_int=10*input_
9、data_int+5; value=input_data_int+input_data_dec; _gcvt(value,10,datatostr_buffer); m_showresultedit=(lpctstr)datatostr_buffer;/ todo: add your control notification handler code hereupdatedata(false);void cmycalculatordlg:on6button() if(decimal)input_data_dec=input_data_dec+6/count;count=10*count;els
10、e input_data_int=10*input_data_int+6; value=input_data_int+input_data_dec; _gcvt(value,10,datatostr_buffer); m_showresultedit=(lpctstr)datatostr_buffer;/ todo: add your control notification handler code hereupdatedata(false);void cmycalculatordlg:on7button() if(decimal)input_data_dec=input_data_dec+
11、7/count;count=10*count;else input_data_int=10*input_data_int+7; value=input_data_int+input_data_dec; _gcvt(value,10,datatostr_buffer); m_showresultedit=(lpctstr)datatostr_buffer;/ todo: add your control notification handler code hereupdatedata(false);void cmycalculatordlg:on8button() if(decimal)inpu
12、t_data_dec=input_data_dec+8/count;count=10*count;else input_data_int=10*input_data_int+8; value=input_data_int+input_data_dec; _gcvt(value,10,datatostr_buffer); m_showresultedit=(lpctstr)datatostr_buffer;/ todo: add your control notification handler code hereupdatedata(false);void cmycalculatordlg:o
13、n9button() if(decimal)input_data_dec=input_data_dec+9/count;count=10*count;else input_data_int=10*input_data_int+9; value=input_data_int+input_data_dec; _gcvt(value,10,datatostr_buffer); m_showresultedit=(lpctstr)datatostr_buffer;/ todo: add your control notification handler code hereupdatedata(fals
14、e);void cmycalculatordlg:on0button() if(decimal)input_data_dec=input_data_dec+0/count;count=10*count;else input_data_int=10*input_data_int+0; value=input_data_int+input_data_dec; _gcvt(value,10,datatostr_buffer); m_showresultedit=(lpctstr)datatostr_buffer;/ todo: add your control notification handle
15、r code hereupdatedata(false);void cmycalculatordlg:ondecimalbutton() decimal=true;/ todo: add your control notification handler code herevoid cmycalculatordlg:onequalbutton() inputdata2=input_data_int+input_data_dec;decimal=false;input_data_int=0;input_data_dec=0;count=10;switch(calculator_type_i)ca
16、se 0: value=inputdata1-inputdata2; break; case 1: value=inputdata1*inputdata2; break; case 2: if(inputdata2=0) messagebox(除数不能为0!); break; else value=inputdata1/inputdata2; break; case 3: value=inputdata1+inputdata2; break;_gcvt(value,10,datatostr_buffer);m_showresultedit=(lpctstr)datatostr_buffer;u
17、pdatedata(false); void cmycalculatordlg:onchangeshowresultedit() / 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: a
18、dd your control notification handler code herevoid cmycalculatordlg:onerrspaceshowresultedit() / todo: add your control notification handler code herevoid cmycalculatordlg:onselchangeoperatorlist() inputdata1=input_data_int+input_data_dec; decimal=false; input_data_int=0; input_data_dec=0; count=10; calculator_type_i=m_operatorbox.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学英语六年级上册Recycle1教案
- 2024年银川客运车资格证考试题
- 2024年泰州客运资格证模拟考试
- 2024年阿里客运资格证模拟考试
- 2024年柳州道路客运输从业资格证理论考题
- 小学体育与健康教学
- 部编版语文二年级下册1-8单元知识点复习
- 2024年福建省龙岩市新罗区部分事业单位招聘5人历年高频500题难、易错点模拟试题附带答案详解
- 2024年福建省莆田市秀屿区工业和信息化局招聘编外工作人员高频500题难、易错点模拟试题附带答案详解
- 2024年福建省福州闽侯县县直政府系统临聘人员招聘27人高频500题难、易错点模拟试题附带答案详解
- 病人晕倒应急预案总结
- 上海/福建2022兴业银行总行科技运维部招聘考试参考题库含答案详解
- 高考英语应用文写作之科技篇
- 新南威尔士大学
- 新课改下小学美术情境化教学研究 论文
- 临床基因扩增检验实验室技术验收表
- 教学设计 《图书馆狮子》阅读指导课教学设计
- 四川省定额建设工程费用定额总说明
- 冀教版信息技术四年级教案
- GB/T 39218-2020智慧化工园区建设指南
- 开户风险测评建议答案
评论
0/150
提交评论