这个是我仿照做的,你最好把其中一些东西修改一下,主要代码如下,这些代码都是在Dlg。cpp文件中:
int math_i;
double count=10;
char buffer[10];
double input_int=0,input_dec=0;
double input1=0,input2=0;
bool decimal=FALSE;
double value=0;
以上为全局变量:
在OnInitDialog()函数中添加如下代码:
m_Operator.AddString("加");
m_Operator.AddString("减");
m_Operator.AddString("乘");
m_Operator.AddString("除");
其他函数代码如下:
void CCaculator2Dlg::On1Button()
{
// TODO: Add your control notification handler code here
if(decimal)
{
input_dec=input_dec+1/count;
count=count*10;
}
else input_int=input_int*10+1;
input1=input_int+input_dec;
value=input1;
_gcvt(value,15,buffer);
m_EditResult=(LPCTSTR)buffer;
UpdateData(FALSE);
}
void CCaculator2Dlg::On0Button()
{
// TODO: Add your control notification handler code here
if(decimal)
{
input_dec=input_dec+0/count;
count=count*10;
}
else
{
input_int=input_int*10+0;
}
input1=input_int+input_dec;
value=input1;
_gcvt(value,15,buffer);
m_EditResult=(LPCTSTR)buffer;
UpdateData(FALSE);
}
void CCaculator2Dlg::On2Button()
{
// TODO: Add your control notification handler code here
if(decimal)
{
input_dec=input_dec+2/count;
count=count*10;
}
else input_int=input_int*10+2;
input1=input_int+input_dec;
value=input1;
_gcvt(value,15,buffer);
m_EditResult=(LPCTSTR)buffer;
UpdateData(FALSE);
}
void CCaculator2Dlg::On3Button()
{
// TODO: Add your control notification handler code here
if(decimal)
{
input_dec=input_dec+3/count;
count=count*10;
}
else input_int=input_int*10+3;
input1=input_int+input_dec;
value=input1;
_gcvt(value,15,buffer);
m_EditResult=(LPCTSTR)buffer;
UpdateData(FALSE);
}
void CCaculator2Dlg::On4Button()
{
// TODO: Add your control notification handler code here
if(decimal)
{
input_dec=input_dec+4/count;
count=count*10;
}
else input_int=input_int*10+4;
input1=input_int+input_dec;
value=input1;
_gcvt(value,15,buffer);
m_EditResult=(LPCTSTR)buffer;
UpdateData(FALSE);
}
void CCaculator2Dlg::On5Button()
{
// TODO: Add your control notification handler code here
if(decimal)
{
input_dec=input_dec+5/count;
count=count*10;
}
else input_int=input_int*10+5;
input1=input_int+input_dec;
value=input1;
_gcvt(value,15,buffer);
m_EditResult=(LPCTSTR)buffer;
UpdateData(FALSE);
}
void CCaculator2Dlg::On6Button()
{
// TODO: Add your control notification handler code here
if(decimal)
{
input_dec=input_dec+6/count;
count=count*10;
}
else input_int=input_int*10+6;
input1=input_int+input_dec;
value=input1;
_gcvt(value,15,buffer);
m_EditResult=(LPCTSTR)buffer;
UpdateData(FALSE);
}
void CCaculator2Dlg::On7Button()
{
// TODO: Add your control notification handler code here
if(decimal)
{
input_dec=input_dec+7/count;
count=count*10;
}
else input_int=input_int*10+7;
input1=input_int+input_dec;
value=input1;
_gcvt(value,15,buffer);
m_EditResult=(LPCTSTR)buffer;
UpdateData(FALSE);
}
void CCaculator2Dlg::On8Button()
{
// TODO: Add your control notification handler code here
if(decimal)
{
input_dec=input_dec+8/count;
count=count*10;
}
else input_int=input_int*10+8;
input1=input_int+input_dec;
value=input1;
_gcvt(value,15,buffer);
m_EditResult=(LPCTSTR)buffer;
UpdateData(FALSE);
}
void CCaculator2Dlg::On9Button()
{
// TODO: Add your control notification handler code here
if(decimal)
{
input_dec=input_dec+9/count;
count=count*10;
}
else input_int=input_int*10+9;
input1=input_int+input_dec;
value=input1;
_gcvt(value,15,buffer);
m_EditResult=(LPCTSTR)buffer;
UpdateData(FALSE);
}
void CCaculator2Dlg::OnDecimalButton()
{
// TODO: Add your control notification handler code here
decimal=TRUE;
}
void CCaculator2Dlg::OnSelchangeOperatorList()
{
// TODO: Add your control notification handler code here
//input1=input_int+input_dec;
decimal=FALSE;
input_int=0;
input_dec=0;
count=10;
math_i=m_Operator.GetCurSel();
}
void CCaculator2Dlg::OnEqualButton()
{
// TODO: Add your control notification handler code here
//UpdateData(TRUE);
input2=input_int+input_dec;
decimal=FALSE;
input_int=0;
input_dec=0;
count=10;
switch(math_i)
{
case 0:
value=input1+input2;
break;
case 1:
value=input1-input2;
break;
case 2:
value=input1*input2;
break;
case 3:
value=input1/input2;
break;
}
_gcvt(value,15,buffer);
m_EditResult=(LPCTSTR)buffer;
UpdateData(FALSE);
}
你需要的地方你修改一下吧。
界面如下图: