diff --git a/stoppested.cpp b/stoppested.cpp index 7fc6079d4a22e3b93b5efd3d28c828baed382aef..dafed2443c0363356eab8ca2cd0c8d541b85dc1b 100644 --- a/stoppested.cpp +++ b/stoppested.cpp @@ -45,18 +45,20 @@ void Stoppested::skrivData() { void Stoppested::skrivDataMedNaboer(vector<Stoppested*> alleStoppesteder) { skrivData(); - int i = 0; + int i, pos; + i = pos = 0; bool naboEksisterer = false; cout << "\nHar forbindelser til:"; // << '\n'; for (const auto& val : indexNaboStoppene) { cout << "\nSize indexes naboer: " << indexNaboStoppene.size(); cout << "\nSize tid: " << antallMinutterTilNaboStopp.size(); if (val < alleStoppesteder.size()) { - cout << "\n\t" << i + 1 << " - Navn: " << alleStoppesteder[val]->hentNavn() + pos++; + cout << "\n\t" << i + pos << " - Navn: " << alleStoppesteder[val]->hentNavn() << '\t' << "Tid i minutter mellom stopp: " << antallMinutterTilNaboStopp[i]; //<< '\n'; - naboEksisterer = true; + naboEksisterer = true; } i++; } @@ -161,4 +163,47 @@ int Stoppested::hentTidForsteNabo() { if (antallMinutterTilNaboStopp.size() > 1) return antallMinutterTilNaboStopp.front(); return -1; +} + +void Stoppested::lesFraFil(ifstream& inn) { + string heleLinjen = ""; + getline(inn, heleLinjen); + int pos = 0; + vector<string> linjenSplittet = hjelp.splittStreng('*', heleLinjen); + int storrelse = linjenSplittet.size(); + cout << "\nStorrelse splittet: " << storrelse << "\n\n"; + for (int i = 0; i < storrelse - 1; i++) { + switch (i) { + case 0: + navn = linjenSplittet[0]; + break; + case 1: + indexNaboStoppene.push_back(stoi(linjenSplittet[1])); + break; + case 2: { + if (storrelse > 5) + indexNaboStoppene.push_back(stoi(linjenSplittet[2])); + else + antallMinutterTilNaboStopp.push_back(stoi(linjenSplittet[2])); + break; + } + case 3: + antallMinutterTilNaboStopp.push_back(stoi(linjenSplittet[3])); + break; + case 4: + antallMinutterTilNaboStopp.push_back(stoi(linjenSplittet[4])); + break; + } + } +} + +void Stoppested::skrivTilFil(ofstream& ut) { + ut << navn << '*'; + for (const auto& val : indexNaboStoppene) { + ut << val << '*'; + } + for (const auto& val : antallMinutterTilNaboStopp) { + ut << val << '*'; + } + ut << '\n'; } \ No newline at end of file diff --git a/stoppested.h b/stoppested.h index 036ef37dff4e7389b8642739a52d3833c894ccbc..526e211847220ae8d4efe5b1db86f70cbd660c51 100644 --- a/stoppested.h +++ b/stoppested.h @@ -1,6 +1,7 @@ #ifndef __STOPPESTED_H #define __STOPPESTED_H +#include <fstream> #include <iostream> #include <string> #include <vector> @@ -42,5 +43,7 @@ class Stoppested { void leggTilNaboBakerst(const int & index, const int & tid); int hentAntallStoppestedVector(); int hentTidForsteNabo(); + void lesFraFil(std::ifstream& inn); + void skrivTilFil(std::ofstream & ut); }; #endif //__STOPPESTED_H \ No newline at end of file