-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
109 lines (93 loc) · 2.59 KB
/
main.cpp
File metadata and controls
109 lines (93 loc) · 2.59 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
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include "StudentTree.h"
#include <algorithm>
using namespace std;
/// Handle commands
void processCommand(StudentTree*& root, const string& commandLine) {
istringstream iss(commandLine);
string command;
iss >> command;
/// Insert command
if (command == "insert") {
string name, idStr;
iss >> quoted(name) >> idStr;
root = root->insert(root, Student(name, idStr));
}
/// PrintInorder command
else if (command == "printInorder") {
ostringstream oss;
root->printInOrder(root, oss);
string output = oss.str();
cout <<output<< endl;
}
/// PrintPreorder command
else if (command == "printPreorder") {
ostringstream oss;
root->printPreOrder(root, oss);
string output = oss.str();
cout <<output<< endl;
}
/// PrintPostorder command
else if (command == "printPostorder") {
ostringstream oss;
root->printPostOrder(root, oss);
string output = oss.str();
cout <<output<< endl;
}
/// PrintLevelCount command
else if (command == "printLevelCount") {
root->printLevels(root);
cout << endl;
}
/// Remove command
else if (command == "remove") {
string idStr;
iss >> idStr;
root = root->removeID(root, idStr);
}
/// RemoveInorder command
else if (command == "removeInorder") {
int n;
iss >> n;
root->removeNthInorder(root, n);
}
/// Search command
else if (command == "search") {
string query;
// Handle quotation strings
if (!(iss >> std::quoted(query))) {
cout << "unsuccessful" << endl;
return;
}
// Check if is numeric iterating through all the string
bool isNumeric = !query.empty() && all_of(query.begin(), query.end(), ::isdigit);
// If numeric, calls searchID
if (isNumeric) {
root->searchID(root, query);
}
// Else, calls searchName
else {
root->searchName(root, query);
}
} else {
cout << "unsuccessful" << endl;
}
}
int main() {
// Empty tree
StudentTree* root = nullptr;
// Number of commands
int numberOfCommands;
cin >> numberOfCommands;
cin.ignore();
// Loop according to the number of commands, and execute them with processCommand
string line;
for (int i = 0; i < numberOfCommands; i++) {
getline(cin, line);
processCommand(root, line);
}
return 0;
}