-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewCode.cpp
More file actions
581 lines (512 loc) · 14.3 KB
/
Copy pathNewCode.cpp
File metadata and controls
581 lines (512 loc) · 14.3 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include <iostream>
#include <string>
using namespace std;
class Course;
class CourseNode;
class Student;
class StudentNode;
class EnrollmentHistory;
class EnrollmentNode;
class Prerequisites;
class WaitList;
class Stack
{
private:
struct StackNode
{
int courseId;
StackNode *next;
StackNode(int id) : courseId(id), next(nullptr) {}
};
StackNode *top;
public:
Stack() : top(nullptr) {}
void push(int courseId)
{
StackNode *newNode = new StackNode(courseId);
newNode->next = top;
top = newNode;
}
bool pop()
{
if (isEmpty())
return false;
StackNode *temp = top;
top = top->next;
delete temp;
return true;
}
bool isEmpty()
{
return top == nullptr;
}
void display()
{
StackNode *current = top;
while (current != nullptr)
{
cout << current->courseId << " ";
current = current->next;
}
cout << endl;
}
~Stack()
{
while (!isEmpty())
{
pop();
}
}
};
class Queue
{
private:
struct QueueNode
{
int studentId;
QueueNode *next;
QueueNode(int id) : studentId(id), next(nullptr) {}
};
QueueNode *front, *rear;
public:
Queue() : front(nullptr), rear(nullptr) {}
void enqueue(int studentId)
{
QueueNode *newNode = new QueueNode(studentId);
if (rear == nullptr)
{
front = rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}
int dequeue()
{
if (isEmpty())
return -1;
QueueNode *temp = front;
int studentId = front->studentId;
front = front->next;
if (front == nullptr)
rear = nullptr;
delete temp;
return studentId;
}
bool isEmpty()
{
return front == nullptr;
}
~Queue()
{
while (!isEmpty())
{
dequeue();
}
}
};
class EnrollmentHistory
{
private:
struct EnrollmentNode
{
int courseId;
string grade;
EnrollmentNode *prev;
EnrollmentNode *next;
EnrollmentNode(int id) : courseId(id), grade(""), prev(nullptr), next(nullptr) {}
};
EnrollmentNode *head;
EnrollmentNode *tail;
public:
EnrollmentHistory() : head(nullptr), tail(nullptr) {}
void addEnrollment(int courseId)
{
EnrollmentNode *newNode = new EnrollmentNode(courseId);
if (!head)
{
head = tail = newNode;
}
else
{
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
bool setGrade(int courseId, string grade)
{
EnrollmentNode *current = head;
while (current)
{
if (current->courseId == courseId)
{
current->grade = grade;
return true;
}
current = current->next;
}
return false;
}
void display()
{
EnrollmentNode *current = head;
while (current)
{
cout << "Course ID: " << current->courseId;
if (!current->grade.empty())
{
cout << ", Grade: " << current->grade;
}
cout << endl;
current = current->next;
}
}
bool isEnrolled(int courseId)
{
EnrollmentNode *current = head;
while (current)
{
if (current->courseId == courseId && current->grade.empty())
{
return true;
}
current = current->next;
}
return false;
}
bool hasCompleted(int courseId)
{
EnrollmentNode *current = head;
while (current)
{
if (current->courseId == courseId && !current->grade.empty())
{
return true;
}
current = current->next;
}
return false;
}
};
class Student
{
public:
int id;
string name;
string email;
string phone;
string address;
string password;
EnrollmentHistory *enrollmentHistory;
Student(int _id, string _name, string _email, string _phone, string _address, string _password)
: id(_id), name(_name), email(_email), phone(_phone), address(_address), password(_password)
{
enrollmentHistory = new EnrollmentHistory();
}
~Student()
{
delete enrollmentHistory;
}
};
class Course
{
public:
int id;
string name;
int credits;
string instructor;
int maxStudents;
int currentEnrollment;
Stack prerequisites;
Queue waitlist;
Course(int _id, string _name, int _credits, string _instructor, int _maxStudents)
: id(_id), name(_name), credits(_credits), instructor(_instructor),
maxStudents(_maxStudents), currentEnrollment(0) {}
};
class StudentRecords
{
private:
struct StudentNode
{
Student *data;
StudentNode *next;
StudentNode(Student *student) : data(student), next(nullptr) {}
};
StudentNode *head;
public:
StudentRecords() : head(nullptr) {}
bool addStudent(int id, string name, string email, string phone, string address, string password)
{
if (findStudent(id) != nullptr)
{
cout << "Error: Student with ID " << id << " already exists" << endl;
return false;
}
Student *newStudent = new Student(id, name, email, phone, address, password);
StudentNode *newNode = new StudentNode(newStudent);
if (!head)
{
head = newNode;
}
else
{
StudentNode *current = head;
while (current->next)
{
current = current->next;
}
current->next = newNode;
}
cout << "Student with ID " << id << " was successfully added" << endl;
return true;
}
StudentNode *findStudent(int student_id)
{
if (head == NULL)
{
return NULL;
}
StudentNode *current = head;
while (current != NULL)
{
if (current->data->id == student_id)
{
return current;
}
current = current->next;
}
return NULL;
}
bool deleteStudent(int id)
{
if (!head)
{
cout << "Error: No students in the system" << endl;
return false;
}
if (head->data->id == id)
{
StudentNode *temp = head;
head = head->next;
delete temp->data;
delete temp;
cout << "Student with ID " << id << " was successfully deleted" << endl;
return true;
}
StudentNode *current = head;
while (current->next && current->next->data->id != id)
{
current = current->next;
}
if (current->next)
{
StudentNode *temp = current->next;
current->next = temp->next;
delete temp->data;
delete temp;
cout << "Student with ID " << id << " was successfully deleted" << endl;
return true;
}
cout << "Error: Student with ID " << id << " not found" << endl;
return false;
}
void display()
{
StudentNode *current = head;
while (current)
{
cout << "ID: " << current->data->id << endl;
cout << "Name: " << current->data->name << endl;
cout << "Email: " << current->data->email << endl;
cout << "Phone: " << current->data->phone << endl;
cout << "Address: " << current->data->address << endl;
cout << "Enrollment History:" << endl;
current->data->enrollmentHistory->display();
cout << "------------------------" << endl;
current = current->next;
}
}
~StudentRecords()
{
while (head)
{
StudentNode *temp = head;
head = head->next;
delete temp->data;
delete temp;
}
}
};
class CourseRecords
{
private:
struct CourseNode
{
Course *data;
CourseNode *left;
CourseNode *right;
CourseNode(Course *course) : data(course), left(nullptr), right(nullptr) {}
};
CourseNode *root;
CourseNode *insertHelper(CourseNode *node, Course *course)
{
if (!node)
{
return new CourseNode(course);
}
if (course->id < node->data->id)
{
node->left = insertHelper(node->left, course);
}
else if (course->id > node->data->id)
{
node->right = insertHelper(node->right, course);
}
return node;
}
public:
CourseRecords() : root(nullptr) {}
bool addCourse(int id, string name, int credits, string instructor, int maxStudents)
{
if (findCourse(id) != nullptr)
{
cout << "Error: Course with ID " << id << " already exists" << endl;
return false;
}
Course *newCourse = new Course(id, name, credits, instructor, maxStudents);
root = insertHelper(root, newCourse);
cout << "Course with ID " << id << " was successfully added" << endl;
return true;
}
// search on the BST
Course *findCourse(int course_id)
{
CourseNode *current = root;
while (current != NULL){
if (current->id == course_id)
{
return current;
}
if (course_id > current->id)
{
current = current->right;
}
else if (course_id < current->id)
{
current = current->left;
}
}
return current;
}
bool addPrerequisite(int courseId, int prerequisiteId)
{
Course *course = findCourse(courseId);
Course *prerequisite = findCourse(prerequisiteId);
if (!course || !prerequisite)
{
cout << "Error: Course or prerequisite not found" << endl;
return false;
}
course->prerequisites.push(prerequisiteId);
cout << "Prerequisite (Course " << prerequisiteId << ") was successfully added to Course " << courseId << endl;
return true;
}
bool enrollStudent(int courseId, int studentId, StudentRecords &students)
{
Course *course = findCourse(courseId);
Student *student = students.findStudent(studentId);
if (!course || !student)
{
cout << "Error: Course or student not found" << endl;
return false;
}
if (student->enrollmentHistory->isEnrolled(courseId))
{
cout << "Error: Student " << studentId << " is already enrolled in Course " << courseId << endl;
return false;
}
Stack tempStack = course->prerequisites;
while (!tempStack.isEmpty())
{
int prereqId = tempStack.pop();
if (!student->enrollmentHistory->hasCompleted(prereqId))
{
cout << "Error: Student " << studentId << " has not completed prerequisite Course " << prereqId << endl;
return false;
}
}
if (course->currentEnrollment >= course->maxStudents)
{
course->waitlist.enqueue(studentId);
cout << "Course " << courseId << " is full. Student " << studentId << " has been added to the waitlist." << endl;
return false;
}
course->currentEnrollment++;
student->enrollmentHistory->addEnrollment(courseId);
cout << "Student " << studentId << " was successfully enrolled in Course " << courseId << endl;
return true;
}
bool dropCourse(int courseId, int studentId, StudentRecords &students)
{
Course *course = findCourse(courseId);
Student *student = students.findStudent(studentId);
if (!course || !student)
{
cout << "Error: Course or student not found" << endl;
return false;
}
if (!student->enrollmentHistory->isEnrolled(courseId))
{
cout << "Error: Student " << studentId << " is not enrolled in Course " << courseId << endl;
return false;
}
student->enrollmentHistory->setGrade(courseId, "F");
course->currentEnrollment--;
cout << "Student " << studentId << " has dropped Course " << courseId << " and received grade F" << endl;
if (!course->waitlist.isEmpty())
{
int nextStudentId = course->waitlist.dequeue();
if (enrollStudent(courseId, nextStudentId, students))
{
cout << "Student " << nextStudentId << " was enrolled from the waitlist for Course " << courseId << endl;
}
else
{
cout << "Could not enroll next student from waitlist" << endl;
}
}
return true;
}
bool completeCourse(int courseId, int studentId, string grade, StudentRecords &students)
{
Course *course = findCourse(courseId);
Student *student = students.findStudent(studentId);
if (!course || !student)
{
cout << "Error: Course or student not found" << endl;
return false;
}
if (!student->enrollmentHistory->isEnrolled(courseId))
{
cout << "Error: Student " << studentId << " is not enrolled in Course " << courseId << endl;
return false;
}
student->enrollmentHistory->setGrade(courseId, grade);
course->currentEnrollment--;
cout << "Student " << studentId << " has completed Course " << courseId << " with grade " << grade << endl;
if (!course->waitlist.isEmpty())
{
int nextStudentId = course->waitlist.dequeue();
if (enrollStudent(courseId, nextStudentId, students))
{
cout << "Student " << nextStudentId << " was enrolled from the waitlist for Course " << courseId << endl;
}
else
{
cout << "Could not enroll next student from waitlist" << endl;
}
}
return true;
}
};