[英]Putting a variable into an array in C
我正在做一个项目,我有一个二进制数作为变量,我试图将它放入一个数组中,这样我就可以遍历索引并测试 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
您的规格有点不清楚,但是如果我拿走您拥有的东西并在上面使用我刚抛光的水晶球,我会得到以下信息:
#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);
}
如果没有,那么......使用下面的评论部分。
让我们尝试通过定义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;
}
}
它需要解码的无符号整数、要解码的位数以及用零和一填充的数组:
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
快速而肮脏的解决方案
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.