河北工业大学离散数学综合实验一
《河北工业大学离散数学综合实验一》由会员分享,可在线阅读,更多相关《河北工业大学离散数学综合实验一(14页珍藏版)》请在装配图网上搜索。
1、实验一 计算器设计与实现一、实验目的与要求实验目的:锻炼可视化编程能力实验要求:1、熟悉VC+编程环境。2、运用C+程序设计知识对实验题目进行分析和设计。3、进行程序编写和调试工作。二、实验内容 使用MFC框架在Visual Studio 6.0环境下编写一个简易的计算器,能实现简单的加、减、乘、除四则运算,并加入特定的一些效果,如使计算器的颜色在红色,绿色和蓝色之间轮流变换等。完成诸如计算器对话框的创建、控件编辑、界面设计、关键功能的实现等工作。三、实现方法1、设计界面。创建一个对话框,在对话框中添加文本框用来显示计算结果,按钮:0-9、小数点、+、-、*、/、=等。2、实现各个按钮的功能。
2、3、实现简单的语法分析,保证正确的计算结果。4、实现颜色的变换。四、程序源代码/ caaDlg.cpp : implementation file/#include stdafx.h#include caa.h#include caaDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutint m=0,n=255,k=0; /定义全局变量改变窗体颜色class CAboutDlg : publ
3、ic CDialogpublic: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); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDl
4、g)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg: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 mess
5、age handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CCaaDlg dialogCCaaDlg:CCaaDlg(CWnd* pParent /*=NULL*/): CDialog(CCaaDlg:IDD, pParent)/AFX_DATA_INIT(CCaaDlg)m_result = _T();/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME); /
6、*/m_coff=0.1f; /为变量进行初始m_bCoff=0; /为变量进行初始m_errorState = ErrNone; /为变量进行初始m_bOperandAvail=FALSE; /为变量进行初始m_operator=OpNone; m_accum=0; /*/void CCaaDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CCaaDlg)DDX_Text(pDX, IDC_EDIT, m_result);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(
7、CCaaDlg, CDialog)/AFX_MSG_MAP(CCaaDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_WM_CTLCOLOR()ON_BN_CLICKED(IDC_BUTTON1, On7)ON_BN_CLICKED(IDC_BUTTON2, On8)ON_BN_CLICKED(IDC_BUTTON3, On9)ON_BN_CLICKED(IDC_BUTTON6, On4)ON_BN_CLICKED(IDC_BUTTON7, On5)ON_BN_CLICKED(IDC_BUTTON8, On6)ON_BN_CL
8、ICKED(IDC_BUTTON11, On1)ON_BN_CLICKED(IDC_BUTTON12, On2)ON_BN_CLICKED(IDC_BUTTON14, On3)ON_BN_CLICKED(IDC_BUTTON17, On0)ON_BN_CLICKED(IDC_BUTTON18, Ondian)ON_BN_CLICKED(IDC_BUTTON19, Onclear)ON_BN_CLICKED(IDC_BUTTON22, Ondengyu)ON_BN_CLICKED(IDC_BUTTON4, Onjia)ON_BN_CLICKED(IDC_BUTTON5, Onjian)ON_BN
9、_CLICKED(IDC_BUTTON9, Oncheng)ON_BN_CLICKED(IDC_BUTTON10, Onchu)ON_BN_CLICKED(IDC_BUTTON13, Onhongse)ON_BN_CLICKED(IDC_BUTTON20, Onlanse)ON_BN_CLICKED(IDC_BUTTON21, Onlvse)ON_BN_CLICKED(IDC_BUTTON24, Onhuangse)ON_BN_CLICKED(IDC_BUTTON15, OnCANCEL)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CCaaDlg message handle
10、rsBOOL CCaaDlg: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 AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the
11、 icon for this dialog. The framework does this automatically/ 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 herem_brush.CreateSolidBrush(RGB(m, n, k);return TRUE; / return TRUE unless you
12、 set the focus to a controlvoid CCaaDlg: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 application
13、s using the document/view model,/ this is automatically done for you by the framework.void CCaaDlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXI
14、CON);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 user drags/
15、 the minimized window.HCURSOR CCaaDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;HBRUSH CCaaDlg:OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) /HBRUSH hbr = CDialog:OnCtlColor(pDC, pWnd, nCtlColor);/ TODO: Change any attributes of the DC here/ TODO: Return a different brush if the default is not de
16、siredreturn m_brush;/*/*输入模块*/void CCaaDlg:OnOperandInput(int a) /处理0-9 数字的输入函数 if (m_errorState != ErrNone) /判断当前运算是否有误,若有则返回return; if (!m_bOperandAvail) /判断是否输入新的数字,1-是0-否m_operand = 0; if(!m_bCoff) /判断是否是小数输入,1-是0-否m_operand=m_operand*10+(a); else m_operand=m_operand+(a)*m_coff; m_coff*=0.1f; m_
17、bOperandAvail=TRUE; UpdateDisplay(); /更新显示 /*计算处理模块*/void CCaaDlg:Calculate() /处理计算函数 if (m_errorState != ErrNone) return; if (m_bOperandAvail) /如果没有计算符就将m_operand值赋予m_accum if (m_operator = OpNone) m_accum = m_operand; else if (m_operator = OpMultiply) /否则按计算符进行计算m_accum *= m_operand; else if (m_op
18、erator = 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.1f; UpdateDisplay(); /*显示模块*/void CCaaDlg:UpdateDis
19、play() /处理显示函数 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); /将float型转化为CString型int i=m_result.GetLength();while(m_result.GetAt
20、(i-1)=0) m_result.Delete(i-1,1);i-=1; /用编辑控件变量m_result改变编辑控件的值,更新显示UpdateData(FALSE); /*/ The system calls this to obtain the cursor to display while the user drags/ the minimized window.void CCaaDlg:Ondengyu() / TODO: Add your control notification handler code hereCalculate(); m_operator = OpNone;v
21、oid CCaaDlg:On0() / TODO: Add your control notification handler code hereOnOperandInput(0); void CCaaDlg:On1() / TODO: Add your control notification handler code hereOnOperandInput(1); void CCaaDlg:On2() / TODO: Add your control notification handler code hereOnOperandInput(2);void CCaaDlg:On3() / TO
22、DO: Add your control notification handler code hereOnOperandInput(3);void CCaaDlg:On4() / TODO: Add your control notification handler code hereOnOperandInput(4);void CCaaDlg:On5() / TODO: Add your control notification handler code hereOnOperandInput(5);void CCaaDlg:On6() / TODO: Add your control not
23、ification handler code hereOnOperandInput(6);void CCaaDlg:On7() / TODO: Add your control notification handler code hereOnOperandInput(7);void CCaaDlg:On8() / TODO: Add your control notification handler code hereOnOperandInput(8);void CCaaDlg:On9() / TODO: Add your control notification handler code h
24、ereOnOperandInput(9);void CCaaDlg:Oncheng() / TODO: Add your control notification handler code hereCalculate(); m_operator = OpMultiply;void CCaaDlg:Onchu() / TODO: Add your control notification handler code herCalculate();m_operator = OpDivide;void CCaaDlg:Onjia() / TODO: Add your control notificat
25、ion handler code hereCalculate(); m_operator = OpAdd;void CCaaDlg:Onjian() / TODO: Add your control notification handler code hereCalculate(); m_operator = OpSubtract; void CCaaDlg:Ondian() / TODO: Add your control notification handler code herem_bCoff=1;UpdateDisplay(); void CCaaDlg:Onclear() / TOD
26、O: Add your control notification handler code here m_coff=0.1f; /为变量进行还原m_bCoff=0; /为变量进行还原m_errorState = ErrNone; /为变量进行还原m_bOperandAvail=FALSE; /为变量进行还原m_operator=OpNone; m_accum=0;m_operand=0;Calculate(); m_operator = OpNone;UpdateData(FALSE);void CCaaDlg:Onhongse() / TODO: Add your control notif
27、ication handler code here m_brush.DeleteObject();Invalidate();m = 220;n = 20;k = 60;m_brush.CreateSolidBrush(RGB(m, n, k);UpdateWindow();void CCaaDlg:Onlanse() / TODO: Add your control notification handler code herem_brush.DeleteObject();Invalidate();m = 0;n = 0;k = 255;m_brush.CreateSolidBrush(RGB(
28、m, n, k);UpdateWindow();void CCaaDlg:Onlvse() / TODO: Add your control notification handler code herem_brush.DeleteObject();Invalidate();m = 0;n = 255;k = 127;m_brush.CreateSolidBrush(RGB(m, n, k);UpdateWindow();void CCaaDlg:Onhuangse() / TODO: Add your control notification handler code herem_brush.
29、DeleteObject();Invalidate();m = 255;n = 255;k = 0;m_brush.CreateSolidBrush(RGB(m, n, k);UpdateWindow();void CCaaDlg:OnCANCEL() / TODO: Add your control notification handler code hereCCaaDlg:OnCANCEL();六、程序截图 七、实验总结 通过对MFC课程的学习我掌握了MFC开发控件的基础能力,利用基本的菜单和窗体工具可以创建一些自己想要的窗体和控件,并按照自己的意愿设置属性。为以后的更高级的语言学习做准备。
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。