2016-07-14 77 views
-1

我正在爲一所學校的項目工作,所以即時通訊不尋找某人爲我做我的工作,但我無法弄清楚這一點。我需要編寫一個程序,將基數爲10的數字轉換爲二進制和十六進制。我無法弄清楚爲什麼我的代碼不打印出二進制數字。結果總是打印0001並且不採取整個輸出。我不好解釋這是誠實的,但繼承人的代碼。不正確打印二進制

#include<stdio.h> 
#define MAX 1000 

int main() 
{ 
    long int baseTenNum,remainder,hexQuotient; 
    int i=1,j,temp; 
    char hexNum[MAX], binaryNum[MAX]; 

    printf("Enter any Base10 number: "); 
    scanf("%ld",&baseTenNum); 

    // quotient variable to convert to hexidecimal value 
    hexQuotient = baseTenNum; 

    // while loop to get hexidecimal value 
    while(hexQuotient!=0) 
    { 
     temp = hexQuotient % 16; 

     // Converts integer to character 
     if(temp < 10) 
      temp =temp + 48; 
     else 
     temp = temp + 55; 

     hexNum[i++]= temp; 
     hexQuotient = hexQuotient/16; 
    } 



    printf("\nhexadecimal value of base 10 number %d: ",baseTenNum); 
    for(j = i -1 ;j> 0;j--) 
     printf("%c",hexNum[j]); 
     printf("\n\n"); 

     if (hexNum[j] = 0){ 
      printf("0000"); 
     } 
     else if(hexNum[j] = 1){ 
      printf("0001"); 
      } 
     else if(hexNum[j] = 2){ 
      printf("0010"); 
     } 
     else if(hexNum[j] = 3) 
     { 
      printf("0011"); 
     } 
     else if(hexNum[j] = 4){ 
      printf("0100"); 
     } 
     else if(hexNum[j] = 5){ 
      printf("0101"); 
     } 
     else if(hexNum[j] = 6){ 
      printf("0110"); 
     } 
     else if(hexNum[j] = 7){ 
      printf("0111"); 
     } 
     else if(hexNum[j] = 8){ 
      printf("1000"); 
     } 
     else if(hexNum[j] = 9){ 
      printf("1001"); 
     } 
     else if(hexNum[j] = "A"){ 
      printf("1010"); 
     } 
     else if(hexNum[j] = "B"){ 
      printf("1011"); 
     } 
     else if(hexNum[j] = "C"){ 
      printf(1100); 
     } 
     else if(hexNum[j] = "D"){ 
      printf("1101"); 
     } 
     else if(hexNum[j] = "E"){ 
      printf("1110"); 
     } 
     else if(hexNum[j] = "F"){ 
      printf("1111"); 
     } 
     printf("\n%c",hexNum[2]); 


    return 0; 


} 
+0

當心:1和 '1' 和 「1」 都非常* *不同 –

+0

1是整數1時, '1' 是A *的char * ASCII值不是數字1,和「1」是一個*指針*到一個常量字符數組,這將不能很好地與比較 –

回答

3

您應該使用if (hexNum[j] == '0'){代替if (hexNum[j] = 0){。有這麼多的線條。在你的程序中,你使用=來製作hexNum[j] value to 0。要比較您需要使用==

在這種情況下,您應該在所有比較中使用char符號(如'0'而不是0或「0」)。 0,'0'和'0'是不同的。 0是一個整數,'0'是一個字符,「0」是一個字符串。

這裏是工作代碼。

#include<stdio.h> 
#define MAX 1000 

int main() 
{ 
    long int baseTenNum,remainder,hexQuotient; 
    int i=0,j,temp; 
    char hexNum[MAX], binaryNum[MAX]; 

    printf("Enter any Base10 number: "); 
    scanf("%ld",&baseTenNum); 

    // quotient variable to convert to hexidecimal value 
    hexQuotient = baseTenNum; 
    temp = baseTenNum; 

    // while loop to get hexidecimal value 
    while(hexQuotient != 0) 
    { 
     temp = hexQuotient % 16; 
     // Converts integer to character 
     if(temp <= 10) 
      temp = temp + 48; 
     else 
      temp = temp + 55; 

     hexNum[i++]= temp; 
     hexQuotient = hexQuotient/16; 
    } 


    printf("\nhexadecimal value of base 10 number %ld: ",baseTenNum); 
    for(j = i-1 ;j >= 0;j--) 

     if (hexNum[j] == '0'){ 
      printf("0000"); 
     } 
     else if(hexNum[j] == '1'){ 
      printf("0001"); 
     } 
     else if(hexNum[j] == '2'){ 
      printf("0010"); 
     } 
     else if(hexNum[j] == '3') 
     { 
      printf("0011"); 
     } 
     else if(hexNum[j] == '4'){ 
      printf("0100"); 
     } 
     else if(hexNum[j] == '5'){ 
      printf("0101"); 
     } 
     else if(hexNum[j] == '6'){ 
      printf("0110"); 
     } 
     else if(hexNum[j] == '7'){ 
      printf("0111"); 
     } 
     else if(hexNum[j] == '8'){ 
      printf("1000"); 
     } 
     else if(hexNum[j] == '9'){ 
      printf("1001"); 
     } 
     else if(hexNum[j] == 'A'){ 
      printf("1010"); 
     } 
     else if(hexNum[j] == 'B'){ 
      printf("1011"); 
     } 
     else if(hexNum[j] == 'C'){ 
      printf("1100"); 
     } 
     else if(hexNum[j] == 'D'){ 
      printf("1101"); 
     } 
     else if(hexNum[j] == 'E'){ 
      printf("1110"); 
     } 
     else if(hexNum[j] == 'F'){ 
      printf("1111"); 
     } 

    puts(""); 
    return 0; 
} 
+0

所以我這樣做,改變代碼的所有必要的部分。我甚至在底部添加了一個打印語句,打印出hexNum [2]的值來測試以確保我獲得了合適的字符。每當我運行該程序,並打輸入字符串與二進制數字不打印出來。因此,讓我們說當hexNum [2] == 6出現時,字符串0110應顯示在終端中。但是,當char值匹配 –

+0

時,if else塊中的任何字符串都不會打印。當我在for循環中註釋printf(「%c」,hexNum [j])和printf(「\ n \ n」)行時(第35行和第36行,我相信)代碼被執行到二進制數字所在的位置現在我不明白爲什麼我不能理解爲什麼十六進制值和二進制值都沒有打印,它只會顯示一個或另一個,並讓它顯示二進制字符串,我必須排除行打印十六進制值的代碼 –

+0

想象出來,我需要在括號之間放置for循環的內容,這樣整個代碼纔會被執行。 –