-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
96 lines (77 loc) · 2.25 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Splaytree.h"
void readFile(std::vector<double> &number, std::string &fileName){
std::ifstream inFile(fileName);
std::string line;
while (std::getline(inFile, line)) {
std::vector <double> temp;
std::istringstream stream(line);
double val;
while(stream >> val) {
number.push_back(val);
}
}
}
int menu(){
//prints the menu and gets the user input for menu options and returns it
//will only return valid options
int choice = 0;
while(choice < 1 || choice > 5){
std::cout << "1. Insert Number" << std::endl;
std::cout << "2. Search for number" << std::endl;
std::cout << "3. Print level order" << std::endl;
std::cout << "4. Read numbers from file" << std::endl;
std::cout << "5. Quit" << std::endl;
std::cin >> choice;
}
return choice;
}
int main(int argc, char *argv[]){
std::string file_name(argv[1]);
std::vector<double> number;
readFile(number, file_name);
Splaytree tree;
int choice = 0;
while(choice != 5){
choice = menu();
if(choice == 1){
//insert number
std::cout << "Enter a number: ";
int num;
std::cin >> num;
tree.insert(num);
}
else if( choice == 2){
//search number
std::cout << "Enter a number to search for: ";
int num;
std::cin >> num;
bool results = tree.search(num);
if(results){
std::cout << "True" << std::endl;
}
else{
std::cout << "False, No number exists" << std::endl;
}
}
else if(choice == 3){
//tree level order
std::cout << "------ Level Order ------"<< std::endl;
tree.levelOrder();
std::cout << "-------------------------" << std::endl;
}
else if(choice == 4){
//read numbers from file
for(auto num : number){
tree.insert(num);
}
}
else{
std::cout << "Goodbye" << std::endl;
}
}
return 0;
}