c_sharp
c_sharp

Reputation: 281

evaluating an enum

Is there a way to evaluate an enum? I have an enum that is incorporated into a struct:

typedef enum {MW, TR} days;

typedef struct {
  int hour, min;
} Time;

typedef struct {
  char Dept[5];
  int course, sect;
  days meet_days;
  Time start, end;
  char instr[20];
} sched_record;

My print statement for the enum is:

 data[i].meet_days == MW ? "MW" : "TR"

What I am trying to do is to get my typedef struct of sched_record to print only the records with say MW in it. My "menu" for the program is as follows:

 fread(data, sizeof(sched_record), MAX_RECORD, filePointer);
        fclose(filePointer);
        printf("Enter the Department or A for any Department: ");
        scanf("%s", tempDept);
        printf("Enter the Course or 0 for any course: ");
        scanf("%d", &tempCourse);
        printf("Enter the Days; M = MW, T = TTH or D=Don't Care: ");
        scanf("%s", tempDay);
        printf("Enter the Time; A=Mornings, P=Afternoons or D=Don't Care: ");
        scanf("%s", tempTime);

I got my sched_records to print out by time with a simple statement of:

 else if ((strcmp(tempDept, "A")==0) && tempCourse == 0 && (strcmp(tempDay, "D")==0) && (strcmp(tempTime, "P")==0)) {
                            if (data[i].start.hour >= 12) {  // <---Time comparison
                                printf("%s %d %d %2s %02d%02d %02d%02d %s\n", data[i].Dept, data[i].course, data[i].sect, data[i].meet_days == MW ? "MW" : "TR",
                                   data[i].start.hour, data[i].start.min, data[i].end.hour, data[i].end.min, data[i].instr);
                     }
                }

                else if ((strcmp(tempDept, "A")==0) && tempCourse == 0 && (strcmp(tempDay, "M")==0) && (strcmp(tempTime, "D")==0)) {
                        printf("\n%s %d", data[i].Dept, data[i].course);

I am wondering if there is a simple way like the time comparison to do the same with the enum. If so can someone show me?

Upvotes: 0

Views: 602

Answers (2)

Mark Nunberg
Mark Nunberg

Reputation: 3691

A different approach to this is to define your enum values to have a certain bit set.. for example, ensure that days 'monday' through 'friday' all have a specific bit set:

thus:

#define WEEKDAY 0x100
typedef enum {
 SUNDAY   = 1,
 MONDAY   = 2 | WEEKDAY,
 TUESDAY  = 3 | WEEKDAY,
 WEDNESDAY= 4 | WEEKDAY,
 THURSDAY = 5 | WEEKDAY,
 FRIDAY   = 6 | WEEKDAY,
 SATURDAY = 7
} days;

Then, when checking whether the day is a weekday:

if (day & WEEKDAY) {
 printf("Day %d is a weekday!\n", day);
}

You can get even fancier by using an XMACRO for this.. but I'll leave that as an exercise for the OP :)

Using the bitflag approach allows you to add additional arbitrary classifications for your days without actually doing ranged comparisons all the time

Upvotes: 1

ams
ams

Reputation: 25569

You can compare enumerated values in the same way as any other integer variable:

if (data[i].meet_days == MW)
  .....

Or, if say you had an enumeration for all days:

enum days {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

then you can test ranges like this:

if (day >= Monday || day <= Friday)
  printf ("It's a weekday!\n");

Hope that helps.

Upvotes: 2

Related Questions