( 2009 -- 2010 年度第 2学期)
C++课程设计报告
计算器的设计
专业 计算机科学与技术(软件开发)
王 榕 2010年6月21日
学生姓名 班学
级 号
指导教师 完成日期
C++课程设计报告(2010))
目 录
1 概 述 ......................................................... 1 1.1 课程设计目的 .................................................. 1 1.2 课程设计内容 .................................................. 1 2 系统需求分析 .................................................... 1 2.1 系统目标 ...................................................... 1 2.2 主体功能 ...................................................... 1 2.3 开发环境 ...................................................... 1 3 系统概要设计 .................................................... 2 3.1 系统流程图 .................................................... 2 3.2 系统功能划分 .................................................. 2 4系统详细设计..................................................... 3 4.1 设计步骤 ...................................................... 3 4.2 界面设计 ...................................................... 5 5 测试 ............................................................ 7 5.1 测试方案 ...................................................... 7 5.2 测试结果 ...................................................... 7 6 小结 ............................................................ 8 参考文献 ......................................................... 10 附 录 .......................................................... 11 附录1 源程序清单 ................................................. 11
C++课程设计报告(2010)
计算器的设计 1 概 述
1.1 课程设计目的
1、巩固并加深学生对C++语言程序设计知识的理解;
2、培养学生面向对象的程序设计思想,使学生认识面向过程和面向对象两种设计方法的区别;
3、进一步掌握和应用VC++ 6.0集成开发环境; 4、提高运用C++语言解决实际问题的能力;
5、初步掌握开发小型实用软件的基本方法,能独立设计、实现基本的MIS系统; 6、掌握书写程序设计开发文档的能力(书写课程设计实验报告)。 1.2 课程设计内容 课题名称:计算器的实现 说明:实现一个计算器。 要求:
1)用“计算器”的标准视图执行简单的计算。 2)用其科学型视图执行高级的科学计算。
2 系统需求分析
2.1 系统目标
我们在学习生活中,常会遇到一些繁杂的数值运算,这时候我们就必须用到科学计算器,所以便着手开发了这个计算器程序,以便用于自己的学习工作。要计算功能有以下几个方面:加法,减法,乘法,除法,求幂,求模,求平方根,求Sin,求Cos。 2.2 主体功能
1、十进制数的加、减、乘、除、乘方、取模等简单计算。
2、科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等函数运算。
3、以角度、弧度两种方式实现上述部分函数。 2.3 开发环境
Windows操作系统,VC++ 6.0, OFFICE WORD
1
C++课程设计报告(2010)
3 系统概要设计
3.1 系统流程图
开始 输入第一个操作数 加法 减法 乘法 除法 求幂 求模 寄存器清零 开平方 求正弦 求余弦 求对数 输入第二个操作数 显示结果 结束 图3-1 系统流程图
3.2 系统功能划分
表3-1 系统功能表
序号 1 2 3 4 5 6 7 8 9 10 11 12 13 文件名 + - * / % ^ S Sin Cos ln n! = C 主要功能 加法求和 减法求差 乘法求积 除法求商 求模 求x的y次幂 求平方根 求正弦 求余弦 求对数 求阶乘 等于 寄存器清零 备注 两个操作数 两个操作数 两个操作数 两个操作数 两个操作数 两个操作数 一个操作数 一个操作数 一个操作数 一个操作数 一个操作数 1) 假设执行操作“+”,先输入一个数,再输入“+”,再输入另一个数,最
2
C++课程设计报告(2010)
后输入“+”。整个功能“+”就执行完毕。
2)执行操作“Sin”,先输入一个数,在输入“Sin”,整个功能“Sin”就执行完毕。
3)执行操作“n!”,先输入一个数,再输入“n!’,整个功能“n!”执行完毕。
4系统详细设计
4.1 设计步骤
打开Microsoft Visual C++ 6.0,在文件中点击新建,在弹出框内选择MFC AppWizard[exe]工程,输入工程名yeyahui及其所在位置,点击确定,如图4-1所示。
图4-1 新建MFC AppWizard[exe]工程
将弹出MFC AppWizard-step 1对话框,选择基本对话框,点击完成,如图4-2所示。
3
C++课程设计报告(2010)
图4-2基本对话框
这样,MFC AppWizard就建立了一个基于对话窗口的程序框架,如图4-3所示。
图4-3新建的对话框
4
C++课程设计报告(2010)
4.2 界面设计
界面设计主要是创建控件,在图4-3所示的Resoure View选项卡中打开Dialog资源组,双击IDD_ZHOUTONG_DIALOG,在右边的窗口中显示出待编辑的对话框。开始摆放控件,包括编辑框和按钮的创建。按钮的创建以“1”为例进行介绍,其他按钮的创建可参照此进行操作。
1)在图4-3中Controls的“编辑框”按钮上单击鼠标左键,在对话框编辑窗口上合适的位置按下鼠标左键并拖动鼠标画出一个大小合适的编辑框。在编辑框上单击鼠标右键,在弹出的快捷莱单中选择属性选项,此时弹出Edit属性对话框,如图4-4所示,在该对话框中输入ID属性。
图4-4 Edit属性对话框
2)在图4-2中Controls的“Button”按钮上单击鼠标左键,在对话框上的合适的位置上按下鼠标左键并拖动鼠标画出一个大小合适的下压式按钮。在按钮上单击鼠标右键,在弹出的快捷菜单中选择属性选项,此时也弹出Push Button属性对话框,如图4-5所示,在该对话框中输入控件的ID值和标题属性。
图4-5 Push Button属性
按照上面的操作过程编辑其他按钮和编辑框等对象的属性。
5
C++课程设计报告(2010)
对象 编辑框 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 按钮 表4-1 各按钮和编辑框等对象的属性
ID 标题或说明 IDC_xianshi 结果显示 IDC_BUTTON0 0 IDC_BUTTON1 1 IDC_BUTTON2 2 IDC_BUTTON3 3 IDC_BUTTON4 4 IDC_BUTTON5 5 IDC_BUTTON6 6 IDC_BUTTON7 7 IDC_BUTTON8 8 IDC_BUTTON9 9 IDC_change +/- IDC_point . IDC_add + IDC_difference - IDC_multiply * IDC_devide / IDC_sqrt sqrt IDC_daoshu 1/x IDC_jiecheng n! IDC_ln ln IDC_sin sin IDC_cos cos IDC_pow x^y IDC_Onequal =
完成后界面如图4-6所示。
图4-6 设计界面
加入标准型和科学型按键:
6
C++课程设计报告(2010)
图4-7 加入标准型和科学型按键界面
标准型:
图4-8标准型界面
科学型:
图4-9科学型界面
5 测试
5.1 测试方案
先用标准型进行简单的四则运算:+ - * /,测试完成后。再用科学型进行稍复杂的运算:sin,cos,tan,log,ln,n!等,核对其计算结果的正确性。 5.2 测试结果
经测试标准型计算器,进行简单的四则运算,结果均正确,如图5-1所示。
7
C++课程设计报告(2010)
图5-1 四则运算测试结果图
测试科学型计算器,进行sin,cos,tan,log,ln,n!等运算,结果均正确,求sin30`(如图5-2所示)。
图5-2 求sin30测试结果图
求5 !(如图5-3所示),测试完成。
图5-3 求5!测试结果图
6 小结
通过两个星期的折腾,总算把课程设计给完成了,这是一个坚苦而又漫长的过程。看着劳动成果,很欣慰!
刚开始,可以说是没有头绪,于是就去图书馆找资料,找到了一些关于程序方面的,可是这点小进展远远不够,这只是一个小小的开始。下一步是上网查,找到了些与我题目相似的,那时我很高兴,可是那还不是我要的,于是又上网查到了些有关的函数等等,终于在我的努力下,完成了这个程序。
8
C++课程设计报告(2010)
虽然对着电脑做程序,有点累有点热,可是当看到劳动成果时,真是别有一番滋味在心头啊!世上无难事,只怕有心人,的确如此。
做完这个课程设计,我的自信一下子提高了,我也会写程序了;尽管对于有些人这种程序会很简单,可对我说,已经很不容易了。这次体验为以后的学习计算机的我增强了信心。享受劳动成果的滋味实在很美妙啊!
9
C++课程设计报告(2010)
参考文献
[1]谭浩强. C程序设计(第三版)(M).北京:清华大学出版社,2005.
[2]谭浩强. C程序设计题解与上机指导(第三版) (M).北京:清华大学出版社,2005. [3]甄炜. C++语言和面向对象程序设计教程(M).北京:机械工业出版社,2005.
10
C++课程设计报告(2010)
附 录
附录1 源程序清单
// CalculatorDlg.cpp : implementation file #include \"stdafx.h\" #include \"Calculator.h\" #include \"CalculatorDlg.h\" #include \"math.h\" #ifdef _DEBUG
#define new DEBUG_NEW #undef THIS_FILE
static char THIS_FILE[] = __FILE__; #endif
///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About
class CAboutDlg : public CDialog {
public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() };
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT }
void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP }
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CCalculatorDlg dialog
11
// DDX/DDV support C++课程设计报告(2010)
CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/) : CDialog(CCalculatorDlg::IDD, pParent) { //{{AFX_DATA_INIT(CCalculatorDlg) m_result = _T(\"\"); m_isDegree = -1; m_sentific = -1; //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_hAccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDD)); DtoR=1; m_coff=0.1; m_bCoff=0; m_errorState = ErrNone; m_bOperandAvail=FALSE; m_operator=OpNone; }
void CCalculatorDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCalculatorDlg) DDX_Text(pDX, IDC_DISPLAY, m_result); DDX_Radio(pDX, IDC_DEGREE, m_isDegree); DDX_Radio(pDX, IDC_SENTIFIC, m_sentific); //}}AFX_DATA_MAP }
BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog) //{{AFX_MSG_MAP(CCalculatorDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_COMMAND_RANGE(IDC_0, IDC_9, OnOperandInput) ON_BN_CLICKED(IDC_ADD, OnAdd) ON_BN_CLICKED(IDC_MINUS, OnMinus) ON_BN_CLICKED(IDC_DIVID, OnDivid) ON_BN_CLICKED(IDC_MULTIPLY, OnMultiply) ON_BN_CLICKED(IDC_EQUAL, OnEqual) ON_BN_CLICKED(IDC_SQRT, OnSqrt) ON_BN_CLICKED(IDC_SIGN, OnSign) ON_BN_CLICKED(IDC_RECIPROCAL, OnReciprocal) ON_BN_CLICKED(IDC_POINT, OnPoint) ON_BN_CLICKED(IDC_CLEAR, OnClear) ON_BN_CLICKED(IDC_LOG, OnLog) ON_BN_CLICKED(IDC_LN, OnLn) ON_BN_CLICKED(IDC_FACTORIAL, OnFactorial) ON_BN_CLICKED(IDC_EXP, OnExp) ON_BN_CLICKED(IDC_SENTIFIC, OnSentific) ON_BN_CLICKED(IDC_SIN, OnSin) ON_BN_CLICKED(IDC_SQUAR, OnSquar) ON_BN_CLICKED(IDC_TAN, OnTan) ON_BN_CLICKED(IDC_STANDARD, OnStandard) ON_BN_CLICKED(IDC_DEGREE, OnDegree) ON_BN_CLICKED(IDC_RAD, OnRad) ON_BN_CLICKED(IDC_COS, OnCos) //}}AFX_MSG_MAP END_MESSAGE_MAP()
12
C++课程设计报告(2010)
///////////////////////////////////////////////////////////////////////////// // CCalculatorDlg message handlers BOOL CCalculatorDlg::OnInitDialog() { CDialog::OnInitDialog(); // 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 < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { 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 SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here GetWindowRect(rcSentific); CWnd* m_pMark=GetDlgItem(IDC_DISPLAY); ASSERT(m_pMark); CRect rect; m_pMark->GetWindowRect(rect); rcStandard=rcSentific; rcStandard.right=rect.right+5; m_bIsExpand=0; m_sentific=1; OnStandard(); m_result=\"0.\"; m_isDegree=1; UpdateData(FALSE); return TRUE; // return TRUE unless you set the focus to a control }
void CCalculatorDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } }
// 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 framework.
13
C++课程设计报告(2010)
void CCalculatorDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(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 icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } }
// The system calls this to obtain the cursor to display while the user drags // the minimized window.
HCURSOR CCalculatorDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; }
void CCalculatorDlg::OnOperandInput(UINT nID) { ASSERT(nID >= IDC_0 && nID <= IDC_9); if (m_errorState != ErrNone) return; if(m_func!=FuncNone) { m_func=FuncNone; Calculate(); } if (!m_bOperandAvail) m_operand = 0; if(!m_bCoff) m_operand=m_operand*10+(nID-IDC_0); else { m_operand=m_operand+(nID-IDC_0)*m_coff; m_coff*=0.1; } m_bOperandAvail=TRUE; UpdateDisplay(); }
void CCalculatorDlg::Calculate() { if (m_errorState != ErrNone) return; if (m_bOperandAvail) {
14
C++课程设计报告(2010)
if (m_operator == OpNone) m_accum = m_operand; else if (m_operator == OpMultiply) m_accum *= m_operand; else if (m_operator == OpDivide) { if (m_operand == 0) m_errorState = ErrDivideByZero; else m_accum /= m_operand; } else if (m_operator == OpAdd) m_accum += m_operand; else if (m_operator == OpSubtract) m_accum -= m_operand; } m_bOperandAvail = FALSE; m_bCoff=0; m_coff=0.1; UpdateDisplay(); }
void CCalculatorDlg::UpdateDisplay() { if (GetSafeHwnd() == NULL) return; if (m_errorState != ErrNone) m_result=\"除数不能为零\"; else { float lVal = (m_bOperandAvail) ? m_operand : m_accum; m_result.Format(_T(\"%f\"), lVal); int i=m_result.GetLength(); while(m_result.GetAt(i-1)=='0') { m_result.Delete(i-1,1); i-=1; } } UpdateData(FALSE); }
BOOL CCalculatorDlg::Keyboard(LPCTSTR szButton) { switch (szButton[0]) { case 'c': case 'C': OnClear(); break; case '/': OnDivid(); break; case '+': OnAdd(); break; case '-': OnMinus();
15
C++课程设计报告(2010)
break; case '*': OnMultiply(); break; case '=': OnEqual(); break; default: if (szButton[0] >= '0' && szButton[0] <= '9') { if (m_errorState != ErrNone) return FALSE; if (!m_bOperandAvail) m_operand = 0; m_operand=m_operand*10+szButton[0] - '0'; m_bOperandAvail=TRUE; UpdateDisplay(); } else return FALSE; break; } return TRUE; }
void CCalculatorDlg::OnAdd() { // TODO: Add your control notification handler code here Calculate(); m_operator = OpAdd; }
void CCalculatorDlg::OnMinus() { // TODO: Add your control notification handler code here Calculate(); m_operator = OpSubtract; }
void CCalculatorDlg::OnDivid() { // TODO: Add your control notification handler code here Calculate(); m_operator = OpDivide; }
void CCalculatorDlg::OnMultiply() { // TODO: Add your control notification handler code here Calculate(); m_operator = OpMultiply; }
void CCalculatorDlg::OnEqual() { // TODO: Add your control notification handler code here Calculate(); m_operator = OpNone; }
void CCalculatorDlg::OnSign()
16
C++课程设计报告(2010)
{ // TODO: Add your control notification handler code here m_operand*=-1; UpdateDisplay(); }
void CCalculatorDlg::OnSqrt() { // TODO: Add your control notification handler code here m_func=FuncSqrt; Run_Func(); }
void CCalculatorDlg::OnReciprocal() { // TODO: Add your control notification handler code here m_func=FuncRec; Run_Func(); }
void CCalculatorDlg::OnPoint() { // TODO: Add your control notification handler code here m_bCoff=1; UpdateDisplay(); }
void CCalculatorDlg::OnClear() { // TODO: Add your control notification handler code here m_operator = OpNone; m_operand = 0; m_accum = 0; m_bOperandAvail = FALSE; m_errorState = ErrNone; m_coff=0.1; UpdateDisplay(); }
BOOL CCalculatorDlg::PreTranslateMessage(MSG* pMsg) { if (m_hAccel != NULL && TranslateAccelerator(m_hWnd, m_hAccel, pMsg)) return TRUE; return CDialog::PreTranslateMessage(pMsg); }
void CCalculatorDlg::OnLog() { // TODO: Add your control notification handler code here m_func=FuncLog; Run_Func(); }
void CCalculatorDlg::OnLn() { // TODO: Add your control notification handler code here m_func=FuncLn; Run_Func(); }
void CCalculatorDlg::OnFactorial() { // TODO: Add your control notification handler code here m_func=FuncN; Run_Func();
17
C++课程设计报告(2010)
}
void CCalculatorDlg::OnExp() { // TODO: Add your control notification handler code here m_func=FuncExp; Run_Func(); }
void CCalculatorDlg::OnSentific() { // TODO: Add your control notification handler code here m_bIsExpand=1; m_sentific=0; ExpandToSentific(); }
void CCalculatorDlg::OnSin() { // TODO: Add your control notification handler code here m_func=FuncSin; Run_Func(); }
void CCalculatorDlg::OnSquar() { // TODO: Add your control notification handler code here m_func=FuncSqre; Run_Func(); }
void CCalculatorDlg::OnTan() { // TODO: Add your control notification handler code here m_func=FuncTan; Run_Func(); }
void CCalculatorDlg::OnStandard() { // TODO: Add your control notification handler code here m_bIsExpand=0; m_sentific=1; ExpandToSentific(); }
void CCalculatorDlg::Run_Func() { if (m_errorState != ErrNone) return; if (m_bOperandAvail) { if(m_func==FuncExp) m_operand=exp(m_operand); if(m_func==FuncLn) m_operand=log(m_operand); if(m_func==FuncLog) m_operand=log10(m_operand); if(m_func==FuncSqrt) m_operand=sqrt(m_operand); if(m_func==FuncSqre) m_operand=pow(10,m_operand); if(m_func==FuncSin) m_operand=sin(m_operand*DtoR);
18
C++课程设计报告(2010)
if(m_func==FuncCos) m_operand=cos(m_operand*DtoR); if(m_func==FuncTan) m_operand=tan(m_operand*DtoR); if(m_func==FuncRec) m_operand=1/m_operand; if(m_func=FuncN) { int i; for(i=m_operand-1;i>=1;i--) m_operand*=i; } } UpdateDisplay(); // m_func=FuncNone; }
void CCalculatorDlg::OnDegree() { // TODO: Add your control notification handler code here m_isDegree=0; DtoR=(2*PI)/360; UpdateData(FALSE); }
void CCalculatorDlg::OnRad() { // TODO: Add your control notification handler code here m_isDegree=1; DtoR=1; UpdateData(TRUE); }
void CCalculatorDlg::OnCos() { // TODO: Add your control notification handler code here m_func=FuncCos; Run_Func(); }
void CCalculatorDlg::ExpandToSentific() { if(m_bIsExpand) { SetWindowPos(NULL,0,0,rcSentific.Width(),rcSentific.Height(), SWP_NOMOVE|SWP_NOZORDER); SetVisibleCtrl(); } else { SetWindowPos(NULL,0,0,rcStandard.Width(),rcSentific.Height(), SWP_NOMOVE|SWP_NOZORDER); SetVisibleCtrl(); } UpdateData(FALSE); }
void CCalculatorDlg::SetVisibleCtrl() { CWnd* pCtrl=GetWindow(GW_CHILD); CRect rcTest; CRect rcControl;
19
C++课程设计报告(2010)
CRect rcShow;
GetWindowRect(rcShow); while(pCtrl!=NULL) { pCtrl->GetWindowRect(rcControl); if(rcTest.IntersectRect(rcShow,rcControl)) pCtrl->EnableWindow(TRUE); else pCtrl->EnableWindow(FALSE); pCtrl=pCtrl->GetWindow(GW_HWNDNEXT); }
}
20
因篇幅问题不能全部显示,请点此查看更多更全内容