如何初始化包含多个元素中唯一项的结构 [英] How to Initialize structure that includes unique items out of multiple elements
问题描述
我有 4 个数组表示:
- 颜色(r、g、b、y)
- 形状(A、B、C、D)
- 数字(1、2、3、4)
- 纹理(a、b、c、d)
并且每个数组可以有 4 个值.我想在一个数组中包含所有这些元素的所有可能组合.例如rA1a、rB1a、rC1a等.我正在考虑创建一个结构,然后以某种方式创建该结构的这 256 个元素.但是我完全不知道如何做到这一点 - 我的意思是创建 256 个元素!有人可以指出我正确的方向吗?
我想到的结构体是这样的,但也许结构体不是我需要的?接下来我打算做的是随机选择这 256 个元素中的一些,并将选定的元素放入二维数组中以进一步操作它们.但首先要注意!
typedef 结构体{炭色;炭形;字符编号;炭纹理;}ST1;
这个解决方案建立在每个元素都有四种可能性的想法之上.如果我们用 2 位来表示每个元素,则可以使用位域结构,作为 8 位值,256 个值代表所有可能性.为了使初始化更容易,我将它们放在 union
中.
不是处理项目描述本身,而是留给输出翻译.
如果您想更直接地使用它们,您可以设置 enum
,例如 enum colors { col_r, col_g, col_b, col_y }
等等.
注意描述子'b'
在问题陈述中是重复的.
#include #include #include 类型定义联合{无符号字符值;结构{无符号字符颜色:2;无符号字符形状:2;无符号字符编号:2;无符号字符纹理:2;} 位;} ST1;const char colch [4] = { 'r', 'g', 'b', 'y' };//说明const char shpch [4] = { 'A', 'B', 'C', 'D' };const char numch [4] = { '1', '2', '3', '4' };const char texch [4] = { 'a', 'b', 'c', 'd' };无效表演(ST1 rec){printf("%c%c%c%c\n", colch[rec.bits.colour], shpch[rec.bits.shape],numch[rec.bits.nr], texch[rec.bits.texture]);}int main(void){ST1 特征[256];国际我;for(i = 0; i <256; i++) {特征[i].val = i;//初始化所有权限}srand((无符号)时间(NULL));for(i = 0; i <5; i++) {显示(特征[兰特()%256]);}}
程序输出:
<前>rD4abC4drB2cbB4bbD3aI have 4 arrays that represent:
- Colour (r , g, b, y)
- Shape (A , B , C, D)
- Number (1 , 2 , 3, 4)
- Texture (a, b , c, d)
and also each array can have 4 values. I want to have in an array all the possible combinations of all these elements. For example rA1a, rB1a,rC1a and so on. I am thinking of creating a structure and then somehow create these 256 elements of the structure. However I am totally blind to how I can do this—I mean create the 256 elements! Can someone please point me in the right direction?
The struct I am thinking of is like this, but maybe a structure is not what I need? What I intent to do next is then randomly pick some of these 256 elements, and put the selected ones in a 2d array to manipulate them even further. But first things first!
typedef struct
{
char colour;
char shape;
char nr;
char texture;
}ST1;
This solution builds on the idea that each element has four possibilities. If we get 2 bits to represent each element, a struct of bitfields can be used, and as an 8-bit value, the 256 values represent all of the possibilites. To make initialisation easier, I have put them in a union
.
Instead of working with the item descriptions themselves, that is left for the output to translate.
If you wanted to work with them more directly you could set up enum
s such as enum colours { col_r, col_g, col_b, col_y }
and so on.
Note that the descriptor 'b'
is duplicated in the problem statement.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef union {
unsigned char val;
struct {
unsigned char colour: 2;
unsigned char shape: 2;
unsigned char nr: 2;
unsigned char texture: 2;
} bits;
} ST1;
const char colch [4] = { 'r', 'g', 'b', 'y' }; // descriptions
const char shpch [4] = { 'A', 'B', 'C', 'D' };
const char numch [4] = { '1', '2', '3', '4' };
const char texch [4] = { 'a', 'b', 'c', 'd' };
void show(ST1 rec)
{
printf("%c%c%c%c\n", colch[rec.bits.colour], shpch[rec.bits.shape],
numch[rec.bits.nr], texch[rec.bits.texture]);
}
int main(void)
{
ST1 feature[256];
int i;
for(i = 0; i < 256; i++) {
feature[i].val = i; // initiliase all perms
}
srand((unsigned)time(NULL));
for(i = 0; i < 5; i++) {
show(feature[ rand() % 256 ]);
}
}
Program output:
rD4a bC4d rB2c bB4b bD3a
这篇关于如何初始化包含多个元素中唯一项的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!