1

如果这令人困惑,我很抱歉......到目前为止,我正在将十进制数转换为二进制数。这样做时,我将二进制表示的数字存储到一个 int 数组中。

EX:对于数字 4。(这是在下面的 dec2bin 中完成的)

    temp[0] = 1
    temp[1] = 0
    temp[2] = 0

我想将此数组存储到另一个数组(例如 BinaryArray)中,该数组将包含多个“临时”数组。

我希望 BinaryArray 声明为 main,传递给 dec2bin,并保存当前临时数组的副本。然后转到下一个号码。

我无法弄清楚指针和不需要的东西。如果有人可以帮助我如何在 main 中声明所需的数组以及如何从 dec2bin 添加到它。

谢谢!主要的:

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

    int main()
    {      

      void dec2bin(int term, int size);

      int size, mincount;
      int * ptr;
      int x;
      x=0;

      scanf("%d %d", &size, &mincount);
      printf("Variables: %d\n", size);
      printf("Count of minterms: %d\n", mincount);

      int input[mincount+1];

      while(x < mincount){
        scanf("%d", &input[x]);
        x++;
      }
      x = 0;

      while(x < mincount){
        dec2bin(input[x], size);

十二月二箱:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 32

    void
    dec2bin(int term,int size){
      int i, j, temp[size], remain, quotient;
      quotient = term;
      i = size-1;
      // set all temp to 0
      for(j=size-1;j>=0; j--){
        temp[j] = 0;
        }

      //change to binary
      while(quotient != 0){
        remain = quotient % 2;
        quotient/=2;
        if(remain != 0){
          temp[i] = 1;
         } else {
          temp[i] = 0;
         }
         i--;
        }

        //print array
        for(i=0; i<size; i++)
          printf("%d", temp[i]);

        printf("\n");
    }
4

2 回答 2

6

不确定我是否理解您想要做什么,但您似乎想要创建一个“int 数组数组”。例子:

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

int main(){
    int i;
    int n;
    int **myArray;

    n = 10;
    myArray = (int**)malloc(n*sizeof(int*));

    //Usage example
    int myIntArray[] = {1,2,3,4,5};

    myArray[0] = myIntArray;

    //This call should print "4"
    printf("%d\n",myArray[0][3]);

    return;
}   

这样,您将拥有一个数组 (myArray),其中每个元素都是一个整数数组。

于 2012-09-18T21:50:26.463 回答
0

要“将所有温度设置为 0”,请使用 memset()。我假设您想以二进制显示整数。您可以通过执行逻辑和 0x80000000 然后左移变量来检查每个位。这是一个粗略的例子:

int x = 27;
string bin;

for ( int index = 0; index < sizeof(int) * 8; ++index ) {
if ( x & 0x80000000 ) {
    bin += '1';
} else {
    bin += '0';
}
x = x << 1;
}
cout << bin << endl;

为什么要将整数的二进制表示形式存储在整数数组中?我想不出这样做的理由。

于 2012-09-18T21:21:14.167 回答