-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
157 lines (141 loc) · 5.17 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
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
#include "provided.h"
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
using namespace std;
// START OF WHAT YOU CAN REMOVE ONCE YOU'VE IMPLEMENTED string directionOfLine(const GeoSegment& gs)
// If you want the turn-by-turn directions to give a real direction like
// east or southwest instead of IN_SOME_DIRECTION, you'll need to
// implement the ordinary function
// string directionOfLine(const GeoSegment& gs)
// to return a string like "east" or "southwest" based on the angle of the
// GeoSegment gs according to the table at the bottom of page 20 of the spec.
// When you do that, you can delete this comment and the template function
// below that appears here solely to allow this main.cpp to build.
// Why didn't we just write the real function for you? Because it's also
// a function you'd find useful in producing the NavSegments in the navigate()
// method. Since it's useful in more than one .cpp file, its declaration
// should go in support.h and its implementation in support.cpp.
template<typename T>
string directionOfLine(const T&)
{
return "IN_SOME_DIRECTION";
}
void printDirectionsRaw(string start, string end, vector<NavSegment>& navSegments);
void printDirections(string start, string end, vector<NavSegment>& navSegments);
int main(int argc, char *argv[])
{
bool raw = false;
if (argc == 5 && strcmp(argv[4], "-raw") == 0)
{
raw = true;
argc--;
}
if (argc != 4)
{
cout << "Usage: BruinNav mapdata.txt \"start attraction\" \"end attraction name\"" << endl
<< "or" << endl
<< "Usage: BruinNav mapdata.txt \"start attraction\" \"end attraction name\" -raw" << endl;
return 1;
}
Navigator nav;
if ( ! nav.loadMapData(argv[1]))
{
cout << "Map data file was not found or has bad format: " << argv[1] << endl;
return 1;
}
if ( ! raw)
cout << "Routing..." << flush;
string start = argv[2];
string end = argv[3];
vector<NavSegment> navSegments;
NavResult result = nav.navigate(start, end, navSegments);
if ( ! raw)
cout << endl;
switch (result)
{
case NAV_NO_ROUTE:
cout << "No route found between " << start << " and " << end << endl;
break;
case NAV_BAD_SOURCE:
cout << "Start attraction not found: " << start << endl;
break;
case NAV_BAD_DESTINATION:
cout << "End attraction not found: " << end << endl;
break;
case NAV_SUCCESS:
if (raw)
printDirectionsRaw(start, end, navSegments);
else
printDirections(start, end, navSegments);
break;
}
}
void printDirectionsRaw(string start, string end, vector<NavSegment>& navSegments)
{
cout << "Start: " << start << endl;
cout << "End: " << end << endl;
cout.setf(ios::fixed);
cout.precision(4);
for (auto ns : navSegments)
{
switch (ns.m_command)
{
case NavSegment::PROCEED:
cout << ns.m_geoSegment.start.latitudeText << ","
<< ns.m_geoSegment.start.longitudeText << " "
<< ns.m_geoSegment.end.latitudeText << ","
<< ns.m_geoSegment.end.longitudeText << " "
<< ns.m_direction << " "
<< ns.m_distance << " "
<< ns.m_streetName << endl;
break;
case NavSegment::TURN:
cout << "turn " << ns.m_direction << " " << ns.m_streetName << endl;
break;
}
}
}
void printDirections(string start, string end, vector<NavSegment>& navSegments)
{
cout.setf(ios::fixed);
cout.precision(2);
cout << "You are starting at: " << start << endl;
double totalDistance = 0;
string thisStreet;
GeoSegment effectiveSegment;
double distSinceLastTurn = 0;
for (auto ns : navSegments)
{
switch (ns.m_command)
{
case NavSegment::PROCEED:
if (thisStreet.empty())
{
thisStreet = ns.m_streetName;
effectiveSegment.start = ns.m_geoSegment.start;
}
effectiveSegment.end = ns.m_geoSegment.end;
distSinceLastTurn += ns.m_distance;
totalDistance += ns.m_distance;
break;
case NavSegment::TURN:
if (distSinceLastTurn > 0)
{
cout << "Proceed " << distSinceLastTurn << " miles "
<< directionOfLine(effectiveSegment) << " on " << thisStreet << endl;
thisStreet.clear();
distSinceLastTurn = 0;
}
cout << "Turn " << ns.m_direction << " onto " << ns.m_streetName << endl;
break;
}
}
if (distSinceLastTurn > 0)
cout << "Proceed " << distSinceLastTurn << " miles "
<< directionOfLine(effectiveSegment) << " on " << thisStreet << endl;
cout << "You have reached your destination: " << end << endl;
cout.precision(1);
cout << "Total travel distance: " << totalDistance << " miles" << endl;
}