Aufgabenblock_2 fast fertig

This commit is contained in:
Steffen Vogel 2011-11-03 14:44:41 +01:00
parent 0e7fbbb855
commit 1e8e9f4749
14 changed files with 320 additions and 176 deletions

View File

@ -123,7 +123,7 @@ void vAufgabe2() {
(*it)->vAusgabe();
}
cout << endl << "Globale Zeit: " << dGlobaleZeit;
cout << endl << "Globale Zeit: " << dGlobaleZeit;
}
}
@ -134,8 +134,8 @@ void vAufgabe3() {
dGlobaleZeit += 1.0;
velo.vAbfertigung();
vw.vAbfertigung();
boat.vAbfertigung();
vw.vAbfertigung();
boat.vAbfertigung();
Fahrzeug::vAusgabeHeader();
cout << vw << endl << velo << endl << boat << endl << endl;
@ -146,7 +146,7 @@ void vAufgabe3() {
else {
cout << "Der Golf ist bereits weiter gefahren" << endl;
}
cout << endl;
cout << endl;
Fahrrad veloKopie = velo; /* benutze Kopier Konstrukutor */
Fahrrad veloKopie2;
@ -163,16 +163,16 @@ typedef void (*aufgabe_t)(void);
int main() {
int iWahl;
aufgabe_t pAufgaben[] = {
&vAufgabe1_deb,
&vAufgabe1,
&vAufgabe2,
&vAufgabe3
};
aufgabe_t pAufgaben[] = {
&vAufgabe1_deb,
&vAufgabe1,
&vAufgabe2,
&vAufgabe3
};
retry:
cout << "0: vAufgabe1_deb()" << endl;
cout << "0: vAufgabe1_deb()" << endl;
cout << "1: vAufgabe1()" << endl;
cout << "2: vAufgabe2()" << endl;
cout << "3: vAufgabe3()" << endl;
@ -180,20 +180,20 @@ int main() {
cin >> iWahl;
cout << endl;
if (iWahl > NUM_AUFGABEN || iWahl < 0) {
cerr << "Ungültige Eingabe! Bitte versuchen Sie es erneut" << endl;
goto retry;
}
if (iWahl > NUM_AUFGABEN || iWahl < 0) {
cerr << "Ungültige Eingabe! Bitte versuchen Sie es erneut" << endl;
goto retry;
}
pAufgaben[iWahl](); /* Funktionspointer aufrufen */
cout << endl << endl << "Nochmal? (0/1): ";
cin >> iWahl;
cout << endl;
cin >> iWahl;
cout << endl;
if (iWahl) {
goto retry;
}
if (iWahl) {
goto retry;
}
return 0;
}

View File

@ -25,4 +25,3 @@ double Fahrrad::dGeschwindigkeit() const {
return dGeschwindigkeit;
}

View File

@ -6,7 +6,7 @@
#include "Weg.h"
#include "FzgFahren.h"
#include "FzgParken.h"
#include "AktivesVO.h"
#include "SimuClient.h"
extern double dGlobaleZeit;
@ -28,8 +28,7 @@ Fahrzeug::Fahrzeug(string sName, double dMaxGeschwindkeit) :
}
/* Kopierkonstruktor */
Fahrzeug::Fahrzeug(Fahrzeug &fz) :
AktivesVO(fz) {
Fahrzeug::Fahrzeug(Fahrzeug &fz) : AktivesVO(fz) {
vInitialisierung();
p_dMaxGeschwindigkeit = fz.p_dMaxGeschwindigkeit;
}
@ -106,3 +105,7 @@ double Fahrzeug::getAbschnittStrecke() const {
bool Fahrzeug::operator<(Fahrzeug &fz) const {
return (this->p_dGesamtStrecke < fz.p_dGesamtStrecke);
}
void Fahrzeug::vZeichnen(Weg *pWeg) const {
bZeichneFahrrad(getName(), pWeg->getName(), getAbschnittStrecke() / pWeg->getLaenge(), dGeschwindigkeit());
}

View File

@ -19,6 +19,7 @@ public:
virtual void vAbfertigung();
virtual double dTanken(double dMenge = 0.0);
virtual void vZeichnen(Weg *pWeg) const;
virtual double dGeschwindigkeit() const;
void vNeueStrecke(Weg *pWeg, double dStartZeit = 0.0);

View File

@ -14,9 +14,9 @@ FzgVerhalten::~FzgVerhalten()
double FzgVerhalten::getMaxgeschwindigkeit() {
switch (p_pWeg->getLimit()) {
case Innerorts: return 50;
case Landstrasse: return 100;
case Autobahn: return DBL_MAX; /* unbegrenzt */
case Weg::Innerorts: return 50;
case Weg::Landstrasse: return 100;
case Weg::Autobahn: return DBL_MAX; /* unbegrenzt */
default: return 0;
}
}

View File

@ -1,60 +1,84 @@
/*** LAZYAKTION.H ***/
#ifndef __LazyAktion_h
#define __LazyAktion_h
#ifndef LAZYAKTION_H_
#define LAZYAKTION_H_
#include <list>
using namespace std;
// Oberklasse LazyAktion
template <class T>
class LazyAktion
{
public:
LazyAktion( list<T>* ptLazyListe )
: p_ptLazyListe( ptLazyListe ) {}
virtual ~LazyAktion() {}
virtual void vAusfuehren() = 0;
protected:
list<T>* p_ptLazyListe; // Zeiger auf p_ListeObjekte
};
template<class T> class LazyAktion {
public:
LazyAktion(list<T> *ptLazyListe) : p_ptLazyListe(ptLazyListe) { }
virtual ~LazyAktion() { }
virtual void vAusfuehren() = 0;
protected:
list<T> *p_ptLazyListe; // Zeiger auf p_ListeObjekte auf die die Aktionen angewendet werden sollen
};
// LazyPushFront
template <class T>
class LazyPushFront : public LazyAktion<T>
{
public:
LazyPushFront(const T& einObjekt, list<T>* eineListe)
: LazyAktion<T>(eineListe)
, p_tObjekt(einObjekt) {}
virtual ~LazyPushFront() {}
void vAusfuehren() { p_ptLazyListe->push_front(p_tObjekt); }
private:
T p_tObjekt;
};
template<class T> class LazyPushFront : public LazyAktion<T> {
public:
LazyPushFront(const T &einObjekt, list<T> *eineListe) :
LazyAktion<T> (eineListe),
p_tObjekt(einObjekt)
{ }
virtual ~LazyPushFront() { }
void vAusfuehren() {
p_ptLazyListe->push_front(p_tObjekt);
}
private:
using LazyAktion<T>::p_ptLazyListe;
T p_tObjekt;
};
// LazyPushBack
template<class T> class LazyPushBack : public LazyAktion<T> {
...
public:
LazyPushBack(const T &einObjekt, list<T> *eineListe) :
LazyAktion<T> (eineListe),
p_tObjekt(einObjekt)
{ }
virtual ~LazyPushBack() { }
// LazyErase
template <class T>
class LazyErase : public LazyAktion<T>
{
// typedef fuer iterator
typedef typename list<T>::iterator iterator;
typedef typename list<T>::const_iterator const_iterator;
void vAusfuehren() {
p_ptLazyListe->push_back(p_tObjekt);
}
public:
LazyErase( ... ) : ... {}
virtual ~LazyErase() {}
void vAusfuehren() { ... }
private:
iterator p_itObjekt; // bei erase Iterator speichern
private:
using LazyAktion<T>::p_ptLazyListe; // für gcc notwendig
T p_tObjekt;
};
template <class T> class LazyErase : public LazyAktion<T> {
// typedef für iterator
typedef typename list<T>::iterator iterator;
typedef typename list<T>::const_iterator const_iterator;
#endif
public:
LazyErase(const iterator &itObjekt, list<T> *eineListe) :
LazyAktion<T> (eineListe),
p_itObjekt(itObjekt)
{
cout << "reihe " << *itObjekt << " zur löschung ein" << endl;
}
virtual ~LazyErase() { }
void vAusfuehren() {
p_ptLazyListe->erase(p_itObjekt);
}
private:
using LazyAktion<T>::p_ptLazyListe; // für gcc notwendig
iterator p_itObjekt; // bei erase Iterator speichern
};
#endif /* LAZYAKTION_H_ */

View File

@ -1,92 +1,97 @@
/*** LAZYLISTE.H ***/
#ifndef __LazyListe_h
#define __LazyListe_h
#ifndef LAZYLISTE_H_
#define LAZYLISTE_H_
#include <list>
#include "LazyAktion.h"
template <class T>
class LazyListe
{
public:
// typedef fuer iterator
typedef typename list<T>::iterator iterator;
typedef typename list<T>::const_iterator const_iterator;
using namespace std;
// Konstruktor / Destruktor
LazyListe() { bChanged = false; }
virtual ~LazyListe()
{
if (bChanged)
{
// ggf. noch anstehende Aktionen löschen
do
{
delete *(p_ListeAktionen.begin());
p_ListeAktionen.pop_front();
}
while (p_ListeAktionen.size() > 0);
}
}
template<class T> class LazyListe {
// Lesefunktionen
const_iterator begin() const {return p_ListeObjekte.begin();}
const_iterator end() const {...}
iterator begin() {return p_ListeObjekte.begin();}
iterator end() {...}
bool empty() const {...}
public:
// typedef für Iterator
typedef typename list<T>::iterator iterator;
typedef typename list<T>::const_iterator const_iterator;
// Schreibfunktionen
void push_back( const T einObjekt )
{
p_ListeAktionen.push_back(new LazyPushBack<T>(...));
bChanged = true;
return;
}
// Konstruktor / Destruktor
LazyListe() {
bChanged = false;
}
void push_front( const T einObjekt )
{
...
virtual ~LazyListe() {
if (bChanged) {
// ggf. noch anstehende Aktionen löschen
do {
delete *(p_ListeAktionen.begin());
p_ListeAktionen.pop_front();
} while (p_ListeAktionen.size() > 0);
}
}
bChanged = true;
return;
}
// Lesefunktionen
const_iterator begin() const {
return p_ListeObjekte.begin();
}
const_iterator end() const {
return p_ListeObjekte.end();
}
void erase( iterator itObjekt )
{
...
iterator begin() {
return p_ListeObjekte.begin();
}
bChanged = true;
return;
}
iterator end() {
return p_ListeObjekte.end();
}
// Änderungen auf Objektliste übertragen
void vAktualisieren()
{
list <LazyAktion<T>*>::const_iterator itL;
bool empty() const {
return p_ListeObjekte.empty();
}
if ( bChanged )
{
// ausstehende Aktionen durchfuehren
for (itL=... )
{
// Aktion ausführen
...
// Zeiger auf Action-Element löschen
...
}
// Liste der Aktionen leeren
p_ListeAktionen.clear();
// Schreibfunktionen
void push_back(const T einObjekt) {
p_ListeAktionen.push_back(new LazyPushBack<T>(einObjekt, &p_ListeObjekte));
bChanged = true;
}
bChanged = false;
}
}
void push_front(const T einObjekt) {
p_ListeAktionen.push_back(new LazyPushFront<T>(einObjekt, &p_ListeObjekte));
bChanged = true;
}
private:
list<T> p_ListeObjekte;
list<LazyAktion<T>*> p_ListeAktionen;
bool bChanged;
void erase(iterator itObjekt) {
p_ListeAktionen.push_back(new LazyErase<T>(itObjekt, &p_ListeObjekte));
bChanged = true;
}
// Änderungen auf Objektliste übertragen
void vAktualisieren() {
if (bChanged) {
// ausstehende Aktionen durchfuehren
typename list<LazyAktion<T> *>::const_iterator it; // TODO warum typename?!
for (it = p_ListeAktionen.begin(); it != p_ListeAktionen.end(); it++) {
cout << "führe aktion aus, noch " << p_ListeAktionen.size() << endl;
// Aktion ausführen
//LazyAktion<T> *pAktion = *it;
//pAktion->vAusfuehren();
(*it)->vAusfuehren();
// Zeiger auf Action-Element löschen
delete *it;
}
// Liste der Aktionen leeren
p_ListeAktionen.clear();
bChanged = false;
}
}
private:
list<T> p_ListeObjekte;
list<LazyAktion<T> *> p_ListeAktionen;
bool bChanged;
};
#endif
#endif /* LAZYLISTE_H_ */

View File

@ -11,5 +11,8 @@ Losfahren::~Losfahren()
{ }
void Losfahren::vBearbeiten() {
cout << "Fahrausnahme: Losfahren (Fzg: " << *p_pFahrzeug << ", Weg: " << *p_pWeg << ")" << endl;
cerr << "Fahrausnahme: Losfahren (Fzg: " << *p_pFahrzeug << ", Weg: " << *p_pWeg << ")" << endl;
p_pWeg->vAbgabe(p_pFahrzeug);
p_pWeg->vAnnahme(p_pFahrzeug);
}

View File

@ -2,6 +2,7 @@
#include <iomanip>
#include "PKW.h"
#include "SimuClient.h"
using namespace std;
@ -48,6 +49,10 @@ double PKW::dTanken(double dMenge) {
return p_dTankinhalt - dAlterInhalt;
}
void PKW::vZeichnen(Weg *pWeg) const {
bZeichnePKW(getName(), pWeg->getName(), getAbschnittStrecke() / pWeg->getLaenge(), dGeschwindigkeit(), getTankinhalt());
}
void PKW::vAbfertigung() {
if (p_dTankinhalt > 0) { /* prüfen, ob etwas im Tank ist */
p_dTankinhalt -= (dGlobaleZeit - p_dZeit) * p_dMaxGeschwindigkeit

View File

@ -25,6 +25,7 @@ public:
ostream& ostreamAusgabe(ostream &stream) const;
double dVerbrauch() const;
double dTanken(double dMenge = 0.0);
void vZeichnen(Weg *pWeg) const;
double getTankinhalt() const;
private:

View File

@ -11,5 +11,7 @@ Streckenende::~Streckenende()
{ }
void Streckenende::vBearbeiten() {
cout << "Fahrausnahme: Streckenende (Fzg: " << *p_pFahrzeug << ", Weg: " << *p_pWeg << ")" << endl;
cerr << "Fahrausnahme: Streckenende (Fzg: " << *p_pFahrzeug << ", Weg: " << *p_pWeg << ")" << endl;
p_pWeg->vAbgabe(p_pFahrzeug);
}

View File

@ -1,5 +1,6 @@
#include <iostream>
#include <iomanip>
#include <algorithm>
#include "Weg.h"
#include "Fahrzeug.h"
@ -22,18 +23,17 @@ Weg::~Weg()
/* fertige alle Fahrzeuge auf Weg ab */
void Weg::vAbfertigung() {
list<Fahrzeug *>::const_iterator iterator;
list<Fahrzeug *>::iterator it;
for (iterator = p_pFahrzeuge.begin(); iterator != p_pFahrzeuge.end(); ++iterator) {
for (it = p_pFahrzeuge.begin(); it != p_pFahrzeuge.end(); it++) {
try {
(*iterator)->vAbfertigung();
(*it)->vAbfertigung();
} catch (FahrAusnahme &ausnahme) {
ausnahme.vBearbeiten();
}
}
p_dZeit = dGlobaleZeit;
}
void Weg::vAnnahme(Fahrzeug *pFz, double dStartZeit) {
@ -41,11 +41,21 @@ void Weg::vAnnahme(Fahrzeug *pFz, double dStartZeit) {
p_pFahrzeuge.push_back(pFz);
}
void Weg::vAbgabe(Fahrzeug *pFz) {
list<Fahrzeug *>::iterator result;
result = find(p_pFahrzeuge.begin(), p_pFahrzeuge.end(), pFz);
if (result != p_pFahrzeuge.end()) {
p_pFahrzeuge.erase(result);
}
}
double Weg::getLaenge() const {
return p_dLaenge;
}
Begrenzung Weg::getLimit() const {
Weg::Begrenzung Weg::getLimit() const {
return p_eLimit;
}
@ -54,9 +64,9 @@ ostream& Weg::ostreamAusgabe(ostream &stream) const {
<< resetiosflags(ios::left) << setiosflags(ios::right)
<< setw(24) << p_dLaenge << " ( ";
list<Fahrzeug *>::const_iterator iterator;
for (iterator = p_pFahrzeuge.begin(); iterator != p_pFahrzeuge.end(); ++iterator) {
stream << (*iterator)->getName() << " ";
list<Fahrzeug *>::const_iterator it;
for (it = p_pFahrzeuge.begin(); it != p_pFahrzeuge.end(); it++) {
stream << (*it)->getName() << " ";
}
stream << ")";

View File

@ -10,20 +10,22 @@ using namespace std;
class Fahrzeug; /* Forward Deklaration */
typedef enum {
Innerorts, /* 50 km/h */
Landstrasse, /* 100 km/h */
Autobahn /* unbegrenzt */
} Begrenzung;
class Weg : public AktivesVO {
public:
typedef enum {
Innerorts, /* 50 km/h */
Landstrasse, /* 100 km/h */
Autobahn /* unbegrenzt */
} Begrenzung;
Weg();
Weg(string sName, double dLaenge, Begrenzung eLimit = Autobahn);
virtual ~Weg();
void vAbfertigung();
void vAnnahme(Fahrzeug *pFz, double dStartZeit = 0);
void vAbgabe(Fahrzeug *pFz);
double getLaenge() const;
Begrenzung getLimit() const;

View File

@ -1,12 +1,14 @@
#include <string>
#include <vector>
#include <iostream>
#include <stdlib.h>
#include "SimuClient.h"
#include "Fahrzeug.h"
#include "Fahrrad.h"
#include "PKW.h"
#include "Weg.h"
#include "LazyListe.h"
using namespace std;
@ -152,7 +154,7 @@ void vAufgabe3() {
}
void vAufgabe4() {
Weg weg("Allee", 150.0, Landstrasse);
Weg weg("Allee", 150.0, Weg::Landstrasse);
PKW vw("Golf", 110, 6.7, 88);
Fahrrad velo("Haibike", 22);
@ -168,8 +170,8 @@ void vAufgabe4() {
}
void vAufgabe5() {
Weg hin("Hinweg", 500.0, Landstrasse);
Weg rueck("Rueckweg", 500.0, Landstrasse);
Weg hin("Hinweg", 500.0, Weg::Landstrasse);
Weg rueck("Rueckweg", 500.0, Weg::Landstrasse);
PKW vw("Golf", 110, 6.7, 88);
Fahrrad velo("Haibike", 22);
@ -192,8 +194,9 @@ void vAufgabe5() {
rueck.vAbfertigung();
vSetzeZeit(dGlobaleZeit);
bZeichnePKW(vw.getName(), hin.getName(), vw.getAbschnittStrecke() / hin.getLaenge(), vw.dGeschwindigkeit(), vw.getTankinhalt());
bZeichneFahrrad(velo.getName(), rueck.getName(), velo.getAbschnittStrecke() / hin.getLaenge(), velo.dGeschwindigkeit());
vw.vZeichnen(&hin);
velo.vZeichnen(&rueck);
cout << vw << endl << velo << endl << hin << endl << rueck << endl;
@ -203,33 +206,119 @@ void vAufgabe5() {
vBeendeGrafik();
}
void vAufgabe6() {
Weg weg("Allee", 300.0, Weg::Landstrasse);
PKW vw("Golf", 110, 6.7, 88);
weg.vAnnahme(&vw);
Fahrzeug::vAusgabeHeader();
while (dGlobaleZeit < 10) {
dGlobaleZeit += 0.5;
weg.vAbfertigung();
cout << vw << endl << weg << endl;
}
}
void vListeAusgeben(LazyListe<int> tListe) {
LazyListe<int>::iterator it;
for (it = tListe.begin(); it != tListe.end(); it++) {
cout << *it << ", ";
}
cout << endl;
}
void vAufgabe6a() {
LazyListe<int> tListe;
LazyListe<int>::iterator it;
/* PRNG mit konstantem seed initialisieren */
srand(55);
/* Mit Zufallszahlen füllen */
cout << "Fülle mit Zufallszahlen" << endl;
for (int i = 0; i < 10; i++) {
tListe.push_back(rand() % 10 + 1);
}
cout << "Führe LazyAktionen aus" << endl;
tListe.vAktualisieren();
cout << "Gebe Liste aus: ";
vListeAusgeben(tListe);
cout << "Lösche Werte > 5..." << endl;
for (it = tListe.begin(); it != tListe.end(); it++) {
if (*it > 5) {
tListe.erase(it);
}
}
cout << "Gebe Liste aus: ";
vListeAusgeben(tListe);
cout << "Führe LazyAktionen aus" << endl;
tListe.vAktualisieren();
cout << "Gebe Liste aus: ";
vListeAusgeben(tListe);
cout << "Weitere Änderungen..." << endl;
tListe.push_front(44);
tListe.push_back(33);
cout << "Führe LazyAktionen aus" << endl;
tListe.vAktualisieren();
cout << "Gebe Liste aus: ";
vListeAusgeben(tListe);
}
typedef void (*aufgabe_t)(void);
#define NUM_AUFGABEN 7
int main() {
int iWahl;
aufgabe_t pAufgaben[] = {
&vAufgabe1_deb,
&vAufgabe1,
&vAufgabe2,
&vAufgabe3,
&vAufgabe4,
&vAufgabe5,
&vAufgabe6,
&vAufgabe6a
};
retry:
cout << "1: vAufgabe1()" << endl;
cout << "0: vAufgabe1_deb()" << endl;
cout << "1: vAufgabe1()" << endl;
cout << "2: vAufgabe2()" << endl;
cout << "3: vAufgabe3()" << endl;
cout << "4: vAufgabe4()" << endl;
cout << "5: vAufgabe5()" << endl;
cout << "6: vAufgabe6()" << endl;
cout << "7: vAufgabe6a()" << endl;
cout << "Bitte wähen Sie eine Aufgabe: ";
cin >> iWahl;
cout << endl;
switch (iWahl) {
case 1: vAufgabe1(); break;
case 0: vAufgabe1_deb(); break;
case 2: vAufgabe2(); break;
case 3: vAufgabe3(); break;
case 4: vAufgabe4(); break;
case 5: vAufgabe5(); break;
default:
cerr << "Ungültige Eingabe! Bitte versuchen Sie es erneut" << endl;
goto retry;
if (iWahl > NUM_AUFGABEN || iWahl < 0) {
cerr << "Ungültige Eingabe! Bitte versuchen Sie es erneut" << endl;
goto retry;
}
pAufgaben[iWahl](); /* Funktionspointer aufrufen */
cout << endl << endl << "Nochmal? (0/1): ";
cin >> iWahl;
cout << endl;
if (iWahl) {
goto retry;
}
return 0;
}