【发布时间】:2012-08-12 07:47:09
【问题描述】:
我需要帮助来修复程序的第二部分,将十进制转换为二进制,这是我目前所拥有的,当我编译它时,我一直得到 0,所以我不确定我做错了什么。请问有什么帮助吗?
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
char string[100];
int s;
char a;
char j;
int sum = 0;
int r;
int q;
printf("B = B to D\n");
printf("D = D to B\n");
printf("choose which one to convert to:");
scanf("%c%c", &a, &j);
if (a == 'B')
{
printf("enter binary number to convert to decimal: ");
scanf("%s", string);
for(s = strlen(string)-1; s >= 0; s--)
{
if(string[s] == '1')
{
sum = sum + pow(2, strlen(string) - (s +1));
}
}
printf("the decimal number is: %d\n", sum);
}
if (a == 'D')
{
printf("enter decimal number to convert to binary: ");
scanf("%s", string);
while (r > 0)
{
r = q%2;
q = q%2;
}
printf("the binary number is: %d\n", r);
}
return 0;
}
【问题讨论】:
-
你在给它赋值之前使用
r。 -
还有余数,但我不确定如何首先将字符串除以 2...
-
@23ewt3tqa 在你分配它之前,它不是余数,inside循环。但此时,
while已经测试了该值。这是 Musa 试图指出的错误。