From 7cec83dec0b4ef0bd82086822de3d076bd452052 Mon Sep 17 00:00:00 2001 From: Elijah Opoku-Nyarko Date: Sun, 17 Apr 2022 20:16:23 -0400 Subject: [PATCH] Updated function to printAll towns and roads - still needs some work --- project5.cc | 2 +- province.cc | 38 +++++++++++++++++++++++--------------- province.h | 2 +- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/project5.cc b/project5.cc index b7b147a..4b8cacb 100644 --- a/project5.cc +++ b/project5.cc @@ -21,7 +21,7 @@ using namespace std; int main() { while(!cin.eof()) { Province theProvince(cin); - theProvince.printAll(cout); + theProvince.printAll(0, cout); } } diff --git a/province.cc b/province.cc index 49fa0a9..55f0948 100644 --- a/province.cc +++ b/province.cc @@ -68,18 +68,26 @@ Province::Province(std::istream & source) { } } -// void Province::printAll(std::ostream & output) { -// output << "The input data is:" << endl << endl; -// cout << endl << "Size of towns: " << sizeof(&_towns)/sizeof(&_towns[0]) << endl; -// for (int i = 0; i < sizeof(&_towns)/sizeof(&_towns[0]); i++) { -// output << _towns[i]._name << endl; -// for (Road _road: _towns[i]._roads) { -// output << "\t" << road._tail << " " << road._length << " mi"; -// if (road._isBridge == 'B' || 'b') { -// output << " via bridge" << endl; -// } else { -// output << endl; -// } -// } -// } -// } \ No newline at end of file +void Province::printAll(int start, std::ostream & output) { + // keep track of whether a town(vertex) has been scheduled to be visited + bool scheduled[_numberOfTowns]; + for (int i = 0; i < _numberOfTowns; i++){ + scheduled[i] = false; + } + + // Keep track of which towns have been visited + queue toVisit; // use queue to keep track of which town to visit next + toVisit.push(start); + scheduled[start] = true; + output << "The input data is:" << endl << endl; + + // Visit every town in the queue + while (!toVisit.empty()) { + int current = toVisit.front(); + toVisit.pop(); + + output << " "; + output << _towns[current]._name << endl; + } + +} diff --git a/province.h b/province.h index be8a6d7..5b231ef 100644 --- a/province.h +++ b/province.h @@ -30,7 +30,7 @@ class Province * Print towns and roads in province in breadth-first search order * @param output Stream to print data to */ - void printAll(std::ostream & output); + void printAll(int start, std::ostream & output); void printShortestPath(std::ostream & output) const; /**