From 90d1c524730948e1a81d61a60fa96106d3d9cff9 Mon Sep 17 00:00:00 2001 From: Bartosz Rodziewicz Date: Mon, 21 May 2018 01:02:42 +0200 Subject: [PATCH] Make algorithms print output only when needed --- DirectedGraph.cpp | 54 +++++++++++++++++++++++++++------------------ DirectedGraph.h | 4 ++-- UndirectedGraph.cpp | 28 ++++++++++++++--------- UndirectedGraph.h | 4 ++-- 4 files changed, 54 insertions(+), 36 deletions(-) diff --git a/DirectedGraph.cpp b/DirectedGraph.cpp index 9127ba4..fd4f089 100644 --- a/DirectedGraph.cpp +++ b/DirectedGraph.cpp @@ -80,9 +80,9 @@ string DirectedGraph::runAlgorithm(char index, char arg1, int arg2, int arg3) { if (index == 1) { if (arg1 == 0) { - output = dijkstrasAlgorithmOnMatrix(arg2, arg3); + output = dijkstrasAlgorithmOnMatrix(arg2, arg3, true); } else if (arg1 == 1) { - output = dijkstrasAlgorithmOnList(arg2, arg3); + output = dijkstrasAlgorithmOnList(arg2, arg3, true); } else { throw "Nieznany blad!"; // should never be thrown } @@ -148,7 +148,7 @@ void DirectedGraph::loadRawDataToList(std::vector rawData) { // private -std::string DirectedGraph::dijkstrasAlgorithmOnMatrix(int beginVertex, int endVertex) { +std::string DirectedGraph::dijkstrasAlgorithmOnMatrix(int beginVertex, int endVertex, bool print) { if (incidenceMatrix.size() == 0) throw "Graf pusty!"; @@ -210,23 +210,28 @@ std::string DirectedGraph::dijkstrasAlgorithmOnMatrix(int beginVertex, int endVe } - string output; - output = "Najkrotsza droga z wierzch.: " + to_string(beginVertex) + " do wierzch.: " + to_string(endVertex) + " wynosi: " + to_string(pathLength[endVertex]) + ".\n"; - output += "Prowadzi nastepujaca droga: "; + if (print) { + string output; + output = "Najkrotsza droga z wierzch.: " + to_string(beginVertex) + " do wierzch.: " + to_string(endVertex) + + " wynosi: " + to_string(pathLength[endVertex]) + ".\n"; + output += "Prowadzi nastepujaca droga: "; + + currentVertex = endVertex; - currentVertex = endVertex; + output += to_string(currentVertex); - output += to_string(currentVertex); + while (currentVertex != beginVertex) { + currentVertex = previousVertex[currentVertex]; + output += " <- " + to_string(currentVertex); + } - while (currentVertex != beginVertex) { - currentVertex = previousVertex[currentVertex]; - output += " <- " + to_string(currentVertex); + return output; } - return output; + return ""; } -std::string DirectedGraph::dijkstrasAlgorithmOnList(int beginVertex, int endVertex) { +std::string DirectedGraph::dijkstrasAlgorithmOnList(int beginVertex, int endVertex, bool print) { if (adjacencyList.size() == 0) throw "Graf pusty!"; @@ -282,18 +287,23 @@ std::string DirectedGraph::dijkstrasAlgorithmOnList(int beginVertex, int endVert } - string output; - output = "Najkrotsza droga z wierzch.: " + to_string(beginVertex) + " do wierzch.: " + to_string(endVertex) + " wynosi: " + to_string(pathLength[endVertex]) + ".\n"; - output += "Prowadzi nastepujaca droga: "; + if (print) { + string output; + output = "Najkrotsza droga z wierzch.: " + to_string(beginVertex) + " do wierzch.: " + to_string(endVertex) + + " wynosi: " + to_string(pathLength[endVertex]) + ".\n"; + output += "Prowadzi nastepujaca droga: "; + + currentVertex = endVertex; - currentVertex = endVertex; + output += to_string(currentVertex); - output += to_string(currentVertex); + while (currentVertex != beginVertex) { + currentVertex = previousVertex[currentVertex]; + output += " <- " + to_string(currentVertex); + } - while (currentVertex != beginVertex) { - currentVertex = previousVertex[currentVertex]; - output += " <- " + to_string(currentVertex); + return output; } - return output; + return ""; } diff --git a/DirectedGraph.h b/DirectedGraph.h index d9cf8fd..e8d518f 100644 --- a/DirectedGraph.h +++ b/DirectedGraph.h @@ -25,8 +25,8 @@ class DirectedGraph : public Graph { void loadRawDataToList(std::vector rawData) override ; private: - std::string dijkstrasAlgorithmOnMatrix(int beginVertex, int endVertex); - std::string dijkstrasAlgorithmOnList(int beginVertex, int endVertex); + std::string dijkstrasAlgorithmOnMatrix(int beginVertex, int endVertex, bool print); + std::string dijkstrasAlgorithmOnList(int beginVertex, int endVertex, bool print); }; diff --git a/UndirectedGraph.cpp b/UndirectedGraph.cpp index 7b604f6..1fc4e40 100644 --- a/UndirectedGraph.cpp +++ b/UndirectedGraph.cpp @@ -82,9 +82,9 @@ string UndirectedGraph::runAlgorithm(char index, char arg1, int arg2, int arg3) if (index == 1) { if (arg1 == 0) { - output = primsAlgorithmOnMatrix(); + output = primsAlgorithmOnMatrix(true); } else if (arg1 == 1) { - output = primsAlgorithmOnList(); + output = primsAlgorithmOnList(true); } else { throw "Nieznany blad!"; // should never be thrown } @@ -151,7 +151,7 @@ void UndirectedGraph::loadRawDataToList(std::vector rawData) { // private -string UndirectedGraph::primsAlgorithmOnMatrix() { +std::string UndirectedGraph::primsAlgorithmOnMatrix(bool print) { // prepare vector for output vector> minimumSpanningTree; @@ -219,14 +219,18 @@ string UndirectedGraph::primsAlgorithmOnMatrix() { } while (foundVertices.size() < numberOfVertices); - string output = "Minimalne drzewo rozpinajace\n"; + if (print) { + string output = "Minimalne drzewo rozpinajace\n"; - output += printMatrix(minimumSpanningTree); + output += printMatrix(minimumSpanningTree); - return output; + return output; + } + + return ""; } -string UndirectedGraph::primsAlgorithmOnList() { +std::string UndirectedGraph::primsAlgorithmOnList(bool print) { // prepare vector for output std::vector> minimumSpanningTree; @@ -273,9 +277,13 @@ string UndirectedGraph::primsAlgorithmOnList() { } while (foundVertices.size() < numberOfVertices); - string output = "Minimalne drzewo rozpinajace\n"; + if (print) { + string output = "Minimalne drzewo rozpinajace\n"; - output += printList(minimumSpanningTree); + output += printList(minimumSpanningTree); - return output; + return output; + } + + return ""; } diff --git a/UndirectedGraph.h b/UndirectedGraph.h index 6bd6bc7..15c6883 100644 --- a/UndirectedGraph.h +++ b/UndirectedGraph.h @@ -25,8 +25,8 @@ class UndirectedGraph : public Graph { void loadRawDataToList(std::vector rawData) override ; private: - std::string primsAlgorithmOnMatrix(); - std::string primsAlgorithmOnList(); + std::string primsAlgorithmOnMatrix(bool print); + std::string primsAlgorithmOnList(bool print); };