forked from OPCODE-Open-Spring-Fest/CPP_Mini_Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimetable.cpp
More file actions
65 lines (60 loc) · 2.24 KB
/
timetable.cpp
File metadata and controls
65 lines (60 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "timetable.h"
#include <algorithm>
#include <iostream>
using namespace std;
void Timetable::generateRandom() {
if (rooms.empty() || slotsPerDay <= 0 || numDays <= 0) return;
sessions.clear();
srand(time(nullptr));
for (auto &course : courses) {
for (int i = 0; i < course.sessionsPerWeek; ++i) {
Session s;
s.courseName = course.name;
s.instructor = course.instructor;
s.room = rooms[rand() % rooms.size()].name;
s.day = rand() % numDays;
s.slot = rand() % slotsPerDay;
sessions.push_back(s);
}
}
}
int Timetable::computeConflicts() const {
int conflicts = 0;
for (size_t i = 0; i < sessions.size(); ++i) {
for (size_t j = i + 1; j < sessions.size(); ++j) {
const Session &a = sessions[i];
const Session &b = sessions[j];
if (a.day == b.day && a.slot == b.slot) {
if (a.room == b.room) conflicts++;
if (a.instructor == b.instructor) conflicts++;
}
}
}
for (auto &s : sessions) {
auto it = find_if(courses.begin(), courses.end(),
[&](const Course &c){ return c.name == s.courseName; });
if (it != courses.end() && !it->allowedSlots.empty()) {
if (find(it->allowedSlots.begin(), it->allowedSlots.end(), s.slot) == it->allowedSlots.end())
conflicts++;
}
}
return conflicts;
}
void Timetable::print() const {
vector<Session> sorted = sessions;
sort(sorted.begin(), sorted.end(), [](const Session &a, const Session &b){
if (a.day != b.day) return a.day < b.day;
return a.slot < b.slot;
});
cout << "\n===== Optimized Timetable =====\n";
for (auto &s : sorted) {
string slotLabel = (s.slot >= 0 && s.slot < (int)slotLabels.size() && !slotLabels[s.slot].empty())
? slotLabels[s.slot]
: ("Slot " + to_string(s.slot + 1));
cout << "Day " << s.day+1 << ", " << slotLabel
<< " | " << s.courseName
<< " | Instructor: " << s.instructor
<< " | Room: " << s.room << "\n";
}
cout << "===============================\n";
}