用c语言编:从键盘输入一个二进制,将其转化成等值的十进制

2024-11-30 15:38:06
推荐回答(2个)
回答1:

void main()
{int a,b,c,d[100],i=0,k=0,m,n=0;
scanf("shu,jinzhi/n",&a,&b);
while (a!=0)
{c=a%b;
a=a/b;
push(st,c);
i=i+1;
}
for (k=0;k<=i;k++)
{d[k]=ptop(st);
d[k]=d[k]*10^k;
}
for (k=0;k<=i;k++)
{n=n+d[k];
}
printf("jieguo\n",&n);

}

回答2:

//---------------------------------------------------------------------------

#include
#include
#include

long int btd(char *s)
{
int i,len=strlen(s)-1;
long int d=0;
for (i = len; i>=0; i--) {
d+=s[i]=='1'?(long int)pow(2,len-i):0;
}
return d;
}
int main(int argc, char* argv[])
{
long int b;
char s[20];
scanf("%[1,0]",s);
b=btd(s);
printf("%s(B)=%ld(D)\n",s,b);
return 0;
}
//---------------------------------------------------------------------------