Navigate Map

Build in memory, as a structure of interconnected nodes, the graph described by these files:

http://rabbit.eng.miami.edu/class/een318/intersections.txt

http://rabbit.eng.miami.edu/class/een318/connections.txt

http://rabbit.eng.miami.edu/class/een318/geog.txt

Your program should then ask the user to specify a starting location, and allow him/her/itto interactively navigate through the map.

example

Location to start: 19999

Location 19999, 0.66 miles from Shelby, NC

roads leading away:

1: NC-18, 1.005 miles to location 19977

2: US-74, 2.521 miles to location 19991

3: US-74, 3.749 miles to location 20006

4: NC-18, 7.565 miles to location 20159

take which road? 3

Location 20006, 1.85 miles from Light Oak, NC

roads leading away:

1: bus-US-74, 3.812 miles to location 19977

2: US-74, 3.749 miles to location 19999

3: US-74, 4.805 miles to location 20022

take which road? 1

Location 19977, … etc. 

Solution 

Connection.h 

#ifndef __CONNECTION_H__

#define __CONNECTION_H__

#include <string>

#include <sstream>

using namespace std;

class Connection {

private:

string name;

intbeg_index;

intend_index;

double distance;

public:

Connection(string s, int beg, int end, double dist) {name = s; beg_index = beg; end_index = end; distance = dist;};

stringto_string()

{

stringstreamss;

ss<< name << “, ” << distance << ” miles to location ” <<end_index;

returnss.str();

};

intget_beg_index() {return beg_index;};

intget_end_index() {return end_index;};

};

#endif 

graph.cpp 

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include “Connection.h”

#include “Location.h”

using namespace std;

int main()

{

int start;

vector<Location> locations;

doublelon, lat, dist;

string state, place, dummy;

intbeg_index, end_index, loc_index = 0;

ifstreamloc_file(“intersections.txt”);

getline(loc_file, dummy); // skip the first line

while (loc_file>>lon>>lat>>dist>> state)

{

getline(loc_file, place);

Location loc(loc_index, place, state, dist);

locations.push_back(loc);

loc_index++;

}

loc_file.close();

cout<<locations.size() << ” locations read” <<endl;

ifstreamconn_file(“connections.txt”);

while (conn_file>> place >> dummy >>beg_index>>end_index>>dist)

{

// add connections both for beg_index and end_index locations

Connection conn1(place, beg_index, end_index, dist);

locations[beg_index].add_connection(conn1);

Connection conn2(place, end_index, beg_index, dist);

locations[end_index].add_connection(conn2);

}

conn_file.close();

cout<< “Location to start: “;

cin>>loc_index;

cout<<locations.size() <<endl;

if (loc_index>= 0 &&loc_index<locations.size())

{

while (true) {

// vector of current location’s connections

vector<Connection>current_conn = locations[loc_index].get_connections();

cout<< locations[loc_index].to_string() <<endl;

cout<< “roads leading away:” <<endl;

// list roads

for (int i = 0; i <current_conn.size(); i++) {

cout<< ”  ” << i + 1 << “: ” <<current_conn[i].to_string() <<endl;

}

// get the road from user (make sure it is valid)

intconn_index = 0;

while (conn_index< 1 || conn_index>current_conn.size()) {

cout<< “take which road? “;

cin>>conn_index;

if (conn_index< 1 || conn_index>current_conn.size())

cout<< “Invalid index” <<endl;

}

// move to the new location

loc_index = current_conn[conn_index – 1].get_end_index();

cout<<endl;

}

}

else

{

cout<< “Invalid location index” <<endl;

}

return 0;

} 

Location.h 

#ifndef __LOCATION_H__

#define __LOCATION_H__

#include <vector>

#include <string>

#include <sstream>

using namespace std;

class Location {

private:

int index;

string place;

string state;

double distance;

vector<Connection> connections;

public:

Location(intind, string pl, string st, double dist) {index = ind; place = pl; state = st; distance = dist;};

stringto_string()

{

stringstreamss;

ss<< “Location ” << index << ” ” << distance << ” miles from ” << place << “, ” << state;

returnss.str();

};

voidadd_connection(Connection conn) {connections.push_back(conn);};

vector<Connection>get_connections() {return connections;};

};

#endif