华师C语言简单计算器实验报告

时间:2024.4.21

院  系:计 算 机 学 院

实验课程:计算机基础实验

实验项目:程序设计入门基础

指导老师:杨志强

    开课时间: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)      设计按钮,并修改其相应的IDCaption.

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编程的乐趣,首次使用到了人机交流的界面的实现,还是比较兴奋的。

更多相关推荐:
计数器实验报告

实验4计数器及其应用一实验目的1学习用集成触发器构成计数器的方法2掌握中规模集成计数器的使用及功能测试方法二实验原理计数器是一个用以实现计数功能的时序部件它不仅可用来计脉冲数还常用作数字系统的定时分频和执行数字...

简单的计算器实验报告

HUNANUNIVERSITY程序设计训练简单的计算器报告学生姓名田博鑫学生学号专业班级指导老师20xx年6月16日至20xx年7月10日1吴蓉晖1程序设计目的和要求目的此次程序设计的目的主要是为了我们能更好的...

计算器实验报告 (1)

HTML网页实验报告院系计算机控制与工程学院班级计1241学号姓名完成日期1一实验名称设计一个网页计算器二需求分析计算器是日常生活中十分便捷有效的工具能实现加减乘除开方求倒数等简单运算的工具要实现计算功能可以用...

计算器实验报告

目录一系统开发的背景1二系统分析与设计1一二计算器的主要功能1系统模块结构设计1三系统的设计与实现2一二计算器的初始界面2一般计算3三解一元二次方程5四计算银行本利和5四系统测试7一二测试主函数MAIN7测试一...

C#计算器实验报告

C计算器实验报告班级学号20xx13432姓名蔡启林一实验目的和要求设计一个简单计算器具有一般计算功能能进行基本的加减乘除运算还具有求根号倒数等功能特点是能进行不同进制的运算和不同进制间的转换主要的工作主要是操...

计算器实验报告

计科系实验报告单

模拟计算器实验报告

课程设计报告课程设计题目:模拟机算器程序学生姓名:专业:网络工程班级:指导教师:20XX年11月27日东华理工大学课程设计评分表学生姓名:XXX班级:XXX学号:21课程设计题目:模拟机算器程序目录一.课程设计…

计算器设计报告

QT程序设计报告之简易计算器作者lovelycc126com我的处女作哦一需求分析当今社会是信息社会科技经济高速发展的社会为了更方便人们的工作生活和加速人们处理信息的速度计算器应运而生然而电脑已经成为当代人生活...

vb计算器实训 实验报告

江西科技师范大学实验报告年级班姓名实验日期成绩实训8实验报告一实验目的制作一个计算器二实验进程完成了十六进制八进制的代码书写添加了form3完成了窗体转化三实验原理与分析1控件属性设置指导教师郝谦江西科技师范大...

8086实现计算器实验报告

实验报告一题目数字计算器的汇编语言实现二设计任务利用8086微处理器可编程并行通信接口芯片8255A等相关器件在proteus中设计仿真一个简单数字计算器利用8086汇编语言编写完成加减乘除幂运算阶乘运算求余运...

计算器C++实验报告附源代码

四川大学软件学院实验报告学号1043111051姓名王金科专业软件工程班级20xx级5班

单片机设计实验报告简单计算器的设计

目录1总体方案选择211实验要求212方案设计22硬件原理电路图的设计及分析221主控模块2211STC89C52单片机主要特性3212STC89C52单片机管脚图4213STC89C52单片机的中断系统421...

计算器实验报告(47篇)