【发布时间】:2015-12-18 07:38:27
【问题描述】:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
char *bin=malloc(sizeof(char)*100);
int i=0,p=0,dec=0,g;
printf("\nenter the binary number\n");
gets(bin);
i=strlen(bin)-1;
while(*(bin+p)!=NULL){
if(*(bin+p)=='1'){
g=pow(2,(i-p));
dec=dec+g;
}
p++;
}
printf("\n%d",dec);
return 0;
}
以上程序应将任何位二进制数转换为十进制数。
示例输入:
10011
预期输出:
19
实际输出:
1
【问题讨论】:
-
gets在 4 年前从 C 语言中删除,在 16 年前被贬低,可能在 30 年前被认为是不好的做法。 -
请格式化您的代码。
-
@MichaelWalz 请再次检查我已经编辑了一些部分...
-
@Lundin 但它仍然适用于我的编译器...我已经检查过
-
@AkshayMilmile 不,它不需要文件名,它需要像
stdin这样的流。 LMRTFMFY.
标签: c pointers binary output decimal