C语言:编程实现二进制数转换成十进制数

如题所述

#include <stdio.h>
int main()
{
int a=0,b=0,c=0;
printf("请输入一个二进制数:");
scanf("%d",&a);//这里我就不对输入进行检查了
for(int i=0;;i++)
{
if(a==0)
break;
b=a%2;
a=a/10;
for(int j=i;j>0;j--)
{
if(b==0)
break;
else
b=b*2;
}
c+=b;

}
printf("%d",c);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-06-02
#include<iostream>
#include<cstring>
using namespace std;
void main()
{
const int max=10;
char ch[max];
cin>>ch;
int l,i,s;
l=strlen(ch);
s=0;
for (i=0;i<l;i++)
{
s*=2;
s+=ch[i]-'0';
}
cout<<s<<endl;
}
第2个回答  2020-12-28

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网