华师c语言简单计算器实验报告.docx
《华师c语言简单计算器实验报告.docx》由会员分享,可在线阅读,更多相关《华师c语言简单计算器实验报告.docx(17页珍藏版)》请在冰豆网上搜索。
华师c语言简单计算器实验报告
院系:
计算机学院
实验课程:
计算机基础实验
实验项目:
程序设计入门基础
指导老师:
杨志强
开课时间:
2010~2011年度第2学期
专业:
计算机类
班级:
10本4
学生:
杨晓添
学号:
20102100114
华南师范大学教务处
1.题目:
简单计算器
2.实验目的:
模仿日常生活中所用的计算器,自行设计一个简单的计算器程序,实现简单的计算功能。
3.实验主要硬件软件环境:
Window7VisualC++6.0
4.实验内容:
(1)体系设计:
程序是一个简单的计算器,能正确输入数据,能实现加、减、乘、除等算术运算,能进行简单三角运算,运算结果能正确显示,可以清楚数据等。
(2)设计思路:
1)先在VisualC++6.0中建立一个MFC工程文件,名为calculator.
2)在对话框中添加适当的编辑框、按钮、静态文件、复选框和单选框
3)设计按钮,并修改其相应的ID与Caption.
4)选择和设置各控件的单击鼠标事件。
5)为编辑框添加double类型的关联变量m_edit1.
6)在calculatorDlg.h中添加math.h头文件,然后添加public成员。
7)打开calculatorDlg.cpp文件,在构造函数中,进行成员初始化和完善各控件的响应函数代码。
(3)程序清单:
程序代码如下:
//calculator_1Dlg.cpp:
implementationfile
//
#include"stdafx.h"
#include"calculator_1.h"
#include"calculator_1Dlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
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()
/////////////////////////////////////////////////////////////////////////////
//CCalculator_1Dlgdialog
CCalculator_1Dlg:
:
CCalculator_1Dlg(CWnd*pParent/*=NULL*/)
:
CDialog(CCalculator_1Dlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CCalculator_1Dlg)
m_edit1=0.0;
//}}AFX_DATA_INIT
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
tempvalue=0;
result=0;
sort=0;
append=0;
}
voidCCalculator_1Dlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCalculator_1Dlg)
DDX_Text(pDX,IDC_EDIT1,m_edit1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCalculator_1Dlg,CDialog)
//{{AFX_MSG_MAP(CCalculator_1Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1,OnButton1)
ON_BN_CLICKED(IDC_BUTTON2,OnButton2)
ON_BN_CLICKED(IDC_BUTTON3,OnButton3)
ON_BN_CLICKED(IDC_BUTTON4_plus,OnBUTTON4plus)
ON_BN_CLICKED(IDC_BUTTON5_4,OnButton54)
ON_BN_CLICKED(IDC_BUTTON6_5,OnButton65)
ON_BN_CLICKED(IDC_BUTTON7_6,OnButton76)
ON_BN_CLICKED(IDC_BUTTON8_minus,OnBUTTON8minus)
ON_BN_CLICKED(IDC_BUTTON12_multi,OnBUTTON12multi)
ON_BN_CLICKED(IDC_BUTTON16_div,OnBUTTON16div)
ON_BN_CLICKED(IDC_BUTTON9_7,OnButton97)
ON_BN_CLICKED(IDC_BUTTON10_8,OnButton108)
ON_BN_CLICKED(IDC_BUTTON11_9,OnButton119)
ON_BN_CLICKED(IDC_BUTTON13_zero,OnBUTTON13zero)
ON_BN_CLICKED(IDC_BUTTON14_equal,OnBUTTON14equal)
ON_BN_CLICKED(IDC_RADIO1_sin,OnRADIO1sin)
ON_BN_CLICKED(IDC_RADIO2_cos,OnRADIO2cos)
ON_BN_CLICKED(IDC_RADIO3_tan,OnRADIO3tan)
ON_BN_CLICKED(IDC_RADIO5_log10,OnRADIO5log10)
ON_BN_CLICKED(IDC_BUTTON15_clean,OnBUTTON15clean)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CCalculator_1Dlgmessagehandlers
BOOLCCalculator_1Dlg:
:
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
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCCalculator_1Dlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCCalculator_1Dlg:
:
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.
HCURSORCCalculator_1Dlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
voidCCalculator_1Dlg:
:
OnButton1()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+1;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnButton2()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+2;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnButton3()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+3;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnBUTTON4plus()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
sort=1;
tempvalue=result;
m_edit1=0;
append=1;
}
voidCCalculator_1Dlg:
:
OnButton54()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+4;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnButton65()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+5;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnButton76()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+6;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnBUTTON8minus()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
sort=2;
tempvalue=result;
m_edit1=0;
append=1;
}
voidCCalculator_1Dlg:
:
OnBUTTON12multi()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
sort=3;
tempvalue=result;
m_edit1=0;
append=1;
}
voidCCalculator_1Dlg:
:
OnBUTTON16div()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
sort=4;
tempvalue=result;
m_edit1=0;
append=1;
}
voidCCalculator_1Dlg:
:
OnButton97()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+7;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnButton108()
XX文库-让每个人平等地提升自我{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+8;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnButton119()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+9;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnBUTTON13zero()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(append==1)result=0;
result=result*10+0;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnBUTTON14equal()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
switch(sort)
{
case1:
result=result+tempvalue;break;
case2:
result=tempvalue-result;break;
case3:
result=tempvalue*result;break;
case4:
result=tempvalue/result;break;
}
m_edit1=result;
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnRADIO1sin()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_edit1=sin(result);
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnRADIO2cos()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_edit1=cos(result);
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnRADIO3tan()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_edit1=tan(result);
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnRADIO5log10()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_edit1=log10(result);
UpdateData(FALSE);
}
voidCCalculator_1Dlg:
:
OnBUTTON15clean()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
tempvalue=0;
result=0;
m_edit1=0.0;
UpdateData(FALSE);
}
(4)运行结果
5实验小结:
该次实验,是首次使用面向对象窗口进行的实验。
经过老师的使用讲解后,能够顺利的写出程序,并且没有出现输入进错误的符号以及遗漏的现象。
因此,没有出现什么问题。
但是,在进行实验的过程中,第一次感受到了用MFC编程的乐趣,首次使用到了人机交流的界面的实现,还是比较兴奋的。