【发布时间】:2019-06-27 08:02:02
【问题描述】:
我正在编写一个代码来显示结果是显示单个数字和十进制等效值。 例如,如果 n 为 6,输入的数字为 110011,则打印输出为 1 1 0 0 1 1 十进制等值是 51
我已经获取并编辑了一个代码,但是它显示 “110011 110011 110011 110011 110011 110011”代替 “1 1 0 0 1 1”。
#include <math.h>
void main()
{
int num, binary, decimal = 0, base = 1, remainder, n, digits;
printf("Please enter the number of digits:\n");
scanf("%d", &n);
printf("Please enter the %d digits:\n",n);
scanf("%d", &num);
binary = num;
while (n > 0)
{
remainder = num % 10;
decimal = decimal + remainder * base;
num = num / 10 ;
base = base * 2;
printf("%d ", binary);
n--;
}
printf("\nIts decimal equivalent is = %d \n", decimal);
}
【问题讨论】:
-
提前谢谢,如果它是一个糟糕的 qns,请原谅!
-
我看到
binary = num;和printf("%d ", binary);之间的确切位置您希望binary发生变化吗? -
除非您在嵌入式系统上的不符合标准的独立环境中进行编码,否则
void main()是不正确的。请参阅 C11 Standard - §5.1.2.1 Freestanding environment 至 C11 Standard - §5.1.2.2.1 Program startup。