C语言工程训练报告之计算器编写.docx

上传人:b****8 文档编号:9488505 上传时间:2023-02-04 格式:DOCX 页数:35 大小:283.46KB
下载 相关 举报
C语言工程训练报告之计算器编写.docx_第1页
第1页 / 共35页
C语言工程训练报告之计算器编写.docx_第2页
第2页 / 共35页
C语言工程训练报告之计算器编写.docx_第3页
第3页 / 共35页
C语言工程训练报告之计算器编写.docx_第4页
第4页 / 共35页
C语言工程训练报告之计算器编写.docx_第5页
第5页 / 共35页
点击查看更多>>
下载资源
资源描述

C语言工程训练报告之计算器编写.docx

《C语言工程训练报告之计算器编写.docx》由会员分享,可在线阅读,更多相关《C语言工程训练报告之计算器编写.docx(35页珍藏版)》请在冰豆网上搜索。

C语言工程训练报告之计算器编写.docx

C语言工程训练报告之计算器编写

C语言工程实验报告

专业:

电子信息科学与技术

班级:

0311411

学号:

031141129

姓名:

章光旭

指导老师:

杨老师

日期:

2012-05-31

成绩:

_________

目录

1.计算机外壳...

2.定义函数…

3.计算机各功能的具体实现…

1数字实现…

2算法实现…

3小数点实现…

4正负号实现…

5开根号实现…

6等号实现…

7倒数的实现…

3.清除号实现…

4.软件的源代码…

5.小结…

6.致谢...

7.参考文献...

1、简易界面的制作

打开vc6.0单击文件新建会弹出一个新建窗口。

在文件、工程、工作区、其他文档四个中选择工程。

在工程很多菜单中点击MFCAppWizard(exe),并在右侧工程名称中填写工程名,点击确定。

此时会弹出一个窗口,在步骤一中选择基本对话框后点击完成。

此时我们就进入了自己所创建的工程。

左侧有classes、resources、file三部分。

右边有一个工程窗口(我命名为计算器)。

左上方有compilebuildbuildExecute。

我只用到build和buildExecute。

在计算器窗口中删除确定,取消和DOTO。

在控件中点击ab,选定计算器窗口中上部分作为计算器显示器。

在控件中点击按钮,在计算机窗口中设定25个按钮。

分别为0—9。

+-*/=sqrtx~2x~3π小数点+/-1/xMCMRCBackspac。

此时计算器界面设定就到此结束了。

下面将是计算器程序的设计。

(我所设计的计算器界面)

2、添加成员变量和函数

a.在classes中右键点击CmyDlg,选择addmembervariable.弹出窗口

在此窗口中依次添加m_firstm_secondm_coffm-operatorm这些成员变量。

类型为double型。

例如我要添加m_first.在变量类型中填写double在变量名称中填写m_first.其他的方法一样,就不再说明。

b.同样在CmyDlg上点击右键,选择addmemberfunction.弹出窗口

添加函数CaculateUpdateDisplay.

在函数类型中填写void,在函数描述中填写Caculate(void).在函数类型中填写void,在函数描述中填写UpdateDisplay(doublelval)。

classCMy031141129__Dlg:

publicCDialog

{

public:

voidUpdateDisplay(doubleX);

voidCalculate(void);

doublem_first;

doublem_second;

doublem_coff;

CStringm_operator;

CMy031141129__Dlg(CWnd*pParent=NULL);//standardconstructor

//DialogData

//{{AFX_DATA(CMy031141129__Dlg)

enum{IDD=IDD_MY031141129___DIALOG};

CStringm_display;

//}}AFX_DATA

//ClassWizardgeneratedvirtualfunctionoverrides

//{{AFX_VIRTUAL(CMy031141129__Dlg)

protected:

virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport

//}}AFX_VIRTUAL

//Implementation

protected:

HICONm_hIcon;

//Generatedmessagemapfunctions

//{{AFX_MSG(CMy031141129__Dlg)

virtualBOOLOnInitDialog();

afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);

afx_msgvoidOnPaint();

afx_msgHCURSOROnQueryDragIcon();

afx_msgvoidOnButton1();

afx_msgvoidOnButton2();

afx_msgvoidOnButton3();

afx_msgvoidOnButton4();

afx_msgvoidOnButton5();

afx_msgvoidOnButton6();

afx_msgvoidOnButton7();

afx_msgvoidOnButton8();

afx_msgvoidOnButton9();

afx_msgvoidOnButton10();

afx_msgvoidOnButton11();

afx_msgvoidOnButton12();

afx_msgvoidOnButton13();

afx_msgvoidOnButton14();

afx_msgvoidOnButton15();

afx_msgvoidOnButton16();

afx_msgvoidOnButton17();

afx_msgvoidOnButton18();

afx_msgvoidOnButton19();

afx_msgvoidOnButton20();

afx_msgvoidOnButton21();

