计算器制作步骤

  • 格式:doc
  • 大小:392.00 KB
  • 文档页数:8

下载文档原格式

  / 8
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_MINUS, OnMinus)
ON_BN_CLICKED(IDC_MUTIPLY, OnMutiply)
ON_BN_CLICKED(IDC_DIVID, OnDivid)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_SIGN, OnSign)
ON_BN_CLICKED(IDC_POINT, OnPoint)
ON_BN_CLICKED(IDC_EQUAL, OnEqual)
ON_BN_CLICKED(IDC_SQRT, OnSqrt)
ON_BN_CLICKED(IDC_RECIP, OnRecip)
UpdateDisplay();
}
(2)运算符消息处理函数
void CCalculatorDlg::OnAdd()//加
{
// TODO: Add your control notification handler code here
Calculate();
m_operator=OpAdd;
}
void CCalculatorDlg::OnMinus()//减
if(!m_bOperandAvail)
m_operand=0;
if(!m_bCoff)
m_operand=m_operand*10+(iID-IDC_0);
else
{
m_operand=m_operand+(iID-IDC_0)*m_coff;
m_coff*=0.1;
}
m_bOperandAvail=TRUE;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_coff=0.1;
m_bCoff=0;
m_errorState = ErrNone;
m_bOperandAvail=FALSE;
m_operator=OpNone;
}
另:在文件CalculatorDlg.cpp中添加
简易计算器制作步骤:
1、创建基于对话框的MFC(EXE)应用程序Calculator;
2、在对话框窗体上顺序创建0到9十个数字按钮,并设置其标识符分别为IDC_0到IDC_9,其它按钮按下表设置属性:
3、按表2添加各运算按钮的消息处理函数
4、为使0到9十个数字按钮响应相同的消息处理函数,定义宏ON_COMMAND_RANGE
ON_COMMAND_RANGE(IDC_0,IDC_9,OnOperandInput)//使用一个消息函数来处理对某个ID范围内所有控件的命令响应
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
5、在头文件CalculatorDlg.h中添加类型、成员变量及成员函数
(1)自定义类型(定义在类外)
}
m_bOperandAvail=FALSE;
m_bCoff=0;
m_coff=0.1;
UpdateDisplay();
}
void CCalculatorDlg::Run_Func()//处理求根和求倒
{
if (m_errorState != ErrNone)
return;
if (m_bOperandAvail)
{
// TODO: Add your control notification handler code here
Calculate();
m_operator=OpSubtract;
}
void CCalculatorDlg::OnMutiply()//乘
{
// TODO: Add your control notification handler code here
(1)// CalculatorDlg.h
//{{AFX_MSG(CCalculatorDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
while(m_result.GetAt(i-1)=='0')
{
m_result.Delete(i-1,1);
i-=1;
}
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnClear()//处理清空
{
// TODO: Add your control notification handler code here
FuncN, FuncRec, FuncExp, FuncNone};
(2)成员变量及函数(此处应定义为类CalculatorDlg的公有成员)
float m_operand; //存储当前输入的操作数
float m_accum; //存储当前的计算结果
BOOL m_bCoff; //标识当前输入是否是小数
: CDialog(CCalculatorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCalculatorDlg)
m_result = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
//}}AFX_MSG
(2)//CalculatorDlg.cpp
BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
//{{AFX_MSG_MAP(CCalculatorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnAdd();
afx_msg void OnMinus();
afx_msg void OnMutiply();
afx_msg void OnDivid();
afx_msg void OnClear();
afx_msg void OnSign();
afx_msg void OnPoint();
afx_msg void OnEqual();
afx_msg void OnSqrt();
afx_msg void OnRecip();
afx_msg void OnOperandInput(UINT iID);//注意先向类中添加protected型成员函数OnOperandInput,然后再屏蔽掉类中的该函数声明,在此位置添加此说明
float m_coff; //小数输入时的系数
Operator m_operator; //enum型变量用以标识当前运算符
CalcError m_errorState; //enum型变量用以标识当前运算状态
Func m_func; //enum型变量用以标识当前运算函数类型
BOOL m_bOperandAvail; //标识当前输入是否为新输入数字
m_func=FuncSqrt;
Run_Func();
}
void CCalculatorDlg::OnRecip()//求倒数1/x
{
// TODO: Add your control notification handler code here
m_func=FuncRec;
Run_Func();
}
m_operator = OpNone;
m_operand = 0;
m_accum = 0;
m_bOperandAvail = FALSE;
m_errorState = ErrNone;
m_coff=0.1;
m_bCoff=0;
UpdateDisplay();
}
void CCalculatorDlg::Run_Func()//处理函数运算
void CCalculatorDlg::OnPoint()//处理小数点
{
// TODO: Add your control notification handler code here
m_bCoff=1;
UpdateDisplay();
}
void CCalculatorDlg::Calculate()//处理计算,注意用类向导先添加此成员函数
{
if(m_func==FuncSqrt)
m_operand=sqrt(m_operand);
if(m_func==FuncRec)
m_operand=1/m_operand;
}
UpdateDisplay();
}
void CCalculatorDlg::UpdateDisplay()//处理显示
{
if(GetSafeHwnd()==NULL)
#include "math.h"
7、添加各功能代码
(1)数字输入消息处理函数
void CCalculatorDlg::OnOperandInput(UINT iID)
{
ASSERT(iID >= IDC_0 && iID <= IDC_9);
if(m_errorState!=ErrNone)
return;
}
void CCalculatorDlg::OnSign()//处理正负号
{
// TODO: Add your control notification handler code here
m_operand*=-1;
UpdateDisplay();
}
void CCalculatorDlg::OnEqual()//处理等号
{
// TODO: Add your control notification handler code here
Calculate();
m_operator=OpNone;
}
void CCalculatorDlg::OnSqrt()//处理开根号
{
// TODO: Add your control notification handler code here
voidCalculate();//处理普通计算(用类向导,公有成员)
voidUpdateDisplay();//处理显示(用类向导,公有成员)
voidRun_Func();//处理函数运算(用类向导,公有成员)
(3)为编辑框添加Cstring变量m_result
6、部分变量的初始化
CCalculaห้องสมุดไป่ตู้orDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
enum Operator { OpNone,OpAdd,OpSubtract,OpMultiply,OpDivide};
enum CalcError { ErrNone,ErrDivideByZero};
enum Func { FuncSin, FuncTan, FuncCos, FuncSqrt, FuncSqre, FuncLn, FuncLog,
{
if(m_errorState!=ErrNone)
return;
if(m_bOperandAvail)
{
if(m_operator==OpNone)
m_accum=m_operand;
else if(m_operator==OpMultiply)
m_accum*=m_operand;
else if(m_operator==OpDivide)
return;
if(m_errorState!=ErrNone)
m_result="除数不能为零";
else
{
float lval=(m_bOperandAvail)?m_operand:m_accum;
m_result.Format(_T("%f"),lval);
int i=m_result.GetLength();
{
if(m_operand==0)
m_errorState=ErrDivideByZero;
else
m_accum/=m_operand;
}
else if(m_operator==OpAdd)
m_accum+=m_operand;
else if(m_operator==OpSubtract)
m_accum-=m_operand;
Calculate();
m_operator=OpMultiply;
}
void CCalculatorDlg::OnDivid()//除
{
// TODO: Add your control notification handler code here
Calculate();
m_operator=OpDivide;