版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、摘要随着科学的发展,计算机的应用越來越广泛,而运用计算机中的计算机语言c卄写 程序必然是很多的。c+是目前国内外最广泛使用的程序设计语言之一,它具右处理功 能丰富、表达能力强、使用方便灵活、执行程序效率较高等优点。而11 c+得是一种结 构化程序设计语言;另外,c+的函数式结构也为实现程序化模块设计提供了强有力的 保正。所以很多人都把c+作为编程的首选语言。本次课程设计就是利用c+的丰富表达能力来编写一个简单计算器,模拟windows 系统的计算器进行简单的计算;在编写这次课程设计中要求利用mfc來画出计算器的图 形界面,并且保证可以进行基本的计算。关键字:简单计算器 mfc图形界面基本计算课
2、程设计任务书设计题目:基于mfc的简单计算器设计学生姓名岑余霞课程名称而向对象程序设计课程设计专业班级计科1003班1305/1306起止时间设计内容及要求耸简单的计算器.rn x以windows xp自带的计算器为蓝本,设计一个简单的10进制数计算 器能进行实数(整数和浮点数)的算术运算(加、减、乘、除和求余数), 耍求在编辑框屮输入运算量的输入。参考界而如下图所示,英屮红色框标 注的部分要求必须完成,蓝色框标注的部分属选作内容。该计算器主要进行实数的单目和双目运算,使用过程如下:1)单目运算,例如对于1"要求先输入数字,然后点击“in”按钮, 完成取对数运算并把结果显示在编辑框中
3、。2)双口运算,例如进行加法要求先输入第1加数,然后点击“ + ” 按钮,再输入第2加数最后点击“二”按钮,完成计算并在编辑框屮显示和 写x 抄0 果意次轆协 结注数肿划 算 偶程 运 取 课设 计 参 数进度要求参考资料!i-、t<说明1.本表应在每次实丿施前一周由负责教师填写二份,院系审批后交院系办备案,一份 由负责教师昭用。2.若填写内容较多可另纸附后。3.题多名学生共用的,在设计内容、参数、要求等方面应有所区别。教研室主任:指导教师:伍建全/陈应祖/焦小军/易军/张亚军2011 年6 月 22口目录djj1课程瓦彳注矗前二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二
4、 2第一章 设计内容及要求61.1计算器的基本要求61.2需实现的主要功能6第二章需求分析72界面及布局设计72.2简单计算的实现方法7第三章系统设计83总体功能图83.2总体流程图8第四章主要功能设计流程114.1单目运算实现流程图114.2双目运算流程图12第五章代码实现135.1代码的实现135.2运行效果图14第六章 系统测试16致谢19参考文献:21附录:源程序代码22第一章设计内容及要求1.1计算器的基本要求(1) 设计mfc的计算器界面。计算器界面是基于mfc的dialog bases所画出来的; 其中界面主要曲edit properties和push button proper
5、ties所组成。通过鼠标点击 按键即可在edit中显示数据和进行运算。(2) 程序要求用户在运行过程中注意不同函数的定义域,若用户按键的数值不符合 耍求,计算器则会弹出一个提醒你的对话框。(3) 用户在计算一次以后,可以通过按键ac清空以前记录,进行下一次的计算。1. 2需实现的主要功能(1) 需完成简单的双口运算,例如加、减、乘、除等。(2) 需完成三角函数sin, cos, tan的计算,并且要注意其定义域。(3) 需完成简单的与,或,非,异或等简单运算。(4) 需完成对数函数以10为底的log, in,及简单的阶乘运算。(5) 需完成任意数的开根和次方,并且要注意数值要求。(6) 述需完
6、成对数值的清除及清除全部内容的功能。第二章需求分析2. 1界面及布局设计系统布局主要采用了 mfc的dialog的对话框,运用了文本框、按键、面板等组件; 文本框的主要功能:联系按键,通过增加文本框的成员,将按键运用按键函数和文本框 联系起来,即按键即可在文木框中显示相应的数值。按键功能:按键用于调用按键函数, 通过按键函数來实现计算器和应的计算步骤。2.2简单计算的实现方法该计算器的简单计算实现是基于switch case语彳u,定义一个全局变量flag,通 过对flag赋值调用相应的case语句实现相应的计算。(1) 加减乘除运算的实现。该计算主要以简单的运算符+、-、*、/来实现。(2)
7、 较复杂的运算的实现,例如简单的对数运算,与或非运算,开根和次方。该计 算主要运用math, h中的函数调用实现的,不同的运算调用和应的函数來实现, 但是需注意的就是相应的函数就只能进行运算而已,而数值是否符合该函数就 需要自己进行判断,如若数值不符合函数定义域,计算则会出现错误。(3) 阶乘运算的实现。该运算的实现运用了递归,较为方便的写出递归函数即可进行阶乘运算。第三章系统设计3. 1总体功能图根据需求所作的分析,需要完成简单的双目运算,例如加、减、乘、除等;需完成 三角函数sin, cos, tan的计算,并且耍注意其定义域;需完成简单的与,或,非, 异或等简单运算;需完成对数函数以10
8、为底的log, in,及简单的阶乘运算;需完成任 意数的开根和次方,并且要注意数值要求;还需完成对数值的清除及清除全部内容的功 能。根据系统需求,计算器的总功能图如图3-1所示:主界而按键输入数字rv单口运算tv双目运算iv按键输入数7nv求余 x妁n次根 x的n次方 与或止 加减乘除 e的n次方求一歼沧乘运算 v开二次k 平方立方 三角羽数ow5运算图3-1计算机总功能图3.2总体流程图根据对计算器的功能分析,设计得到计算器总流程,如图3-2所示:计算器界面图3-2计算器流程图第四章主要功能设计流程4.1单目运算实现流程图根据对单冃运算的分析,可得单冃运算流程图,如图41所示:输入数字图41
9、单目运算流程图4.2双目运算流程图根据对双目运算的分析,可得双目运算流程图,如图42所示:计算器界面输入第一个数字是、r输入第二个数字输出结杲图4-2双目运算流程图第五章代码实现5. 1代码的实现该计算器是基于mfc下创立的,mfc会给出一部分程序代码;计算器所耍编写的 类就只有文件所给岀的caboutdlg类。在caboutdlg类屮,存在某一部分的函数代码, 而我们所要增加的就是和界面相联系的按键的按键函数,即对那件数字上的显示和运算 的执行。首先,画好基本的计算器图,如图51所示:±3 calcutorsincostanbackspace+editloginn!square_r
10、ootpi406+/-*or/andmodxorn-th root2图51运算器基本架构图5.2运行效果图开始主界面:% calcutor图5-2计算器主界面运行时界面,以求4的阶乘为例子:f图53运行时计算器界面(1)乂 calcutor图5-3运行吋计算器界面(2)x% calcutor0 5-3运行吋计算器界面(3)第六章系统测试1、void ccalcutordlg:onbuttonfushu();用例 1: input: 12output: -12用例 2: input: -12output: 122、void ccalcutordlg:onbackspace(); 用例:input
11、: 123 output: 123、void ccalcutordlg:onbuttonjia();void ccalcutordlg:onbuttondengyu(); 加法 用例:input: 12 output: 34、void ccalcutordlg:onbuttonjin();void ccalcutordlg:onbuttondengyu(); 减法 用例:input: 5 4 output: 15、void ccalcutordlg:onbuttoncheng();void ccalcutordlg:onbuttondengyu(); 乘法用例:input: 5 4 outpu
12、t: 206、void ccalcutordlg:onbutton 除();void ccalcutordlg:onbuttondengyu(); 除法用例 1: input: 16 4output: 4用例2: input: 16 0output除数不能为零!7、void ccalcutordlg:onbuttonsin();void ccalcutordlg:onbuttondengyu(); /sin用例:input: 30 output: 0.58 void ccalcutordlg:onbuttoncos();void ccalcutordlg:onbuttondengyu(); /
13、cos用例:input: 60 output: 0.59 void ccalcutordlg:onbuttontan();void ccalcutordlg:onbuttondengyu(); /tan用例 1: input: 45 output: 110、void ccalcutordlg:onbuttonlog();void ccalcutordlg:onbuttondengyu( ); /log 以 10 为底 用例:input: 10 output: 1用例2: input: -1 output:真数必须大于零!11、void ccalcutordlg:onbuttonln();voi
14、d ccalcutordlg:onbuttondengyu( ); /in用例 1 : input: 1 output: 0用例2: input: -1 output:真数必须大于零!12> void ccalcutordlg:onbuttonn();void ccalcutordlg:onbuttondengyu(); 阶乘用例:input: 4 output: 2413、void ccalcutordlg:onbuttonxy();void ccalcutordlg:onbuttondengyu( ); /x 的 y 次方 用例:input: 5 2 output: 2514、voi
15、d ccalcutordlg:onbuttonxx();void ccalcutordlg:onbuttondengyu( ); /x 的平方用例:input: 2 output: 415、void ccalcutordlg:onbuttonxxx();void ccalcutordlg:onbuttondengyu( ); /x 的立方用例:input: 2 output: 816、void ccalcutordlg:onbuttonmod();void ccalcutordlg:onbuttondengyu(); 求余数 mod 用例:input: 910 output: 917、void
16、 ccalcutordlg:onbuttonxor();void ccalcutordlg:onbuttondengyu( ); /异或用例:input: 10 output: 118、void ccalcutordlg:onbuttonexp();void ccalcutordlg:onbuttondengyu( ); /e 的多少次方用例:input: 1 output: 2.71828219、void ccalcutordlg:onbuttonor();void ccalcutordlg:onbuttondengyu(); 或用例:input: 10 output: 120、void c
17、calcutordlg:onbuttonand();void ccalcutordlg:onbuttondengyu(); 且用例:input: 10 output: 021、void ccalcutordlg:onbuttonx();void ccalcutordlg:onbuttondengyu( ); / 1 / x用例 1: input: 10 output: 0用例2: input: 0 output:分母不能为零!22、void ccalcutordlg:onbuttonlog();void ccalcutordlg:onbuttondengyu(); 开 2 次根号用例 1: i
18、nput: 100 output: 10用例2: input: -100 output:不符合定义域!23、void ccalcutordlg:onbuttonlog();void ccalcutordlg:onbuttondengyu( ); /x 开 y 次根号用例 1: input: 8 3 output: 2用例 2: input: -8 3 output:2致谢在木次课程设计中,我想如果没有老师耐心的指导,我是很难才会把这个程序写出 来的;在这里我要忠心地感谢老师对我们的帮助,述有老师对我们的监督,如果没有老 师,我想我们很多同学都不会那么有耐心的把程序写出來的。其次,我要感谢的是我
19、的朋友和同学,因为没有他们细心的帮我讲解,我想我是不 可能这么快的就把程序写出来的。最后所要感谢的就是学校,因为这几天來重庆的温度都是很高的,学校给了我们一 个舒适的环境,好让我们可以好好的写程序。签名:xxx口期:2011年7月口参考文献:1 熊歆斌.visual c+程序设计培训教程.北京:清华大学出版社2002.2 李英.visual c+编程与项目开发上海:华东理工大学出版社2008.附录:源程序代码/ calcutordlg.cpp : implementation file/#include "stdafx.h"#include "calcutor.h
20、"#include ''calcutordlg.h"#include "math.h"#define pi 3.1415926535897932384626433832795#ifdef .debug#define new debug_new#undefthis_filestatic char this_file = _file_;# endiflllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllii caboutdlg dialog u
21、sed for app aboutclass caboutdlg : public cdialogpublic:caboutdlgo;/ dialog data/afx_data(caboutdlg) enum idd = idd_aboutbox ;/ afx_data/ classwizard generated virtual function oven ides/afx_virtual(caboutdlg)/ ddx/ddv supportprotected:virtual void dodataexchange(cdataexchange* pdx); /) afx_virtual/
22、 implementationprotected:/afx_msg(caboutdlg)/)afx_msgdeclare_message_map();caboutdlg:caboutdlg(): cdialog(caboutdlg:idd) /afx_datanit(caboutdlg)/ afx_datanit void caboutdlg:dodataexchange(cdataexchange* pdx)cdialog: :dodataexchange(pdx);/afx_data_map(caboutdlg)/ afx_data_mapbegin_message_map(caboutd
23、lg, cdialog)/afx_msg_map(caboutdlg)/ no message handlers/ afx_msg_mapend_message_map()lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll/ ccalcutordig dialogccalcutordlg:ccalcutordlg(cwnd* pparent /*=null*/):cdialog(ccalcutordlg: :idd, pparent)/afx_data_init(ccalcutordlg)m
24、_a = _t(“”);/)afx_data_init/ note that loadicon does not require a subsequent destroylcon in win32 m_hlcon = afxgetapp()->loadicon(idr_mainframe);void ccalcutordlg:dodataexchange(cdataexchange* pdx)cdialog: :dodataexchange(pdx);/afx_data_map(ccalcutordlg)ddx_text(pdx, idc.edit1, m_a);/)afx_data_m
25、apbegin_message_map(ccalcutordlg, cdialog)/afx.msg.mapcccalcutordlg) on_wm_syscommand()on_wm_paint()on_wm_querydragicon() on_bn_clicked(idc_button_1, onbutton 1) 0n_bn_clicked(idc_butt0n_2, onbutton2) on_bn_clicked(idc_button_0, onbuttono) on_bn_cllcked(idc_button_3,onbutton3) on_bn_clicked(idc_butt
26、on_4, onbutton4) on_bn_clicked(idc_button_5,onbutton5) on_bn_clicked(idc_button_6, onbutton6)on_bn_clicked(idc_button_7, onbutton7)on_bn_clicked(idc_button_8, onbutton8)on_bn_cl1cked(1dc_button_9, onbutton9) on_bn_cllcked(ldc_button_dot, onbuttondot) on_bn_clicked(idc_button_fushu, onbuttonfushu) on
27、_bn_clicked(idc_buttonjia, onbuttonjia) on_bn_clicked(idc_button_dcngyu, onbuttondcngyu) on_bn_clicked(idc_buttonin, onbuttonjin) on_bn_clicked(ldc_button_cheng, onbuttoncheng) on_bn_cllcked(ldc_button_chu, onbuttonchu) on_bn_clicked(idc_backspace, onbackspace) on_bn_clicked(idc_button_sin, onbutton
28、sin)on_bn_cllcked(idc_button_cos, onbuttoncos)on_bn_clicked(idc_button_tan, onbuttontan) on_bn_clicked(idc_ac, onac)on_bn_clicked(idc_button_log, onbuttoniog) on_bn_clicked(idc_buttonn, onbuttonln) on_bn_clicked(idc_button_n, onbuttonn) on_bn_clicked(idc_button_xy,onbuttonxy) on_bn_cllcked(ldc_butto
29、n_xx, onbuttonxx) on_bn_clicked(idc_button_xxx, onbuttonxxx) on_bn_clicked(idc_button_mod,onbuttonmod) on_bn_clicked(idc_button_xor, onbuttonxor) on_bn_clicked(idc_button_pi, onbuttonpi) on_bn_clicked(idc_button_exp, onbuttonexp) on_bn_cl1cked(idc_c, onc)on_bn_cllcked(ldc_button_or, onbuttonor)on_bn
30、_clicked(idc_button_and, onbuttonand)on_bn_clicked(idc_button_x, onbuttonx)on_bn_clicked(idc_button_squarcroot, onbuttonsquarcroot) on_bn_clicked(idc_button_nroot, onbuttonnroot) /)afx_msg_mapend_message_map()/ ccalcutordig message handlersbool ccalcutordlg:oninitdialog()cdialog:onlnitdialog();/add
31、'*about." menu item to system menu./ idm_aboutbox must be in the system command range.assert(1dm_aboutbox & oxfffo) = 1dm_aboutbox); assert(idm_aboutbox < oxfooo);cmenu* psysmenu = getsystemmenu(false);if (psysmenu != null)cstring straboutmenu; straboutmenu.loadstring(ids_aboutbox);讦
32、(istraboutmenu.isemptyo) psysmenu->appendmenu(mf_separator); psysmenu->appendmenu(mf_string, idm_aboutbox, straboutmenu);/ set the icon for this dialog. the framework does this automatically / when the application's main window is not a dialog setlcon(m_hlcon, true);/ set big iconsetlcon(m
33、_hlcon, false); / set small icon/ todo: add extra initialization heren = 0; nl =0; n2 = 0;flag = 0;return true; / return true unless you set the focus to a control void ccalcutordlg:onsyscommand(uint nld, lparam iparam)if(nid & oxfffo) = idm_aboutbox)caboutdlg dlgabout;dlgabout.domodak);)elsecdi
34、alog:onsyscommand(nid, iparam);/ if you add a minimize button to your dialog, you will need the code below/ to draw the icon. for mfc applications using the document/view model,/ this is automatically done for you by the frameworkvoid ccalcutordlg:onpaint()if (islconic()cpaintdc dc(this); / device c
35、ontext for paintingsendmessage(wm_iconerasebkgnd, (wparam) dc.getsafehdc(), 0);/ center icon in client rectangleint cxlcon = getsystemmetrics(sm_cxlcon);int cylcon = getsystemmetrics(sm_cylcon);crect rect;getclientrect(&rect);int x = (rect.width() - cxlcon + 1) / 2;int y = (rect.height() cylcon
36、+ 1)/2;/ draw the icondc.drawicon(x, y, m_hlcon);elseicdialog:onpaint();/ the system calls this to obtain the cursor to display while the user drags/ the minimized window.hcursor ccalcutordlg:onqueiydragicon()return (hcursor) m_hlcon;m_a += t;updatedata(false);void ccalcutordlg:onbutton2() m_a += n2
37、k;updatedata(false);void ccalcutordlg:onbuttono() m_a += m0m;updatedata(false);void ccalcutordlg:onbutton3() m_a += m3h;updatedata(false);void ccalcutordlg:onbutton4() m_a += n4h;updatedata(false);void ccalcutordlg:onbutton5() m_a += n5h; updatedata(false);void ccalcutordlg:onbutton6() -m_a += h6h;u
38、pdatedata(false);)void ccalcutordlg:onbutton7() m_a += n7n;updatedata(false);)void ccalcutordlg:onbutton8() m_a += n8n;updatcdata(false);m_a += m9h;updatedata(false);)void ccalcutordlg:onbuttonciot()if( n = 0 )m_a +=n = 1;)updatcdata(false);void ccalcutordlg:onbackspace() int lcn = m a.gctlcngtho;if
39、( lcn != 05m_a.deiete(len-l, 1);updatedata(false);void ccalcutordlg:onbuttonfushu() n2 = atof( m_a );n2 = 0 n2;m_a.format( "%0.6gh, n2 );updatedata(false);void ccalcutordlg:onac() 全部清除? nl =0;n2 = 0;m_a.empty();m_a = ”0”;updatedata(false);m_a.releasebuffer(o);void ccalcutordlg:onc() 屏幕清空? m_a.e
40、mpty();u pdatedata(fals e);m_a = "0"m_a.releasebuffer(o);void ccalcutordlg:onbuttonpi() m_a.format( ,%>7g, pi);updatedata(false);double result = 0.0; n2 = atof( m_a );switch( flag)case 1: result = nl + n2; break;case 2: result = nl - n2; break;case 3: result = nl * n2; break;case 4:if(n
41、2 = 0)messagebox(m除数不能为零! ”); result = nl / n2; break;case 5: result = sin(nl); break;case 6: result = cos(nl); break;case 7: result = tan(nl); break;case 8: result = nl;brcak;case 9: result = nl;break;case 10:result = nl;break;case 11:result = pow(n 1, n2); break;case 12: result = nl; break;case 13
42、: result = nl; break;case 14: result = (int)nl % (int)n2; break;case 15: result = (int)nl a (int)n2;brcak;case 16: result = exp(nl);break;case 17: result = (int)nl | (int)n2;break;case 48: result = (int)nl && (int)n2;break;case 19: result = nl; break;case20: result = nl; break;case 21: resul
43、t = pow( nl, l/n2 );brcak;)m_a.format( "%.7gm, result);updatedata(false); m_a.releasebuffer(o);n 1 = atof(m_a);void ccalcutordlg:onbuttonjia() 加法n = 0;if( flag != 0)onbuttondengyu();elsenl = atof( m_a ); /updatedata(false); _ m_a.releaseb uffer(o); 把缓存区间清除 flag 二 1;n = 0;if(flag != 0)onbuttonde
44、ngyu();elsenl = atof( m_a );m_a.releasebuffer(o);flag = 2;)void ccalcutordlg:onbuttoncheng() 乘法n = 0;nl = atof( m_a );m a.releasebuffer(o);flag = 3;void ccalcutordlg:onbuttonchu() 除法n = 0;nl = atof( m_a );m_a.releasebuffer(o);flag = 4;void ccalcutordlg:onbuttonsin() /sinn = 0;nl 二 atof( m_a );nl 二 n
45、l* pi/180;m_a.releasebuffer(0);flag = 5;void ccalcutordlg:onbuttoncos() /cosn = 0;nl = atof( m_a );nl =nl *pl7 180;m_a.releaseb u ffer(o);flag = 6;void ccalcutordlg:onbuttontan()/tann = 0;nl = atof( m_a );if(nl !=0&&int(nl * pi /180) % 90 = 0 && int(nl * pi / 180) % 90 !=0) messagebox( “定义域错误!”);nl =nl *pi/ 180; m_a.releasebuffer(0); flag = 7;void ccalcutordlg:onbuttonlog() /log 以 10 为底 n = 0;nl = atof( m_a );if( nl <= 0 )mcssagcbox( ”真
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 护理远程医疗教学目标概述
- 护理伦理与实践
- 2026年放射科火灾疏散演练题
- 2026年机关工会工作实务应知应会知识竞赛题
- 2026年厂矿企业安全生产诚信体系建设题库
- 2026年国有林场招聘面试题库
- 青年时代英雄演讲稿
- 让更多的学生快乐演讲稿
- 南昌银行贺州市平桂区2026届秋招数据挖掘岗笔试核心题库
- 日语我的学园生活演讲稿
- T-CAPE 10001--2017 设备管理体系 要求
- 2026年山东省综合评标专家(水利工程)核心备考题库(含典型题、重点题)
- 2025年中国商用具身智能白皮书
- 归纳玉器在中国古代文化中的审美意义和文化内涵 美术鉴赏论文
- 《阿Q正传》第七、八章课件
- DB3401∕T 267-2022 水稻沼液施肥技术规程
- 外贸合同取消协议模板
- DB3204-T 1044-2022 建设工程综合监督用语标准
- DB61-T 1364-2020 海绵城市透水铺装设计规范
- 新版中华民族共同体概论课件第十讲中外会通与中华民族巩固壮大(明朝时期)-2025年版
- 氟化工艺作业安全培训
评论
0/150
提交评论