能帮我解释一段代码吗?希望每句都有注释 5
voidCCCalculatorDlg::OnNum(UINTuID){UINTindex=uID-IDC_NUM_0;CStringnum;CStringstr;num...
void CCCalculatorDlg::OnNum(UINT uID)
{
UINT index=uID- IDC_NUM_0;
CString num;
CString str;
num.Format(_T("%d"),index);
//AfxMessageBox(num);//弹出对话框提醒
switch(st)
{
case _CE:
str=num;
m_ctlDisplay.SetWindowText(str);
st=_A;
break;
case _A:
m_ctlDisplay.GetWindowText(str);
str+=num;
m_ctlDisplay.SetWindowText(str);
break;
case _B:
str=num;
m_ctlDisplay.SetWindowText(str);
st=_C;
break;
case _C:
m_ctlDisplay.GetWindowText(str);
str+=num;
m_ctlDisplay.SetWindowText(str);
break;
case _D:
str=num;
m_ctlDisplay.SetWindowText(str);
st=_A;
break;
default:;
}
}
double CCCalculatorDlg::GetCurrentNum()
{ double num=0;
CString str;
m_ctlDisplay.GetWindowText(str);
num=atof(str);
return num;
}//获得当前显示的数字
void CCCalculatorDlg::DisplayNum(double d)
{
CString str;
str.Format(_T("%f"),d);
m_ctlDisplay.SetWindowText(str);
}//显示数字
void CCCalculatorDlg:: Clear()
{ op=None;
st=_CE;
m_dCurrentNum=m_dPreNum=0;
}
void CCCalculatorDlg::OnOperator(_operator_ _op)
{
switch(st){
case _A:
op=_op;
m_dPreNum=GetCurrentNum();
st=_B;
break;
case _C:
m_dCurrentNum=GetCurrentNum();
m_dPreNum=GetValue(m_dPreNum,m_dCurrentNum);
DisplayNum(m_dPreNum);
op=_op;
st=_B;
break;
case _D:
st=_B;
m_dPreNum=GetCurrentNum();
op=_op;
break;
default:;
}
}
double CCCalculatorDlg::GetValue(double m,double n)
{ double dr=0;
double br=m;
double cr=1/m;//求第一个操作数的倒数用来求x^y
int i;
switch(op)
{
case Add:
dr=m+n;
break;
case Multi:
dr=m*n;
break;
case Minus:
dr=m-n;
break;
case Div:
if(n==0.0)
{
AfxMessageBox(_T("分母不能为0!"));
OnCe();
}
else
{
dr=m/n;
}
break;
case Mod:
dr=(int)m%(int)n;//这里简单起见,转换为整数求余数
break;
case Exp:
if(n>=0)//n为正数
for(i=0;i<n;i++)
{
br=br*10;//乘以10的n次幂
}
else//n为负数
{ n=-n;
for(i=0;i<n;i++)
{
br=br*0.1;//乘以0.1的n次幂
}
}
dr=br;
break;
case SQUAR:
if(n>=0)
for (i=0;i<n-1;i++)
{
br=br*m;
}
else
{ br=1/br;
n=-n;
for(i=0;i<n-1;i++)
{
br=br*cr;//乘以0.1的n次幂
}
}
dr=br;
break;
default:;
}
return dr;
} 展开
{
UINT index=uID- IDC_NUM_0;
CString num;
CString str;
num.Format(_T("%d"),index);
//AfxMessageBox(num);//弹出对话框提醒
switch(st)
{
case _CE:
str=num;
m_ctlDisplay.SetWindowText(str);
st=_A;
break;
case _A:
m_ctlDisplay.GetWindowText(str);
str+=num;
m_ctlDisplay.SetWindowText(str);
break;
case _B:
str=num;
m_ctlDisplay.SetWindowText(str);
st=_C;
break;
case _C:
m_ctlDisplay.GetWindowText(str);
str+=num;
m_ctlDisplay.SetWindowText(str);
break;
case _D:
str=num;
m_ctlDisplay.SetWindowText(str);
st=_A;
break;
default:;
}
}
double CCCalculatorDlg::GetCurrentNum()
{ double num=0;
CString str;
m_ctlDisplay.GetWindowText(str);
num=atof(str);
return num;
}//获得当前显示的数字
void CCCalculatorDlg::DisplayNum(double d)
{
CString str;
str.Format(_T("%f"),d);
m_ctlDisplay.SetWindowText(str);
}//显示数字
void CCCalculatorDlg:: Clear()
{ op=None;
st=_CE;
m_dCurrentNum=m_dPreNum=0;
}
void CCCalculatorDlg::OnOperator(_operator_ _op)
{
switch(st){
case _A:
op=_op;
m_dPreNum=GetCurrentNum();
st=_B;
break;
case _C:
m_dCurrentNum=GetCurrentNum();
m_dPreNum=GetValue(m_dPreNum,m_dCurrentNum);
DisplayNum(m_dPreNum);
op=_op;
st=_B;
break;
case _D:
st=_B;
m_dPreNum=GetCurrentNum();
op=_op;
break;
default:;
}
}
double CCCalculatorDlg::GetValue(double m,double n)
{ double dr=0;
double br=m;
double cr=1/m;//求第一个操作数的倒数用来求x^y
int i;
switch(op)
{
case Add:
dr=m+n;
break;
case Multi:
dr=m*n;
break;
case Minus:
dr=m-n;
break;
case Div:
if(n==0.0)
{
AfxMessageBox(_T("分母不能为0!"));
OnCe();
}
else
{
dr=m/n;
}
break;
case Mod:
dr=(int)m%(int)n;//这里简单起见,转换为整数求余数
break;
case Exp:
if(n>=0)//n为正数
for(i=0;i<n;i++)
{
br=br*10;//乘以10的n次幂
}
else//n为负数
{ n=-n;
for(i=0;i<n;i++)
{
br=br*0.1;//乘以0.1的n次幂
}
}
dr=br;
break;
case SQUAR:
if(n>=0)
for (i=0;i<n-1;i++)
{
br=br*m;
}
else
{ br=1/br;
n=-n;
for(i=0;i<n-1;i++)
{
br=br*cr;//乘以0.1的n次幂
}
}
dr=br;
break;
default:;
}
return dr;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询