2012-08-16 56 views
0

我需要幫助,試圖修復我的程序的第二部分,將十進制轉換爲二進制,這是我迄今爲止,當我編譯它我一直得到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; 
} 
+2

你給它分配一個值之前使用'r'。 – Musa 2012-08-16 00:19:56

+0

以及其餘的部分,但我不知道如何讓字符串在第一位除以2 ... – 2012-08-16 00:37:01

+0

@ 23ewt3tqa這不是剩下的,直到你將其分配,*在循環中。但是在那個時候,'while'已經測試了這個值。這是Musa試圖指出的錯誤。 – unwind 2012-08-16 08:38:22

回答

1

這裏有幾個問題。首先,你第一次檢查r,它是未初始化的。另一個問題是,你每次通過while循環時都將r和q設置爲相同的值。你可能想要q = q/2而不是q = q%2。最後,你會覆蓋循環中的每一遍,而不是構建一串比特。下面是一些僞代碼,你想要做什麼:

output_string = "" 

while input > 0: 
    output_string = concat(input%2, output_string) 
    input /= 2 

print output_string 

請注意,你也永遠不會在你閱讀的字符串轉換爲整數,並把在Q,所以你需要做的這一點。

1

最簡單的事情可能是將字符串輸入轉換爲適當的整數(例如使用strtol),並將該數字轉換爲僅包含1和0的字符串。

喜歡的東西:

/* Convert a (possibly signed) decimal number in a string to a long integer */ 
unsigned long number = (unsigned long) strtol(string, NULL, 10); 

char output_string[65]; /* If longs are 64 bits, plus one for terminator */ 
char *output_ptr = output_string; 

/* Start with the highest bit, go down to the lowest */ 
/* sizeof(long) is either 4 or 8 depending on 32 or 64 bit platforms */ 
/* Multiply with 8 to get the number of bits */ 
/* -1 because bits are numbered from 0 to 31 (or 63) */ 
for (int bit = (sizeof(unsigned long) * 8) - 1; bit >= 0; bit--) 
{ 
    /* Using right shift to get the current bit into the lowest position */ 
    /* Doing bitwise AND to see if the lowest bit is a one or a zero */ 
    /* Adding '0' makes a a printable ASCII value of a digit */ 
    *output_ptr++ = ((number >> bit) & 1) + '0'; 

    /* `*output_ptr` gets the value that `output_ptr` points to */ 
    /* Then use the `++` operator to increase the pointer */ 
    /* Now `output_ptr` points to the next character in `output_string` */ 
} 

/* Terminate string */ 
*output_ptr = '\0'; 

printf("%ld in binary is %s\n", number, output_string); 
1

,如果你想打印爲二進制數字與符號的字符串負數這C99代碼將這樣的伎倆:

if (a == 'D') 
{ 
    int r; 
    printf("enter decimal number to convert to binary: "); 
    scanf("%d", &r); 
    int i = 0; 
    int p = (r >= 0) ? (r = -r, 1) : 0; 
    string[i++] = '\0'; 

    do 
    { 
     string[i++] = (r % 2) == 0 ? '0' : '1'; 
     r /= 2; 
    } while (r != 0); 
    if (!p) 
     string[i++] = '-'; 

    int k = 0; 
    while (--i > k) 
    { 
     char t = string[i]; 
     string[i] = string[k]; 
     string[k++] = t; 
    } 

    printf("the binary number is: %s\n", string); 
} 

例如,給定-1234(十進制),輸出爲-10011010010(二進制)。它還可以處理兩個極端:INT_MAX-INT_MAXINT_MIN(假設32位int):

B = B to D 
D = D to B 
choose which one to convert to: D 
enter decimal number to convert to binary: 2147483647 
the binary number is: 1111111111111111111111111111111 

B = B to D 
D = D to B 
choose which one to convert to: D 
enter decimal number to convert to binary: -2147483647 
the binary number is: -1111111111111111111111111111111 

B = B to D 
D = D to B 
choose which one to convert to: D 
enter decimal number to convert to binary: -2147483648 
the binary number is: -10000000000000000000000000000000 

如果,另一方面,你要對應值的位模式,然後Joachim Pileborg的回答呢那對你。

(這是C99代碼,因爲它通過一個塊宣佈在方便的分部分的方式變量,而不是作爲C89需要一個塊的開始。)

相關問題