Here is my code. City.h #include<iostream> using namespace std; const int MAX =
ID: 644783 • Letter: H
Question
Here is my code.
City.h
#include<iostream>
using namespace std;
const int MAX = 5;
struct City{
char cityName[20];
int position;
int weight[MAX];
bool mark;
};
class Graph{
public:
void loadFromFile();
int showDepartureCities();
int showDestinationCities(int);
void directConnections(int, int);
private:
City GraphList[MAX];
};
////////////////////////////////////////////////////////////////
City.cpp
#include<iostream>
#include<fstream>
#include "City.h"
using namespace std;
void Graph:: loadFromFile()
{
ifstream loadFile;
loadFile.open("load.txt");
for (int i = 0; i <= MAX-1; i++)
{
GraphList[i].position = i;
GraphList[i].mark = false;
loadFile >> GraphList[i].cityName;
for (int j = 0; j <= 4; j++)
{
loadFile >> GraphList[i].weight[j];
}
}
}
int Graph::showDepartureCities()
{
int departureChoice;
cout << "Departure cities" << endl;
cout << "=================="<<endl;
for (int i = 0; i <= MAX-1; i++)
{
cout << i+1<<"."<<GraphList[i].cityName << endl;
}
cout << "Choose city: ";
cin >> departureChoice;
return departureChoice;
}
int Graph::showDestinationCities(int depCityIdx)
{
int destinationChoice;
cout << endl;
cout << "Destination cities" << endl;
cout << "===================" << endl;
for (int i = 0; i <= MAX-1; i++)
{
if (i+1 != depCityIdx)
{
cout <<i+1<<"."<< GraphList[i].cityName << endl;
}
}
cout << "Choose city: ";
cin >> destinationChoice;
return destinationChoice;
}
void Graph::directConnections(int depCh, int destCh)
{
if (GraphList[depCh - 1].weight[destCh - 1] == 0)
{
cout << "No direct connection between " << GraphList[depCh - 1].cityName << " and " << GraphList[destCh - 1].cityName << endl;
}
else
{
cout << "Direct connection between " << GraphList[depCh - 1].cityName << " and " << GraphList[destCh - 1].cityName << " is " << GraphList[depCh - 1].weight[destCh - 1]<< endl;
}
}
////////////////////////////////////////////////////////////////////////////////////
test.cpp
#include<iostream>
#include<string>
#include "City.h"
using namespace std;
int main()
{
Graph cityList;
int choice;
cityList.loadFromFile();
do
{
cout << " Menu ";
cout << "==================== ";
cout << "1.Choose departure city ";
cout << "2.Exit ";
cout << "Enter your choice: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1: int depChoice, destChoice;
depChoice=cityList.showDepartureCities();
destChoice = cityList.showDestinationCities(depChoice);
cityList.directConnections(depChoice, destChoice);
cityList.throughConnection(depChoice);
break;
case 2:
break;
}
} while (choice != 2);
return 0;
}
////////////////////////////////////////////////////////////
load.txt file
Fargo 0 1426 583 0 0
Seattle 1426 0 0 0 0
Topeka 583 0 0 1188 0
Philladelphia 0 0 1188 0 584
Detroit 0 0 0 584 0
///////////////////////////////////////////////////////////////
for finishing my programm it also shoud check this
through connection
If there is no through connection between the departure and destination cities (in other words, only a direct connection), then the program will output:
No through connection between [name of departure city] and [name of destination city]
Press any key to return to menu [after which the menu displayed at program startup is re-displayed]
If there is at least one through connection between the departure and destination cities, then the program will output all through connections (there may be more than one) in ascending order of distance, listing the intermediate cities, as follows:
Through connection between [name of departure city] and [name of destination city] via [list name or names of cities in between] - [number of miles] miles
After this listing, the program prompts the user to "Press any key to return to menu", after which the menu displayed at program startup is re-displayed.
Depth-First or Breadth-First methods of searching should be used.
?
?
?
Explanation / Answer
#include
#include
using namespace std;
const int MAX = 5;
struct City{
char cityName[20];
int position;
int weight[MAX];
bool mark;
};
class Graph{
public:
Graph::Graph(int V)
{
this->V = V;
adj = new list[V];
}
void loadFromFile();
int showDepartureCities();
int showDestinationCities(int);
void directConnections(int, int);
bool throughConnections(int,int);
void addEdge(int,int);
private:
City GraphList[MAX];
int V; // No. of vertices
list *adj; // Pointer to an array containing adjacency lists
}
*********************
#include
#include
#include "City.h"
using namespace std;
void Graph:: loadFromFile()
{
ifstream loadFile;
loadFile.open("load.txt");
for (int i = 0; i <= MAX-1; i++)
{
GraphList[i].position = i;
GraphList[i].mark = false;
loadFile >> GraphList[i].cityName;
for (int j = 0; j <= 4; j++)
{
loadFile >> GraphList[i].weight[j];
}
}
}
int Graph::showDepartureCities()
{
int departureChoice;
cout << "Departure cities" << endl;
cout << "=================="<
for (int i = 0; i <= MAX-1; i++)
{
cout << i+1<<"."<
}
cout << "Choose city: ";
cin >> departureChoice;
return departureChoice;
}
int Graph::showDestinationCities(int depCityIdx)
{
int destinationChoice;
cout << endl;
cout << "Destination cities" << endl;
cout << "===================" << endl;
for (int i = 0; i <= MAX-1; i++)
{
if (i+1 != depCityIdx)
{
cout <
}
}
cout << "Choose city: ";
cin >> destinationChoice;
return destinationChoice;
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.