Can you please debug this Thank you in advance This program
Can you please debug this? Thank you in advance! This program is supposed to use stacks and find the paths of an airplane. It should tell you whether or not the plane goes from the requested city to the requested destination. requestFile.txt contains the requests, flightFile.txt contains the flights and cityFile.txt contains the city\'s that the airline serves.
My errors: my output is simply \"Hpair does not serve Los Angeles.\" Thats it and it stops.
It should look something like (these are not real values of the file just an example):
\"Request is to fly from cityA to CityB.
Hpair flight from cityA to CityB.
Request is to fly from cityC to cityD.
Sorry, HPAir does not serve City D
Request is to fly from cityE to CityF
Sorry, HPAir does not fly from cityE to CityF\"
**It said to use ispath.cpp but I got confused on how to implement it completely.\"
isPath.cpp
//***I did not include ispath.cpp in my program. Parts of it are in the map.h portion.
main.cpp
#include
#include
#include \"Map.h\"
using namespace std;
int main()
{
Map aMap;
aMap.readFlights();
aMap.verifyRequestedFile();
}
StackInterface.h
#ifndef StackInterface_h
#define StackInterface_h
template
class StackInterface
{
/** Checks if the stack is empty.
@return True if the stack is empty or false if the stack is not.*/
virtual bool isEmpty() const = 0;
/** Adds a new item to the stack
@post newEntry is at the top of the stack
@param newEntry is the object to be added to the stack
@return True if successfully added, otherwise false. */
virtual bool push(const ItemType& newEntry) = 0;
/** Removes item from stack
@post The top item in the stack is removed
@return True if the item was removed, false if it was not. */
virtual bool pop() = 0;
/** Returns the top item in the stack
@pre The stack is not empty
@post The top item in the stack is returned
@return The top of the stack */
virtual ItemType peek() const = 0;
};
#endif /* StackInterface_h */
Node.h
#ifndef node_h
#define node_h
template
class Node {
public:
Node();
Node (const ItemType &anItem);
Node(const ItemType &anItem, Node * nextNodePtr);
void setItem(const ItemType &anItem);
void setNext(Node * nextNodePtr);
ItemType getItem() const;
Node* getNext() const;
private:
ItemType item;
Node* next;
};
template
Node::Node():next(nullptr){}
template
Node::Node(const ItemType &anItem):item(anItem), next(nullptr){}
template
Node::Node(const ItemType &anItem, Node * nextNodePtr):item(anItem), next(nextNodePtr)
{
}
template
void Node::setItem(const ItemType &anItem){
item = anItem;
}
template
void Node::setNext(Node * nextNodePtr) {
next = nextNodePtr;
}
template
ItemType Node::getItem() const {
return item;
}
template
Node* Node::getNext()const{
return next;
}
#endif /* node_h */
LinkedStack.h
#include
#ifndef LinkedStack_h
#define LinkedStack_h
#include \"Node.h\"
#include \"StackInterface.h\"
template
class LinkedStack:public StackInterface{
public:
LinkedStack();
LinkedStack(const LinkedStack &aStack); //space matter?***********
virtual ~LinkedStack();
bool isEmpty() const;
bool push(const ItemType &newItem);
bool pop();
ItemType peek() const;
private:
Node* topPtr;
};
template
LinkedStack::LinkedStack() : topPtr(nullptr)
{
}
template
LinkedStack::LinkedStack(const LinkedStack &aStack)
{
Node* origChainPtr = aStack->topPtr;
if (origChainPtr == nullptr)
topPtr = nullptr;
else
{
topPtr = new Node();
topPtr->setItem(origChainPtr->getItem());
/**Points to the last node in the new chain
*/
Node* newChainPtr = topPtr;
while (origChainPtr != nullptr)
{
origChainPtr = origChainPtr->getNext();
ItemType nextItem = origChainPtr->getItem();
Node* newNodePtr = new Node(nextItem);
newChainPtr->setNext(newNodePtr);
newChainPtr = newChainPtr->getNext();
}
newChainPtr->setNext(nullptr);
}
}
/**destructor
*/
template
LinkedStack::~LinkedStack()
{
//pops until the stack is empty
while (!isEmpty())
pop();
}
/**isEmpty
*/
template
bool LinkedStack::isEmpty() const
{
return topPtr == nullptr;
}
/**Push
**/
template
bool LinkedStack::push(const ItemType &newItem)
{
Node* newNodePtr = new Node(newItem, topPtr);
topPtr = newNodePtr;
newNodePtr = nullptr;
return true;
}
template
bool LinkedStack::pop()
{
bool result = false;
/* If stack is not empty delete the top item
*/
if (!isEmpty())
{
Node * nodeToDeletePtr = topPtr;
topPtr = topPtr->getNext();
//returns deleted node to the system
nodeToDeletePtr->setNext(nullptr);
delete nodeToDeletePtr;
nodeToDeletePtr = nullptr;
result = true;
} //end if
return result;
} //end pop
template
ItemType LinkedStack::peek() const
{
assert (!isEmpty()); //Enforces precondition
return topPtr->getItem();
}
#endif /* LinkedStack_h */
Map.h
#ifndef Map_h
#define Map_h
#include
#include \"LinkedStack.h\"
#include \"City.h\"
#include
#include
using namespace std;
template
class Map
{
public:
void readFlights();
void addToHead(City&anOriginCity, City&aDestinationCity);
void verifyRequestedFile();
bool isPath(City&flightCity, City&RequestedCity);
private:
vector>cityVector;
LinkedStack>stackCity;
CitynotCity;
};
template
bool Map::isPath(City &flightCity, City &requestedCity)
{
bool result, done;
City*requestedPtr = new City(requestedCity);
for (City *tempPtr = flightCity.headPtr;
tempPtr != 0;
tempPtr = tempPtr->headPtr)
{
if (tempPtr->destinationCity == requestedPtr->destinationCity)
{
done = true;
return done;
}
}
done = false;
return done;
}
template
void Map::verifyRequestedFile()
{
fstream requestedRoutes;
string originCity, destinationCity;
bool doesNotMatch;
bool correctPath;
requestedRoutes.open(\"RequestFile.txt\", ios::in);
char test;
if (requestedRoutes.is_open())
{
while (!requestedRoutes.eof())
{
getline(requestedRoutes, originCity, \',\'); //switched values, so switch back
requestedRoutes.get(test);
while (requestedRoutes.peek() == \'\\t\')
{
requestedRoutes.get(test);
}
getline(requestedRoutes, destinationCity, \'\ \');
doesNotMatch = false;
for (int i = 0; i < cityVector.size(); i++)
{
if (cityVector[i].getOriginCity() == originCity)
{
doesNotMatch = true;
notCity.setOriginCity(originCity);
notCity.setDestinationCity(destinationCity);
correctPath = isPath(cityVector[i], notCity);
if (correctPath)
cout << \"HPair serves from \" << originCity << \" to \" << destinationCity << endl;
else
cout << \"HPair does NOT serve from \" << originCity << \" to \" << destinationCity << endl;
}
}
if (!doesNotMatch)
cout << \"HPair does not serve \" << originCity << endl;
}
}
}
template
void Map::addToHead(City &anOriginCity, City &aDestinationCity)
{
City* newOne = new City(aDestinationCity);
City* newSecond = new City(anOriginCity);
if (anOriginCity.headPtr == 0)
{
anOriginCity.headPtr = newOne;
}
else
{
newOne->headPtr = anOriginCity.headPtr;
anOriginCity.headPtr = newOne;
}
}
template
void Map::readFlights()
{
fstream readCities, readFlights;
City aCity;
string originCity, destinationCity;
char test;
readCities.open(\"CityFile.txt\", ios::in);
if (readCities.is_open())
while (!readCities.eof())
while (getline(readCities, originCity))
{
aCity.setOriginCity(originCity);
cityVector.push_back(aCity);
}
for (int i = 0; i < cityVector.size(); i++)
readCities.close();
readFlights.open(\"FlightFile.txt\", ios::in);
if (readFlights.is_open())
{
while (!readFlights.eof())
{
getline(readFlights, originCity, \',\');
readFlights.get(test);
while (readFlights.peek() == \'\\t\')
{
readFlights.get(test);
}
getline(readFlights, destinationCity, \'\ \');
for (int i = 0; i < cityVector.size(); i++)
{
if (cityVector[i].getOriginCity() == originCity)
{
notCity.setOriginCity(originCity);
notCity.setDestinationCity(destinationCity);
addToHead(cityVector[i], notCity);
stackCity.push(notCity);
}
}
}
}
readFlights.close();
}
#endif /* Map_h */
City.h
#ifndef City_h
#define City_h
#include
#include
using namespace std;
template
class City
{
public:
string originCity;
string destinationCity;
City *headPtr;
City();
City(string origin, string destination);
City(const City &aCity);
void setOriginCity(string aCityOrigin);
void setDestinationCity(string aCityDestination);
string getOriginCity();
string getDestinationCity();
void displayOriginCity();
};
template
City::City()
{
originCity = \" \";
destinationCity = \" \";
headPtr = 0;
}
template
City::City(string origin, string destination)
{
originCity = origin;
destinationCity = destination;
}
template
City::City(const City &aCity)
{
this->originCity = aCity.originCity;
this->destinationCity = aCity.destinationCity;
this->headPtr = aCity.headPtr;
}
template
void City::setOriginCity(string aCityOrigin)
{
this->originCity = aCityOrigin;
}
template
void City::setDestinationCity(string aCityDestination)
{
this->destinationCity = aCityDestination;
}
template
void City::displayOriginCity()
{
cout << this->originCity << endl;
}
template
string City::getOriginCity()
{
return (this->originCity);
}
template
string City::getDestinationCity()
{
return(this->destinationCity);
}
#endif /* City_h */
requestFile.txt
Los Angeles, Houston
Glendale, Houston
Dallas, Austin
Cleveland, Chicago
Indianapolis, San Francisco
San Francisco, Washington
Boston, Columbia
Miami, Denver
flightFile.txt
Atlanta, Chicago
Boise, Chicago
Boston, Chicago
Chicago, Atlanta
Chicago, Boise
Chicago, Boston
Chicago, Houston
Cleveland, Houston
Columbia, Houston
Dallas, Houston
Denton, Houston
Denver, Houston
Houston, Glendale
Houston, Cleveland
Houston, Columbia
Houston, Dallas
Houston, Denton
Houston, Denver
Houston, Chicago
Indianapolis, Los Angeles
Los Angeles, Indianapolis
Miami, Los Angeles
Miami, Atlanta
Atlanta, Miami
Los Angeles, Miami
San Francisco, Los Angeles
Los Angeles, San Francisco
Tampa, Los Angeles
Los Angeles, Tampa
Washington, Los Angeles
Los Angeles, Washington
cityFile.txt
Atlanta
Austin
Boise
Boston
Buffalo
Chicago
Cincinnati
Cleveland
Columbia
Dallas
Denton
Denver
Detroit
Everett
Glendale
Honolulu
Houston
Indianapolis
Irvine
Los Angeles
Miami
Nashville
Oakland
Reno
Salt Lake City
San Francisco
Tampa
Washington
Solution
class GraphFindAllPaths- > getAllPaths(T source, T destination) { validate(source, destination); List
- > paths = new ArrayList
- >(); recursive(source, destination, paths, new LinkedHashSet
- > paths, LinkedHashSet
- > paths = new ArrayList
- >(); // ABD List

















