How to Initialize structure that includes unique i

2019-07-30 10:19发布

问题:

I have 4 arrays that represent:

  1. Colour (r , g, b, y)
  2. Shape (A , B , C, D)
  3. Number (1 , 2 , 3, 4)
  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;

回答1:

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 enums 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


回答2:

First, in C, if you want to access your struct as ST1, you need to define it like this:

typedef struct ST1 {
    char colour;
    char shape; 
    char nr;
    char texture;        
} ST1;

Second, this is a fine way to do that. But you might prefer to use enums for the values of colour, shape, number, and texture. Define an enum like this:

enum colour_t { COLOUR_R, COLOUR_G, COLOUR_B, COLOUR_Y };

And use it like this:

enum colour_t c = COLOUR_R;

In response to your comment, I think it is not necessary for you to instantiate all 256 possibilities, but instead to create them on demand. Like so:

ST1 MakeST1FromOrdinal(char n) {
    ST1 retval;
    retval.colour = n & 0x03;
    retval.shape = (n & 0x0C) >> 2;
    retval.nr = (n & 0x30) >> 4;
    retval.texture = (n & 0xC0) >> 6;
    return retval;
}

void main() {
    char ordinal = random() % 256;
    ST1 randomResult = MakeST1FromOrdinal(ordinal);
}


回答3:

You can take an array of structures! Just do this:

 struct abc         
    {
    char colour;
    char shape; 
    char nr;
    char texture;        
    } ST1[256];

If you want to access the shape of the 5th array, then write: ST1[4].shape.



回答4:

You can take an array of structures and use bit manipulation for reduced size as:-

struct abc         
    {
    int colour : 2;
    int shape : 2; 
    int number : 2;
    int texture : 2;        
    } S[256];

since you had only 4 values for each of your variables only 2 bits for each of them would suffice.

You can access them in the normal way as structures are used using the (.) operator or the structure pointer(->) operator.



标签: c structure