afx_msgvoidOnButton22();

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

在对话框类的定义下,再定义其中声明的函数。

在计算器的执行文件中,于析构函数中初始化对话框类中定义的成员变量。

在此软件中对话框是主体,其它控件各司其职。

编辑框需设定一个成员变量与它相关联,进行数值的显示;其它的按钮如上节所述进行各自相应的功能。

它们的响应函数将在《计算器各功能的具体实现》中进行叙述。

计算器各功能的具体实现

先在对话框类中定义:

doublem_first,m_second用于保存输入的数值和计算的结果;又定义:

CStringm_dispay与编辑框相对应通过UpdateData()进行数值的传输。

为了方便编辑与程序的维护,定义两个成员函数voidCalculate()、voidUpdateDisplay(doubleX),分别进行混合运算和编辑框的数值显示。

两函数的具体定义如下:

voidCMy031141129__Dlg:

:

Calculate()

{

switch(m_operator.GetAt(0))

{

case'+':

m_first+=m_second;break;

case'-':

m_first-=m_second;break;

case'*':

m_first*=m_second;break;

case'/':

if(fabs(m_second)<=0.000001)

{m_display="error";

UpdateData(false);

return;

}

m_first/=m_second;break;

}

m_second=0.0;

m_coff=1.0;

UpdateDisplay(m_first);

}

voidCMy031141129__Dlg:

:

UpdateDisplay(doubleX)

{

m_display.Format(_T("%f"),X);

inti=m_display.GetLength();

while(m_display.GetAt(i-1)=='0')

{m_display.Delete(i-1,1);

i--;

}

UpdateData(false);}

首先是数字的输入:

voidCMy031141129__Dlg:

:

OnButton1()

{if(m_coff==1.0)

m_second=m_second*10+1;

else

{m_second=m_second+1*m_coff;

m_coff*=0.1;

}

UpdateDisplay(m_second);

}

voidCMy031141129__Dlg:

:

OnButton2()

{if(m_coff==1.0)

m_second=m_second*10+2;

else

{m_second=m_second+2*m_coff;

m_coff*=0.1;

}

UpdateDisplay(m_second);

}

voidCMy031141129__Dlg:

:

OnButton3()

{if(m_coff==1.0)

m_second=m_second*10+3;

else

{m_second=m_second+3*m_coff;

m_coff*=0.1;

}

UpdateDisplay(m_second);

}voidCMy031141129__Dlg:

:

OnButton4()

{if(m_coff==1.0)

m_second=m_second*10+4;

else

{m_second=m_second+4*m_coff;

m_coff*=0.1;

}

UpdateDisplay(m_second);

}

voidCMy031141129__Dlg:

:

OnButton5()

{if(m_coff==1.0)

m_second=m_second*10+5;

else

{m_second=m_second+5*m_coff;

m_coff*=0.1;

}

UpdateDisplay(m_second);

}

voidCMy031141129__Dlg:

:

OnButton6()

{if(m_coff==1.0)

m_second=m_second*10+6;

else

{m_second=m_second+6*m_coff;

m_coff*=0.1;

}

UpdateDisplay(m_second);

}

voidCMy031141129__Dlg:

:

OnButton7()

{if(m_coff==1.0)

m_second=m_second*10+7;

else

{m_second=m_second+7*m_coff;

m_coff*=0.1;

}

UpdateDisplay(m_second);

}voidCMy031141129__Dlg:

:

OnButton8()

{if(m_coff==1.0)

m_second=m_second*10+8;

else

{m_second=m_second+8*m_coff;

m_coff*=0.1;

}

UpdateDisplay(m_second);

}

voidCMy031141129__Dlg:

:

OnButton9()

{if(m_coff==1.0)

m_second=m_second*10+9;

else

{m_second=m_second+9*m_coff;

m_coff*=0.1;

}

UpdateDisplay(m_second);

}

voidCMy031141129__Dlg:

:

OnButton10()

{if(m_coff==1.0)

m_second=m_second*10+0;

else

{m_second=m_second+0*m_coff;

m_coff*=0.1;

}

UpdateDisplay(m_second);

}

其中的m_coff的定义为:

doublem_coff。

用于保存小数点的输入。

再是四则运算的实现:

加法:

voidCMy031141129__Dlg:

:

OnButton15()

{Calculate();

m_operator="+";

}

减法:

voidCMy031141129__Dlg:

:

OnButton16()

{Calculate();

m_operator="-";

}

乘法:

voidCMy031141129__Dlg:

:

OnButton14()

{Calculate();

m_operator="*";

}

除法:

voidCMy031141129__Dlg:

:

OnButton13()

{Calculate();

m_operator="/";

}

其中m_operator的定义为:

CStringm_operator。

用于保存运算符号。

小数点的实现:

voidCMy031141129__Dlg:

:

OnButton12()

{m_coff=0.1;

}

正负号的实现:

