C语言工程训练报告之计算器编写Word文档格式.docx
- 文档编号:22624551
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:35
- 大小:283.46KB
C语言工程训练报告之计算器编写Word文档格式.docx
《C语言工程训练报告之计算器编写Word文档格式.docx》由会员分享,可在线阅读,更多相关《C语言工程训练报告之计算器编写Word文档格式.docx(35页珍藏版)》请在冰豆网上搜索。
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;
-'
m_first-=m_second;
*'
m_first*=m_second;
/'
if(fabs(m_second)<
=0.000001)
{m_display="
error"
;
UpdateData(false);
return;
}
m_first/=m_second;
}
m_second=0.0;
m_coff=1.0;
UpdateDisplay(m_first);
}
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);
}
OnButton2()
m_second=m_second*10+2;
{m_second=m_second+2*m_coff;
OnButton3()
m_second=m_second*10+3;
{m_second=m_second+3*m_coff;
}voidCMy031141129__Dlg:
OnButton4()
m_second=m_second*10+4;
{m_second=m_second+4*m_coff;
OnButton5()
m_second=m_second*10+5;
{m_second=m_second+5*m_coff;
OnButton6()
m_second=m_second*10+6;
{m_second=m_second+6*m_coff;
OnButton7()
m_second=m_second*10+7;
{m_second=m_second+7*m_coff;
OnButton8()
m_second=m_second*10+8;
{m_second=m_second+8*m_coff;
OnButton9()
m_second=m_second*10+9;
{m_second=m_second+9*m_coff;
OnButton10()
m_second=m_second*10+0;
{m_second=m_second+0*m_coff;
其中的m_coff的定义为:
doublem_coff。
用于保存小数点的输入。
再是四则运算的实现:
加法:
OnButton15()
{Calculate();
m_operator="
+"
减法:
OnButton16()
-"
乘法:
OnButton14()
*"
除法:
OnButton13()
/"
其中m_operator的定义为:
CStringm_operator。
用于保存运算符号。
小数点的实现:
OnButton12()
{m_coff=0.1;
正负号的实现:
OnButton11()
{m_second=-m_second;
等号的实现:
OnButton20()
m_first=0.0;
开平方的实现:
OnButton17()
{m_second=sqrt(m_second);
百分比的实现:
OnButton18()
{switch(m_operator.GetAt(0))
{case'
break;
erroy"
m_first=m_first/100;
倒数的实现:
OnButton19()
{if(fabs(m_second)<
{m_display="
return;
m_second=1.0/m_second;
主清除的实现:
OnButton21()
{m_first=0.0;
UpdateDisplay(0.0);
逐个清除操作"
Backspace"
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("
),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>
/////////////////////////////////////////////////////////////////////////////
//CMy031141129__Dlgdialog
//Construction
//{{AFX_INSERT_LOCATION}}
//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
#endif//!
//031141129_章光旭_计算器Dlg.cpp:
implementationfile
#include"
stdafx.h"
math.h"
031141129_章光旭_计算器.h"
031141129_章光旭-计算器Dlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
//CAboutDlgdialogusedforAppAbout
classCAboutDlg:
CAboutDlg();
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//{{AFX_VIRTUAL(CAboutDlg)
//DDX/DDVs
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 工程 训练 报告 计算器 编写