我需要幫助,試圖修復我的程序的第二部分,將十進制轉換爲二進制,這是我迄今爲止,當我編譯它我一直得到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'。 – Musa 2012-08-16 00:19:56
以及其餘的部分,但我不知道如何讓字符串在第一位除以2 ... – 2012-08-16 00:37:01
@ 23ewt3tqa這不是剩下的,直到你將其分配,*在循環中。但是在那個時候,'while'已經測試了這個值。這是Musa試圖指出的錯誤。 – unwind 2012-08-16 08:38:22