voidCMy031141129__Dlg:

:

OnButton11()

{m_second=-m_second;

UpdateDisplay(m_second);

}

等号的实现:

voidCMy031141129__Dlg:

:

OnButton20()

{Calculate();

m_first=0.0;

m_operator="+";

}

开平方的实现:

voidCMy031141129__Dlg:

:

OnButton17()

{m_second=sqrt(m_second);

UpdateDisplay(m_second);

}

百分比的实现:

voidCMy031141129__Dlg:

:

OnButton18()

{switch(m_operator.GetAt(0))

{case'+':

m_first+=m_second;break;

case'-':

m_first-=m_second;break;

case'*':

m_first*=m_second;break;

case'/':

if(fabs(m_second)<=0.000001)

{m_display="erroy";

UpdateData(false);

return;

}

m_first/=m_second;break;

}

m_first=m_first/100;

UpdateDisplay(m_first);

m_first=0.0;

m_second=0.0;

}

倒数的实现:

voidCMy031141129__Dlg:

:

OnButton19()

{if(fabs(m_second)<=0.000001)

{m_display="error";

UpdateData(false);

return;

}

m_second=1.0/m_second;

UpdateDisplay(m_second);

}

主清除的实现:

voidCMy031141129__Dlg:

:

OnButton21()

{m_first=0.0;

m_second=0.0;

m_operator="+";

m_coff=1.0;

UpdateDisplay(0.0);

}

逐个清除操作"Backspace"

voidCMy031141129__Dlg:

:

OnButton22()

{

longinti=0,m_s;

doublej=0;

CStringC;

if(m_coff==1.0)

{m_s=(longint)m_second;

m_s=m_s-(m_s%10);

m_second=m_s/10;

}

/*else

{C.Format(_T("%f"),m_second);

i=C.GetLength();

while(C.GetAt(i-1)!

='.')

{j++;

i--;

}

m_second=m_second*pow(10,j);

m_s=(longint)m_second;

m_second=m_s-(m_s%10);

m_second=m_second/pow(10,j);

}*/

软件的主要源代码

//031141129_章光旭_计算器Dlg.h:

headerfile

//

#if!

defined(AFX_031141129__DLG_H__7C30298E_2AD1_4000_BDD8_42CAB69573BC__INCLUDED_)

#defineAFX_031141129__DLG_H__7C30298E_2AD1_4000_BDD8_42CAB69573BC__INCLUDED_

#if_MSC_VER>1000

#pragmaonce

#endif//_MSC_VER>1000

/////////////////////////////////////////////////////////////////////////////

//CMy031141129__Dlgdialog

classCMy031141129__Dlg:

publicCDialog

{

//Construction

public:

voidUpdateDisplay(doubleX);

voidCalculate(void);

doublem_first;

doublem_second;

doublem_coff;

CStringm_operator;

CMy031141129__Dlg(CWnd*pParent=NULL);//standardconstructor

//DialogData

//{{AFX_DATA(CMy031141129__Dlg)

enum{IDD=IDD_MY031141129___DIALOG};

CStringm_display;

//}}AFX_DATA

//ClassWizardgeneratedvirtualfunctionoverrides

//{{AFX_VIRTUAL(CMy031141129__Dlg)

protected:

virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport

//}}AFX_VIRTUAL

//Implementation

protected:

HICONm_hIcon;

//Generatedmessagemapfunctions

//{{AFX_MSG(CMy031141129__Dlg)

virtualBOOLOnInitDialog();

afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);

afx_msgvoidOnPaint();

afx_msgHCURSOROnQueryDragIcon();

afx_msgvoidOnButton1();

afx_msgvoidOnButton2();

afx_msgvoidOnButton3();

afx_msgvoidOnButton4();

afx_msgvoidOnButton5();

afx_msgvoidOnButton6();

afx_msgvoidOnButton7();

afx_msgvoidOnButton8();

afx_msgvoidOnButton9();

afx_msgvoidOnButton10();

afx_msgvoidOnButton11();

afx_msgvoidOnButton12();

afx_msgvoidOnButton13();

afx_msgvoidOnButton14();

afx_msgvoidOnButton15();

afx_msgvoidOnButton16();

afx_msgvoidOnButton17();

afx_msgvoidOnButton18();

afx_msgvoidOnButton19();

afx_msgvoidOnButton20();

afx_msgvoidOnButton21();

afx_msgvoidOnButton22();

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

//{{AFX_INSERT_LOCATION}}

//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.

#endif//!

defined(AFX_031141129__DLG_H__7C30298E_2AD1_4000_BDD8_42CAB69573BC__INCLUDED_)

//031141129_章光旭_计算器Dlg.cpp:

implementationfile

//

#include"stdafx.h"

#include"math.h"

#include"031141129_章光旭_计算器.h"

#include"031141129_章光旭-计算器Dlg.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/DDVs

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1