1、软件基础课程设计报告 一、需求分析系统目标:设计的计算器至少能够进行简单的四则运算和求倒数求反以及开方运算。主体功能:程序能实现:加,减,乘,除,开方,倒数等运算功能;还要实现数据的输入,输出,计算,显示及程序退出等功能。另外还可以实现多种科学计算的功能,如:三角函数的计算,弧度与角度间的转换,对数指数的计算等。开发环境:操作系统:Windows XP编程环境:MicrosoftVisualC+6.0 二、功能说明(1)包含的功能有:加、减、乘、除运算,开方、求倒数、三角函数、弧度与角度间的转换、对数指数的计算等功能。(2)计算器上数字09为一个控件数组,加、减、乘、除为一个控件数组,其余为单
2、一的控件。(3)给对话框添加菜单。(4)计算功能基本上是用系统内部函数。(5)程序可以能自动判断输入数据的正确性,保证不出现多于一个小数点、以0开头等不正常现象。(6)“CE”按钮可以清除所有已输入的数据从头计算,“Back”按钮可以实现退位功能。(7)能够显示时间、日期。三、详细设计(1)流程图创建工程(创建一个对话框应用程序)设计对话框界面设置各控件属性为编辑框连接成员变量为类添加数据成员,初始化构造函数。为所有按钮添加事件对类添加成员函数(更新编辑框)并进行实现实现对各按钮事件的实现(2)编辑资源(3)效果图四、程序附录计算器Dlg.cpp :#include stdafx.h#incl
3、ude 计算器.h#include 计算器Dlg.h#include math.h#includeFirstDlg.h#include #define PI 3.14;#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD
4、 = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAbout
5、Dlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMyDlg dialogCMyDlg:CMyDlg(
6、CWnd* pParent /*=NULL*/): CDialog(CMyDlg:IDD, pParent)/AFX_DATA_INIT(CMyDlg)m_result = 0.0;/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);void CMyDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX
7、_DATA_MAP(CMyDlg)DDX_Control(pDX, IDC_EDIT1, m_EDIT);DDX_Text(pDX, IDC_EDIT1, m_result);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CMyDlg, CDialog)/AFX_MSG_MAP(CMyDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_NUM0, OnNum0)ON_BN_CLICKED(IDC_NUM1, OnNum1)ON_BN_CLICKED(IDC_NUM2, OnNum2)
8、ON_BN_CLICKED(IDC_NUM3, OnNum3)ON_BN_CLICKED(IDC_NUM4, OnNum4)ON_BN_CLICKED(IDC_NUM5, OnNum5)ON_BN_CLICKED(IDC_NUM6, OnNum6)ON_BN_CLICKED(IDC_NUM7, OnNum7)ON_BN_CLICKED(IDC_NUM8, OnNum8)ON_BN_CLICKED(IDC_NUM9, OnNum9)ON_BN_CLICKED(IDC_DOT, OnDot)ON_BN_CLICKED(IDC_CE, OnCe)ON_BN_CLICKED(IDC_jia, Onji
9、a)ON_BN_CLICKED(IDC_jian, Onjian)ON_BN_CLICKED(IDC_cheng, Oncheng)ON_BN_CLICKED(IDC_chu, Onchu)ON_BN_CLICKED(IDC_dengyu, Ondengyu)ON_BN_CLICKED(IDC_daoshu, Ondaoshu)ON_BN_CLICKED(IDC_zhengfu, Onzhengfu)ON_BN_CLICKED(IDC_BACKSPACE, OnBackspace)ON_BN_CLICKED(IDC_Ln, OnLn)ON_BN_CLICKED(IDC_log, Onlog)O
10、N_BN_CLICKED(IDC_RADIO2, OnRadio2)ON_BN_CLICKED(IDC_RADIO1, OnRadio1)ON_BN_CLICKED(IDC_sqrt, Onsqrt)ON_BN_CLICKED(IDC_sin, Onsin)ON_BN_CLICKED(IDC_cos, Oncos)ON_BN_CLICKED(IDC_tan, Ontan)ON_BN_CLICKED(IDC_pingfang, Onpingfang)ON_BN_CLICKED(IDC_lifang, Onlifang)ON_BN_CLICKED(IDC_mi, Onmi)ON_BN_CLICKE
11、D(IDC_Exp, OnExp)ON_BN_CLICKED(IDC_10demi, On10demi)ON_BN_CLICKED(IDC_jiecheng, Onjiecheng)ON_COMMAND(ID_TIME, OnTime)ON_COMMAND(ID_DATE, OnDate)ON_COMMAND(ID_EXIT, OnExit)ON_COMMAND(ID_HELP, OnHelp)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMyDlg message handlersBOOL CMyDlg:OnInitDialog()CDialog:OnInitDialog(
12、);/ Add About. menu item to system menu./ IDM_ABOUTBOX must be in the system command range.ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this aut
13、omatically/ when the applications main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small icon/ TODO: Add extra initialization hereCheckRadioButton(IDC_RADIO1,IDC_RADIO2,IDC_RADIO1);UpdateData(FALSE); quan2=1; dotflag=0; numflag=0; return TRUE; / return TR
14、UE unless you set the focus to a controlvoid CMyDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog:OnSysCommand(nID, lParam);/ If you add a minimize button to your dialog, you will need the code below/ to draw the icon. For MFC
15、 applications using the document/view model,/ this is automatically done for you by the framework.void CMyDlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMet
16、rics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;/ Draw the icondc.DrawIcon(x, y, m_hIcon);elseCDialog:OnPaint();/ The system calls this to obtain the cursor to display while the
17、user drags/ the minimized window.HCURSOR CMyDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CMyDlg:OnNum0() UpdateData();if(dotflag)quan2*=10;m_result+=0/(double)quan2;elseif(numflag)m_result=m_result*10+0;else m_result=0; numflag=1;UpdateData(FALSE);void CMyDlg:OnNum1() UpdateData();if(dotflag)q
18、uan2*=10;m_result+=1/(double)quan2;elseif(numflag)m_result=m_result*10+1;else m_result=1; numflag=1;UpdateData(FALSE);void CMyDlg:OnNum2() UpdateData();if(dotflag)quan2*=10;m_result+=2/(double)quan2;elseif(numflag)m_result=m_result*10+2;else m_result=2; numflag=1;UpdateData(FALSE);void CMyDlg:OnNum3
19、() UpdateData();if(dotflag)quan2*=10;m_result+=3/(double)quan2;elseif(numflag)m_result=m_result*10+3;else m_result=3; numflag=1;UpdateData(FALSE);void CMyDlg:OnNum4() UpdateData();if(dotflag)quan2*=10;m_result+=4/(double)quan2;elseif(numflag)m_result=m_result*10+4;else m_result=4; numflag=1;UpdateDa
20、ta(FALSE);void CMyDlg:OnNum5() UpdateData();if(dotflag)quan2*=10;m_result+=5/(double)quan2;elseif(numflag)m_result=m_result*10+5;else m_result=5; numflag=1;UpdateData(FALSE);void CMyDlg:OnNum6() UpdateData();if(dotflag)quan2*=10;m_result+=6/(double)quan2;elseif(numflag)m_result=m_result*10+6;else m_
21、result=6; numflag=1;UpdateData(FALSE);void CMyDlg:OnNum7() UpdateData();if(dotflag)quan2*=10;m_result+=7/(double)quan2;elseif(numflag)m_result=m_result*10+7;else m_result=7; numflag=1;UpdateData(FALSE);void CMyDlg:OnNum8() UpdateData();if(dotflag)quan2*=10;m_result+=8/(double)quan2;elseif(numflag)m_
22、result=m_result*10+8;else m_result=8; numflag=1;UpdateData(FALSE);void CMyDlg:OnNum9() UpdateData();if(dotflag)quan2*=10;m_result+=9/(double)quan2;elseif(numflag)m_result=m_result*10+9;else m_result=9; numflag=1;UpdateData(FALSE);void CMyDlg:OnDot() UpdateData();dotflag=1;m_result+=0.0;UpdateData(FA
23、LSE);void CMyDlg:OnCe() UpdateData();m_EDIT.SetSel(0,-1);m_EDIT.ReplaceSel();m_result=0;num1=0;num2=0;numflag=0;dotflag=0;quan2=1;UpdateData(FALSE);void CMyDlg:Onjia() UpdateData();num1=m_result;cal=+;numflag=0;dotflag=0;quan2=1;void CMyDlg:Onjian() UpdateData();num1=m_result;cal=-;numflag=0;dotflag
24、=0;quan2=1;void CMyDlg:Oncheng() UpdateData();num1=m_result;cal=*;numflag=0;dotflag=0;quan2=1;void CMyDlg:Onchu() UpdateData();num1=m_result;cal=/;numflag=0;dotflag=0;quan2=1;void CMyDlg:Ondengyu() UpdateData();num2=m_result;switch(cal)case+:m_result=num1+num2;break;case-:m_result=num1-num2;break;ca
25、se*:m_result=num1*num2;break;case/:m_result=num1/num2;break;casex:m_result=pow(num1,num2);break;numflag=0;dotflag=0;quan2=1;UpdateData(FALSE);void CMyDlg:Ondaoshu() UpdateData();m_result=1/m_result;numflag=0;dotflag=0;quan2=1;UpdateData(FALSE);void CMyDlg:Onzhengfu() UpdateData();m_result=0-m_result
26、;UpdateData(FALSE);void CMyDlg:OnBackspace() UpdateData();static int dotquan=quan2;long temp;if(dotflag&numflag)if(dotquan=10)temp=(long)(m_result*dotquan);m_result=(double)(temp/10);dotquan/=10;m_result=m_result/dotquan;else/UpdateData();if(numflag&m_result)m_result=(long)m_result/10;UpdateData(FAL
27、SE);void CMyDlg:OnLn() UpdateData();m_result=log(m_result);numflag=0;dotflag=0;quan2=1;UpdateData(FALSE);void CMyDlg:Onlog() UpdateData();m_result=log(m_result)/log(10);numflag=0;dotflag=0;quan2=1;UpdateData(FALSE);void CMyDlg:OnRadio2() m_IsDegree=1;i=1;UpdateData(TRUE);void CMyDlg:OnRadio1() m_IsD
28、egree=0;i=(2*3.1415926)/360;UpdateData(FALSE);void CMyDlg:Onsqrt()UpdateData(); if(m_result0)MessageBox(输入无效!);return;m_result = sqrt(m_result);UpdateData(FALSE);void CMyDlg:Onsin() m_result=sin(m_result*i);UpdateData(FALSE);void CMyDlg:Oncos() m_result=cos(m_result*i);UpdateData(FALSE);void CMyDlg:
29、Ontan() m_result=tan(m_result*i);UpdateData(FALSE);void CMyDlg:Onpingfang() UpdateData();m_result=m_result*m_result;UpdateData(FALSE);void CMyDlg:Onlifang() UpdateData();m_result=m_result*m_result*m_result;UpdateData(FALSE);void CMyDlg:Onmi() UpdateData();num1=m_result;cal=x;numflag=0;dotflag=0;quan
30、2=1;void CMyDlg:OnExp() m_result=exp(m_result);UpdateData(FALSE);void CMyDlg:On10demi() UpdateData();m_result=pow(10,m_result);UpdateData(FALSE);void CMyDlg:Onjiecheng() if(m_result=1;q-)m_result*=q;UpdateData(FALSE);void CMyDlg:OnDate() CFirstDlg dlg;dlg.DoModal();void CMyDlg:OnTime() UpdateData();
31、TIME=1;CTime tNow;tNow=CTime:GetCurrentTime();if(TIME)m_EDIT.SetSel(0,-1);m_EDIT.ReplaceSel(); CString sNow=tNow.Format(%I:%M:%S); m_EDIT.SetSel(0,-1);m_EDIT.ReplaceSel(sNow);elseCString sNow=tNow.Format(%I:%M:%S); m_EDIT.SetSel(0,-1);m_EDIT.ReplaceSel(sNow);void CMyDlg:OnExit() OnOK();void CMyDlg:OnHelp() MessageBox(班级:09通信一班 组员:陈熙竹 叶文晖 周方);