| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 649 人关注过本帖
标题:2进制和10进制的转换?
只看楼主 加入收藏
兜兜里有枪
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-4-22
收藏
 问题点数:0 回复次数:4 
2进制和10进制的转换?

#include <iostream>
using namespace std;
double power(double x,int n);

int main()
{
int i;
int value;
char ch;

cout<<"enter an 8 bit binary number";
for(i=7;i>=0;i--)
{
cin>>ch;
if(ch=='1')
value+=int(power(2,i));
}
cout<<"the total is"<<value<<endl;
}
double power(double x,int n)
{
double val=1.0;
while(n--)
val*=x;
return(val);


}假如输入01101001,运行结果真么和预期的比一样呢?

搜索更多相关主题的帖子: 进制 
2007-04-22 10:10
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
value没有初始化

[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-04-22 10:25
边城路人
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:9
帖 子:591
专家分:0
注 册:2007-3-8
收藏
得分:0 
for(i=7;i>=0;i--)
{
cin>>ch;
if(ch=='1')
value+=int(power(2,i));
}
把它换个位置试试

当你开始怀疑自己时,说明你已经失去了判断力
2007-04-22 11:01
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

#include <iostream>
using namespace std;
double power(double x,int n);

void main()
{
int i;
int value=0;
char ch;

cout<<"enter an 8 bit binary number:\n";
for(i=7;i>=0;i--)
{
cin>>ch;
if(ch=='1')
value+=power(2,i);

}
cout<<"the total is"<<value<<endl;
}
double power(double x,int n)
{
int val=1;
while(n--)
val*=x;
return val;


}
这样可以了


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-04-22 11:55
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 
int(power(2,i));
在进行double 向 int的强制转换很可能出现问题,最好开始就定义为int的类型,在二进制与十进制转换的时候不需要 double型的

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-04-22 12:00
快速回复:2进制和10进制的转换?
数据加载中...
 
   



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

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