-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonTreeInfo.cpp
More file actions
152 lines (140 loc) · 3.33 KB
/
PersonTreeInfo.cpp
File metadata and controls
152 lines (140 loc) · 3.33 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
/*
* PersonTreeInfo.cpp
*
* Created on: Oct 21, 2020
* Author: 13027
*/
#include "PersonTreeInfo.hpp"
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
PersonTreeInfo::PersonTreeInfo(string name) {
fn = name;
tree = new BST();
readFile();
}
void PersonTreeInfo::Interface() {
char c = 'a';
while (c != 'q') {
cout <<endl<<"****************************************************"<<endl;
cout <<"Choose Option:\n a\tGet Person\n b\tadd a person\n c\tremove a person\n d\tprint tree\n q\tquit"<< endl;
cin >> c;
switch (c) {
case 'a': {
cout << "Enter Last: "<<endl;
string l;
cin >> l;
cout << "Enter First: "<<endl;
string f;
cin >> f;
TNode *t = tree->find(l,f);
if (t == NULL) {
cout << f << " " << l <<" not found in database. " << endl;
}
else {
TNode *s = tree->find(l,f);
s->printNode();
}
break;
}
case 'b':{
string studarr[6];
cout << "Enter Last: "<<endl;
cin >> studarr[0];
cout << "Enter First: " << endl;
cin >> studarr[3];
cout << "Enter Age: " << endl;
if (tree->insert(studarr)) {
cout << endl <<studarr[3] << "\t" << studarr[0]<< " inserted successfully "<<endl;
}
else {
TNode *t = tree->find(studarr[0],studarr[3]);
t->printNode();
cout << "... already in tree with " << endl;
}
break;
}
case 'c': {
cout << "Enter Last: "<<endl;
string l;
cin >> l;
cout << "Enter First: "<<endl;
string f;
cin >> f;
TNode *t = tree->remove(l,f);
if (t == NULL) {
cout << endl << f << " " << l << " not in tree " << endl;
}
else {
t->printNode();
cout << " ...has been removed. "<<endl;
}
break;
}
case 'd': {
cout << "Choose a for pre, b for in, or c for post-order\t";
char k;
cin >> k;
cout << endl;
cout << "************************************************"<<endl;
if (k == 'a') {
cout << "*********** Printing Tree Pre Order ************"<<endl;
cout << "************************************************"<<endl;
tree->printTreePre();
}
else if (k == 'b') {
cout << "*********** Printing Tree In Order *************"<<endl;
cout << "************************************************"<<endl;
tree->printTreeIO();
}
if (k == 'c') {
cout << "*********** Printing Tree Post Order ***********"<<endl;
cout << "************************************************"<<endl;
tree->printTreePost();
}
break;
}
}
}
}
string PersonTreeInfo::strip(string s, string sarr[]) {
const char* WhiteSpace = " \t\v\r\n";
const char* EndChar = ":\t\n";
for (int i = 0; i < 3; i++) {
size_t start = s.find_first_not_of(WhiteSpace);
int len = s.length();
s = s.substr(start,len-start+1);
size_t end = s.find_first_of(EndChar);
if (end > 0){
sarr[i] = s.substr(0,end);
s = s.substr(end+1, len-end);
}
}
return sarr[3];
}
void PersonTreeInfo::readFile() {
ifstream file(fn.c_str());
string word;
string phrase;
string sarr[3];
//file >> word;
if (file.is_open()) {
while (getline(file, phrase)) {
phrase=strip(phrase, sarr);
tree->insert(sarr);
}
file.close();
} else {
cout << "Unable to open file" << endl;
}
/*while (!file.eof()) {
getline(file,phrase);
phrase = strip(phrase, sarr);
tree->insert(sarr);
//file >> word;
}*/
tree->printTreeIO();
return;
}