计算机MFC课程设计报告——模拟计算器姓名:学号:02011227联系电话:指导教师:东南大学机械工程学院2012年10月14日模拟计算器摘要摘要内容:以课本简单计算器为基础,首先实现了加、减、乘、除、求倒数和平方根的混合运算,并能进行清屏及倒退操作,然后自行完善了书中未能实现的乘、除运算的连续操作,最后自行设计并完成了lg、ln 、sin、cos、^、.、pi、e八个按钮控件的消息映射及程序代码的添加和修改。
从中学会了制作简单的基于对话框的小工具、掌握了常用控件的使用、明白了消息映射及消息处理、提高了VC++编程水平。
关键词:四则运算小数点计算器Analog calculatorAbstractContent of abstract: Simple calculator in textbooks as the foundation, first realized the addition, subtraction, multiplication, division, and reciprocal and square root of the mixed operation, and can clear screen and reverse operation, and then to improve the book failed to realize the multiplication, division operation of continuous operation, and finally to be designed and completed the eight button control news mapping and program code to add and modify. Learnt how to make simple dialog-based small tools, mastered the use of commonly used controls, see news mapping and information processing, improve the level of programming.Key word: arithmetic Decimal point Calculator本计算器是基于VC++的MFC编程,可完成大多数简单操作,其运行后界面如下图:以课本简单计算器为基础,首先实现了加、减、乘、除、求倒数和平方根的混合运算,并能进行清屏及倒退操作,然后自行完善了书中未能实现的乘、除运算的连续操作,最后自行设计并完成了lg、ln 、sin、cos、^、.、pi、e 八个按钮控件的消息映射及程序代码的添加和修改。
完整的操作步骤:1、创建MFC应用程序框架。
使用MFC AppWizard【exe】向导生成一个基于对话框的应用程序 Calculator。
2、编辑对话框。
按图添加前21个控件,其中1个为编辑框,20个为按钮,控件ID及Caption设置如下:IDC_EDIT_PUTOUTIDC_NUMBER1 1 IDC_NUMBER2 2IDC_NUMBER3 3 IDC_NUMBER4 4 IDC_NUMBER5 5 IDC_NUMBER6 6 IDC_NUMBER7 7 IDC_NUMBER8 8IDC_NUMBER9 9IDC_NUMBER0 0IDC_ADD + IDC_SUBTRACT - IDC_MULTIPLY * IDC_DIVIDE / IDC_RESULT =IDC_MINUS +/-IDC_BACK BackIDC_CLEAR ClearIDC_SQRT SqrtIDC_RECIPROCAL 1/xIDC_LG lgIDC_LN lnIDC_SIN sin IDC_COS cosIDC_CHENGFANG ^IDC_POINT .IDC_PI piIDC_CHAOYUESHU e3、添加成员变量。
为编辑框在CCalculatorDlg对话框类中添加double型成员变量m_result,再为CCalculatorDlg类添加:int m_OperationCount (存放加减乘除等运算符的顺序号)、 int m_NumberCount (存放操作数的顺序号)、double m_number[15](存放操作数)、 double m_Operation[15] (存放操作符)、 bool flag (判断整数和小数)、 double p(小数退位)。
4、添加消息映射及成员函数(1)、在CalculatorDlg.h头文件中声明消息映射函数。
afx_msg void OnNumberKey(UINT nID); //响应数字按钮的单击操作afx_msg void OnOperationKey(UINT nID); //响应操作符按钮的单击操作(2)、在CalculatorDlg.cpp实现文件消息映射表中加入ON_COMMAND_RANGE 命令消息ON_COMMAND_RANGE(IDC_NUMBER1,IDC_NUMBER0,OnNumberKey)ON_COMMAND_RANGE(IDC_ADD,IDC_CHAOYUESHU,OnOperationKey) //有新增功能时命令消息中参数记得修改(3)、在CalculatorDlg.cpp实现文件中加入消息处理函数。
void CCaculatorDlg::OnNumberKey (UINT nID){int n=0;switch(nID){case IDC_NUMBER1:n=1;break;case IDC_NUMBER2:n=2;break;case IDC_NUMBER3:n=3;break;case IDC_NUMBER4:n=4;break;case IDC_NUMBER5:n=5;break;case IDC_NUMBER6:n=6;break;case IDC_NUMBER7:n=7;break;case IDC_NUMBER8:n=8;break;case IDC_NUMBER9:n=9;break;case IDC_NUMBER0:n=0;break;}if(flag==true) //判断是否为小数{p*=0.1;m_number[m_NumberCount]=m_number[m_NumberCount]+n*p; }else{m_number[m_NumberCount]=m_number[m_NumberCount]*10+n;flag=false;}m_result=m_number[m_NumberCount];UpdateData(false); //功能加强,不仅可输入整数,还可输入小数,使运算范围大大扩增}void CCaculatorDlg::OnOperationKey (UINT nID){int i;switch(nID){case IDC_ADD:m_Operation[m_OperationCount]=1;break;case IDC_SUBTRACT:m_Operation[m_OperationCount]=2;break;case IDC_MULTIPLY:m_Operation[m_OperationCount]=3;break;case IDC_DIVIDE:m_Operation[m_OperationCount]=4;break;case IDC_RESULT:cal();break;case IDC_MINUS:m_number[m_NumberCount]=-m_number[m_NumberCount];break;case IDC_BACK:if(flag==false){m_number[m_NumberCount]=(int)m_number[m_NumberCount]/10;m_result=m_number[m_NumberCount];}else{m_number[m_NumberCount]=m_number[m_NumberCount]/p;m_number[m_NumberCount]=(int)m_number[m_NumberCount]/10;m_number[m_NumberCount]=m_number[m_NumberCount]*10*p;m_result=m_number[m_NumberCount];}//增加小数之后Back操作有所改变UpdateData(false);break;case IDC_CLEAR:for(i=1;i<11;i++){m_number[i]=0;m_Operation[i]=999;m_NumberCount=1;m_OperationCount=1;flag=false;p=1; //增加小数之后Clear操作有所改变m_result=0;UpdateData(false);}break;case IDC_SQRT:m_number[m_NumberCount]=sqrt(m_number[m_NumberCount]);break;case IDC_RECIPROCAL:m_number[m_NumberCount]=(double)1/m_number[m_NumberCount];break;case IDC_LG:m_number[m_NumberCount]=log10(m_number[m_NumberCount]);break;case IDC_LN:m_number[m_NumberCount]=log(m_number[m_NumberCount]);break;case IDC_SIN:m_number[m_NumberCount]=sin(m_number[m_NumberCount]);break;case IDC_COS:m_number[m_NumberCount]=cos(m_number[m_NumberCount]);break;case IDC_CHENGFANG:m_Operation[m_OperationCount]=5;break;case IDC_POINT:flag=true;break;case IDC_PI:m_result=m_number[m_NumberCount]=acos(-1);UpdateData(false);break;case IDC_CHAOYUESHU:m_result=m_number[m_NumberCount]=exp(1);UpdateData(false);break;}//新增功能,使运算更完善if(m_Operation[m_OperationCount]<6){m_NumberCount++;m_OperationCount++;flag=false;p=1;}//呼应小数点和乘方的功能}(4)、在CalculatorDlg对话框中添加void型成员函数cal(),并在CalculatorDlg.cpp实现文件前加上包含语句 #include <math.h>。