#include #include #include typedef struct { int day; int hour; } ctime; typedef struct { int size; char *name; ctime *(*t); } section; typedef struct { int size; char *name; section *(*s); } course; typedef struct { int size; course *(*c); } data; #define DAYS 6 #define HOURS 17 #define tableP int[][DAYS][HOURS] #define tableR int table[][DAYS][HOURS] void printTable (data *, tableP); void run (data *, tableP, int); int isfree (section *, tableP); void fill (course *, tableP, int, int); void empty (section *, tableP); void readData(data*); void freeData(data*); void run (data *d, tableR, int c) { int i; if (c == d->size) printTable(d, table); else { for (i = 0; i < d->c[c]->size; i++) { if (isfree (d->c[c]->s[i], table)) { fill (d->c[c], table, c, i); run (d, table, c + 1); empty (d->c[c]->s[i], table); } } } } int isfree (section *s, tableR) { int i; for (i = 0; i < s->size; i++) if (table[0][s->t[i]->day][s->t[i]->hour] != -1) return 0; return 1; } void fill (course *c, tableR, int cNum, int sNum) { int i; for (i = 0; i < c->s[sNum]->size; i++) { table[0][c->s[sNum]->t[i]->day][c->s[sNum]->t[i]->hour] = cNum; table[1][c->s[sNum]->t[i]->day][c->s[sNum]->t[i]->hour] = sNum; } } void empty (section *s, tableR) { int i; for (i = 0; i < s->size; i++) { table[0][s->t[i]->day][s->t[i]->hour] = -1; table[1][s->t[i]->day][s->t[i]->hour] = -1; } } void printTable (data * d, tableR) { int i, j; printf(",Mon\,Tue,Wed,Thu,Fri\n"); for (i = 9; i < HOURS; i++) { printf ("%d:00,", i); for (j = 0; j < DAYS; j++) { if (table[0][j][i] == -1) { printf (","); } else { printf ("%s:%s,", d->c[table[0][j][i]]->name, d->c[table[0][j][i]]->s[table[1][j][i]]->name); } } printf("\n"); } printf("-------------------------------------\n"); } int main (void) { data d; int tt[2][DAYS][HOURS]; int i, j, k; for (i = 0; i < 2; i++) for (j = 0; j < DAYS; j++) for (k = 0; k < HOURS; k++) tt[i][j][k] = -1; readData(&d); run (&d, tt, 0); freeData(&d); return 0; } #define CUR_COURSE d->c[d->size - 1] #define CUR_SECTION CUR_COURSE->s[CUR_COURSE->size - 1] #define CUR_TIME CUR_SECTION->t[CUR_SECTION->size - 1] void readData(data *d) { char c; char *s; int day, h; d->size = 0; d->c = malloc (0); s = malloc (128); while(scanf("%c", &c) == 1) { // printf("Top\n"); switch (c) { case '+': // printf("Add course\n"); // Another course. Create the struct. d->size++; d->c = realloc(d->c, sizeof(course*) * d->size); CUR_COURSE = malloc(sizeof(course)); //Read the course name scanf ("%s", s); // printf("%s\n", s); // Make space for it CUR_COURSE->name = malloc (strlen(s) + 1); // Set the course name memcpy (CUR_COURSE->name, s, strlen(s) + 1); // printf("Course name: %s\n", CUR_COURSE->name); CUR_COURSE->size = 0; CUR_COURSE->s = malloc(0); break; case '|': // Another section. Create the struct. CUR_COURSE->size++; CUR_COURSE->s = realloc(CUR_COURSE->s, sizeof(section) * CUR_COURSE->size); CUR_SECTION = malloc (sizeof(section)); // Read the course name scanf ("%s", s); // printf("%s\n", s); // Make space for it CUR_SECTION->name = malloc (strlen(s) + 1); // Set the course name memcpy (CUR_SECTION->name, s, strlen(s) + 1); // printf("Section name: %s\n", CUR_SECTION->name); CUR_SECTION->size = 0; CUR_SECTION->t = malloc(0); break; case '-': // printf("Add time\n"); // Another time. Create the struct. CUR_SECTION->size++; CUR_SECTION->t = realloc(CUR_SECTION->t, sizeof(ctime) * CUR_COURSE->size); CUR_TIME = malloc(sizeof(ctime)); // Read the course name scanf ("%d %d", &day, &h); CUR_TIME->day = day; CUR_TIME->hour = h; break; //default: printf("Bad char: %c", c); } } /* for (i = 0; i < d->size; i++) { printf("Course: %s\n", d->c[i]->name); for (j = 0; j < d->c[i]->size; j++) { printf("Section: %s\n", d->c[i]->s[j]->name); for (k = 0; k < d->c[i]->s[j]->size; k++) { printf("Time: %d, %d\n", d->c[i]->s[j]->t[k]->day, d->c[i]->s[j]->t[k]->hour); } } } */ } void freeData(data *d) { int i, j, k; for (i = 0; i < d->size; i++) { for (j = 0; j < d->c[i]->size; j++) { for (k = 0; k < d->c[i]->s[j]->size; k++) { free(d->c[i]->s[j]->t[k]); } free(d->c[i]->s[j]); } free(d->c[i]); } }