当前位置:文档之家› 计算器制作步骤

计算器制作步骤

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;
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();
void CCalculatorDlg::OnPoint()//处理小数点
{
// TODO: Add your control notification handler code here
m_bCoff=1;
UpdateDisplay();
}
void CCalculatorDlg::Calculate()//处理计算,注意用类向导先添加此成员函数
Calculate();
m_operator=OpMultiply;
}
void CCalculatorDlg::OnDivid()//除
{
// TODO: Add your control notification handler code here
Calculate();
m_operator=OpDivide;
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()//处理函数运算
{
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)
{
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;
{
// TODO: Add your control notification handler code here
Calculate();
m_operator=OpSubtract;
}
void CCalculatorDlg::OnMutiply()//乘
{
// TODO: Add your control notification handler code here
afx_msg void OnPoint();
afx_msg void OnEqual();
afx_msg void OnSqrt();
afx_msg void OnRecip();
afx_msg void OnOperandInput(UINT iID);//注意先向类中添加protected型成员函数OnOperandInput,然后再屏蔽掉类中的该函数声明,在此位置添加此说明
简易计算器制作步骤:
1、创建基于对话框的MFC(EXE)应用程序Calculator;
2、在对话框窗体上顺序创建0到9十个数字按钮,并设置其标识符分别为IDC_0到IDC_9,其它按钮按下表设置属性:
3、按表2添加各运算按钮的消息处理函数
4、为使0到9十个数字按钮响应相同的消息处理函数,定义宏ON_COMMAND_RANGE
{
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)
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)
(1)// CalculatorDlg.h
//{{AFX_MSG(CCalculatorDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
#include "math.h"
7、添加各功能代码
(1)数字输入消息处理函数
void CCalculatorDlg::OnOperandInput(UINT iID)
{
ASSERT(iID >= IDC_0 && iID <= IDC_9);
if(m_errorState!=ErrNone)
return;
float m_coff; //小数输入时的系数
Operator m_operator; //enum型变量用以标识当前运算符
CalcError m_errorState; //enum型变量用以标识当前运算状态
Func m_func; //enum型变量用以标识当前运算函数类型
BOOL m_bOperandAvail; //标识当前输入是否为新输入数字
{
// TODO: Add your control notification handler code here
Calculate();
m_operator=OpNone;
}
void CCalculatorDlg::OnSqrt()//处理开根号
{
// TODO: Add your control notification handler code here
}
m_bOperandAvail=FALSE;
m_bCoff=0;
m_coff=0.1;
UpdateDisplay();
}
void CCalculatorDlg::Run_Func()//处理求根和求倒
{
if (m_errorState != ErrNone)
return;
if (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_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_coff=0.1;
m_bCoff=0;
m_errorState = ErrNone;
m_bOperandAvail=FALSE;
m_operator=OpNone;
}
另:在文件CalculatorDlg.cpp中添加
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
enum Operator { OpNone,OpAdd,OpSubtract,OpMultiply,OpDivide};
enum CalcError { ErrNone,ErrDivideByZero};
enum Func { FuncSin, FuncTan, FuncCos, FuncSqrt, FuncSqre, FuncLn, FuncLog,
: 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
相关主题