Skip to content
Snippets Groups Projects
Commit 07b45bdf authored by Frederik Simonsen's avatar Frederik Simonsen
Browse files

ruter lesFraFil og skrivTilFil

parent c4db71f3
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,20 @@
* @file bane.cpp
* @author Sondre Sand & Frederik Simonsen
*/
#include <fstream>
#include "bane.h"
using namespace std;
//TODO: Fyll på mer her etter som header-fila tar form
Bane::Bane(ifstream & inn) : Rute (inn) {
inn >> antVogner >> lengde; inn.ignore();
}
/**
* skrivTilFil(...)
*/
void Bane::skrivTilFil(ofstream & ut) {
ut << "A\n";
Rute::skrivTilFil(ut);
ut << antVogner << ' ' << lengde << '\n';
}
\ No newline at end of file
......@@ -7,6 +7,7 @@
#ifndef __BANE_H
#define __BANE_H
#include <fstream>
#include "rute.h"
/**
......@@ -17,6 +18,8 @@ class Bane : public Rute {
int antVogner, lengde;
public:
Bane(std::ifstream & inn);
virtual void skrivTilFil(std::ofstream & ut);
};
......
......@@ -8,3 +8,18 @@
using namespace std;
// TODO: Fyll inn mer her etter som header-fila tar form
Buss::Buss(ifstream & inn) : Rute(inn) {
inn >> antSitt >> antStaa >> leddbuss; inn.ignore();
}
/**
* skrivtilfil(...)
* @param ut
*/
void Buss::skrivTilFil(ofstream & ut) {
ut << "U\n";
Rute::skrivTilFil(ut);
ut << antSitt << ' ' << antStaa
<< ' ' << leddbuss << '\n';
}
\ No newline at end of file
......@@ -7,6 +7,7 @@
#ifndef __BUSS_H
#define __BUSS_H
#include <fstream>
#include "rute.h"
/**
......@@ -19,6 +20,8 @@ class Buss : public Rute {
bool leddbuss;
public:
Buss(std::ifstream & inn);
virtual void skrivTilFil(std::ofstream & ut);
};
......
......@@ -5,7 +5,44 @@
* @author Sondre Sand & Frederik Simonsen
*/
#include <list>
#include <fstream>
#include "rute.h"
using namespace std;
// TODO: Fyll på her etter som header-fila tar mer form
/**
* Rute::Rute(...)
*/
Rute::Rute(ifstream & inn) {
int antStopp,
stoppNr,
antMin;
inn >> antStopp; inn.ignore();
for (int i = 0; i < antStopp; i++) {
inn >> stoppNr >> antMin; inn.ignore();
stoppene.push_back(new Stopp(stoppNr, antMin));
}
}
/**
* @brief Construct a new Stopp:: Stopp object
*
* @param stoppNr
* @param antMin
*/
Stopp::Stopp(const int stoppNr, const int antMin) {
nr = stoppNr; minutter = antMin;
}
void Stopp::skrivTilFil(ofstream & ut) {
ut << nr << ' ' << minutter << '\n';
}
/**
* skrivTilFil(...)
*/
void Rute::skrivTilFil(ofstream & ut) {
ut << stoppene.size() << '\n';
for (auto it = stoppene.begin(); it != stoppene.end(); it++)
(*it)->skrivTilFil(ut);
}
\ No newline at end of file
......@@ -9,6 +9,7 @@
#define __RUTE_H
#include <list>
#include <fstream>
/**
* Stopp (med unikt nummer og antall minutter fra stoppested)
......@@ -16,6 +17,8 @@
struct Stopp {
int nr, minutter; // Litt usikker på om denne skal deklareres slik
// Eller om den skal under public eller private i Rute
Stopp(const int stoppNr, const int antMin);
void skrivTilFil(std::ofstream & ut);
};
/**
......@@ -26,7 +29,8 @@ class Rute {
std::list <Stopp*> stoppene;
public:
Rute(std::ifstream & inn);
virtual void skrivTilFil(std::ofstream & ut);
};
......
......@@ -4,10 +4,14 @@
* @file ruter.cpp
* @author Sondre Sand & Frederik Simonsen
*/
#include <fstream>
#include <iostream>
#include <map>
#include "LesData3.h"
#include "ruter.h"
#include "rute.h"
#include "bane.h"
#include "buss.h"
using namespace std;
/**
......@@ -40,6 +44,27 @@ void Ruter::handling() {
* Leser informasjon om ruter fra fil
*/
void Ruter::lesFraFil() {
int key,
antRuter;
char ruteType;
ifstream innfil("ruter.dta"); // Åpner aktuell fil
if (innfil) { // Hvis filen funnet:
cout << "Leser fra filen 'ruter.dta'...\n";
innfil >> antRuter; innfil.ignore(); // Leser antall ruter
innfil >> key; // Prøver å lese første port
while (!innfil.eof()) { // Så lenge ikke slutt på fil:
innfil >> ruteType; innfil.ignore();
switch (ruteType) {
case 'U': rutene[key] = new Buss(innfil); break;
case 'A': rutene[key] = new Bane(innfil); break;
}
innfil >> key;
}
innfil.close();
cout << rutene.size();
} else
cout << "\nFant ikke filen 'ruter.dta'...\n";
}
......@@ -87,5 +112,13 @@ void Ruter::skrivMeny() {
* Skriver informasjon om ruter til fil
*/
void Ruter::skrivTilFil() {
ofstream utfil("ruter.dt2"); // Åpner aktuell fil for skriving
cout << "\nSkriver til filen 'ruter.dt2'...\n";
utfil << rutene.size() << '\n';
for (const auto & val : rutene) {
utfil << val.first << ' ';
(val.second)->skrivTilFil(utfil);
}
utfil.close();
}
\ No newline at end of file
2
1 U
3
1 0
2 3
3 5
50 10 0
2 A
3
4 0
5 3
6 5
5 50
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment