-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathABCUCoursePlanner.cpp
More file actions
479 lines (406 loc) · 13.3 KB
/
ABCUCoursePlanner.cpp
File metadata and controls
479 lines (406 loc) · 13.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
//============================================================================
// Name : ABCUCoursePlanner.cpp
// Author : Jarrod Schantz
// Version : 1.0
// Description : Course planning system for ABCU Computer Science Department
// Uses Binary Search Tree for efficient course management
//============================================================================
#include <iostream>
#include <fstream>
#include <sstream>
#include <utility>
#include <vector>
#include <algorithm>
#include <string>
#include <limits>
using namespace std;
//============================================================================
// Course Structure Definition
//============================================================================
/**
* Structure to hold course information
* Contains course number, title, and list of prerequisites
*/
struct Course {
string courseNumber;
string courseTitle;
vector<string> prerequisites;
// Default constructor
Course() = default;
// Parameterized constructor
Course(const string &number, string title) {
courseNumber = number;
courseTitle = std::move(title);
}
};
//============================================================================
// Binary Search Tree Node Structure
//============================================================================
/**
* Internal structure for tree node
* Each node contains a course and pointers to left and right children
*/
struct Node {
Course course;
Node* left;
Node* right;
// Default constructor
Node() {
left = nullptr;
right = nullptr;
}
// Constructor with course
explicit Node(Course aCourse) : Node() {
course = std::move(aCourse);
}
};
//============================================================================
// Binary Search Tree Class Definition
//============================================================================
/**
* Binary Search Tree class for managing courses
* Provides efficient insertion, search, and in-order traversal
*/
class BinarySearchTree final {
Node* root;
static void addNode(Node* node, const Course& course);
static void inOrder(const Node* node);
static void deleteRecursive(const Node* node);
public:
BinarySearchTree();
~BinarySearchTree();
void InOrder() const;
void Insert(const Course& course);
[[nodiscard]] Course Search(const string& courseNumber) const;
};
/**
* Default constructor
* Initializes root to nullptr creating an empty tree
*/
BinarySearchTree::BinarySearchTree() {
root = nullptr;
}
/**
* Destructor
* Recursively deletes all nodes to prevent memory leaks
*/
BinarySearchTree::~BinarySearchTree() {
deleteRecursive(root);
}
// Helper function to delete all nodes
// Deletes children first, then parent
void BinarySearchTree::deleteRecursive(const Node* node) {
if (node != nullptr) {
deleteRecursive(node->left);
deleteRecursive(node->right);
delete node;
}
}
/**
* Insert a course into the tree
*
* @param course The course to insert
*/
void BinarySearchTree::Insert(const Course& course) {
if (root == nullptr) {
root = new Node(course);
} else {
addNode(root, course);
}
}
/**
* Recursive helper to add a course to the correct position in tree
*
* @param node Current node in traversal
* @param course Course to add
*/
void BinarySearchTree::addNode(Node* node, const Course& course) {
// Compare course numbers to determine placement
if (course.courseNumber < node->course.courseNumber) {
// Add to left subtree
if (node->left == nullptr) {
node->left = new Node(course);
} else {
addNode(node->left, course);
}
} else {
// Add to right subtree
if (node->right == nullptr) {
node->right = new Node(course);
} else {
addNode(node->right, course);
}
}
}
/**
* Public method to traverse tree in order
*/
void BinarySearchTree::InOrder() const {
inOrder(root);
}
/**
* Recursive in-order traversal
* Visits nodes in sorted order: left -> root -> right
*
* @param node Current node being visited
*/
void BinarySearchTree::inOrder(const Node* node) {
if (node != nullptr) {
// Traverse left subtree
inOrder(node->left);
// Print current node
cout << node->course.courseNumber << ", "
<< node->course.courseTitle << endl;
// Traverse right subtree
inOrder(node->right);
}
}
/**
* Search for a course by course number
* Iteratively traverses tree based on comparisons
*
* @param courseNumber The course number to search for
* @return The course if found, empty course otherwise
*/
Course BinarySearchTree::Search(const string& courseNumber) const {
Node* current = root;
// Traverse tree until found or reach end
while (current != nullptr) {
// Found matching course
if (current->course.courseNumber == courseNumber) {
return current->course;
}
// Search left subtree if target is smaller
if (courseNumber < current->course.courseNumber) {
current = current->left;
}
// Search right subtree if target is larger
else {
current = current->right;
}
}
// Course not found, return empty course
Course course;
return course;
}
//============================================================================
// Utility Functions
//============================================================================
/**
* Split a string by a delimiter
*
* @param str The string to split
* @param delimiter The character to split on
* @return Vector of string tokens
*/
vector<string> tokenize(const string& str, char delimiter) {
vector<string> tokens;
stringstream ss(str);
string token;
while (getline(ss, token, delimiter)) {
// Trim whitespace from token
token.erase(0, token.find_first_not_of(" \t\r\n"));
token.erase(token.find_last_not_of(" \t\r\n") + 1);
tokens.push_back(token);
}
return tokens;
}
/**
* Load courses from a file into the BST
* Performs two-pass validation to ensure data integrity
*
* @param filename Path to the course data file
* @param bst Pointer to the binary search tree
* @return true if load successful, false otherwise
*/
bool loadCourses(const string& filename, BinarySearchTree* bst) {
ifstream file(filename);
// Check if file opened successfully
if (!file.is_open()) {
cout << "Error: Could not open file " << filename << endl;
return false;
}
cout << "Loading course data from " << filename << "..." << endl;
vector<Course> courses;
vector<string> validCourseNumbers;
string line;
int lineNumber = 0;
// First pass: Read and validate basic structure
while (getline(file, line)) {
lineNumber++;
// Skip empty lines
if (line.empty()) {
continue;
}
// Parse line into tokens
vector<string> tokens = tokenize(line, ',');
// Validate minimum number of fields
if (tokens.size() < 2) {
cout << "Error: Line " << lineNumber << " has insufficient data" << endl;
cout << "Each line must have at least course number and title" << endl;
file.close();
return false;
}
// Create course object
Course course;
course.courseNumber = tokens[0];
course.courseTitle = tokens[1];
// Add prerequisites (if any) - skip empty strings
for (size_t i = 2; i < tokens.size(); i++) {
// Only add non-empty prerequisites
if (!tokens[i].empty()) {
course.prerequisites.push_back(tokens[i]);
}
}
// Store course and track valid course numbers
courses.push_back(course);
validCourseNumbers.push_back(course.courseNumber);
}
file.close();
// Second pass: Validate prerequisites exist
for (const auto& course : courses) {
for (const auto& prereq : course.prerequisites) {
// Skip empty prerequisites
if (prereq.empty()) {
continue;
}
// Check if prerequisite exists in course list
if (ranges::find(validCourseNumbers, prereq)
== validCourseNumbers.end()) {
cout << "Error: Prerequisite " << prereq << " for course "
<< course.courseNumber << " does not exist" << endl;
return false;
}
}
}
// All validation passed - load into BST
for (const auto& course : courses) {
bst->Insert(course);
}
cout << "Successfully loaded " << courses.size() << " courses." << endl;
return true;
}
/**
* Display the main menu
*/
void displayMenu() {
cout << "\n========================================" << endl;
cout << "Welcome to the course planner." << endl;
cout << "========================================" << endl;
cout << " 1. Load Data Structure" << endl;
cout << " 2. Print Course List" << endl;
cout << " 3. Print Course" << endl;
cout << "\n 9. Exit" << endl;
cout << "========================================" << endl;
cout << "What would you like to do? ";
}
/**
* Print information for a specific course including prerequisites
*
* @param bst Pointer to the binary search tree
* @param courseNumber Course number to search for
*/
void printCourse(const BinarySearchTree* bst, string courseNumber) {
// Extract only the course number (first token before comma or space)
const size_t commaPos = courseNumber.find(',');
const size_t spacePos = courseNumber.find(' ');
if (const size_t endPos = min(commaPos, spacePos); endPos != string::npos) {
courseNumber = courseNumber.substr(0, endPos);
}
// Trim whitespace
courseNumber.erase(0, courseNumber.find_first_not_of(" \t\r\n"));
courseNumber.erase(courseNumber.find_last_not_of(" \t\r\n") + 1);
// Convert to uppercase for case-insensitive search
ranges::transform(courseNumber,
courseNumber.begin(), ::toupper);
const Course course = bst->Search(courseNumber);
// Check if course was found
if (course.courseNumber.empty()) {
cout << "Course " << courseNumber << " not found." << endl;
return;
}
// Print course information
cout << course.courseNumber << "," << course.courseTitle << endl;
// Print prerequisites
if (course.prerequisites.empty()) {
cout << "Prerequisites: None" << endl;
} else {
cout << "Prerequisites: ";
for (size_t i = 0; i < course.prerequisites.size(); i++) {
cout << course.prerequisites[i];
if (i < course.prerequisites.size() - 1) {
cout << ", ";
}
}
cout << endl;
}
}
//============================================================================
// Main Function
//============================================================================
/**
* Main program entry point
* Provides menu-driven interface for course management
*/
int main() {
auto* bst = new BinarySearchTree();
string filename;
string courseNumber;
int choice = 0;
bool dataLoaded = false;
cout << "\nABCU Course Planner" << endl;
// Main program loop
while (choice != 9) {
displayMenu();
// Get user input with error checking
if (!(cin >> choice)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\nInvalid input. Please enter a number." << endl;
continue;
}
// Clear input buffer
cin.ignore(numeric_limits<streamsize>::max(), '\n');
switch (choice) {
case 1:
// Load data structure
cout << "Enter the file name: ";
getline(cin, filename);
if (loadCourses(filename, bst)) {
dataLoaded = true;
}
break;
case 2:
// Print course list
if (!dataLoaded) {
cout << "\nError: No data loaded. Please load data first (Option 1)." << endl;
} else {
cout << "\nHere is a sample schedule:\n" << endl;
bst->InOrder();
}
break;
case 3:
// Print course information
if (!dataLoaded) {
cout << "\nError: No data loaded. Please load data first (Option 1)." << endl;
} else {
cout << "What course do you want to know about? (Enter course number): ";
getline(cin, courseNumber);
cout << endl;
printCourse(bst, courseNumber);
}
break;
case 9:
// Exit program
cout << "\nThank you for using the course planner!" << endl;
break;
default:
// Invalid option
cout << "\n" << choice << " is not a valid option." << endl;
break;
}
}
// Clean up memory
delete bst;
return 0;
}