【问题标题】:Putting a variable into an array in C将变量放入C中的数组中
【发布时间】:2016-09-30 15:04:01
【问题描述】:

我正在做一个项目,我有一个二进制数作为变量,我试图将它做成一个数组,这样我就可以遍历索引并测试 0 或 1。我尝试在此处查找其他问题但找不到我要找的东西。现在我已经预定义了数组,所以我可以检查我的 if 语句是否正常工作并且它们可以正常工作。通过参数传入的十进制将被发送到函数 decToBin 以转换为二进制并返回二进制数。我希望发生这样的事情:

binA[size] = decToBin(decimal);

size 变量是二进制数的大小。

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

评论: 二进制数的类型为 int。 我做第二个循环的原因是最后一个词没有“'s”。 以下是二进制数为 1010 的输出示例: 父亲的母亲的父亲。 当二进制数的大小为 1 时打印 Self。 这是我的 decToBin 函数的代码

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

【问题讨论】:

  • 什么是“二进制数”?这是什么类型的变量?
  • for( int i = 1; i &lt; (size - 1) ; i++) 中,您为什么从数组索引1 开始,并以与数组大小无关的大小结束?
  • 二进制数字计算机上,每个变量都是“二进制变量”。你不能把变量放在某个地方。但是您可以将其值分配给另一个变量。
  • 我希望你能找到帮助和解决方案here将整数转换为二进制并将其存储在指定大小的整数数组中有趣的代码hereint x = 0xdeadbeef; // Your integer? int arr[sizeof(int)*CHAR_BIT]; for(int i = 0 ; i &lt; sizeof(int)*CHAR_BIT ; ++i) { arr[i] = (x &amp; (0x01 &lt;&lt; i)) ? 1 : 0; // Take the i-th bit }
  • 确实不需要所有这些,您可以直接使用例如检查整数变量的位。 (decimal &amp; (1 &lt;&lt; i)) != 0 其中i 是要检查的位的索引(当然是从0 开始)。如果设置了该位,则此表达式将为 1。

标签: c arrays


【解决方案1】:

你的规格有点不清楚,但如果我拿走你的东西并在上面使用我刚抛光的水晶球,我会得到以下信息:

#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);
}

如果没有,那么...请使用下面的评论部分。

【讨论】:

    【解决方案2】:

    让我们尝试通过定义decToBin() 来按照您设想的方式解决它:

    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;
        }
    }
    

    解码需要无符号整数,需要解码的位数,以及用 0 和 1 填充的数组:

    unsigned size = floor(log2(decimal)) + 1;
    
    unsigned char binA[size];
    
    decToBin(decimal, size, binA);
    

    您可以在decToBin() 中添加错误检查,以确保size 大于零。我们还可以稍微简化一下relation() 函数:

    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
    

    【讨论】:

      【解决方案3】:

      快速而肮脏的解决方案

      #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;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-12-23
        • 2021-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-25
        • 2011-04-30
        • 2020-12-28
        • 1970-01-01
        相关资源
        最近更新 更多