1、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单击文件新建会弹出一个新建窗口。在文件、工程、工作区、其他文档四个中选择工程。在工程很多菜单中点击MFC AppWizard(exe),并在右侧工程名称中填写工程名,点击确定。此时会弹出一个
2、窗口,在步骤一中选择基本对话框后点击完成。此时我们就进入了自己所创建的工程。左侧有classes、resources、file三部分。右边有一个工程窗口(我命名为计算器)。左上方有compile build buildExecute。我只用到build和buildExecute。 在计算器窗口中删除确定,取消和DOTO。在控件中点击ab,选定计算器窗口中上部分作为计算器显示器。在控件中点击按钮,在计算机窗口中设定25个按钮。分别为09。+ - * / = sqrt x2 x3 小数点 +/- 1/x MC MR C Backspac。此时计算器界面设定就到此结束了。下面将是计算器程序的设计。
3、(我所设计的计算器界面)2、添加成员变量和函数 a.在classes中右键点击CmyDlg,选择add member variable.弹出窗口在此窗口中依次添加m_first m_second m_coff m-operator m这些成员变量。类型为double型。例如我要添加m_first.在变量类型中填写double 在变量名称中填写m_first.其他的方法一样,就不再说明。 b.同样在CmyDlg上点击右键,选择add member function.弹出窗口添加函数Caculate UpdateDisplay.在函数类型中填写void,在函数描述中填写Caculate(void)
4、.在函数类型中填写void,在函数描述中填写UpdateDisplay(double lval)。class CMy031141129_Dlg : public CDialogpublic: void UpdateDisplay(double X); void Calculate(void); double m_first; double m_second; double m_coff; CString m_operator; CMy031141129_Dlg(CWnd* pParent = NULL); / standard constructor/ Dialog Data /AFX_DAT
5、A(CMy031141129_Dlg) enum IDD = IDD_MY031141129_DIALOG ; CString m_display; /AFX_DATA / ClassWizard generated virtual function overrides /AFX_VIRTUAL(CMy031141129_Dlg) protected: virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support /AFX_VIRTUAL/ Implementationprotected: HICON m_hIcon; /
6、 Generated message map functions /AFX_MSG(CMy031141129_Dlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnButton1(); afx_msg void OnButton2(); afx_msg void OnButton3(); afx_msg void OnButton4
7、(); afx_msg void OnButton5(); afx_msg void OnButton6(); afx_msg void OnButton7(); afx_msg void OnButton8(); afx_msg void OnButton9(); afx_msg void OnButton10(); afx_msg void OnButton11(); afx_msg void OnButton12(); afx_msg void OnButton13(); afx_msg void OnButton14(); afx_msg void OnButton15(); afx_
8、msg void OnButton16(); afx_msg void OnButton17(); afx_msg void OnButton18(); afx_msg void OnButton19(); afx_msg void OnButton20(); afx_msg void OnButton21(); afx_msg void OnButton22(); /AFX_MSG DECLARE_MESSAGE_MAP();在对话框类的定义下,再定义其中声明的函数。在计算器的执行文件中,于析构函数中初始化对话框类中定义的成员变量。在此软件中对话框是主体,其它控件各司其职。编辑框需设定一个成
9、员变量与它相关联,进行数值的显示;其它的按钮如上节所述进行各自相应的功能。它们的响应函数将在计算器各功能的具体实现中进行叙述。计算器各功能的具体实现 先在对话框类中定义:double m_first,m_second用于保存输入的数值和计算的结果;又定义:CString m_dispay与编辑框相对应通过UpdateData( )进行数值的传输。 为了方便编辑与程序的维护,定义两个成员函数void Calculate( )、void UpdateDisplay(double X),分别进行混合运算和编辑框的数值显示。两函数的具体定义如下:void CMy031141129_Dlg:Calcul
10、ate() 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);void CMy03
11、1141129_Dlg:UpdateDisplay(double X) m_display.Format(_T(%f),X); int i=m_display.GetLength(); while(m_display.GetAt(i-1)=0) m_display.Delete(i-1,1); i-; UpdateData(false);首先是数字的输入: void CMy031141129_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; U
12、pdateDisplay(m_second); void CMy031141129_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); void CMy031141129_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
13、(m_second); void CMy031141129_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); void CMy031141129_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);
14、void CMy031141129_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); void CMy031141129_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); void CMy0311
15、41129_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); void CMy031141129_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); void CMy031141129_Dlg:On
16、Button10() 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的定义为:double m_coff。用于保存小数点的输入。再是四则运算的实现:加法:void CMy031141129_Dlg:OnButton15() Calculate(); m_operator=+; 减法:void CMy031141129_Dlg:OnButton16() Calculate(); m_operator=-;
17、乘法:void CMy031141129_Dlg:OnButton14() Calculate(); m_operator=*; 除法: void CMy031141129_Dlg:OnButton13() Calculate(); m_operator=/; 其中m_operator的定义为:CString m_operator。用于保存运算符号。小数点的实现:void CMy031141129_Dlg:OnButton12() m_coff=0.1; 正负号的实现:void CMy031141129_Dlg:OnButton11() m_second=-m_second; UpdateDi
18、splay(m_second); 等号的实现:void CMy031141129_Dlg:OnButton20() Calculate(); m_first=0.0; m_operator=+; 开平方的实现:void CMy031141129_Dlg:OnButton17() m_second=sqrt(m_second); UpdateDisplay(m_second); 百分比的实现:void CMy031141129_Dlg:OnButton18() switch(m_operator.GetAt(0) case +: m_first+=m_second;break; case -:
19、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; 倒数的实现:void CMy031141129_Dlg:OnButton19() if(fabs(m_second) 1000#p
20、ragma once#endif / _MSC_VER 1000/ CMy031141129_Dlg dialogclass CMy031141129_Dlg : public CDialog/ Constructionpublic: void UpdateDisplay(double X); void Calculate(void); double m_first; double m_second; double m_coff; CString m_operator; CMy031141129_Dlg(CWnd* pParent = NULL); / standard constructor
21、/ Dialog Data /AFX_DATA(CMy031141129_Dlg) enum IDD = IDD_MY031141129_DIALOG ; CString m_display; /AFX_DATA / ClassWizard generated virtual function overrides /AFX_VIRTUAL(CMy031141129_Dlg) protected: virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support /AFX_VIRTUAL/ Implementationprote
22、cted: HICON m_hIcon; / Generated message map functions /AFX_MSG(CMy031141129_Dlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnButton1(); afx_msg void OnButton2(); afx_msg void OnButton3();
23、afx_msg void OnButton4(); afx_msg void OnButton5(); afx_msg void OnButton6(); afx_msg void OnButton7(); afx_msg void OnButton8(); afx_msg void OnButton9(); afx_msg void OnButton10(); afx_msg void OnButton11(); afx_msg void OnButton12(); afx_msg void OnButton13(); afx_msg void OnButton14(); afx_msg v
24、oid OnButton15(); afx_msg void OnButton16(); afx_msg void OnButton17(); afx_msg void OnButton18(); afx_msg void OnButton19(); afx_msg void OnButton20(); afx_msg void OnButton21(); afx_msg void OnButton22(); /AFX_MSG DECLARE_MESSAGE_MAP();/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert addition
25、al declarations immediately before the previous line.#endif / !defined(AFX_031141129_DLG_H_7C30298E_2AD1_4000_BDD8_42CAB69573BC_INCLUDED_)/ 031141129_章光旭_计算器Dlg.cpp : implementation file/#include stdafx.h#include math.h#include 031141129_章光旭_计算器.h#include 031141129_章光旭-计算器Dlg.h#ifdef _DEBUG#define n
26、ew DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic: 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 s
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1