河北工业大学离散数学综合实验一.docx
- 文档编号:9261261
- 上传时间:2023-05-17
- 格式:DOCX
- 页数:18
- 大小:36.85KB
河北工业大学离散数学综合实验一.docx
《河北工业大学离散数学综合实验一.docx》由会员分享,可在线阅读,更多相关《河北工业大学离散数学综合实验一.docx(18页珍藏版)》请在冰点文库上搜索。
河北工业大学离散数学综合实验一
实验一计算器设计与实现
一、实验目的与要求
实验目的:
锻炼可视化编程能力
实验要求:
1、熟悉VC++编程环境。
2、运用C++程序设计知识对实验题目进行分析和设计。
3、进行程序编写和调试工作。
二、实验内容
使用MFC框架在VisualStudio6.0环境下编写一个简易的计算器,能实现简单的加、减、乘、除四则运算,并加入特定的一些效果,如使计算器的颜色在红色,绿色和蓝色之间轮流变换等。
完成诸如计算器对话框的创建、控件编辑、界面设计、关键功能的实现等工作。
三、实现方法
1、设计界面。
创建一个对话框,在对话框中添加文本框用来显示计算结果,按钮:
0-9、小数点、+、-、*、/、=等。
2、实现各个按钮的功能。
3、实现简单的语法分析,保证正确的计算结果。
4、实现颜色的变换。
四、程序源代码
//caaDlg.cpp:
implementationfile
//
#include"stdafx.h"
#include"caa.h"
#include"caaDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
intm=0,n=255,k=0;//定义全局变量改变窗体颜色
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
//DialogData
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
:
IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
voidCAboutDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//Nomessagehandlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CCaaDlgdialog
CCaaDlg:
:
CCaaDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CCaaDlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CCaaDlg)
m_result=_T("");
//}}AFX_DATA_INIT
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
/**************************************************************************/
m_coff=0.1f;//为变量进行初始
m_bCoff=0;//为变量进行初始
m_errorState=ErrNone;//为变量进行初始
m_bOperandAvail=FALSE;//为变量进行初始
m_operator=OpNone;
m_accum=0;
/**************************************************************************/
}
voidCCaaDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCaaDlg)
DDX_Text(pDX,IDC_EDIT,m_result);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(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_CLICKED(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_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_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CCaaDlgmessagehandlers
BOOLCCaaDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//Add"About..."menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);//Setbigicon
SetIcon(m_hIcon,FALSE);//Setsmallicon
//TODO:
Addextrainitializationhere
m_brush.CreateSolidBrush(RGB(m,n,k));
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCCaaDlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCCaaDlg:
:
OnPaint()
{
if(IsIconic())
{
CPaintDCdc(this);//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialog:
:
OnPaint();
}
}
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCCaaDlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
HBRUSHCCaaDlg:
:
OnCtlColor(CDC*pDC,CWnd*pWnd,UINTnCtlColor)
{
//HBRUSHhbr=CDialog:
:
OnCtlColor(pDC,pWnd,nCtlColor);
//TODO:
ChangeanyattributesoftheDChere
//TODO:
Returnadifferentbrushifthedefaultisnotdesired
returnm_brush;
}
/****************************************************************************/
/********************************输入模块************************************/
voidCCaaDlg:
:
OnOperandInput(inta)//处理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_bOperandAvail=TRUE;
UpdateDisplay();//更新显示
}
/*******************************计算处理模块*******************************/
voidCCaaDlg:
:
Calculate()//处理计算函数
{
if(m_errorState!
=ErrNone)
return;
if(m_bOperandAvail)
{
//如果没有计算符就将m_operand值赋予m_accum
if(m_operator==OpNone)
m_accum=m_operand;
elseif(m_operator==OpMultiply)//否则按计算符进行计算
m_accum*=m_operand;
elseif(m_operator==OpDivide)
{
if(m_operand==0)
m_errorState=ErrDivideByZero;
else
m_accum/=m_operand;
}
elseif(m_operator==OpAdd)
m_accum+=m_operand;
elseif(m_operator==OpSubtract)
m_accum-=m_operand;
}
m_bOperandAvail=FALSE;
m_bCoff=0;
m_coff=0.1f;
UpdateDisplay();
}
/****************************显示模块********************************/
voidCCaaDlg:
:
UpdateDisplay()//处理显示函数
{
if(GetSafeHwnd()==NULL)
return;
if(m_errorState!
=ErrNone)
m_result="除数不能为零";
else//如果当前计算无错误那么进行显示
{
//如果是输入计算数,那么显示输入情况,如果按下计算符,则显示结果
floatlVal=(m_bOperandAvail)?
m_operand:
m_accum;
m_result.Format(_T("%f"),lVal);//将float型转化为CString型
inti=m_result.GetLength();
while(m_result.GetAt(i-1)=='0')
{
m_result.Delete(i-1,1);
i-=1;
}
}
//用编辑控件变量m_result改变编辑控件的值,更新显示
UpdateData(FALSE);
}
/************************************************************************/
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
voidCCaaDlg:
:
Ondengyu()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_operator=OpNone;
}
voidCCaaDlg:
:
On0()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
OnOperandInput(0);
}
voidCCaaDlg:
:
On1()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
OnOperandInput
(1);
}
voidCCaaDlg:
:
On2()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
OnOperandInput
(2);
}
voidCCaaDlg:
:
On3()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
OnOperandInput(3);
}
voidCCaaDlg:
:
On4()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
OnOperandInput(4);
}
voidCCaaDlg:
:
On5()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
OnOperandInput(5);
}
voidCCaaDlg:
:
On6()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
OnOperandInput(6);
}
voidCCaaDlg:
:
On7()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
OnOperandInput(7);
}
voidCCaaDlg:
:
On8()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
OnOperandInput(8);
}
voidCCaaDlg:
:
On9()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
OnOperandInput(9);
}
voidCCaaDlg:
:
Oncheng()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_operator=OpMultiply;
}
voidCCaaDlg:
:
Onchu()
{
//TODO:
Addyourcontrolnotificationhandlercodeher
Calculate();
m_operator=OpDivide;
}
voidCCaaDlg:
:
Onjia()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_operator=OpAdd;
}
voidCCaaDlg:
:
Onjian()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Calculate();
m_operator=OpSubtract;
}
voidCCaaDlg:
:
Ondian()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_bCoff=1;
UpdateDisplay();
}
voidCCaaDlg:
:
Onclear()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
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(F
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 河北 工业大学 离散数学 综合 实验