![](images/smilies/emot/em11.gif)
![](images/smilies/emot/em11.gif)
这个是我仿照做的,你最好把其中一些东西修改一下,主要代码如下,这些代码都是在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);
}
你需要的地方你修改一下吧。
界面如下图:
我试一下,看整个文件能传上来吗?
[URL=E:\我的文件\c++源文件\练习\caculator2.rar]E:\我的文件\c++源文件\练习\caculator2.rar[/URL]c:\documents and settings\wei\my documents\c++\math\math.cpp(6) : error C2065: 'FALSE' : undeclared identifier
c:\documents and settings\wei\my documents\c++\math\math.cpp(6) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
c:\documents and settings\wei\my documents\c++\math\math.cpp(10) : error C2143: syntax error : missing ';' before '.'
c:\documents and settings\wei\my documents\c++\math\math.cpp(10) : error C2501: 'm_Operator' : missing storage-class or type specifiers
c:\documents and settings\wei\my documents\c++\math\math.cpp(10) : error C2143: syntax error : missing ';' before '.'
c:\documents and settings\wei\my documents\c++\math\math.cpp(11) : error C2143: syntax error : missing ';' before '.'
c:\documents and settings\wei\my documents\c++\math\math.cpp(11) : error C2501: 'm_Operator' : missing storage-class or type specifiers
c:\documents and settings\wei\my documents\c++\math\math.cpp(11) : error C2086: 'm_Operator' : redefinition
c:\documents and settings\wei\my documents\c++\math\math.cpp(11) : error C2143: syntax error : missing ';' before '.'
c:\documents and settings\wei\my documents\c++\math\math.cpp(12) : error C2143: syntax error : missing ';' before '.'
c:\documents and settings\wei\my documents\c++\math\math.cpp(12) : error C2501: 'm_Operator' : missing storage-class or type specifiers
c:\documents and settings\wei\my documents\c++\math\math.cpp(12) : error C2086: 'm_Operator' : redefinition
c:\documents and settings\wei\my documents\c++\math\math.cpp(12) : error C2143: syntax error : missing ';' before '.'
c:\documents and settings\wei\my documents\c++\math\math.cpp(13) : error C2143: syntax error : missing ';' before '.'
c:\documents and settings\wei\my documents\c++\math\math.cpp(13) : error C2501: 'm_Operator' : missing storage-class or type specifiers
c:\documents and settings\wei\my documents\c++\math\math.cpp(13) : error C2086: 'm_Operator' : redefinition
c:\documents and settings\wei\my documents\c++\math\math.cpp(13) : error C2143: syntax error : missing ';' before '.'
c:\documents and settings\wei\my documents\c++\math\math.cpp(15) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(26) : error C2065: '_gcvt' : undeclared identifier
c:\documents and settings\wei\my documents\c++\math\math.cpp(27) : error C2065: 'm_EditResult' : undeclared identifier
c:\documents and settings\wei\my documents\c++\math\math.cpp(27) : error C2065: 'LPCTSTR' : undeclared identifier
c:\documents and settings\wei\my documents\c++\math\math.cpp(27) : error C2146: syntax error : missing ';' before identifier 'buffer'
c:\documents and settings\wei\my documents\c++\math\math.cpp(28) : error C2065: 'UpdateData' : undeclared identifier
c:\documents and settings\wei\my documents\c++\math\math.cpp(31) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(46) : error C2146: syntax error : missing ';' before identifier 'buffer'
c:\documents and settings\wei\my documents\c++\math\math.cpp(50) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(62) : error C2146: syntax error : missing ';' before identifier 'buffer'
c:\documents and settings\wei\my documents\c++\math\math.cpp(67) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(79) : error C2146: syntax error : missing ';' before identifier 'buffer'
c:\documents and settings\wei\my documents\c++\math\math.cpp(84) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(96) : error C2146: syntax error : missing ';' before identifier 'buffer'
c:\documents and settings\wei\my documents\c++\math\math.cpp(101) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(113) : error C2146: syntax error : missing ';' before identifier 'buffer'
c:\documents and settings\wei\my documents\c++\math\math.cpp(118) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(130) : error C2146: syntax error : missing ';' before identifier 'buffer'
c:\documents and settings\wei\my documents\c++\math\math.cpp(135) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(147) : error C2146: syntax error : missing ';' before identifier 'buffer'
c:\documents and settings\wei\my documents\c++\math\math.cpp(151) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(163) : error C2146: syntax error : missing ';' before identifier 'buffer'
c:\documents and settings\wei\my documents\c++\math\math.cpp(168) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(180) : error C2146: syntax error : missing ';' before identifier 'buffer'
c:\documents and settings\wei\my documents\c++\math\math.cpp(185) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(188) : error C2065: 'TRUE' : undeclared identifier
c:\documents and settings\wei\my documents\c++\math\math.cpp(188) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
c:\documents and settings\wei\my documents\c++\math\math.cpp(192) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(196) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
c:\documents and settings\wei\my documents\c++\math\math.cpp(200) : error C2228: left of '.GetCurSel' must have class/struct/union type
c:\documents and settings\wei\my documents\c++\math\math.cpp(204) : error C2653: 'CCaculator2Dlg' : is not a class or namespace name
c:\documents and settings\wei\my documents\c++\math\math.cpp(209) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
c:\documents and settings\wei\my documents\c++\math\math.cpp(229) : error C2146: syntax error : missing ';' before identifier 'buffer'
执行 cl.exe 时出错.
math.exe - 1 error(s), 0 warning(s)