99网
您的当前位置:首页MFC设计简单的计算器

MFC设计简单的计算器

来源:99网


石家庄经济学院

实 验 报 告

学 院: 专 业: 计算机

信息工程学院计算机实验中心制

《Windows程序设计》实验报告 姓实验名 学号 指导教师 日期 室 计算机软件技术实验 设备编号 实验题目 实验9 对话框 一、实验目的 1. 掌握对话框类的定义及使用 2. 掌握对话框的数据交换和检验 二、实验内容 1. 编写一个基于对话框的程序,当用户点击对话框上的按钮时弹出一个模态对话框,显示的对话框显示出当前时间。程序界面如下图所示: 2. 试着编写一个可以完成计算器的基于对话框的应用程序,该应用程序具有“加”、“减”、“乘”、“除”的功能 三、源代码及实验结果 1. 显示时间 核心代码: class CMydialog : public CDialog { // Construction public: CMydialog(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CMydialog) enum { IDD = IDD_DIALOG1 }; CString m_time; //}}AFX_DATA // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMydialog) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: // Generated message map functions //{{AFX_MSG(CMydialog) afx_msg void On_OK(); afx_msg void On_Cancel(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CMydialog::CMydialog(CWnd* pParent /*=NULL*/) : CDialog(CMydialog::IDD, pParent) { //{{AFX_DATA_INIT(CMydialog) m_time = _T(\"\"); //}}AFX_DATA_INIT } void CMydialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMydialog) DDX_Text(pDX, IDC_STATIC1, m_time); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CMydialog, CDialog) //{{AFX_MSG_MAP(CMydialog) ON_BN_CLICKED(IDC_BUTTON1, On_OK) ON_BN_CLICKED(IDC_BUTTON2, On_Cancel) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMydialog message handlers void CMydialog::On_OK() { // TODO: Add your control notification handler code here CDialog::OnOK(); } void CMydialog::On_Cancel() { // TODO: Add your control notification handler code here CDialog::OnCancel(); } void CTest10_1Dlg::OnButton1() { // TODO: Add your control notification handler code here CTime time = CTime::GetCurrentTime(); CMydialog dlg; dlg.m_time = time.Format(\"%Y-%m-%d %H:%M:%S\"); UpdateData(FALSE); dlg.DoModal(); } 结果: 图一 2.计算器 核心代码: CTest10_2Dlg::CTest10_2Dlg(CWnd* pParent /*=NULL*/) : CDialog(CTest10_2Dlg::IDD, pParent) { num1 = 0; num2 = 0; //{{AFX_DATA_INIT(CTest10_2Dlg) // m_result = _T(\"\"); m_result.Format(\"%g\ //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1); e = 0; operate = 'N'; _Flag = FALSE; DotFlag = FALSE; } void CTest10_2Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTest10_2Dlg) DDX_Text(pDX, IDC_EDIT1, m_result); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CTest10_2Dlg, CDialog) //{{AFX_MSG_MAP(CTest10_2Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON0, OnButton0) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON2, OnButton2) ON_BN_CLICKED(IDC_BUTTON3, OnButton3) ON_BN_CLICKED(IDC_BUTTON4, OnButton4) ON_BN_CLICKED(IDC_BUTTON5, OnButton5) ON_BN_CLICKED(IDC_BUTTON6, OnButton6) ON_BN_CLICKED(IDC_BUTTON7, OnButton7) ON_BN_CLICKED(IDC_BUTTON8, OnButton8) ON_BN_CLICKED(IDC_BUTTON9, OnButton9) ON_BN_CLICKED(IDC_BUTTON00, OnButton00) ON_BN_CLICKED(IDC_DOT, OnDot) ON_BN_CLICKED(IDC_BUTTON_, OnButton) ON_BN_CLICKED(IDC_ADD, OnAdd) ON_BN_CLICKED(IDC_MINUS, OnMinus) ON_BN_CLICKED(IDC_MUL, OnMul) ON_BN_CLICKED(IDC_DIV, OnDiv) ON_BN_CLICKED(IDC_COUNT, OnCount) //}}AFX_MSG_MAP END_MESSAGE_MAP() void CTest10_2Dlg::OnButton0() { // TODO: Add your control notification handler code here Press(0); } void CTest10_2Dlg::OnButton1() { // TODO: Add your control notification handler code here Press(1); } void CTest10_2Dlg::OnButton2() { // TODO: Add your control notification handler code here Press(2); } void CTest10_2Dlg::OnButton3() { // TODO: Add your control notification handler code here Press(3); } void CTest10_2Dlg::OnButton4() { // TODO: Add your control notification handler code here Press(4); } void CTest10_2Dlg::OnButton5() { // TODO: Add your control notification handler code here Press(5); } void CTest10_2Dlg::OnButton6() { // TODO: Add your control notification handler code here Press(6); } void CTest10_2Dlg::OnButton7() { // TODO: Add your control notification handler code here Press(7); } void CTest10_2Dlg::OnButton8() { // TODO: Add your control notification handler code here Press(8); } void CTest10_2Dlg::OnButton9() { // TODO: Add your control notification handler code here Press(9); } void CTest10_2Dlg::OnButton00() { // TODO: Add your control notification handler code here // Flag00 = TRUE; Press(0); Press(0); } void CTest10_2Dlg::OnDot() { // TODO: Add your control notification handler code here DotFlag = TRUE; } void CTest10_2Dlg::OnButton() { // TODO: Add your control notification handler code here // operate = 'n'; m_result = \"-\"; UpdateData(FALSE); _Flag = TRUE; } void CTest10_2Dlg::OnAdd() { // TODO: Add your control notification handler code here operate = '+'; DotFlag = FALSE; _Flag = FALSE; } void CTest10_2Dlg::OnMinus() { // TODO: Add your control notification handler code here operate = '-'; DotFlag = FALSE; _Flag = FALSE; } void CTest10_2Dlg::OnMul() { // TODO: Add your control notification handler code here operate = '*'; DotFlag = FALSE; _Flag = FALSE; } void CTest10_2Dlg::OnDiv() { // TODO: Add your control notification handler code here operate = '/'; DotFlag = FALSE; _Flag = FALSE; } void CTest10_2Dlg::OnCount() { // TODO: Add your control notification handler code here switch(operate) { case '+': num1 += num2; break; case '-': num1 -= num2; break; case '*': num1 *= num2; break; case '/': if(num2 == 0) { MessageBox(\"除数不可以是0!\"); return; } num1 /= num2; break; case 'n': num1 = 0 - num2; break; case 'N': } break; m_result.Format(\"%g\ UpdateData(FALSE); DotFlag = FALSE; _Flag = FALSE; num1 = 0; num2 = 0; e = 0; operate = 'N'; } void CTest10_2Dlg::Press(int n) { if(DotFlag) { } if(operate == 'N') { if(n) else { } } else { if(_Flag) { num2 -= n*pow(0.1,e); UpdateData(TRUE); m_result += '0'; { if(_Flag) { num1 -= n*pow(0.1,e); } else { num1 += n*pow(0.1,e); } m_result.Format(\"%g\} } num2 += n*pow(0.1,e); m_result.Format(\"%g\} else { if(operate == 'N') { num1 *= 10; if(_Flag) { } else { } m_result.Format(\"%g\} else { num2 *= 10; if(_Flag) { num2 -= n; } else { num2 += n; } m_result.Format(\"%g\} num1 += n; num1 -= n; } ++e; UpdateData(FALSE); } 结果: 整数加法:123+33 小数加法: 23.65+19.8 365.3*23 129.65/15 负数 -78+23 四、实验总结 计算器可以完成简单的整数、小数、正负数的加减乘除运算,不过结果不够精确,使用的数据类型是double型,格式转换时使用的“%g”。对数据的格式输出还是不很熟悉。知识层次有待提高。 教师评语 2012年 月 日 实验成绩 优 良 中 及格 不及格

因篇幅问题不能全部显示,请点此查看更多更全内容