I'm trying to add classes which duration is in minutes into a weekly schedule held on work days from 8:00 to 21:00. The classes should be added to the first free term.
My algorithm is the following:
- I convert every class term into minutes and compare it to 1260 (because 21 hours are equal to 1260 minutes)
- if the number is less or equal to 1260 it should be added to the end of last term
- if the number is grater than 1260 it should be added on a new work day starting from 8:00
Code:
#include <stdio.h>
#include <string.h>
struct Time {
int hour, minute;
} start, end;
struct Class {
char subject[100];
struct Time start, end;
} schedule[100];
int add = 0;
void add_class(struct Class schedule[], int number_of_classes, char *subject,
int duration) {
int time_in_minutes;
add = duration;
schedule[0].start.hour = 8;
schedule[0].start.minute = 0;
if (number_of_classes == 0) {
time_in_minutes =
(schedule->start.hour) * 60 schedule->start.minute duration;
schedule->end.hour = (int)(time_in_minutes / 60);
schedule->end.minute = time_in_minutes - (schedule->end.hour) * 60;
strcpy(schedule[number_of_classes].subject, subject);
} else {
int previous = number_of_classes - 1;
int temp_hour = schedule[previous].end.hour;
int temp_minute = schedule[previous].end.minute;
time_in_minutes = temp_hour * 60 temp_minute duration;
if (time_in_minutes <= 1260) {
strcpy(schedule[number_of_classes].subject, subject);
schedule[number_of_classes].start.hour = temp_hour;
schedule[number_of_classes].start.minute = temp_minute;
schedule[number_of_classes].end.hour = (int)(time_in_minutes / 60);
schedule[number_of_classes].end.minute =
time_in_minutes - (schedule[number_of_classes].end.hour) * 60;
}
if (time_in_minutes > 1260) {
time_in_minutes = 8 * 60 duration;
schedule[number_of_classes].start.hour = 8;
schedule[number_of_classes].start.minute = 0;
schedule[number_of_classes].end.hour = (int)(time_in_minutes / 60);
schedule[number_of_classes].end.minute =
time_in_minutes - (schedule[number_of_classes].end.hour) * 60;
strcpy(schedule[number_of_classes].subject, subject);
}
}
}
void print_day(int day) {
switch (day) {
case 0:
printf("MONDAY:\n");
break;
case 1:
printf("TUESDAY:\n");
break;
case 2:
printf("WEDNESDAY:\n");
break;
case 3:
printf("THURSDAY:\n");
break;
case 4:
printf("FRIDAY:\n");
break;
default:
printf("WRONG\n");
}
}
void print_schedule(struct Class schedule[], int number_of_classes) {
int i;
int day_printed = 0;
int day = 0;
for (i = 0; i < number_of_classes; i ) {
if (day_printed == 0)
print_day(day);
if (schedule[i].start.hour > 9)
printf("%d:", schedule[i].start.hour);
else
printf("0%d:", schedule[i].start.hour);
if (schedule[i].start.minute > 9)
printf("%d - ", schedule[i].start.minute);
else
printf("0%d - ", schedule[i].start.minute);
if (schedule[i].end.hour > 9)
printf("%d:", schedule[i].end.hour);
else
printf("0%d:", schedule[i].end.hour);
if (schedule[i].end.minute > 9)
printf("%d ", schedule[i].end.minute);
else
printf("0%d ", schedule[i].end.minute);
int time_in_minutes =
(schedule[i].end.hour) * 60 schedule[i].end.minute add;
printf("%s\n", schedule[i].subject);
day_printed ;
if (time_in_minutes > 1260) {
day ;
day_printed = 0;
}
}
}
void main() {
int duration_arr[10] = {100, 200, 100, 300, 500, 600, 200, 100, 400, 300}, i;
char subject_arr[10][15] = {
"Subject 1", "Subject 2", "Subject 3", "Subject 4", "Subject 5",
"Subject 6", "Subject 7", "Subject 8", "Subject 9", "Subject 10"};
for (i = 0; i < 10; i )
add_class(schedule, i, subject_arr[i], duration_arr[i]);
print_schedule(schedule, 10);
}
My output:
MONDAY:
08:00 - 09:40 Subject 1
09:40 - 13:00 Subject 2
13:00 - 14:40 Subject 3
14:40 - 19:40 Subject 4
TUESDAY:
08:00 - 16:20 Subject 5
WEDNESDAY:
08:00 - 18:00 Subject 6
THURSDAY:
08:00 - 11:20 Subject 7
11:20 - 13:00 Subject 8
13:00 - 19:40 Subject 9
FRIDAY:
08:00 - 13:00 Subject 10
The correct output would be with Subject 7 on Tuesday (because of that Subject 10 would move to Thursday) like this.
My idea is:
- start with index of zero and and check if class term was added to schedule
- if it was added continue
- add it to schedule right after last if it's less or equal to 1260
- add it to start of next day (8:00) if it's grater than 1260
Could you help me modify my code for this idea or give me some better approach?
CodePudding user response:
The code of yours works properly, however the idea has one flaw: it fits a new class to the first available time-slot after the previously added class, but it does not try to fit the class to the first available time-slot in the whole week. Note that in add_class function, there is no behaviour to check if Subject 7 can fit on Tuesday, it is only compared to Wednesday, where Subject 6 was placed.
This is because you are storing the classes in a static array of structures, which can be suboptimal for this problem. I think you would be better off using linked lists. You could create a linked lists of the scheduled classes (containing also the day of the week) and in add_class function you could check the same you are checking (minutes in the day), but "day-by-day". Then you could add a new class in the correct position (in your case, Subject 7 would go after Subject 5).
CodePudding user response:
I tried to compare struct array after adding all classes with this:
void swap(int a[], int b, int c) {
int temp = a[b];
a[b] = a[c];
a[b] = temp;
}
void sort(struct Class schedule[], int n) {
int i,sum=0;
for (i = 0; i < n; i ) {
int time_in_minutes = (schedule[i].start.hour) * 60 schedule[i].start.minute;
schedule[i].end.hour = (int)(time_in_minutes / 60);
schedule[i].end.minute = time_in_minutes - (schedule[i].end.hour) * 60;
sum =time_in_minutes;
if (sum > 1260) {
swap(schedule,i,i )
}
}
}
Why this doesn't work? Could you help me to sort it correctly?