院 系:计 算 机 学 院
实验课程:计算机基础实验
实验项目:程序设计入门基础
指导老师:杨志强
开课时间:2010 ~ 20##年度第 2学期
专 业:计算机类
班 级: 10本4
学 生:杨晓添
学 号:20102100114
华南师范大学教务处
1. 题目:简单计算器
2. 实验目的:模仿日常生活中所用的计算器,自行设计一个简单的计算器程序,实现简单的计算功能。
3. 实验主要硬件软件环境:Window 7Visual C++ 6.0
4. 实验内容:
(1) 体系设计:
程序是一个简单的计算器,能正确输入数据,能实现加、减、乘、除等算术运算,能进行简单三角运算,运算结果能正确显示,可以清楚数据等。
(2) 设计思路:
1) 先在Visual C++ 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 : implementation file
//
#include "stdafx.h"
#include "calculator_1.h"
#include "calculator_1Dlg.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); // DDX/DDV support
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CCalculator_1Dlg dialog
CCalculator_1Dlg::CCalculator_1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CCalculator_1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCalculator_1Dlg)
m_edit1 = 0.0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
tempvalue=0;
result=0;
sort=0;
append=0;
}
void CCalculator_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_1Dlg message handlers
BOOL CCalculator_1Dlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CCalculator_1Dlg::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.
void CCalculator_1Dlg::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 CCalculator_1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCalculator_1Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
if(append==1) result=0;
result=result*10+1;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnButton2()
{
// TODO: Add your control notification handler code here
if(append==1) result=0;
result=result*10+2;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnButton3()
{
// TODO: Add your control notification handler code here
if(append==1) result=0;
result=result*10+3;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnBUTTON4plus()
{
// TODO: Add your control notification handler code here
sort=1;
tempvalue=result;
m_edit1=0;
append=1;
}
void CCalculator_1Dlg::OnButton54()
{
// TODO: Add your control notification handler code here
if(append==1) result=0;
result=result*10+4;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnButton65()
{
// TODO: Add your control notification handler code here
if(append==1) result=0;
result=result*10+5;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnButton76()
{
// TODO: Add your control notification handler code here
if(append==1) result=0;
result=result*10+6;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnBUTTON8minus()
{
// TODO: Add your control notification handler code here
sort=2;
tempvalue=result;
m_edit1=0;
append=1;
}
void CCalculator_1Dlg::OnBUTTON12multi()
{
// TODO: Add your control notification handler code here
sort=3;
tempvalue=result;
m_edit1=0;
append=1;
}
void CCalculator_1Dlg::OnBUTTON16div()
{
// TODO: Add your control notification handler code here
sort=4;
tempvalue=result;
m_edit1=0;
append=1;
}
void CCalculator_1Dlg::OnButton97()
{
// TODO: Add your control notification handler code here
if(append==1) result=0;
result=result*10+7;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnButton108()
{
// TODO: Add your control notification handler code here
if(append==1) result=0;
result=result*10+8;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnButton119()
{
// TODO: Add your control notification handler code here
if(append==1) result=0;
result=result*10+9;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnBUTTON13zero()
{
// TODO: Add your control notification handler code here
if(append==1) result=0;
result=result*10+0;
m_edit1=result;
append=0;
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnBUTTON14equal()
{
// TODO: Add your control notification handler code here
switch(sort)
{
case 1:result=result+tempvalue;break;
case 2:result=tempvalue-result;break;
case 3:result=tempvalue*result;break;
case 4:result=tempvalue/result;break;
}
m_edit1=result;
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnRADIO1sin()
{
// TODO: Add your control notification handler code here
m_edit1=sin(result);
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnRADIO2cos()
{
// TODO: Add your control notification handler code here
m_edit1=cos(result);
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnRADIO3tan()
{
// TODO: Add your control notification handler code here
m_edit1=tan(result);
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnRADIO5log10()
{
// TODO: Add your control notification handler code here
m_edit1=log10(result);
UpdateData(FALSE);
}
void CCalculator_1Dlg::OnBUTTON15clean()
{
// TODO: Add your control notification handler code here
tempvalue=0;
result=0;
m_edit1=0.0;
UpdateData(FALSE);
}
(4)运行结果
5实验小结:
该次实验,是首次使用面向对象窗口进行的实验。经过老师的使用讲解后,能够顺利的写出程序,并且没有出现输入进错误的符号以及遗漏的现象。因此,没有出现什么问题。
但是,在进行实验的过程中,第一次感受到了用MFC编程的乐趣,首次使用到了人机交流的界面的实现,还是比较兴奋的。