-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectTwo.cpp
More file actions
300 lines (264 loc) · 8.24 KB
/
ProjectTwo.cpp
File metadata and controls
300 lines (264 loc) · 8.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
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <memory>
#include <sstream>
using namespace std;
//global variable holds path to csv file
string csvPath;
//structure to store course information
struct course
{
string courseNumber;
string name;
vector<string> prerequisites;
//adds a prerequisite to the course's list of prerequisites
void addPrereq(string preReq)
{
prerequisites.push_back(preReq);
}
course() : courseNumber(""), name(""), prerequisites() {}
};
//Node structure which holds a course object in the tree
struct Node
{
course nodeCourse; //course associated with node
Node* left; //pointer to left child
Node* right; //pointer to right child
//Default node constructor initializes pointers to null
Node()
{
left = nullptr;
right = nullptr;
}
//Node constructor with course input parameter
Node(course aCourse):Node()
{
nodeCourse = aCourse;
}
};
//BinarySearchTree class
class BinarySearchTree
{
private:
//adds nodes to the tree
void addNode(Node* node, course course);
public:
Node* root;
//default constructor
BinarySearchTree();
//Inserts a course into a node
void Insert(course course);
//finds a specific course by course number
void searchCourse(string courseNumber);
};
//default constructor with root initialized null
BinarySearchTree::BinarySearchTree()
{
root = nullptr;
}
//Inserts a specified course into a node
void BinarySearchTree::Insert(course course)
{
//inserts the course into the root node if null, otherwise adds it with addNode function
if (root == nullptr)
{
root = new Node(course);
}
else
{
this->addNode(root, course);
}
}
//adds a specific node to the tree with the specified course
void BinarySearchTree::addNode(Node* node, course course)
{
//finds the proper insertion location for the node based on course number
if (node->nodeCourse.courseNumber > course.courseNumber)
{
if (node -> left == nullptr)
{
node->left = new Node(course);
}
else
{
addNode(node->left, course);
}
}
else
{
if (node->right == nullptr)
{
node->right = new Node(course);
}
else
{
addNode(node->right, course);
}
}
}
//finds the specified course based on course number
void BinarySearchTree::searchCourse(string courseNumber)
{
// traverses the tree to compare course numbers with the provided course number
Node* current = root;
while (current != nullptr)
{
if (current->nodeCourse.courseNumber.compare(courseNumber) == 0)
{
cout << current->nodeCourse.courseNumber << ", " << current->nodeCourse.name << endl;
if (current->nodeCourse.prerequisites.size() != 0)
{
for (int i = 0; i < current->nodeCourse.prerequisites.size(); i++)
{
cout << "Prerequisites: " << current->nodeCourse.prerequisites[i];
}
}
cout << "\n";
return;
}
else if (current->nodeCourse.courseNumber.compare(courseNumber) < 0)
{
current = current->right;
}
else
{
current = current->left;
}
}
}
//traverses the tree from left to right (lowest course number to greatest)
void inOrderTraversal (Node* node)
{
//returns null if the node is empty, otherwise recursively calls itself to traverse from left to right
//traversal left to right prints the courses from lowest to highest course number
if (node == nullptr)
{
return;
}
inOrderTraversal(node->left);
cout << node->nodeCourse.courseNumber << ", " << node->nodeCourse.name << endl;
inOrderTraversal(node->right);
}
//loads the courses into the specified tree from the specified file
void loadCourses(string& csvPath, BinarySearchTree* tree)
{
ifstream inputFile(csvPath);
//checks to see if file is open
if (!inputFile.is_open())
{
cout << "Error opening file." << endl;
exit(EXIT_FAILURE);
}
string line;
vector<string> numbers;
//while there are lines to read...
while (getline(inputFile, line))
{
bool found = false;
//checks to make sure every course has the proper amount of parameters
if (count(line.begin(), line.end(), ',') < 2)
{
cout << "There is a missing parameter for a course." << endl;
exit(EXIT_FAILURE);
}
course c;
istringstream stream(line);
getline(stream, c.courseNumber, ',');
numbers.push_back(c.courseNumber);
getline(stream, c.name, ',');
//adds the prerequisites to the vector, then adds the course to the tree
while (getline(stream, line))
{
found = false;
c.addPrereq(line);
tree->Insert(c);
}
//breaks the loop if end of file reached
if (inputFile.eof() == true)
{
break;
}
}
//closes input file
inputFile.close();
}
//prompts the menu with the screen
void printMenu()
{
cout << "\n\t1. Load Data Structure." << endl;
cout << "\t2. Print Course List." << endl;
cout << "\t3. Print Course." << endl;
cout << "\t9. Exit" << endl;
cout << "\nWhat would you like to do? ";
}
//uses a switch to allow functionality of the menu
void menuFunctionality(BinarySearchTree* tree)
{
string searchId;
cout << "Welcome to the course planner." << endl;
int input = 0;
bool loaded = false;
while (input != 9)
{
printMenu();
cin >> input;
//switch allows for the functionality of the menu based on user input
switch (input)
{
//Prompts user for file name and calls load courses function with provided file name
case 1:
cin.ignore();
cout << "\nPlease enter the file name to load: " << endl;
getline(cin, csvPath);
loadCourses(csvPath, tree);
cout << "\n" << csvPath << " has been loaded." << endl;
loaded = true;
break;
//will not work if the file has not been read, otherwise prints the contents in order
case 2:
if (loaded == false)
{
cout << "\nThe data structure must be loaded first." << endl;
break;
}
else
{
cout << "\nHere is a sample schedule: " << endl;
cout << "\n" << endl;
inOrderTraversal(tree->root);
break;
}
case 3:
//will not work if the file has not been read, otherwise allows user to search for a specific course with course number
if (loaded == false)
{
cout << "\nThe data structure must be loaded first." << endl;
break;
}
else
{
cin.ignore();
cout << "\nWhat course do you want to know about? (Case sensitive) ";
cin >> searchId;
cout << "\n";
tree->searchCourse(searchId);
break;
}
case 9:
cout << "Thank you for using the course planner!" << endl;
break;
default:
cout << input << " is not a valid option." << endl;
}
}
}
// main function
int main()
{
BinarySearchTree tree;
menuFunctionality(&tree);
}
//file name is stored below for easy copy and paste debugging
//CS 300 ABCU_Advising_Program_Input.csv