-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcitydatabase.cpp
More file actions
237 lines (227 loc) · 7.84 KB
/
citydatabase.cpp
File metadata and controls
237 lines (227 loc) · 7.84 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
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include <sstream>
using namespace std;
class city{ // create node for city
public:
string name;
int x;
int y;
city *left;
city *right;
bool deleted;
};
class database{ // binary tree database
// behind the scenes methods for inputting/searching data
private:
city *root;
// input contents of city struct based on parameters from public method
void insert(city *&temp, string name, int x, int y){
if(temp == NULL){
temp = new city();
temp->name = name;
temp->x = x;
temp->y = y;
temp->left = NULL;
temp->right = NULL;
temp->deleted = false;
}
else{ // place node based on city name in comparison to root
if(name < temp->name){
insert(temp->left, name, x, y);
}
else if(name > temp->name){
insert(temp->right, name, x, y);
}
else{
if(temp->deleted == true){
temp->deleted = false;
temp->x = x;
temp->y = y;
}
else{
insert(temp->right, name, x, y);
}
}
}
}
/* input city name and find where it is in the tree by recursively checking each node
based on whether the searched name's value is bigger or smaller than the one being checked */
city *findByName(city *temp, string name){
if(temp != NULL){
if(temp->name == name) return (temp->deleted == false ? temp : findByName(temp->right, name));
else if(name < temp->name) return findByName(temp->left, name);
else if(name > temp->name) return findByName(temp->right, name);
}
else{ // city cannot be found
return NULL;
}
}
/* input x and y coordinates and find those coordinates in the tree by recursively checking
each set of coordinates */
city *findByCoordinates(city *temp, int x, int y){
if(temp == NULL){
return NULL;
}
else{
city *ret = NULL;
if(temp->x == x && temp->y == y){
if(temp->deleted == false){
ret = temp;
}
}
if(ret == NULL){
ret = findByCoordinates(temp->left, x, y);
}
if(ret == NULL){
ret = findByCoordinates(temp->right, x, y);
}
return ret;
}
}
public:
// base constructor for bst
database(){
root = NULL;
}
// transform every inserted name into uppercase for easier searching
void insert(string name, int x, int y){
transform(name.begin(), name.end(), name.begin(), ::toupper);
insert(root, name, x, y);
}
// return the city class that corresponds to the name being searched
void searchByName(string name){
transform(name.begin(), name.end(), name.begin(), ::toupper);
city *found = findByName(root, name);
if(found == NULL){
cout << "No record found for " << name << endl;
}
else{
cout << "Record found for " << found->name << " with coordinates (" << found->x << ", " << found->y << ")" << endl;
}
}
// return the city class that corresponds to the coordinates being searched
void searchByCoordinates(int x, int y){
city *found = findByCoordinates(root, x, y);
if(found == NULL){
cout << "No city found with coordinates (" << x << ", " << y << ")" << endl;
}
else{
cout << "Record found for " << found->name << " with coordinates (" << found->x << ", " << found->y << ")" << endl;
}
}
// find the city class by name and set its deleted attribute to true to no longer see it when searching
void deleteByName(string name){
city *found = findByName(root, name);
if(found == NULL){
cout << "No record found for " << name << endl;
}
else{
cout << "City " << found->name << " found at (" << found->x << ", " << found->y << ") has been deleted" << endl;
found->deleted = true;
}
}
// find the city class by coordinates and set its deleted attribute to true to no longer see it when searching
void deleteByCoordinates(int x, int y){
city *found = findByCoordinates(root, x, y);
if(found == NULL){
cout << "No city found with coordinates (" << x << ", " << y << ")" << endl;
}
else{
cout << "City " << found->name << " found at (" << found->x << ", " << found->y << ") has been deleted" << endl;
found->deleted = true;
}
}
};
int main(int argc, char* argv[]){
ifstream fin;
string src_filename;
// Set up failsafe for correct usage of parameters
if (argc > 2)
{
fprintf( stderr, "\nUsage: Project4 <src_filename>\n" );
fprintf( stderr, " where src_filename contains the file names to be processed\n" );
exit(-1);
}
else if (argc == 2) // filename
src_filename = argv[1];
else // prompt user for input of file
{
cout << "Input an expressions file: ";
cin >> src_filename;
}
// check to make sure file input is correct
fin.open(src_filename);
if (fin.fail())
{
cout << "Input file opening failed. Make sure you input the name of the file that contains the names of files to be processed" << endl;
return 1;
}
// Display opening message
cout << "-----------------------------------------------------" << endl;
cout << "CITY DB: " << src_filename << " opened successfully" << endl;
cout << "-----------------------------------------------------" << endl;
string line; // get set of data
cout << "-----------------------------------------------------" << endl;
cout << "Database Contents:" << endl;
cout << "-----------------------------------------------------" << endl;
database bst; // initialize bst
while (getline(fin,line)) // take a line from cities file
{
stringstream ss(line);
string token, city;
int val = 0, x, y;
cout << line << endl;
while (getline(ss,token,',')) // separate that line of data by its commas
{
if (val == 0) // city
city = token;
else if (val == 1) // x coord
x = stoi(token);
else if (val == 2) // y coord
{
y = stoi(token);
bst.insert(city, x, y); // insert new city into bst
}
val++;
}
}
// Search "engine"
int input;
while (1)
{
cout << "-----------------------------------------------------" << endl;
cout << "Search options: 1 - by City Name, 2 - by Coordinates, 3 - EXIT: ";
cin >> input;
if (input == 1) // Search by City Name
{
string city;
cout << "Enter a city name: ";
cin >> city;
bst.searchByName(city);
}
else if (input == 2) // Search by Coordinates
{
int x,y;
cout << "Enter x coordinate: ";
cin >> x;
cout << "Enter y coordinate: ";
cin >> y;
bst.searchByCoordinates(x,y);
}
else if (input == 3) // EXIT
{
break;
}
else // Some other integer
{
cout << "Incorrect input" << endl;
}
}
cout << "-----------------------------------------------------" << endl;
cout << "End of Run" << endl;
cout << "-----------------------------------------------------" << endl;
return 0;
}