-
Notifications
You must be signed in to change notification settings - Fork 2
/
airlinepath.cpp
380 lines (333 loc) · 12 KB
/
airlinepath.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include "airlinepath.h"
airlinePath::airlinePath()
{
finalXML = QDomDocument();
filePath = QString();
beginCode = endCode = std::string();
beginID = endID = distance = err = NULL;
errMsg[0] = "FATAL ERROR";
errMsg[1] = "Cannot open XML file!";
errMsg[2] = "Cannot parse XML file!";
errMsg[3] = "Invalid beginning IATA code!";
errMsg[4] = "Invalid ending IATA code!";
errMsg[5] = "Invalid IATA codes!";
errMsg[6] = "Cannot travel to the same location!";
}
airlinePath::~airlinePath()
{
table.clear();
finalXML = QDomDocument();
filePath = QString();
beginCode = endCode = std::string();
beginID = endID = distance = totalCost = err = NULL;
}
void airlinePath::openXML(const QString &filename)
{
QFile file(filename);
if(!file.open(QIODevice::ReadOnly))
{
err = 1; // Can't open file
return;
}
if(!finalXML.setContent(&file))
{
err = 2; // Can't parse file
return;
}
if(!err)
loadMap();
}
void airlinePath::run()
{
if(err)
displayError();
else
do
{
getInput();
findInitialPath();
findShortestPath();
display();
}while(doAgain());
}
void airlinePath::loadMap()
{
// Obtain a list of of each <airport> element
QDomNodeList portElements = finalXML.documentElement().elementsByTagName("airport");
for(size_t i = 0; i < portElements.size(); i++)
{
// Current <airport>
QDomNode currAirport = portElements.item(i);
vertex* portPtr = new vertex();
// Load necessary <airport> information
portPtr->airportID = currAirport.firstChildElement("id").text().toInt();
portPtr->name = currAirport.firstChildElement("name").text().toStdString();
portPtr->city = currAirport.firstChildElement("city").text().toStdString();
portPtr->iata = currAirport.firstChildElement("iata").text().toStdString();
portPtr->fare = currAirport.firstChildElement("fare").text().toFloat();
// Obtain a list of each <route> element
QDomNodeList routeElements = currAirport.firstChildElement("routes").elementsByTagName("route");
for(size_t j = 0; j < routeElements.size(); j++)
{
// Current <route>
QDomNode currRoute = routeElements.at(j);
route* routePtr = new route();
// Load necessary <route> information
routePtr->destinationID = currRoute.firstChildElement("destinationAirportID").text().toInt();
routePtr->distance = currRoute.firstChildElement("distance").text().toDouble();
// Obtain a list of each <airline> element
QDomNodeList lineElements = currRoute.firstChildElement("airlines").elementsByTagName("airline");
for(size_t k = 0; k < lineElements.size(); k++)
{
// Current <airline>
QDomNode currLine = lineElements.at(k);
airline* linePtr = new airline();
// Load necessary <airlines> information
linePtr->id = currLine.firstChildElement("airlineid").text().toInt();
linePtr->name = currLine.firstChildElement("name").text().toStdString();
linePtr->iata = currLine.firstChildElement("iata").text().toStdString();
// Route contains a number of airlines to choose from
routePtr->airlines.push_back(linePtr);
}
// Airport contains a number of routes
portPtr->routes.push_back(routePtr);
}
// Adjacency table contains the vertex and subsequently the destinations
table.insert(std::pair<int, vertex*>(portPtr->airportID, portPtr));
}
}
void airlinePath::clear()
{
beginCode = endCode = std::string();
beginID = endID = distance = totalCost = err = NULL;
path.clear();
for(std::map<int, vertex*>::iterator it = table.begin(); it != table.end(); it++)
{
vertex* ptr = it->second;
ptr->visited = false;
}
}
void airlinePath::getInput()
{
do
{
clear();
getIATA();
if(!validInput())
displayError();
}while(err);
}
void airlinePath::getIATA()
{
std::cout<<"Where are you flying from?: ";
getline(std::cin, beginCode);
std::cout<<"Where are you flying to?: ";
getline(std::cin, endCode);
std::cout<<std::endl;
}
bool airlinePath::doAgain()
{
std::string str;
do
{
std::cout<<"Do you want to do this again? (Y/N): ";
getline(std::cin, str);
str[0] = toupper(str[0]);
std::cout<<std::endl;
}while(str.length() != 1 && (str[0] != 'Y' || str[0] != 'N'));
return str[0] == 'Y';
}
void airlinePath::trimAndUpper(std::string &line)
{
// Remove leading spaces
while(line[0] == ' ')
line.erase(0,1);
// Remove trailing spaces
while(line[line.size()-1] == ' ')
line.erase(line.size()-1,1);
// To Upper
for(int i = 0; i < line.length(); i++)
line.at(i) = toupper(line.at(i));
}
bool airlinePath::validInput()
{
trimAndUpper(beginCode);
trimAndUpper(endCode);
// Match codes with ID
for(std::map<int, vertex*>::iterator it = table.begin(); it != table.end(); it++)
{
if(((vertex*)it->second)->iata == beginCode)
{
beginID = it->first;
if(endID)
break;
}
if(((vertex*)it->second)->iata == endCode)
{
endID = it->first;
if(beginID)
break;
}
}
err = beginID == 0 ? 3 : err; // Invalid begin id
err = endID == 0 ? 4 : err; // Invalid end id
err = beginID == endID ? 5 : err; // Both invalid id
err = err == 5 && beginID > 0 ? 6 : err; // Travel to same place
return err == 0;
}
void airlinePath::findInitialPath()
{
int fromID = beginID;
// Continue finding the a path until we have reached the destination
while(fromID != endID)
{
// Push starting destination into the path stack
path.push_back(fromID);
// When at an airport, set it as visited
vertex* currPort = table[fromID];
currPort->visited = true;
// List of all possible routes from the airport
std::vector<route*> options;
for(int i = 0; i < currPort->routes.size(); i++)
options.push_back(((route*)currPort->routes[i]));
// Find best route (shortest distance)
int bestID = -1;
double distance = DBL_MAX;
for(int i = 0; i < options.size(); i++)
{
double dist = ((route*)options.at(i))->distance;
int id = ((route*)options.at(i))->destinationID;
// Best route must have a shorter distance, unvisited, and route options from it
if(distance > dist && !table.at(id)->visited && table.at(id)->routes.size())
{
distance = dist;
bestID = id;
}
}
// No (best) route: go back and pick another destination
if(bestID == -1)
{
path.pop_back();
fromID = path.at(path.size()-1);
path.pop_back();
continue;
}
fromID = bestID;
}
// Insert destination ID
path.push_back(fromID);
}
void airlinePath::findShortestPath()
{
/* S 1 2 3 4 5 E
---------
if 4 -> E direct: remove everything in between 5
---------
if 3 -> E direct: remove everything in between 4, 5
else if 3 -> 5 direct: remove everything in between 4
---------
if 2 -> E direct: remove everything in between 3, 4, 5
else if 2 -> 5 direct: remove everything in between 3, 4
else if 2 -> 4 direct: remove everything in between 3
---------
and so on...
*/
int toIndex, // Index of "E" to fromIndex+1
fromIndex; // Index from "5" to "S"
// fromIndex starts before toIndex and only decrements when it meets toIndex
for(fromIndex = path.size()-2; fromIndex != -1; fromIndex--)
{
// toIndex starts at the end, and decrements down to meet fromIndex
for(toIndex = path.size()-1; toIndex > fromIndex; toIndex--)
{
// Already a direct route
if(fromIndex == toIndex-1)
continue;
vertex *fromV = table.at(path.at(fromIndex)),
*toV = table.at(path.at(toIndex));
// Check source routes for a direct route from fromIndex to toIndex
for(int i = 0; i < fromV->routes.size(); i++)
{
if(((route*)fromV->routes.at(i))->destinationID == path.at(toIndex))
{
// Erase everying in between the newly found direct route
path.erase(path.begin() + fromIndex+1, path.begin() + toIndex);
// Restart the process
fromIndex = path.size()-2;
toIndex = path.size()-1;
break;
}
}
}
}
}
void airlinePath::display()
{
std::string toPortMsg[] = { "- Leave ", "- Board ", "- Enter " },
toLineMsg[] = { " on ", " and board ", " onto " },
distMsg[] = { " and fly ", " for ", " after flying " },
multiLine = " and choose between ",
destPort = "- Finally, you will arrive at ";
vertex *airport, *next;
bool costError = 0;
// Display cities from and to
std::cout<<"From "<<table.at(beginID)->city<<" to "<<table.at(endID)->city<<std::endl;
for(int i = 0; i < path.size()-1; i++)
{
// Current airports
airport = table.at(path.at(i));
next = table.at(path.at(i+1));
// Display airport information
std::cout<<toPortMsg[i%3]<<"("<<airport->iata<<") "<<airport->name<<", "<<airport->city;
// Find matching route to the next destination
for(int i = 0; i < airport->routes.size(); i++)
{
// Display airline information the given route
route* rPtr = airport->routes.at(i);
if(rPtr->destinationID == next->airportID)
{
// Multiple airlines
if(rPtr->airlines.size() >= 2)
{
airline* aPtr = rPtr->airlines[0];
std::cout<<multiLine;
for(int i = 0; i < rPtr->airlines.size()-1; i++)
{
std::cout<<"("<<aPtr->iata<<") "<<aPtr->name<<", ";
}
aPtr = rPtr->airlines[rPtr->airlines.size()-1];
std::cout<<"and ""("<<aPtr->iata<<") "<<aPtr->name;
}
// Single airline
else
{
airline* aPtr = rPtr->airlines[0];
std::cout<<toLineMsg[i%3]<<"("<<aPtr->iata<<") "<<aPtr->name;
}
// Display distance
std::cout<<distMsg[i%3]<<rPtr->distance * (float)0.0006<<" miles. ";
// Display cost
if(airport->fare > 0)
std::cout<<"($"<<airport->fare<<")";
else
costError = 1;
std::cout<<std::endl;
// Add to total distance and cost
distance += rPtr->distance;
totalCost += airport->fare;
break;
}
}
}
// Final destination
std::cout<<destPort<<"("<<next->iata<<") "<<next->name<<std::endl<<std::endl;
// Total distance and cost
std::cout<<"There will be a total of "<<path.size()<<" airports to stop at."<<std::endl;
std::cout<<"You will have traveled a total of: "<<distance * (float)0.0006<<" miles."<<std::endl;
if(!costError)
std::cout<<"The total cost for this trip will be $"<<totalCost<<std::endl<<std::endl;
}
void airlinePath::displayError()
{
std::cout<<errMsg[err]<<std::endl<<std::endl;
}