-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
107 lines (94 loc) · 2.74 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
#include "provided.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <functional>
using namespace std;
bool loadDeliveryRequests(string deliveriesFile, GeoCoord& depot, vector<DeliveryRequest>& v);
bool parseDelivery(string line, string& lat, string& lon, string& item);
int main(int argc, char *argv[])
{
if (argc != 3)
{
cout << "Usage: " << argv[0] << " mapdata.txt deliveries.txt" << endl;
return 1;
}
StreetMap sm;
if (!sm.load(argv[1]))
{
cout << "Unable to load map data file " << argv[1] << endl;
return 1;
}
GeoCoord depot;
vector<DeliveryRequest> deliveries;
if (!loadDeliveryRequests(argv[2], depot, deliveries))
{
cout << "Unable to load delivery request file " << argv[2] << endl;
return 1;
}
cout << "Generating route...\n\n";
DeliveryPlanner dp(&sm);
vector<DeliveryCommand> dcs;
double totalMiles;
DeliveryResult result = dp.generateDeliveryPlan(depot, deliveries, dcs, totalMiles);
if (result == BAD_COORD)
{
cout << "One or more depot or delivery coordinates are invalid." << endl;
return 1;
}
if (result == NO_ROUTE)
{
cout << "No route can be found to deliver all items." << endl;
return 1;
}
cout << "Starting at the depot...\n";
for (const auto& dc : dcs)
cout << dc.description() << endl;
cout << "You are back at the depot and your deliveries are done!\n";
cout.setf(ios::fixed);
cout.precision(2);
cout << totalMiles << " miles travelled for all deliveries." << endl;
}
bool loadDeliveryRequests(string deliveriesFile, GeoCoord& depot, vector<DeliveryRequest>& v)
{
ifstream inf(deliveriesFile);
if (!inf)
return false;
string lat;
string lon;
inf >> lat >> lon;
inf.ignore(10000, '\n');
depot = GeoCoord(lat, lon);
string line;
while (getline(inf, line))
{
string item;
if (parseDelivery(line, lat, lon, item))
v.push_back(DeliveryRequest(item, GeoCoord(lat, lon)));
}
return true;
}
bool parseDelivery(string line, string& lat, string& lon, string& item)
{
const size_t colon = line.find(':');
if (colon == string::npos)
{
cout << "Missing colon in deliveries file line: " << line << endl;
return false;
}
istringstream iss(line.substr(0, colon));
if (!(iss >> lat >> lon))
{
cout << "Bad format in deliveries file line: " << line << endl;
return false;
}
item = line.substr(colon + 1);
if (item.empty())
{
cout << "Missing item in deliveries file line: " << line << endl;
return false;
}
return true;
}