2016-10-26 9 views
-1

私はバイナリ変換プログラムをc言語で記述するのに助けが必要です。私は配列のサイズを増やす方法が必要です。これは2進数のビット数に応じて、1と0を別の配列に入れ、最初の配列に数値を加えて10進数で変換します。私は最初のプログラミング言語としてCプログラミングを学んでいます。ここ は、私がこれまでバイナリを10進数に変換するプログラム

#include <stdio.h> 

int main (void) 
{ 
    printf("Please input binary number to convert to decimal."); 
    Scanf("%d"); 
    for(i=2;i<= /*number of integers in binary number*/;i++) 
    { 
     int i 
     char conversiontable [2][i] ={ 
      /*array1*/{'1','2',}, 
      /*array2*/{} 
     }; 
     /*inputs 110001101*/ 
     /*increase size of array1 according to number of 1's and 0's in the binary number*/ 
     /*fill empty slots in array 1 with the output of (2^2)2 starting in slot 3 and add 1 to the number outside the parenthesis for each slot*/ 
     /*put binary number in array 2*/ 
     /*add together the numbers in array 1 in correspondence to wherever there is a 1 in array 2*/ 

     /*example*/ 
     /*1,2,4,8,16,32,64,128,256*/ 
     /*1,1,0,0, 0, 1, 1, 0, 1*/ 

     /*1+2+32+64+256=355*/ 
     /*outputs 355 for answer*/ 
    } 
    return 0; 
} 

答えて

0

持っているものだ私はあなたがchar型の配列の2進数を格納し、すべてがより簡単になり、それを分析するかもしれない場合は、あなたのアプローチが最善ではないと思います;)。

私があなただったら、私はこのような何か:

#include <stdio.h> 
#include <math.h> 

int main() 
{ 
    char binary [20]; 
    printf ("Input the binary number: "); 
    scanf ("%s", binary); 

    int length = 0; 
    while (binary [length] != '\0') 
     ++length; 

    int decimal = 0; 
    for (int i = length - 1; i > -1; --i) 
    { 
     if (binary [i] == '1') 
      decimal += pow (2, length - i - 1); 
    } 

    printf ("To decimal: %d\n", decimal); 

    return 0; 
} 
関連する問題