-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject2.cpp
More file actions
373 lines (318 loc) · 10.9 KB
/
Project2.cpp
File metadata and controls
373 lines (318 loc) · 10.9 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
//============================================================================
// Name : Project2.cpp
// Author : Derek KWasniewski
// Version : 1.0
// Description : Software for ABC University (ABCU) to organize course
// information so advisors can help students.
//============================================================================
#include <iostream>
#include <time.h>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
//============================================================================
// Global definitions visible to all methods and classes
//============================================================================
// forward declarations
double strToDouble(string str, char ch);
// define a structure to hold bid information
struct Course {
string courseNum; // unique identifier
string title;
vector<string> prereqs;
Course() {
courseNum = "";
}
};
// Internal structure for tree node
struct Node {
Course course;
Node *left;
Node *right;
// default constructor
Node() {
left = nullptr;
right = nullptr;
}
// initialize with a bid
Node(Course aCourse) :
Node() {
course = aCourse;
}
};
//============================================================================
// Binary Search Tree class definition
//============================================================================
/**
* Define a class containing data members and methods to
* implement a binary search tree
*/
class BinarySearchTree {
private:
Node* root;
void inOrder(Node* node);
public:
BinarySearchTree();
virtual ~BinarySearchTree();
void InOrder();
void Insert(Course course);
Course Search(string courseNum);
};
/**
* Default constructor
*/
BinarySearchTree::BinarySearchTree() {
// Root is equal to nullptr
root = nullptr;
}
/**
* Destructor
*/
BinarySearchTree::~BinarySearchTree() {
// Recurse from root deleting every node
delete root->left;
delete root->right;
}
void BinarySearchTree::Insert(Course course) {
// Create new node with its course as the passed course
Node* newNode = new Node;
newNode->course = course;
// if root equarl to null ptr
if (root == nullptr) {
// root is equal to new node course
root = newNode;
newNode->left = nullptr;
newNode->right = nullptr;
}
else {
// Create new node equal to root node
Node* currNode = root;
// Continue looping through tree until a proper spot has been found for the new node
while (currNode != nullptr) {
// LEFT branch
if (newNode->course.courseNum < currNode->course.courseNum) {
// IF no left node set it to the new node
if (currNode->left == nullptr) {
currNode->left = newNode;
currNode = nullptr;
}
// Set currNode to the new left node
else {
currNode = currNode->left;
}
}
// RIGHT branch
else {
// IF no right node set it to the new node
if (currNode->right == nullptr) {
currNode->right = newNode;
currNode = nullptr;
}
// Set currNode to the new right node
else {
currNode = currNode->right;
}
}
// Once a spot has been found for the new node, set its left & right nodes to nullptr as the newNode is a leaf node
newNode->left = nullptr;
newNode->right = nullptr;
}
}
}
void BinarySearchTree::InOrder() {
this->inOrder(root);
}
/**
* Search for a bid
*/
Course BinarySearchTree::Search(string courseNum) {
// set current node equal to root
Node* currNode = root;
// keep looping downwards until bottom reached or matching courseNum found
while (currNode != nullptr) {
// if match found, return current course
if (currNode->course.courseNum == courseNum) {
return currNode->course;
}
// if courseNum is smaller than current node then traverse left
else if (courseNum < currNode->course.courseNum) {
currNode = currNode->left;
}
// else larger so traverse right
else {
currNode = currNode->right;
}
}
// Course not found, return empty course
Course course;
return course;
}
void BinarySearchTree::inOrder(Node* node) {
//if node is not equal to null ptr
if (node == nullptr) {
return;
}
//InOrder left
inOrder(node->left);
//output courseNum, title
cout << node->course.courseNum << ", " << node->course.title << endl;
//InOder right
inOrder(node->right);
}
void loadCourses(string filePath, BinarySearchTree* bst) {
// Create file stream object
fstream fs;
// Added file extenstion to filename
filePath.append(".txt");
// Open file with filePath
fs.open(filePath, fstream::in);
// If file doesn't exist, return
if (!fs.is_open()) {
cout << "ERROR: No file with that name in main directory!" << endl;
return;
}
// POSSIBLE PROBLEM: Microsoft Text file encoding can cause a problem if it is UTF-8 BOM
// FIX: Save text file as the same file but change the encoding to the normal UTF-8
// Loop over each line in file until '\n' or eof is found
while (!fs.eof()) {
// Declare variable to hold each line, the splitLine, the the vector representing the splitLine
string line;
string splitLine;
vector<string> lineVec;
getline(fs, line);
// Create new course each iteration
Course course;
// Create a stream class to operate on strings (https://cplusplus.com/reference/sstream/stringstream/?kw=stringstream)
// Operate on each line that we get
stringstream stream(line);
// Using the line we get, use the getline function and the stream object to split the string by every comma
// and insert each word into a vector
while (getline(stream, splitLine, ',')) {
lineVec.push_back(splitLine);
}
// Assign courseNum and title to indices 0 and 1 respectively
course.courseNum = lineVec.at(0);
course.title = lineVec.at(1);
// For each element after the 2nd in lineVec, add it to the courses prereq vector
for (int i = 2; i < lineVec.size(); i++)
{
course.prereqs.push_back(lineVec.at(i));
}
bst->Insert(course);
}
cout << "File loaded." << endl;
}
//============================================================================
// Static methods used for testing
//============================================================================
/**
* Display the bid information to the console (std::out)
*
* @param course containing the course info
*/
void displayCourse(Course course) {
// Output data of given course
cout << endl << course.courseNum << ", " << course.title << endl;
// If the course prereq vector variable is not empty
// Print out each prereq course number
if (course.prereqs.size() != 0) {
cout << "Prerequisites: ";
for (int i = 0; i < course.prereqs.size() - 1; i++) { cout << course.prereqs.at(i) << ", "; }
cout << course.prereqs.at(course.prereqs.size() - 1) << endl;
}
cout << endl;
return;
}
/**
* Simple C function to convert a string to a double
* after stripping out unwanted char
*
* credit: http://stackoverflow.com/a/24875936
*
* @param ch The character to strip out
*/
double strToDouble(string str, char ch) {
str.erase(remove(str.begin(), str.end(), ch), str.end());
return atof(str.c_str());
}
/**
* The one and only main() method
*/
int main(int argc, char* argv[]) {
// Declare variable to hold user choice for key and choice for file path
string searchNum;
string filePath;
// Define a binary search tree to hold all courses
BinarySearchTree* bst;
bst = new BinarySearchTree();
// Create empty course
Course course;
string input;
int choice = 0;
while (choice != 9) {
cout << "Welcome To The Course Planner:" << endl << endl;
cout << " 1. Load Data Structure" << endl;
cout << " 2. Print Course List" << endl;
cout << " 3. Print Course" << endl;
cout << " 9. Exit" << endl;
cout << "What would you like to do? ";
getline(cin, input);
// Check if input is integer
// IF NOT, set choice to 0 to go to default switch case
bool isNotInt = false;
for (int i = 0; i < input.size() - 1; i++) {
cout << input.at(i) << endl;
if (!isdigit(input.at(i))) {
choice = 0;
isNotInt = true;
break;
}
}
if (!isNotInt) {
choice = stoi(input);
}
switch (choice) {
case 1:
// Clear scren
system("CLS");
// Get filename to search for
cout << "Enter file name (without .txt): ";
getline(cin, filePath);
// Call the loadCourses function given the filepath name and passing the tree structure
loadCourses(filePath, bst);
break;
case 2:
// Clear screen
system("CLS");
// Call the inOrder function to print all elements
cout << "Course List:" << endl << endl;
bst->InOrder();
cout << endl;
break;
case 3:
// Get user input for desired course number
cout << "Please enter the ID of the course: ";
getline(cin, searchNum);
// One liner to convert each character in input string to uppercase for even comparison since all data is uppercase
for (int i = 0; i < searchNum.length(); i++) { searchNum.at(i) = toupper(searchNum.at(i)); }
// Call search function on binary search tree to find the course with searchNum as its course number
course = bst->Search(searchNum);
// If course is not empty (meaning the course was found), display data
if (!course.courseNum.empty()) {
displayCourse(course);
}
// Else, course not found, output error
else {
cout << "Course number " << searchNum << " not found." << endl;
}
break;
default:
system("CLS");
cout << input << " is not a valid option." << endl;
}
}
cout << "Thank you for using the course planner!" << endl;
return 0;
}