北邮基于MFC的计算器设计.docx
- 文档编号:27393269
- 上传时间:2023-06-30
- 格式:DOCX
- 页数:31
- 大小:81.74KB
北邮基于MFC的计算器设计.docx
《北邮基于MFC的计算器设计.docx》由会员分享,可在线阅读,更多相关《北邮基于MFC的计算器设计.docx(31页珍藏版)》请在冰豆网上搜索。
北邮基于MFC的计算器设计
1.课题概述
1.1课题目标和主要内容
本次课题以编写基于MFC的简易计算器为例,重点是学习如何使用MFC消息响应机制完成一个windows下的交互式软件。
本实验以visualstudio2008为平台。
1.2系统的主要功能
基本功能
单击数字键,显示当前数值
单击“+”、“-”、“*”、“/”建进行运算
单击“=”,显示运算结果
单击“C”,清除已有结果
扩展功能
进行浮点运算
进行倒数、开方运算
2.系统设计
2.1系统总体框架
对话框
显示框
按钮
事件驱动函数函数
运算函数
2.2系统详细设计
[1]模块划分图及描述
(1)对话框:
实现与用户的交互,使用了两类控件:
显示框和按钮。
(2)事件驱动函数:
处理由鼠标单击响应到程序的问题,主要有映射和消息处理函数组成。
例如:
voidCCalculatorDlg:
:
OnBnClickedButtonPoint()
{
m_coff=0.1;
}
(3)运算函数:
实现逻辑运算以及数据交换。
例如:
voidCCalculatorDlg:
:
OnBnClickedButtonSqrt()
{
if(m_second==0)
{m_first=sqrt(m_first);
UpdateDisplay(m_first);}
else
{m_second=sqrt(m_second);
UpdateDisplay(m_second);}
}
[2]类关系图及描述
classCCalculatorDlg:
publicCDialogEx
{
//构造
public:
CCalculatorDlg(CWnd*pParent=NULL);//标准构造函数
//对话框数据
enum{IDD=IDD_CALCULATOR_DIALOG};
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDV支持
//实现
protected:
HICONm_hIcon;
//生成的消息映射函数
virtualBOOLOnInitDialog();
afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);
afx_msgvoidOnPaint();
afx_msgHCURSOROnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
intm_duType;//角度和弧度判断
doublem_first;//存储一次运算的第一个操作数
及一次运算的结果
doublem_second;//存储一次运算的第二个操作数
CStringm_operator;//存储运算符
doublem_coff;//存储小数点的系数权值
CStringm_display;//编辑框IDC_DISPLAY的关联变量,显示计算结果
afx_msgvoidOnBnClickedHudu();
afx_msgvoidOnBnClickedJiaodu();
voidUpdateDisplay(doubledck);
voidCalculate(void);
voidonButtonN(intn);
afx_msgvoidOnBnClickedButton1();
afx_msgvoidOnBnClickedButton2();
afx_msgvoidOnBnClickedButton3();
afx_msgvoidOnBnClickedButton4();
afx_msgvoidOnBnClickedButton5();
afx_msgvoidOnBnClickedButton6();
afx_msgvoidOnBnClickedButton7();
afx_msgvoidOnBnClickedButton8();
afx_msgvoidOnBnClickedButton9();
afx_msgvoidOnBnClickedButton0();
afx_msgvoidOnBnClickedButtonPoint();
afx_msgvoidOnBnClickedButtonSign();
afx_msgvoidOnBnClickedButtonAdd();
afx_msgvoidOnBnClickedButtonMinus();
afx_msgvoidOnBnClickedButtonMutiply();
afx_msgvoidOnBnClickedButtonDiv();
afx_msgvoidOnBnClickedButtonClear();
afx_msgvoidOnBnClickedButtonSqrt();
afx_msgvoidOnBnClickedButtonReci();
afx_msgvoidOnBnClickedButtonEqual();
[3]程序流程图及描述(
是
否
2.3关键算法分析
算法1:
UpdateDisplay
[1]算法功能:
更新显示数据。
[2]算法基本思想:
将输出格式化,更新显示框。
[3]算法空间、时间复杂度分析
时间复杂度为O
(1)。
[4]代码逻辑(可用伪代码描述)
voidCCalculatorDlg:
:
UpdateDisplay(doubledck)
{
m_display.Format(_T("%f"),dck);
inti=m_display.GetLength();
while(m_display.GetAt(i-1)=='0')//将输出格式化
{m_display.Delete(i-1,1);i--;}
UpdateData(false);//更新显示框
}
算法2:
Calculate
[1]算法功能:
实现各种运算。
[2]算法基本思想:
单击按钮,运用消息响应函数从显示框读取操作数,再将运算结果显示。
[3]算法空间、时间复杂度分析
时间复杂度为O
(1)。
[4]代码逻辑(可用伪代码描述)
voidCCalculatorDlg:
:
Calculate(void)
{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="除数不能为0";
UpdateData(false);
return;}
m_first/=m_second;break;
}
m_second=0.0;
m_coff=1.0;
m_operator=_T("+");
UpdateDisplay(m_first);//更新显示框╕
}
voidCCalculatorDlg:
:
onButtonN(intn)
{
if(m_coff==1.0)
m_second=m_second*10+n;//作为整数输入数字
else
{m_second=m_second+n*m_coff;//作为小数输入数字
m_coff*=0.1;}
UpdateDisplay(m_second);//更新显示框
}
2.4其他
对一些函数进行了封装,比如onBUttonN函数,使得主函数看起来比较简洁。
而封装的函数可以重复运用,也减少了代码的长度。
3.程序运行结果分析
4.总结
4.1本课题的评价
此实验主要由按钮和编辑显示框组成,是一个最基本的MFC程序。
其实践让我们在之前面向过程的编程基础上进一步接触了面向对象的程序设计。
4.2心得体会
通过这次实验,我对基于MFC的编程有了更深的了解,能实现特定的功能,同时对C++的使用更加熟练。
此外,细心和耐心也很重要,一次次的纠错让我对这个程序更加熟悉,对以后的学习有很大帮助。
5.参考文献
C++高级语言程序设计案例与实践辅导/徐惠民主编——北京:
人民邮电出版社,2015.3
源代码:
//CalculatorDlg.h:
头文件
//
#pragmaonce
//CCalculatorDlg对话框
classCCalculatorDlg:
publicCDialogEx
{
//构1造ì
public:
CCalculatorDlg(CWnd*pParent=NULL);//标准构造函数
//对话框数据
enum{IDD=IDD_CALCULATOR_DIALOG};
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDV支持
//实现
protected:
HICONm_hIcon;
//生成的消息映射函数
virtualBOOLOnInitDialog();
afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);
afx_msgvoidOnPaint();
afx_msgHCURSOROnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
intm_duType;
doublem_first;
doublem_second;
CStringm_operator;
doublem_coff;
CStringm_display;
afx_msgvoidOnBnClickedHudu();
afx_msgvoidOnBnClickedJiaodu();
voidUpdateDisplay(doubledck);
voidCalculate(void);
voidonButtonN(intn);
afx_msgvoidOnBnClickedButton1();
afx_msgvoidOnBnClickedButton2();
afx_msgvoidOnBnClickedButton3();
afx_msgvoidOnBnClickedButton4();
afx_msgvoidOnBnClickedButton5();
afx_msgvoidOnBnClickedButton6();
afx_msgvoidOnBnClickedButton7();
afx_msgvoidOnBnClickedButton8();
afx_msgvoidOnBnClickedButton9();
afx_msgvoidOnBnClickedButton0();
afx_msgvoidOnBnClickedButtonPoint();
afx_msgvoidOnBnClickedButtonSign();
afx_msgvoidOnBnClickedButtonAdd();
afx_msgvoidOnBnClickedButtonMinus();
afx_msgvoidOnBnClickedButtonMutiply();
afx_msgvoidOnBnClickedButtonDiv();
afx_msgvoidOnBnClickedButtonClear();
afx_msgvoidOnBnClickedButtonSqrt();
afx_msgvoidOnBnClickedButtonReci();
afx_msgvoidOnBnClickedButtonEqual();
doublem_PI;
voidsanjiaoHanshu(void);
afx_msgvoidOnBnClickedExp();
afx_msgvoidOnBnClicked10x();
afx_msgvoidOnBnClicked2dex();
afx_msgvoidOnBnClickedLog();
afx_msgvoidOnBnClickedLn();
afx_msgvoidOnBnClickedSin();
afx_msgvoidOnBnClickedCos();
afx_msgvoidOnBnClickedTan();
afx_msgvoidOnBnClickedArcsin();
afx_msgvoidOnBnClickedArccos();
afx_msgvoidOnBnClickedArctan();
afx_msgvoidOnBnClickedXdey();
boolisXY;
doubletemp;
voidxDEy(void);
};
//CalculatorDlg.cpp:
实害?
现?
文?
件t
//
#include"stdafx.h"
#include"Calculator.h"
#include"CalculatorDlg.h"
#include"afxdialogex.h"
#include"math.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#endif
//用?
于?
应畖用?
程ì序ò“°关?
于?
”±菜?
单蹋?
项?
的?
CAboutDlg对?
话°框ò
classCAboutDlg:
publicCDialogEx
{
public:
CAboutDlg();
//对?
话°框ò数簓据Y
enum{IDD=IDD_ABOUTBOX};
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDV支§持?
//实害?
现?
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialogEx(CAboutDlg:
:
IDD)
{
}
voidCAboutDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialogEx:
:
DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg,CDialogEx)
END_MESSAGE_MAP()
//CCalculatorDlg对?
话°框ò
CCalculatorDlg:
:
CCalculatorDlg(CWnd*pParent/*=NULL*/)
:
CDialogEx(CCalculatorDlg:
:
IDD,pParent)
m_duType(0)
m_first(0.0)
m_second(0.0)
m_operator(_T("+"))
m_coff(0)
m_display(_T("0."))
m_PI(3.141592)
isXY(false)
temp(0)
{
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
voidCCalculatorDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialogEx:
:
DoDataExchange(pDX);
DDX_Text(pDX,IDC_DISPLAY,m_display);
}
BEGIN_MESSAGE_MAP(CCalculatorDlg,CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_HUDU,&CCalculatorDlg:
:
OnBnClickedHudu)
ON_BN_CLICKED(IDC_JIAODU,&CCalculatorDlg:
:
OnBnClickedJiaodu)
ON_BN_CLICKED(IDC_BUTTON1,&CCalculatorDlg:
:
OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2,&CCalculatorDlg:
:
OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3,&CCalculatorDlg:
:
OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON4,&CCalculatorDlg:
:
OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON5,&CCalculatorDlg:
:
OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON6,&CCalculatorDlg:
:
OnBnClickedButton6)
ON_BN_CLICKED(IDC_BUTTON7,&CCalculatorDlg:
:
OnBnClickedButton7)
ON_BN_CLICKED(IDC_BUTTON8,&CCalculatorDlg:
:
OnBnClickedButton8)
ON_BN_CLICKED(IDC_BUTTON9,&CCalculatorDlg:
:
OnBnClickedButton9)
ON_BN_CLICKED(IDC_BUTTON0,&CCalculatorDlg:
:
OnBnClickedButton0)
ON_BN_CLICKED(IDC_BUTTON_POINT,&CCalculatorDlg:
:
OnBnClickedButtonPoint)
ON_BN_CLICKED(IDC_BUTTON_SIGN,&CCalculatorDlg:
:
OnBnClickedButtonSign)
ON_BN_CLICKED(IDC_BUTTON_ADD,&CCalculatorDlg:
:
OnBnClickedButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_MINUS,&CCalculatorDlg:
:
OnBnClickedButtonMinus)
ON_BN_CLICKED(IDC_BUTTON_MUTIPLY,&CCalculatorDlg:
:
OnBnClickedButtonMutiply)
ON_BN_CLICKED(IDC_BUTTON_DIV,&CCalculatorDlg:
:
OnBnClickedButtonDiv)
ON_BN_CLICKED(IDC_SIN,&CCalculatorDlg:
:
OnBnClickedSin)
ON_BN_CLICKED(IDC_BUTTON_CLEAR,&CCalculatorDlg:
:
OnBnClickedButtonClear)
ON_BN_CLICKED(IDC_BUTTON_SQRT,&CCalculatorDlg:
:
OnBnClickedButtonSqrt)
ON_BN_CLICKED(IDC_BUTTON_RECI,&CCalculatorDlg:
:
OnBnClickedButtonReci)
ON_BN_CLICKED(IDC_BUTTON_EQUAL,&CCalculatorDlg:
:
OnBnClickedButtonEqual)
ON_BN_CLICKED(IDC_EXP,&CCalculatorDlg:
:
OnBnClickedExp)
ON_BN_CLICKED(IDC_10X,&CCalculatorDlg:
:
OnBnClicked10x)
ON_BN_CLICKED(IDC_2DEX,&CCalculatorDlg:
:
OnBnClicked2dex)
ON_BN_CLICKED(IDC_XDEY,&CCalculatorDlg:
:
OnBnClickedXdey)
ON_BN_CLICKED(IDC_XDEY,&CCalculatorDlg:
:
OnBnClickedXdey)
ON_BN_CLICKED(IDC_LOG,&CCalculatorDlg:
:
OnBnClickedLog)
ON_BN_CLICKED(IDC_LN,&CCalculatorDlg:
:
OnBnClickedLn)
ON_BN_CLICKED(IDC_SIN,&CCalculatorDlg:
:
OnBnClickedSin)
ON_BN_CLICKED(IDC_COS,&CCalculatorDlg:
:
OnBnClickedCos)
ON_BN_CLICKED(IDC_TAN,&CCalculatorDlg:
:
OnBnClickedTan)
ON_BN_CLICKED(IDC_ARCSIN,&CCalculatorDlg:
:
OnBnClickedArcsin)
ON_BN_CLICKED(IDC_ARCCOS,&CCalculatorDlg:
:
OnBnClickedArccos)
ON_BN_CLICKED(IDC_ARCTAN,&CCalculatorDlg:
:
OnBnClickedArctan)
ON_BN_CLICKED(IDC_XDEY,&CCalculatorDlg:
:
OnBnClickedXdey)
END_MESSAGE_MAP()
//CCalculatorDlg消息处理程序
BOOLCCalculatorDlg:
:
OnInitDialog()
{
CDialogEx:
:
OnInitDialog();
//将关于菜单项添加到系统菜单中
//IDM_ABOUTBOX必须在系统命令范围内
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystem
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 MFC 计算器 设计