2016-09-30 3 views
-1

Ich arbeite an einem Projekt, wo ich eine binäre Zahl als eine Variable habe, die ich versuche, in ein Array zu machen, so dass ich durch den Index gehen und auf 0 oder 1 testen kann habe versucht, andere Fragen hier nachzuschlagen, kann aber nicht genau das finden, wonach ich suche. Im Moment habe ich das Array vordefiniert, damit ich überprüfen kann, ob meine if-Anweisungen ordnungsgemäß funktionieren und sie tun. Die Dezimalzahl, die durch den Parameter eingegeben wird, wird an die Funktion decToBin gesendet, um in eine binäre Zahl umgewandelt zu werden, und gibt die Binärzahl zurück. Ich möchte Folgendes:Eine Variable in ein Array in C einfügen

binA[size] = decToBin(decimal); 

Die Größenvariable ist die Größe der Binärzahl.

void relation (int decimal) 
{ 
    int size = ((floor(log2(decimal)))+ 1); 

    //char binA[size]; 
    int binA[] = {1,1,0,0,1,0,0}; 
    if (size > 1) 
    { 
      for(int i = 1; i < (size - 1) ; i++) 
      { 
        if (binA[i] == 0) 
          printf("Father's "); 
        if (binA[i] == 1) 
          printf("Mother's "); 
      }//end for 

      for(int i = (size -1); i < size; i++) 
      { 
        if (binA[i] == 0) 
          printf("Father "); 
        if (binA[i] == 1) 
          printf("Mother "); 
      }//end for 
    }//end if 
    else 
      printf("Self"); 
    printf("\n"); 
}  //end relation 

Kommentare: Die binäre Zahl ist vom Typ int. Der Grund, warum ich die zweite Schleife mache, ist so, dass das letzte Wort nicht "s" hat. Hier ist ein Beispiel für eine Ausgabe mit der Binärzahl 1010: Vaters Vater. Selbst wird gedruckt, wenn die binäre Zahl nur eine Größe von 1 Hier ist der Code für meine decToBin Funktion

int decToBin (int decimal) 
{ 
    int rem = 0; //sets remainder to 0 
    int binary = 0; //sets answer to 0 
    int x = 1;  //sets x to 1 

    while(decimal != 0) 
    { 
      rem = decimal % 2;  //lets remainder = whatever is left after diving by 2 
      decimal = decimal/2; //lets input = input divided by 2 
      binary = binary + (rem * x); //answer = answer + the remainder times x 
      x = x * 10;      //x now = itself times 10 for the next loop 
    }  //end while 

    //printf("Ahnentafel number in binary: %d\n", binary); 
    return binary; 
}  //end decToBin 
+0

Was ist "Binärzahl"? Welche Art von Variable ist das? –

+0

In 'for (int i = 1; i <(Größe - 1); i ++)' Warum beginnen Sie mit dem Array-Index '1' und enden mit einer Größe, die nicht mit der Array-Größe zusammenhängt? –

+0

Auf einem ** binären ** Digitalrechner ist jede Variable eine "Binärvariable". Sie können Variablen nicht irgendwo platzieren. Sie können den Wert jedoch einer anderen Variablen zuweisen. – Olaf

Antwort

-1

quick and dirty Lösung

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

void relation (int decimal) 
{ 
    int size = ((floor(log2(decimal)))); 
    int c, d; 

    if (size > 1) 
    { 
      for (c = size ; c >= 0 ; c--) 
      { 
       d = decimal >> c; 

       if (d & 1) 
       printf("Mother's "); 
       else 
       printf("Father's "); 
      } 

      printf("\n"); 

      for (c = size ; c >= 0 ; c--) 
      { 
       d = decimal >> c; 

       if (d & 1) 
       printf("Mother "); 
       else 
       printf("Father "); 
      } 
    }//end if 
    else 
     printf("Self"); 

    printf("\n"); 
}  //end relation 

main() 
{ 
    relation(77); 
    printf("\n\n"); 
    relation(0); 

    return 0; 
} 
0

Ihre Spezifikationen ein wenig unklar, aber wenn ich nehme, was Sie haben und meine frisch polierten Kristallkugel verwenden drauf bekomme ich folgende:

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

// ALL CHECKS OMMITTED! 

void relation(int decimal) 
{ 
    // wee need to feed log() a positive number 
    // log(-x) = log(x) + pi*I ; |x| >= 1 and principal branch of log 
    int size = ((floor(log2(abs(decimal)))) + 1); 
    printf("size = %d\n", size); 
    if (size > 1) { 
    for (int i = 1; i < (size - 1); i++) { 
     // please do yourself a favor and always use braces 
     // even if superfluent 
     printf("\nFIRST i = %d, bit = %d\n",i,(decimal>>i)&0x1); 
     // the shift is always defined because 0 < i < (sizeof(int)*CHAR_BIT) 
     if (((decimal>>i)&0x1) == 0) { 
     printf("Father's "); 
     } 
     if (((decimal>>i)&0x1) == 1) { 
     printf("Mother's "); 
     } 
    } 
    puts(""); 
    // This loop runs only one time, is that correct? 
    for (int i = (size - 1); i < size; i++) { 
     printf("\nSECOND i = %d, bit = %d\n",i,(decimal>>i)&0x1); 
     if (((decimal>>i)&0x1) == 0) { 
     printf("Father "); 
     } 
     if (((decimal>>i)&0x1) == 0) { 
     printf("Mother "); 
     } 
    } 
    } 
    else{ 
    printf("Self"); 
    } 
    printf("\n"); 
} 

int main(int argc, char **argv) 
{ 
    int dec; 
    if (argc != 2) { 
    fprintf(stderr, "Usage: %s integer\n", argv[0]); 
    exit(EXIT_FAILURE); 
    } 
    // TODO; use strtol() instead and check all errors 
    dec = atoi(argv[1]); 
    relation(dec); 
    exit(EXIT_SUCCESS); 
} 

Wenn es nicht der Fall ist, na ja ... verwenden, um die comme Abschnitt unten.

0

Lassen Sie uns versuchen, es den Weg zu lösen, die Sie durch die Definition decToBin() vorgestellt:

void decToBin(unsigned decimal, size_t size, unsigned char *array) 
{ 
    for (unsigned i = 0, bit = 1 << (size - 1); i < size; i++, bit >>= 1) 
    { 
     array[i] = (decimal & bit) ? 1 : 0; 
    } 
} 

Es die ganze Zahl ohne Vorzeichen nimmt die Anzahl von Bits zu entschlüsseln, zu entschlüsseln, und ein Array mit Nullen und Einsen zu füllen:

unsigned size = floor(log2(decimal)) + 1; 

unsigned char binA[size]; 

decToBin(decimal, size, binA); 

Sie könnten eine Fehlerprüfung in decToBin() hinzufügen, um sicherzustellen, size größer als Null ist. Wir können auch die relation() Funktion ein wenig vereinfachen:

void relation (unsigned decimal) 
{ 
    unsigned size = floor(log2(decimal)) + 1; 

    unsigned char binA[size]; 

    decToBin(decimal, size, binA); 

    if (size > 1) 
    { 
     for (int i = 1; i < (size - 1); i++) 
     { 
      printf((binA[i] == 0) ? "Father's " : "Mother's "); 
     } // end for 

     printf((binA[size - 1] == 0) ? "Father" : "Mother"); 
    } // end if 
    else 
    { 
      printf("Self"); 
    } 

    printf("\n"); 
} // end relation 
Verwandte Themen