1、计算机课程设计论文题 目:常用功能计算器软件设计学 院:测试与光电工程学院 专业名称:电子科学与技术班级学号:10083129 学生姓名:周炜专 指导教师:黎芳芳 2013 年 6 月 常用功能计算器软件设计摘要:计算器是我们日常生活中常用的工具之一,计算器使用方法简单,操作方便,便于携带,适用于简单快捷的计算。本设计使用 Visual C+语言编写具备常用功能的计算器软件,用Microsoft Visual C+6.0 环境编译调试,制作了简洁的应用界面,使用模块化设计和自顶向下的功能实现。本软件可以进行小数量级下的加减乘除计算以及开根号、求比例等计算功能,还可以使用存储器进行多数据求和,还
2、可以用退位、清零的方式修正操作失误,在进行不符合数学规则的非法计算时会报出错误提示。关键词: 计算器 Visual C+ Microsoft Visual C+ 6.0目录1、引言(1)2、C+语言简介(1)3、Microsoft Visual C+开发环境简介(1) 4、开发,调试及运行硬件和软件环境(1) 5、软件整体设计思路(1)6、软件运行效果(2)7、源代码(3)8、致谢(23)9、参考文献(23)01、引言计算器是我们日常生活中常用的一种工具,随着电子科学的发展,计算器的功能越来越多样化、复杂化,外观也越来越多样化、轻量化,我们可以用很低的价格在市场上买到很实用很漂亮的计算器。为了
3、巩固和加深对计算机软件设计课程的掌握,本课程设计使用高级语言在 Windows 环境下实现了具备常用功能的计算器软件的设计。 2、C+语言简介C+是一种使用非常广泛的计算机编程语言。C+是一种静态数据类型检查的、支持多重编程范式的通用程序设计语言。它支持过程化程序设计、数据抽象、面向对象程序设计、泛型程序设计等多种程序设计风格。3、Microsoft Visual C+开发环境简介 Microsoft Visual C+是 Microsoft 公司推出的开发 Win32 环境程序,面向对象的可视化集成编程系统。它不但具有程序框架自动生成、灵活方便的类管理、代码编写和界面设计集成交互操作、可开发
4、多种程序等优点,而且通过简单的设置就可使其生成的程序框架支持数据库接口、OLE2,WinSock 网络、3D 控制界面。它以拥有“语法高亮” ,IntelliSense(自动完成功能)以及高级除错功能而著称。比如,它允许用户进行远程调试,单步执行等。还有允许用户在调试期间重新编译被修改的代码,而不必重新启动正在调试的程序。其编译及建置系统以预编译头文件、最小重建功能及累加连结著称。这些特征明显缩短程式编辑、编译及连结花费的时间,在大型软件计划上尤其显著。4、开发,调试及运行硬件和软件环境 处理器: Intel Pentium Dual-Core CPU T4200 2.00GHz BIOS:
5、Lenovo内存 : 2GB (金士顿 1066mhz)显卡: Intel Mobile 4 Series Express Chipest Family (256MB) 操作系统: 32-bit Windows XP编程语言: Visual C+编译环境: Microsoft Visual C+ 6.05、软件整体设计思路 1经调查,非科学计算器常用功能主要有以下三个部分:、运算功能常用的运算功能有加法计算、减法计算、乘法计算、除法计算、根式计算、比率计算;、存储功能常用的存储功能有:清除存储器中的数值(MC)、将存于存储器中的数显示在计算器的显示框上(MR)、将显示框的数值存于存储器中(MS
6、) 、将显示框的数与存储器中的数相加并进行存储(M+)。如果存储器中有数值将会显示 M 标志;、清除功能常用的清除功能有清除输入(CE)、清除键(C)、退格键(BACK) 。由于 visual c+ 6.0 自带了许多强大的数学计算库文件,这为软件的开发省去了不少的工作量,而 visual c+6.0 优秀的对话框绘制模块也使得这个小软件美观不少,为了方便计算器的使用,开发者特意加入了许多非法计算的错误提示,例如“Can not divide by zero”等。6、软件运行效果、运用计算器计算:78*80图一、运用计算器计算:78 *80 图二、在计算器上输入:78*图三、在计算器上输入:8
7、0 图四、输出结果:6240、在计算器中输入 2/02图五、非法操作,屏幕会显示错误提示7、源代码aboutdialog.cpp#include #include “aboutdialog.h“#include #include “resource.h“aboutdialog:aboutdialog( ) : CDialog ( IDD_DIALOG1 )int aboutdialog:OnInitDialog( ):PlaySound ( “music.wav“, NULL, SND_FILENAME | SND_ASYNC | SND_LOOP ) ;return 1 ;void abou
8、tdialog:OnOK( ) :PlaySound ( NULL, NULL, NULL ) ;CDialog:OnOK( ) ;void aboutdialog:OnCancel( ) :PlaySound ( NULL, NULL, NULL ) ;CDialog:OnCancel( ) ;l 3aboutdialog.h class aboutdialog : public CDialogpublic :aboutdialog( ) ;int OnInitDialog( ) ;void OnOK( ) ;void OnCancel( ) ;myapp.cpp#include #incl
9、ude “myapp.h“#include “myedit.h“#include “myframe.h“myapp a ;int myapp:InitInstance( )myframe *p ;p = new myframe ;p - ShowWindow ( 1 ) ;m_pMainWnd = p ;return 1 ;myapp.hclass myapp : public CWinApppublic :int InitInstance( ) ;4 ;myedit.cpp#include #include “myedit.h“BEGIN_MESSAGE_MAP ( myedit, CEdi
10、t )ON_WM_CREATE( )ON_WM_LBUTTONDOWN( )ON_WM_MOUSEMOVE( )END_MESSAGE_MAP( )int myedit:OnCreate ( LPCREATESTRUCT l ) CEdit:OnCreate ( l ) ;m_cur = AfxGetApp( ) - LoadStandardCursor ( IDC_ARROW ) ;return 0 ;void myedit:OnLButtonDown ( UINT flags, CPoint pt )void myedit:OnMouseMove ( UINT flags, CPoint
11、pt )SetCursor ( m_cur ) ;myedit.hclass myedit : public CEdit private :HCURSOR m_cur ;public :int OnCreate ( LPCREATESTRUCT l ) ;5void OnLButtonDown ( UINT flags, CPoint pt ) ;void OnMouseMove ( UINT flags, CPoint pt ) ;DECLARE_MESSAGE_MAP( ) ;myframe.cpp#include #include “myedit.h“#include “myframe.
12、h“#include “aboutdialog.h“#include #include “resource.h“BEGIN_MESSAGE_MAP ( myframe, CFrameWnd )ON_WM_CREATE( )ON_COMMAND ( 1001, myabout )ON_COMMAND_RANGE ( 1, 10, numbers0to9 ) ON_COMMAND ( 101, back )ON_COMMAND ( 102, ce ) ON_COMMAND ( 103, c ) ON_COMMAND ( 106, memorycancel ) ON_COMMAND ( 107, d
13、ivide )ON_COMMAND ( 108, squareroot ) ON_COMMAND ( 109, memoryrecall ) ON_COMMAND ( 110, multiply ) ON_COMMAND ( 111, percentage ) ON_COMMAND ( 112, memorystore ) ON_COMMAND ( 113, minus )ON_COMMAND ( 114, onebyx ) ON_COMMAND ( 115, memoryplus ) ON_COMMAND ( 117, plusminus ) ON_COMMAND ( 118, point
14、) ON_COMMAND ( 119, plus ) ON_COMMAND ( 120, equalto ) ON_WM_PARENTNOTIFY( )6ON_WM_KEYDOWN( )END_MESSAGE_MAP()CString myframe:m_helpstrings = “No help topic is associated with this item.“,“Delete the last digit of the displayed number.“,“Clears the displayed number.“,“Clears the current calculation.
15、“,“,“,“Clears any number stored in memory.“,“Puts this number in the calculator display.“,“Puts this number in the calculator display.“,“Puts this number in the calculator display.“,“Divdes.“,“Calculates the square root of the displayed number.“,“Recalls the number stored in memory. The number remai
16、ns in the memory.“,“Puts this number in the calculator display.“,“Puts this number in the calculator display.“,“Puts this number in the calculator display.“,“Multiplies.“,“Displays the result of the multiplication as percentage.“,“Stores the displayed number in the memory.“,“Puts this number in the
17、calculator display.“,“Puts this number in the calculator display.“,“Puts this number in the calculator display.“,“Subracts.“,“Calculates the reciprocal of the displayed number.“,“Adds the displayed number to any number already in memory.“,“Puts this number in the calculator display.“,“Changes the si
18、gn of the displayed number.“,“Inserts a decimal point.“,“Adds.“,“Performs any operation on the previous two numbers. To repeat he last operation click again.“ ;myframe:myframe( )CString mywindowclass ;CBrush mybrush ;7mybrush.CreateStockObject ( LTGRAY_BRUSH ) ;mywindowclass = AfxRegisterWndClass (
19、CS_HREDRAW | CS_VREDRAW,AfxGetApp( ) - LoadStandardCursor ( IDC_ARROW ),mybrush, AfxGetApp( ) - LoadIcon ( IDI_ICON1 ) ) ;Create ( mywindowclass, “Calculator“, WS_SYSMENU | WS_MINIMIZEBOX, CRect ( 100, 100, 400, 400 ), 0, MAKEINTRESOURCE ( IDR_MENU1 ) ) ;BOOL myframe:PreCreateWindow ( CREATESTRUCTcs
20、.dwExStyle return TRUE ;int myframe:OnCreate ( LPCREATESTRUCT l )CFrameWnd:OnCreate ( l ) ;struct buttoninfochar m_str5 ;int id ; ;buttoninfo m30 = “, 100,“Back“, 101,“CE“, 102,“C“, 103,“, 104,“, 105,“MC“, 106,“7“, 8,“8“, 9,8“9“, 10,“/“, 107,“Sqrt“, 108,“MR“, 109,“4“, 5,“5“, 6,“6“, 7,“*“, 110,“%“, 1
21、11,“MS“, 112,“1“, 2,“2“, 3,“3“, 4,“-“, 113,“1/x“, 114,“M+“, 115,“0“, 1,“+/-“, 117,“.“, 118,“+“, 119,“=“, 120 ;m_editrect = CRect ( 10, 10, 275, 35 ) ;m_e.CreateEx ( WS_EX_CLIENTEDGE, “EDIT“, “0.“, WS_VISIBLE | WS_CHILD | ES_RIGHT | ES_MULTILINE, m_editrect, this, 30 ) ;int x1 = 10, y1 = 50, x2, y2 ;
22、for ( int i = 0 ; i = 29 ; i+ )x2 = x1 + 40 ;y2 = y1 + 30 ;m_buttonrecti = CRect ( x1, y1, x2, y2 ) ;if ( i != 0 if ( i = 0 )m_st.CreateEx ( WS_EX_CLIENTEDGE, “STATIC“, “, 9WS_VISIBLE | WS_CHILD | SS_NOTIFY, m_buttonrecti, this, mi.id ) ;x1 += 45 ;if ( ( i + 1 ) % 6 = 0 ) x1 = 10 ;y1 = y1 + 40 ;m_op
23、er = ;m_over = true;m_decipoint = false ;m_mem = 0 ;return 0 ;void myframe:myabout( )aboutdialog d ;d.DoModal( ) ;void myframe:back()CString lf, rt ;m_e.GetWindowText ( m_str ) ;lf = m_str.Left ( m_str.GetLength( ) - 1 ) ;rt = m_str.Right ( 1 ) ;if ( rt = “.“ )m_decipoint = false ;if ( m_str = “0.“
24、)m_over = true ;elsem_str = lf ;else10if ( lf.Right ( 1 ) = “.“ )m_decipoint = false ;if ( lf = “0.“ )m_str = “0.“ ;m_over = true ;elsem_str = lf.Left ( lf.GetLength( ) - 1 ) ;elseif ( lf = “ )m_str = “0.“ ;m_over = true ;elsem_str = lf ;m_e.SetWindowText ( m_str ) ;void myframe:c( )m_num1 = m_num2
25、= 0 ;m_over = true ;m_decipoint = false ;m_str = “0.“ ;m_e. SetWindowText(m_str) ;void myframe:ce( )if ( m_oper = )m_num1 = 0 ;elsem_num2 = 0 ;m_str= “0.“ ;11m_e. SetWindowText ( m_str ) ;m_over = true ;m_decipoint = false ;void myframe:numbers0to9 ( int id )CString temp ;temp.Format ( “%d“, id - 1
26、) ;if ( m_over = false )m_e.GetWindowText ( m_str ) ;if ( m_str = “0.“ )return ;if ( m_str.GetLength( ) = 13 ):MessageBeep ( 1 ) ;elsem_str = m_str + temp ;m_e.SetWindowText ( m_str ) ;elsem_e.SetWindowText ( temp ) ;m_over = false ;void myframe:plusminus( )m_e.GetWindowText ( m_str ) ;m_num = atof
27、( m_str ) ;if ( m_num != 0 )m_num = m_num * -1 ;m_str.Format ( “%.8g“, m_num ) ;m_e. SetWindowText ( m_str ) ;12m_over = true ;void myframe:onebyx( )m_e.GetWindowText ( m_str ) ;m_num = atof ( m_str ) ;if ( m_num != 0 )m_num = 1 / m_num ;m_str.Format ( “%.8g“, m_num ) ;m_e.SetWindowText ( m_str ) ;m
28、_over = true ;m_decipoint = false ;void myframe:squareroot( )m_e.GetWindowText ( m_str ) ;m_num = atof ( m_str ) ;if ( m_num != 0 )m_num = sqrt ( m_num ) ;m_str.Format( “%.8g“, m_num ) ;m_e.SetWindowText(m_str) ;m_over = true ;m_decipoint = false ;void myframe:percentage()double num3 ;if ( m_oper !=
29、 )m_e.GetWindowText ( m_str ) ;m_num2 = atof ( m_str ) ;num3 = m_num2 * m_num1 ;num3 = num3 / 100 ;m_str.Format( “%.8g“, num3 ) ;13m_e.SetWindowText ( m_str ) ;m_over = true ;m_decipoint = false ;m_oper = ;void myframe:point( )if ( m_decipoint = false )if ( m_over = true ) m_e.SetWindowText ( “0.“ )
30、 ;m_over = false ;elsem_e.GetWindowText ( m_str ) ;if ( m_str = “0.“ )return ;elsem_str = m_str + “.“ ;m_e. SetWindowText ( m_str ) ;m_decipoint = true ;else:MessageBeep ( 1 ) ;void myframe:plus( )if ( m_oper != )equalto( ) ;m_oper = + ;m_e.GetWindowText ( m_str ) ;m_num1 = atof ( m_str ) ;m_over =
31、true ;14m_decipoint = false ;void myframe:minus( )if ( m_oper != )equalto( ) ;m_oper = - ;m_e.GetWindowText ( m_str ) ;m_num1 = atof ( m_str ) ;m_over = true ;m_decipoint = false ;void myframe:multiply( )if ( m_oper != )equalto( ) ;m_oper = * ;m_e.GetWindowText ( m_str ) ;m_num1 = atof ( m_str ) ;m_
32、over = true ;m_decipoint = false ;void myframe:divide( )if ( m_oper != )equalto( ) ;m_oper = / ;m_e.GetWindowText ( m_str ) ;m_num1 = atof ( m_str ) ;m_over = true ;m_decipoint = false ;void myframe:equalto( )if ( m_oper = )return ;15double num3 ;m_e.GetWindowText ( m_str ) ;m_num2 = atof ( m_str )
33、;switch ( m_oper )case + :num3 = m_num1 + m_num2 ;break ;case - :num3 = m_num1 - m_num2 ;break ;case * :num3 = m_num1 * m_num2 ;break ;case / :if ( m_num2 = 0 )m_over = true ;m_decipoint = false ;m_e.SetWindowText ( “Cannot divide by zero.“ ) ;return ;elsenum3 = m_num1 / m_num2 ;m_str.Format ( “%.8g
34、“, num3 ) ;if ( m_str = “0“ )m_str = “0.“ ;m_over = true ;m_decipoint = false ;m_oper = ;m_e.SetWindowText ( m_str ) ;16void myframe:memorystore( )m_e.GetWindowText ( m_str ) ;m_mem = atof ( m_str ) ;m_st.SetWindowText ( “M“ ) ;m_over = true ;m_decipoint = false ;m_oper = ;void myframe:memoryrecall(
35、 )m_str.Format( “%.8g“, m_mem ) ;m_e.SetWindowText ( m_str ) ;m_over = true ;m_decipoint = false ;m_oper = ;void myframe:memorycancel( )m_mem = 0 ;m_st.SetWindowText ( “ ) ;void myframe:memoryplus( )m_e.GetWindowText ( m_str ) ;m_num = atof ( m_str ) ; m_mem = m_mem + m_num ; m_st.SetWindowText ( “M
36、“ ) ;m_over = true ;m_decipoint = false ;m_oper = ;void myframe:OnParentNotify ( long int m, unsigned long int p )int mousebutton = LOWORD ( m ) ;17if ( mousebutton = WM_RBUTTONDOWN )int x = LOWORD ( p ) ;int y = HIWORD ( p ) ;CPoint pt ( x, y ) ;if ( m_editrect.PtInRect ( pt ) )MessageBox ( “Shows
37、the result.“, “Help.“, MB_OK | MB_ICONQUESTION ) ;for ( int i = 0 ; i = 29 ; i+ )if ( m_buttonrecti.PtInRect ( pt ) )MessageBox ( m_helpstringsi, “Help.“, MB_OK | MB_ICONQUESTION ) ;void myframe:OnKeyDown ( UINT ch, UINT rep, UINT flag )switch ( ch )case VK_BACK :back( );break ;case VK_NUMPAD7 :case
38、 7 :m_b7.SetState ( 1 ) ;numbers0to9 ( 8 ) ;Sleep ( 20 ) ;m_b7.SetState ( 0 ) ;break ;case VK_NUMPAD8 :case 8 :m_b8.SetState ( 1 ) ;numbers0to9 ( 9 ) ;18Sleep ( 20 ) ;m_b8.SetState ( 0 ) ;break ;case VK_NUMPAD9 :case 9 :m_b9.SetState ( 1 ) ;numbers0to9 ( 10 ) ;Sleep ( 20 ) ;m_b9.SetState ( 0 ) ;brea
39、k ;case VK_DIVIDE :m_b10.SetState ( 1 ) ;divide( );Sleep ( 20 ) ;m_b10.SetState ( 0 ) ;break ;case VK_NUMPAD4 :case 4 :m_b13.SetState ( 1 ) ;numbers0to9 ( 5 ) ;Sleep ( 20 ) ;m_b13.SetState ( 0 ) ;break ;case VK_NUMPAD5 :case 5 :m_b14.SetState ( 1 ) ;numbers0to9 ( 6 ) ;Sleep ( 20 ) ;m_b14.SetState (
40、0 ) ;break ;case VK_NUMPAD6 :case 6 :m_b15.SetState ( 1 ) ;numbers0to9 ( 7 ) ;19Sleep ( 20 ) ;m_b15.SetState ( 0 ) ;break ;case VK_MULTIPLY :m_b16.SetState ( 1 ) ;multiply( ) ;Sleep ( 20 ) ;m_b16.SetState ( 0 ) ;break ;case VK_NUMPAD1 :case 1 :m_b19.SetState ( 1 ) ;numbers0to9 ( 2 ) ;Sleep ( 20 ) ;m
41、_b19.SetState ( 0 ) ;break ;case VK_NUMPAD2 :case 2 :m_b20.SetState ( 1 ) ;numbers0to9 ( 3 ) ;Sleep ( 20 ) ;m_b20.SetState ( 0 ) ;break ;case VK_NUMPAD3 :case 3 :m_b21.SetState ( 1 ) ;numbers0to9 ( 4 ) ;Sleep ( 20 ) ;m_b21.SetState ( 0 ) ;break ;case VK_SUBTRACT :m_b22.SetState ( 1 ) ;minus( );Sleep
42、 ( 20 ) ;20m_b22.SetState ( 0 ) ;break ;case VK_NUMPAD0 :case 0 :m_b25.SetState ( 1 ) ;numbers0to9 ( 1 ) ;Sleep ( 20 ) ;m_b25.SetState ( 0 ) ;break ;case VK_DECIMAL :m_b27.SetState ( 1 ) ;point( ) ;Sleep ( 20 ) ;m_b27.SetState ( 0 ) ;break ;case VK_ADD :m_b28.SetState ( 1 ) ;plus( ) ;Sleep ( 20 ) ;m
43、_b28.SetState ( 0 ) ;break ;case VK_RETURN :m_b29.SetState ( 1 ) ;equalto( ) ;Sleep ( 20 ) ;m_b29.SetState ( 0 ) ;break ;myframe.hclass myframe : public CFrameWnd21private :myedit m_e ;CRect m_editrect ;CButton m_b30 ;CRect m_buttonrect30 ;CStatic m_st ;CString m_str ;double m_num, m_num1, m_num2, m
44、_mem ;char m_oper ;bool m_over , m_decipoint ;static CString m_helpstrings30 ;public :myframe( ) ;BOOL PreCreateWindow ( CREATESTRUCTint OnCreate ( LPCREATESTRUCT l ) ;void myabout( ) ;void back( ) ;void c( ) ;void ce( ) ;void numbers0to9 ( int id ) ;void plusminus( ) ;void onebyx( ) ;void squareroo
45、t( ) ;void percentage( ) ;void point( ) ;void plus( ) ;void minus( ) ;void multiply( ) ;void divide( ) ;void equalto( ) ;void memorystore( ) ;void memoryrecall( ) ;void memorycancel( ) ;void memoryplus( ) ;void OnParentNotify ( long int m, unsigned long int p ) ;void OnKeyDown ( UINT ch, UINT rep, U
46、INT flag ) ;22DECLARE_MESSAGE_MAP( ) ;resource.h/NO_DEPENDENCIES/ Microsoft Developer Studio generated include file./ Used by calculator.rc/#define IDD_DIALOG1 101#define IDI_ICON1 102#define IDR_MENU1 103#define IDC_BUTTON1 1002/ Next default values for new objects/ #ifdef APSTUDIO_INVOKED#ifndef A
47、PSTUDIO_READONLY_SYMBOLS#define _APS_NEXT_RESOURCE_VALUE 107#define _APS_NEXT_COMMAND_VALUE 40001#define _APS_NEXT_CONTROL_VALUE 1003#define _APS_NEXT_SYMED_VALUE 101#endif#endif10、致谢首先要感谢指导老师黎芳芳老师的严格指导和亲切关怀,从一开始选题方向的指导,以及 C+程序上的帮助,最终才使我能够顺利完成项目的设计,曾老师兢兢业业的工作精神、踏实真诚的处事态度也让我受益匪浅。值此成文之际,我向曾老师表示衷心的感谢。同时也感谢同组的周炜专同学以及我们专业其他同学,此次设计的顺利完成少不了你们的毫无保留帮助和倾尽全力的支持,在此我衷心感谢你们。由于自身水平有限,设计中难免存在一些不足之处,敬请各位老师批评指正。11、参考文献1.朱立华等.面向对象程序设计及 C+.人民邮电出版社,20122.郑莉等.C+语言程序设计(第三版).清华大学出版社,20083.余祖龙等.面向对象程序设计与 vc 程序设计入门.北京航空航天大学出版社,20104.谭浩强.C+面向对象程序设计.清