@@ -24,17 +24,87 @@ int Graph::getNumberOfAvailableAlgorithms() {
2424
2525string Graph::printIncidenceMatrix () {
2626 string output = getName ();
27+ output += " \n " ;
28+
29+ output += printMatrix (incidenceMatrix);
30+
31+ return output;
32+ }
33+
34+ string Graph::printAdjacencyList () {
35+ string output = getName ();
36+ output += " \n " ;
37+
38+ output += printList (adjacencyList);
39+
40+ return output;
41+ }
42+
43+ void Graph::loadDataFrom (std::string fileName) {
44+ vector<int > rawData = loadRawDataFrom (fileName);
45+ loadRawDataToMatrix (rawData);
46+ loadRawDataToList (rawData);
47+ }
48+
49+ // protected
50+
51+ vector<int > Graph::loadRawDataFrom (string path) {
52+ vector<int > returnIntVector = vector<int >();
53+ vector<string> values = vector<string>();
54+
55+ fstream file (path, ios::in);
56+
57+ if (!file.is_open ())
58+ throw " Plik nie istnieje, badz zablokowany dostep!" ;
59+
60+ string temp = " " ;
61+ while (file >> temp) {
62+ try {
63+ returnIntVector.push_back (stoi (temp));
64+ } catch (const exception& e) {
65+ returnIntVector.clear ();
66+ throw " Bledna zawartosc pliku! Upewnij sie ze podales odpowiedni format!" ;
67+ }
68+ }
69+
70+ return returnIntVector;
71+ }
72+
73+ bool Graph::edgeBeginningAvailable (int vertex) {
74+ int i = 0 ;
75+ for (auto & v : adjacencyList[vertex]) {
76+ i++;
77+ }
78+
79+ if (i < (adjacencyList.size () - 1 )) return true ;
80+ return false ;
81+ }
82+
83+ bool Graph::edgeEndAvailable (int beginning, int end) {
84+ if (beginning == end) {
85+ return false ;
86+ }
87+
88+ for (auto & v : adjacencyList[beginning]) {
89+ if (v.edgeEnd == end)
90+ return false ;
91+ }
92+ return true ;
93+ }
94+
95+ std::string Graph::printMatrix (vector<vector<int >> v) {
96+ string output = " " ;
2797 string temp = " " ;
2898
29- if (incidenceMatrix .size () == 0 ) {
99+ if (v .size () == 0 ) {
30100 output = " Graf pusty!" ;
31101 return output;
32102 }
33103
34- output += " \n K\\ W ||" ; // lewy gorny rog ma ' K\W ||', podwojny | dla wyroznienia komorki
104+ output += " K\\ W ||" ; // lewy gorny rog ma ' K\W ||', podwojny | dla wyroznienia komorki
35105
36106 // wypisz pierwsza linijke
37- for (int i = 0 ; i < incidenceMatrix [0 ].size (); i++) {
107+ for (int i = 0 ; i < v [0 ].size (); i++) {
38108 temp = to_string (i);
39109
40110 output += " " ;
@@ -47,14 +117,14 @@ string Graph::printIncidenceMatrix() {
47117 output += " \n " ;
48118
49119 // pozioma linia dla odzielenia wierszy
50- int firstLineSize = output.size () - getName (). size () - 2 ; // -2 bo dwa razy \n
120+ int firstLineSize = output.size () - 1 ; // -1 bo \n
51121 for (int i = 0 ; i < firstLineSize; i++) {
52122 output += " -" ;
53123 }
54124 output += " \n " ;
55125
56126 // wypisz kolejne linijki
57- for (int i = 0 ; i < incidenceMatrix .size (); i++) {
127+ for (int i = 0 ; i < v .size (); i++) {
58128 // pierwsza pozycja kolejnej linijki
59129 temp = to_string (i);
60130
@@ -66,8 +136,8 @@ string Graph::printIncidenceMatrix() {
66136 output += temp + " ||" ;
67137
68138 // kolejne pozycje kolejnych linijek
69- for (int j = 0 ; j < incidenceMatrix [0 ].size (); j++) {
70- temp = to_string (incidenceMatrix [i][j]);
139+ for (int j = 0 ; j < v [0 ].size (); j++) {
140+ temp = to_string (v [i][j]);
71141
72142 output += " " ;
73143 for (int k = 0 ; k < 5 - temp.size (); k++) {
@@ -83,24 +153,24 @@ string Graph::printIncidenceMatrix() {
83153 return output;
84154}
85155
86- string Graph::printAdjacencyList ( ) {
87- string output = getName () ;
156+ std:: string Graph::printList (vector<forward_list<Graph::EdgeListElement>> v ) {
157+ string output = " " ;
88158 string temp = " " ;
89159
90- if (adjacencyList .size () == 0 ) {
160+ if (v .size () == 0 ) {
91161 output = " Graf pusty!" ;
92162 return output;
93163 }
94164
95- output += " \n W || wierzcholek konca krawedzi, waga | kolejna... | ...\n " ; // pierwsza linijka
165+ output += " W || wierzcholek konca krawedzi, waga | kolejna... | ...\n " ; // pierwsza linijka
96166
97167 // druga linijka rozdzielajaca
98168 for (int i = 0 ; i < 80 ; i++) {
99169 output += " -" ;
100170 }
101171 output += " \n " ;
102172
103- for (int i = 0 ; i < adjacencyList .size (); i++) {
173+ for (int i = 0 ; i < v .size (); i++) {
104174 // pierwsza pozycja kolejnej linijki
105175 temp = to_string (i);
106176
@@ -112,7 +182,7 @@ string Graph::printAdjacencyList() {
112182 output += temp + " ||" ;
113183
114184 // kolejne pozycje
115- for (auto & element : adjacencyList [i]) {
185+ for (auto & element : v [i]) {
116186 output += " " + to_string (element.edgeEnd ) + " , " + to_string (element.value ) + " |" ;
117187 }
118188
@@ -121,55 +191,3 @@ string Graph::printAdjacencyList() {
121191
122192 return output;
123193}
124-
125- void Graph::loadDataFrom (std::string fileName) {
126- vector<int > rawData = loadRawDataFrom (fileName);
127- loadRawDataToMatrix (rawData);
128- loadRawDataToList (rawData);
129- }
130-
131- // protected
132-
133- vector<int > Graph::loadRawDataFrom (string path) {
134- vector<int > returnIntVector = vector<int >();
135- vector<string> values = vector<string>();
136-
137- fstream file (path, ios::in);
138-
139- if (!file.is_open ())
140- throw " Plik nie istnieje, badz zablokowany dostep!" ;
141-
142- string temp = " " ;
143- while (file >> temp) {
144- try {
145- returnIntVector.push_back (stoi (temp));
146- } catch (const exception& e) {
147- returnIntVector.clear ();
148- throw " Bledna zawartosc pliku! Upewnij sie ze podales odpowiedni format!" ;
149- }
150- }
151-
152- return returnIntVector;
153- }
154-
155- bool Graph::edgeBeginningAvailable (int vertex) {
156- int i = 0 ;
157- for (auto & v : adjacencyList[vertex]) {
158- i++;
159- }
160-
161- if (i < (adjacencyList.size () - 1 )) return true ;
162- return false ;
163- }
164-
165- bool Graph::edgeEndAvailable (int beginning, int end) {
166- if (beginning == end) {
167- return false ;
168- }
169-
170- for (auto & v : adjacencyList[beginning]) {
171- if (v.edgeEnd == end)
172- return false ;
173- }
174- return true ;
175- }
0 commit comments