| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4793 人关注过本帖, 1 人收藏
标题:用C++做一个计算器
只看楼主 加入收藏
sjy851203
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-1-2
收藏(1)
 问题点数:0 回复次数:16 
用C++做一个计算器
各位高手大家帮帮我的忙呀,我是刚学C++的,学得不怎么好,但是老师又要我们交一篇课程设计,是用C++做计算器,我做不出来,请大家帮帮我的忙呀,我们这周周五就要交了呀,先谢谢大家了呀!
搜索更多相关主题的帖子: 计算器 
2007-01-02 13:42
cz522321
Rank: 1
等 级:禁止访问
威 望:2
帖 子:569
专家分:5
注 册:2006-3-13
收藏
得分:0 

这个是我仿照做的,你最好把其中一些东西修改一下,主要代码如下,这些代码都是在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);
}
你需要的地方你修改一下吧。
界面如下图:

[IMG]D:\安装程序-别动\RdfSnap2005\屏幕截图\caculator.bmp[/IMG]

2007-01-02 20:38
cz522321
Rank: 1
等 级:禁止访问
威 望:2
帖 子:569
专家分:5
注 册:2006-3-13
收藏
得分:0 

我试一下,看整个文件能传上来吗?

[URL=E:\我的文件\c++源文件\练习\caculator2.rar]E:\我的文件\c++源文件\练习\caculator2.rar[/URL]

2007-01-03 11:42
cz522321
Rank: 1
等 级:禁止访问
威 望:2
帖 子:569
专家分:5
注 册:2006-3-13
收藏
得分:0 

我再试一下,看整个文件能传上来吗?

[URL=caculator2.rar]caculator2.rar[/URL]

2007-01-03 11:59
cz522321
Rank: 1
等 级:禁止访问
威 望:2
帖 子:569
专家分:5
注 册:2006-3-13
收藏
得分:0 

呵呵,传不上来.


2007-01-03 12:05
清澂居士
Rank: 6Rank: 6
等 级:贵宾
威 望:28
帖 子:1237
专家分:7
注 册:2006-12-19
收藏
得分:0 
````樓上在搞什麼?````點上靣的囬復主題```就能上傳叻```

佛曰:\"前世的500次回眸才换来今生的一次擦肩而过\".我宁愿用来世的一次擦肩而过来换得今生的500次回眸.
2007-01-04 00:23
cz522321
Rank: 1
等 级:禁止访问
威 望:2
帖 子:569
专家分:5
注 册:2006-3-13
收藏
得分:0 
我已经把代码复制到上面了,我试一下,能不能把整个文件用压缩包的形式传上来,但不行呀!

2007-01-04 12:12
清澂居士
Rank: 6Rank: 6
等 级:贵宾
威 望:28
帖 子:1237
专家分:7
注 册:2006-12-19
收藏
得分:0 
圖片上的囬復主題,就可以像髮帖子一樣加附件叻````

图片附件: 游客没有浏览图片的权限,请 登录注册


佛曰:\"前世的500次回眸才换来今生的一次擦肩而过\".我宁愿用来世的一次擦肩而过来换得今生的500次回眸.
2007-01-04 14:36
清澂居士
Rank: 6Rank: 6
等 级:贵宾
威 望:28
帖 子:1237
专家分:7
注 册:2006-12-19
收藏
得分:0 

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)


佛曰:\"前世的500次回眸才换来今生的一次擦肩而过\".我宁愿用来世的一次擦肩而过来换得今生的500次回眸.
2007-01-04 15:39
孙明然
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-4-11
收藏
得分:0 
学C++的人都应该看看这本书
The C++ Programming Language Special 3rd Edition

2007-01-05 21:13
快速回复:用C++做一个计算器
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013456 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved