计算器设计与实现源代码

计算器主要功能的实现
0~9数字的代码(如下)
void CMy7139Dlg::OnButton0()
{
//TODO: Add your control notification handler code here
m_EDIT=m_EDIT+"0";
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
void CMy7139Dlg::OnButton1()
{
//TODO: Add your control notification handler code here
m_EDIT=m_EDIT+"1";
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
void CMy7139Dlg::OnButton2()
{
//TODO: Add your control notification handler code here
m_EDIT=m_EDIT+"2";
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
………………………………………………………………
………………………………………………………………
………………………………………………………………
void CMy7139Dlg::OnButton9()
{
//TODO: Add your control notification handler code here
m_EDIT=m_EDIT+"9";
SetDlgItemText(IDC_EDIT1,m_EDIT);
}

加减乘除法的实现:
void CMy7139Dlg::OnBtnAdd()
{
// TODO: Add your control notification handler code here
temp=m_EDIT;
op='+';
m_EDIT=_T("");
point=false;
}

void CMy7139Dlg::OnBtnDecrease()
{
// TODO: Add your control notification handler code here
temp=m_EDIT;
op='-';
m_EDIT=_T("");
point=false;
}

void CMy7139Dlg::OnBtnDecrease()
{
// TODO: Add your control notification handler code here
temp=m_EDIT;
op='*';
m_EDIT=_T("");
point=false;
}

void CMy7139Dlg::OnBtnDecrease()
{
// TODO: Add your control notification handler code here
temp=m_EDIT;
op='/';
m_EDIT=_T("");
point=false;
}

次方的实现:
void CMy7139Dlg::OnBtnXy()
{
// TODO: Add your control notification handler code here
po temp=m_EDIT;
op='^';
m_EDIT=_T("");
int=false;
}
三次方的实现(二次方的实现同理):
void CMy7139Dlg::OnBtnCube()
{
// TODO: Add your control notification handler code here
char ch2[50];
int squ3;
int squ4;
double temp3;
double temp4;
switch(form)
{
case 'H': squ3=_tcstoul(m_EDIT, 0, 16);
squ4=pow(squ3,3);
m_EDIT.Format("%X",squ4);
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
case 'D': temp3 = atof((LPCTSTR)m_EDIT);
temp4=pow(temp3,3);
m_EDIT.Format("%f",temp4);
SetDlgItemText(IDC_EDIT1,m_EDIT);
point=true;
break;
case 'O': squ3=_tcstoul(m_EDIT, 0, 8);
squ4=pow(squ3,3);
m_EDIT.Format("%o",squ4);
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
case 'B': squ3=_tcstoul(m_EDIT, 0, 2);
squ4=pow(squ3,3);
itoa(squ4,ch2,2);
m_EDIT.Format("%s",ch2);
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
default: return;
}

三角函数的实现:
void CMy7139Dlg::OnBtnSin()
{
// TODO: Add your control notification handler code here
double sintemp;
switch(degngra)
{
case 1:sintemp=atof((LPCTSTR)m_EDIT);
m_EDIT.Format("%f",sin(si

ntemp * PI/180));
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
case 2:sintemp=atof((LPCTSTR)m_EDIT);
m_EDIT.Format("%f",sin(sintemp));
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
}

void CMy7139Dlg::OnBtnCos()
{
// TODO: Add your control notification handler code here
double costemp;
switch(degngra)
{
case 1:costemp=atof((LPCTSTR)m_EDIT);
m_EDIT.Format("%f",cos(costemp * PI/180));
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
case 2:costemp=atof((LPCTSTR)m_EDIT);
m_EDIT.Format("%f",cos(costemp));
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
}

void CMy7139Dlg::OnBtnTan()
{
// TODO: Add your control notification handler code here
double tantemp;
switch(degngra)
{
case 1:tantemp=atof((LPCTSTR)m_EDIT);
if(tantemp!=90)
{
m_EDIT.Format("%f",tan(tantemp * PI/180));
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
else
{
MessageBox("操作数范围错误","ERROR!");
m_EDIT=_T("");
point=false;
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
break;
case 2:tantemp=atof((LPCTSTR)m_EDIT);
if(tantemp!=PI/2)
{
m_EDIT.Format("%f",tan(tantemp));
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
else
{
MessageBox("操作数范围错误","ERROR!");
m_EDIT=_T("");
point=false;
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
break;
}

void CMy7139Dlg::OnBtnCot()
{
// TODO: Add your control notification handler code here
double cottemp;
switch(degngra)
{
case 1:cottemp=atof((LPCTSTR)m_EDIT);
if(cottemp!=0)
{
m_EDIT.Format("%f",1/tan(cottemp * PI/180));
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
else
{
MessageBox("操作数范围错误","ERROR!");
m_EDIT=_T("");
point=false;
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
break;
case 2:cottemp=atof((LPCTSTR)m_EDIT);
if(cottemp!=0)
{
m_EDIT.Format("%f",1/tan(cottemp));
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
else
{
MessageBox("操作数范围错误","ERROR!");
m_EDIT=_T("");
point=false;
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
break;
}

反函数的实现:
void CMy7139Dlg::OnBtnArcsin()
{
// TODO: Add your control notification handler code here
double arcsintemp;
arcsintemp=atof((LPCTSTR)m_EDIT);
if(arcsintemp>=-1 && arcsintemp<=1)
{
switch(degngra)
{
case 1:m_EDIT.Format("%f",asin(arcsintemp)*180/PI);
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
case 2:m_EDIT.Format("%f",asin(arcsintemp));
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
}
}
else
{
MessageBox("操作数范围应为-1到1!","ERROR!");
m_EDIT=_T("");
point=false;
SetDlgItemText(IDC_EDIT1,m_EDIT);
}

void CMy7139Dlg::OnBtnArccos()
{
// TODO: Add your control notification handler code here
double arccost

emp;
arccostemp=atof((LPCTSTR)m_EDIT);
if(arccostemp>=-1 && arccostemp<=1)
{
switch(degngra)
{
case 1:m_EDIT.Format("%f",acos(arccostemp)*180/PI);
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
case 2:m_EDIT.Format("%f",acos(arccostemp));
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
}
}
else
{
MessageBox("操作数范围应为-1到1!","ERROR!");
m_EDIT=_T("");
point=false;
SetDlgItemText(IDC_EDIT1,m_EDIT);
}

void CMy7139Dlg::OnBtnArctan()
{
// TODO: Add your control notification handler code here
double arctantemp;
arctantemp=atof((LPCTSTR)m_EDIT);
switch(degngra)
{
case 1:m_EDIT.Format("%f",atan(arctantemp)*180/PI);
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
case 2:m_EDIT.Format("%f",atan(arctantemp));
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
}

void CMy7139Dlg::OnBtnArccot()
{
// TODO: Add your control notification handler code here
double arccottemp;
arccottemp=atof((LPCTSTR)m_EDIT);
switch(degngra)
{
case 1:m_EDIT.Format("%f",1/atan(arccottemp)*180/PI);
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
case 2:m_EDIT.Format("%f",1/atan(arccottemp));
SetDlgItemText(IDC_EDIT1,m_EDIT);
break;
}

Log、ln:的实现
void CMy7139Dlg::OnBtnLog()
{
// TODO: Add your control notification handler code here
double logtemp;
int logt;
char logch[50];
switch(form)
{
case 'H': logt=_tcstoul(m_EDIT, 0, 16);
if(logt>0)
{
m_EDIT.Format("%X",(int)log10(logt));
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
else
{

MessageBox("操作数应大于零","ERROR!");
m_EDIT=_T("");
point=false;
UpdateData(false);
}
break;
case 'D': logtemp=atof((LPCTSTR)m_EDIT);
if(logtemp>0)
{
m_EDIT.Format("%f",log10(logtemp));
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
else
{

MessageBox("操作数应大于零","ERROR!");
m_EDIT=_T("");
point=false;
UpdateData(false);
}
break;
case 'O': logt=_tcstoul(m_EDIT, 0, 8);
if(logt>0)
{
m_EDIT.Format("%o",(int)log10(logt));
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
else
{

MessageBox("操作数应大于零","ERROR!");
m_EDIT=_T("");
point=false;
UpdateData(false);
}
break;
case 'B': logt=_tcstoul(m_EDIT, 0, 2);
if(logt>0)
{
itoa(log10(logt),logch,2);
m_EDIT.Format("%s",logch);
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
else
{

MessageBox("操作数应大于零","ERROR!");
m_EDIT=_T("");
point=false;
UpdateData(false);
}
break;
default: return;
}

void CMy7139Dlg::OnBtnLn()
{
// TODO: Add your control notification handler code here
double lntemp;
int lnt;
char lnch[50];
switch(form)
{
case 'H': lnt=_tcstoul(m_EDIT, 0

, 16);
if(lnt>0)
{
m_EDIT.Format("%X",(int)log(lnt));
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
else
{

MessageBox("操作数应大于零","ERROR!");
m_EDIT=_T("");
point=false;
UpdateData(false);
}
break;
case 'D': lntemp=atof((LPCTSTR)m_EDIT);
if(lntemp>0)
{
m_EDIT.Format("%f",log(lntemp));
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
else
{

MessageBox("操作数应大于零","ERROR!");
m_EDIT=_T("");
point=false;
UpdateData(false);
}
break;
case 'O': lnt=_tcstoul(m_EDIT, 0, 8);
if(lnt>0)
{
m_EDIT.Format("%o",(int)log(lnt));
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
else
{

MessageBox("操作数应大于零","ERROR!");
m_EDIT=_T("");
point=false;
UpdateData(false);
}
break;
case 'B': lnt=_tcstoul(m_EDIT, 0, 2);
if(lnt>0)
{
itoa(log(lnt),lnch,2);
m_EDIT.Format("%s",lnch);
SetDlgItemText(IDC_EDIT1,m_EDIT);
}
else
{

MessageBox("操作数应大于零","ERROR!");
m_EDIT=_T("");
point=false;
UpdateData(false);
}
break;
default: return;
}

相关文档
最新文档