将变量放入C

时间:2016-09-30 15:04:02

标签: c arrays

我正在开发一个项目,我有一个二进制数作为变量,我试图将其变成一个数组,所以我可以通过索引并测试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。 我正在做第二个循环的原因是,最后一个词没有&#34;&#39; s&#34;。 以下是二进制数为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

3 个答案:

答案 0 :(得分:0)

您的规格有点不清楚,但如果我拿走你所拥有的并使用我刚刚抛光的水晶球,我会得到以下内容:

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

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

答案 1 :(得分:0)

让我们尝试通过定义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;
    }
}

需要解码的无符号整数,要解码的位数以及用零和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

答案 2 :(得分:-1)

快速而肮脏的解决方案

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