22// Created by barto on 19.05.18.
33//
44
5+ #include < fstream>
56#include " Graph.h"
67
8+ using namespace std ;
9+
710// protected
811
9- Graph::Graph (const std:: string name, int numberOfAvailableAlgorithms)
12+ Graph::Graph (const string name, int numberOfAvailableAlgorithms)
1013 : name(name), numberOfAvailableAlgorithms(numberOfAvailableAlgorithms) {}
1114
1215// public
1316
14- std:: string Graph::getName () {
17+ string Graph::getName () {
1518 return name;
1619}
1720
1821int Graph::getNumberOfAvailableAlgorithms () {
1922 return numberOfAvailableAlgorithms;
2023}
2124
22- std:: string Graph::printIncidenceMatrix () {
23- std:: string output = getName ();
24- std:: string temp = " " ;
25+ string Graph::printIncidenceMatrix () {
26+ string output = getName ();
27+ string temp = " " ;
2528
2629 if (incidenceMatrix.size () == 0 ) {
2730 output = " Graf pusty!" ;
@@ -32,7 +35,7 @@ std::string Graph::printIncidenceMatrix() {
3235
3336 // wypisz pierwsza linijke
3437 for (int i = 0 ; i < incidenceMatrix[0 ].size (); i++) {
35- temp = std:: to_string (i);
38+ temp = to_string (i);
3639
3740 output += " " ;
3841 for (int j = 0 ; j < 5 - temp.size (); j++) { // miejsce na 6 znakow dla kazdej komorki
@@ -53,7 +56,7 @@ std::string Graph::printIncidenceMatrix() {
5356 // wypisz kolejne linijki
5457 for (int i = 0 ; i < incidenceMatrix.size (); i++) {
5558 // pierwsza pozycja kolejnej linijki
56- temp = std:: to_string (i);
59+ temp = to_string (i);
5760
5861 output += " " ;
5962 for (int j = 0 ; j < 5 - temp.size (); j++) {
@@ -64,7 +67,7 @@ std::string Graph::printIncidenceMatrix() {
6467
6568 // kolejne pozycje kolejnych linijek
6669 for (int j = 0 ; j < incidenceMatrix[0 ].size (); j++) {
67- temp = std:: to_string (incidenceMatrix[i][j]);
70+ temp = to_string (incidenceMatrix[i][j]);
6871
6972 output += " " ;
7073 for (int k = 0 ; k < 5 - temp.size (); k++) {
@@ -80,9 +83,9 @@ std::string Graph::printIncidenceMatrix() {
8083 return output;
8184}
8285
83- std:: string Graph::printAdjacencyList () {
84- std:: string output = getName ();
85- std:: string temp = " " ;
86+ string Graph::printAdjacencyList () {
87+ string output = getName ();
88+ string temp = " " ;
8689
8790 if (adjacencyList.size () == 0 ) {
8891 output = " Graf pusty!" ;
@@ -99,7 +102,7 @@ std::string Graph::printAdjacencyList() {
99102
100103 for (int i = 0 ; i < adjacencyList.size (); i++) {
101104 // pierwsza pozycja kolejnej linijki
102- temp = std:: to_string (i);
105+ temp = to_string (i);
103106
104107 output += " " ;
105108 for (int j = 0 ; j < 5 - temp.size (); j++) {
@@ -110,11 +113,41 @@ std::string Graph::printAdjacencyList() {
110113
111114 // kolejne pozycje
112115 for (auto & element : adjacencyList[i]) {
113- output += " " + std:: to_string (element.edgeEnd ) + " , " + std:: to_string (element.value ) + " |" ;
116+ output += " " + to_string (element.edgeEnd ) + " , " + to_string (element.value ) + " |" ;
114117 }
115118
116119 output += " \n " ;
117120 }
118121
119122 return output;
120123}
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+ return returnIntVector; // should throw an error
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+ }
0 commit comments