Home > Blockchain >  How should I build-up this structure?
How should I build-up this structure?

Time:12-08

So the first part of the task is to create a structure in the following way:

  1. the trains id (integer)
  2. time ( two integers separated by a colon)
  3. whether they will arrive or start (0 for arrival and 1 for start)

I have no problem with the 1 and 3. But I have no clue how should I do the 2 because of the column. Any ideas would be appreciated!

CodePudding user response:

There are many ways. Here is one:

typedef struct {
    int id;
    int hr;//hr/min can be arranged in a string separated by a space (or colon)
    int min;
    bool direction;//where arriving == false, departing == true
}train_s;

train_s train;

This can be used to create a single instance or an array...

   int main(void)
   {
       train_s train[10] = {0};//array of 10 trains
       ...
   

Or a string can be used for time that would allow room for two values in two columns....

typedef struct {
    int id;
    char time[6];//eg "12:00"
    bool direction;//where arriving == false, departing == true
}train_s;     
  • Related