diff --git a/Makefile b/Makefile index 447fd61..936c3e0 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,6 @@ project5: project5.cc province.cc province.h # test the code against an expected output file # this test should grow into a spaceship and end with blinkers test-requirement1: project5 - ./project5 < test-data/requirement1.in > test-data/project5.out - diff test-data/project5.out test-data/requirement1.out > test.diff - wc -l test.diff \ No newline at end of file + ./project5 < test-data/t0502-combo.in > test-data/project5.out + diff test-data/project5.out test-data/shortest.out > test.diff + rm test-data/project5.out \ No newline at end of file diff --git a/project5 b/project5 index e643f06..68879e5 100755 Binary files a/project5 and b/project5 differ diff --git a/project5.cc b/project5.cc index 611674a..219e615 100644 --- a/project5.cc +++ b/project5.cc @@ -37,29 +37,11 @@ int main(int argc, char *argv[]) { // create a new graph for each loop, which will read all of the // corresponding data per graph Province theProvince(cin); - - std::cout << std::endl; - std::cout << "------------------------------------------------" << std::endl; - std::cout << "---------------- New DataSet: ------------------" << std::endl; - std::cout << "------------------------------------------------" << std::endl; - std::cout << std::endl; - theProvince.printAll(0, cout); - - theProvince.printShortestPath(std::cout); - - std::cout << std::endl; - std::cout << "------------------------------------------------" << std::endl; - std::cout << "------------------------------------------------" << std::endl; - std::cout << std::endl; - theProvince.minSpan(std::cout); - - std::cout << std::endl; - std::cout << "------------------------------------------------" << std::endl; - std::cout << "------------------------------------------------" << std::endl; std::cout << std::endl; - + std::cout << "------------------------------------------------------------------"; + std::cout << endl; } } diff --git a/province.cc b/province.cc index b9c9e1d..67cfebe 100644 --- a/province.cc +++ b/province.cc @@ -79,23 +79,20 @@ void Province::printAll(int start, std::ostream & output) { 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; + 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; // Add current town's(vertex's) unscheduled neighbors to the queue for (Town::RoadList::iterator neighbor = _towns[current]._roads.begin(); neighbor != _towns[current]._roads.end(); neighbor++) { std::string neighborName = _towns[neighbor->_head]._name; - - output << " "; - output << neighborName << " " << neighbor->_length << " mi"; + output << " " << neighborName << " " << neighbor->_length << " mi"; // if the type is bridge, then add to output if (neighbor ->_isBridge) { @@ -113,7 +110,7 @@ void Province::printAll(int start, std::ostream & output) { } } } - output << endl << endl; + output << endl; } int Province::smallest(double dist[], std::list toVisit, @@ -147,7 +144,7 @@ void Province::printShortestPath(std::ostream & output) const { return; } - output << "The shortest routes from " + _towns[0]._name; + output << "The shortest paths from " + _towns[0]._name; output << " are:" << std::endl << std::endl; // keeps track of the index of the predecessor to each @@ -194,7 +191,7 @@ void Province::printShortestPath(std::ostream & output) const { // print out the data for each non capital town for (int i = 1; i < _numberOfTowns; i++) { - output << " " << "The shortest route from " + _towns[0]._name; + output << " " << "The shortest path from " + _towns[0]._name; output << " to " + _towns[i]._name + " is " << dist[i]; output << " mi:" << std::endl; @@ -214,11 +211,12 @@ void Province::printShortestPath(std::ostream & output) const { // print out the names for each entry in the stack while (!predecessors.empty()) { - output << " " << _towns[predecessors.top()]._name; + output << " " << _towns[predecessors.top()]._name; output << std::endl; predecessors.pop(); } } + cout << endl; } /** @@ -308,11 +306,11 @@ void Province::minSpan(std::ostream & output) const { } output << "The road upgrading goal can be achieved at minimal cost by upgrading:"; - output << std::endl << std::endl; + output << std::endl; // Print names of towns in minimum spanning tree of province for (int i = 0; i < minSpanTree.size(); i++) { - output << " "; + output << " "; output << _towns[minSpanTree[i]._head]._name; output << " to "; output << _towns[minSpanTree[i]._tail]._name << std::endl; diff --git a/test-data/project5.out b/test-data/project5.out index 6b241e8..92607ce 100644 --- a/test-data/project5.out +++ b/test-data/project5.out @@ -1,84 +1,42 @@ +The input data is: ------------------------------------------------- ----------------- New DataSet: ------------------ ------------------------------------------------- +Wenham + Hamilton 1 mi +Hamilton + Wenham 1 mi -The input data is : +The shortest paths from Wenham are: - Salem - Beverly 2.4 mi via bridge - Danvers 3.7 mi via bridge - Lynn 4.9 mi - Beverly - Danvers 2.9 mi - Salem 2.4 mi via bridge - Wenham 5.2 mi - Danvers - Beverly 2.9 mi - Wenham 4.2 mi - Salem 3.7 mi via bridge - Lynn - Salem 4.9 mi - Wenham - Beverly 5.2 mi - Danvers 4.2 mi - - -The shortest routes from Salem are: - - The shortest route from Salem to Wenham is 7.6 mi: - Salem - Beverly - Wenham - The shortest route from Salem to Beverly is 2.4 mi: - Salem - Beverly - The shortest route from Salem to Danvers is 3.7 mi: - Salem - Danvers - The shortest route from Salem to Lynn is 4.9 mi: - Salem - Lynn - ------------------------------------------------- ------------------------------------------------- + The shortest path from Wenham to Hamilton is 1 mi: + Wenham + Hamilton The road upgrading goal can be achieved at minimal cost by upgrading: + Hamilton to Wenham - Salem to Beverly - Danvers to Beverly - Wenham to Danvers - Salem to Lynn - ------------------------------------------------- ------------------------------------------------- - +------------------------------------------------------------------ +The input data is: ------------------------------------------------- ----------------- New DataSet: ------------------ ------------------------------------------------- +A + B 1 mi via bridge +B + A 1 mi via bridge + C 1 mi via bridge +C + B 1 mi via bridge -The input data is : +The shortest paths from A are: - Wenham - Hamilton 1 mi - Hamilton - Wenham 1 mi - - -The shortest routes from Wenham are: - - The shortest route from Wenham to Hamilton is 1 mi: - Wenham - Hamilton - ------------------------------------------------- ------------------------------------------------- + The shortest path from A to B is 1 mi: + A + B + The shortest path from A to C is 2 mi: + A + B + C The road upgrading goal can be achieved at minimal cost by upgrading: + B to A + C to B - Hamilton to Wenham - ------------------------------------------------- ------------------------------------------------- - +------------------------------------------------------------------ diff --git a/test-data/requirement1.out b/test-data/requirement1.out deleted file mode 100644 index d5d431a..0000000 --- a/test-data/requirement1.out +++ /dev/null @@ -1,39 +0,0 @@ - ------------------------------------------------- ----------------- New DataSet: ------------------ ------------------------------------------------- - -The input data is : - - Salem - Beverly 2.4 mi via bridge - Danvers 3.7 mi via bridge - Lynn 4.9 mi - Beverly - Danvers 2.9 mi - Salem 2.4 mi via bridge - Wenham 5.2 mi - Danvers - Beverly 2.9 mi - Wenham 4.2 mi - Salem 3.7 mi via bridge - Lynn - Salem 4.9 mi - Wenham - Beverly 5.2 mi - Danvers 4.2 mi - - - ------------------------------------------------- ----------------- New DataSet: ------------------ ------------------------------------------------- - -The input data is : - - Wenham - Hamilton 1 mi - Hamilton - Wenham 1 mi - - diff --git a/test-data/shortest.out b/test-data/shortest.out new file mode 100644 index 0000000..d2366e4 --- /dev/null +++ b/test-data/shortest.out @@ -0,0 +1,45 @@ +The input data is: + +Wenham + Hamilton 1 mi +Hamilton + Wenham 1 mi + +The shortest paths from Wenham are: + + The shortest path from Wenham to Hamilton is 1 mi: + Wenham + Hamilton + +The road upgrading goal can be achieved at minimal cost by upgrading: + Hamilton to Wenham + + +------------------------------------------------------------------ + +The input data is: + +A + B 1 mi via bridge +B + A 1 mi via bridge + C 1 mi via bridge +C + B 1 mi via bridge + +The shortest paths from A are: + + The shortest path from A to B is 1 mi: + A + B + The shortest path from A to C is 2 mi: + A + B + C + +The road upgrading goal can be achieved at minimal cost by upgrading: + B to A + C to B + + +------------------------------------------------------------------