diff options
| -rw-r--r-- | 8.00.Ringliste.cc | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/8.00.Ringliste.cc b/8.00.Ringliste.cc new file mode 100644 index 0000000..3468f08 --- /dev/null +++ b/8.00.Ringliste.cc @@ -0,0 +1,198 @@ +/* + * ===================================================================================== + * + * Filename: 8.00.Ringliste.cc + * + * Description: Implements a generic ringlist + * + * Version: 1.0 + * Created: 25.06.2014 18:32:32 + * Revision: none + * Compiler: gcc + * + * Author: Stefan Suhren (SSuhren), suhren.stefan@fh-swf.de + * Organization: FH Südwestfalen, Iserlohn + * + * ===================================================================================== + */ + +#include <cstdlib> +#include <iostream> +#include <cassert> +#include <string> + +using namespace std; + +typedef unsigned int uint; + +static const uint RinglisteErsatzLaenge = 20; // Ersatzwert für die Listenlänge + +// ===================================================================================== +// Class: Ringliste +// Description: +// ===================================================================================== +template < class T > class Ringliste +{ + public: + // ==================== LIFECYCLE ======================================= + Ringliste ( uint lng = RinglisteErsatzLaenge ); // constructor + Ringliste ( const Ringliste &other ); // copy constructor + ~Ringliste (); // destructor + + // ==================== ACCESSORS ======================================= + T last(); // letzten Wert zurückgeben + void dump(); // gesamte Ringliste ausgeben (Test) + + // ==================== MUTATORS ======================================= + Ringliste& add ( T wert ); // neuen Wert aufnehmen + + // ==================== OPERATORS ======================================= + Ringliste& operator = ( const Ringliste &other ); // assignment operator + + private: + // ==================== DATA MEMBERS ======================================= + uint laenge; // Listenlänge + uint index; // nächste Position + T *element; // Zeiger auf das Feld der Listenelem. + +}; // ----- end of class Ringliste ----- + +// === FUNCTION ====================================================================== +// Name: Ringliste<T>::Ringliste +// Description: +// ===================================================================================== +template < class T > +Ringliste<T>::Ringliste ( uint lng ){ + index = 0; + laenge = lng; + element = new T[laenge]; +} // ----- end of function Ringliste<T>::Ringliste ----- + +// === FUNCTION ====================================================================== +// Name: Ringliste<T>::Ringliste +// Description: +// ===================================================================================== +template < class T > +Ringliste<T>::Ringliste ( const Ringliste<T> &other ){ + // Exeption safty + T *tmp = new T[other.laenge]; + + for(uint i = 0; i < other.laenge; i++){ + tmp[i] = other.element[i]; + } + + laenge = other.laenge; + index = other.index; + + delete[] element; + element = tmp; +} // ----- end of function Ringliste<T>::Ringliste ----- + +// === FUNCTION ====================================================================== +// Name: Ringliste<T>::last +// Description: +// ===================================================================================== +template < class T > +T Ringliste<T>::last (){ + if( index == 0 ){ + return element[laenge - 1]; + } + else{ + return element[index - 1]; + } +} // ----- end of function Ringliste<T>::last ----- + +// === FUNCTION ====================================================================== +// Name: Ringliste<T>::dump +// Description: +// ===================================================================================== +template < class T > +void Ringliste<T>::dump (){ + for(uint i = 0; i < laenge; i++){ + if ( i % 10 == 0 && i != 0 ) cout << "\n"; + cout << element[i] << " "; + } +} // ----- end of function Ringliste<T>::dump ----- + +// === FUNCTION ====================================================================== +// Name: Ringliste<T>::add +// Description: +// ===================================================================================== +template < class T > +Ringliste<T>& Ringliste<T>::add ( T wert ){ + assert( element != NULL ); + assert( index < laenge ); + + element[index] = wert; + + if(++index >= laenge){ + index = 0; + } + + return *this; +} // ----- end of function Ringliste<T>::add ----- + +// === FUNCTION ====================================================================== +// Name: Ringliste<T>::~Ringliste +// Description: +// ===================================================================================== +template < class T > +Ringliste<T>::~Ringliste (){ + delete[] element; +} // ----- end of function Ringliste<T>::~Ringliste ----- + +// === FUNCTION ====================================================================== +// Name: Ringliste<T>::operator= +// Description: +// ===================================================================================== +template < class T > +Ringliste<T>& Ringliste<T>::operator= ( const Ringliste<T> &other ){ + // No self assignments + if ( this != &other ){ + // Exeption safty + T *tmp = new T[other.laenge]; + + for(uint i = 0; i < other.laenge; i++){ + tmp[i] = other.element[i]; + } + + laenge = other.laenge; + index = other.index; + + delete[] element; + element = tmp; + } + + return *this; +} // ----- end of function Ringliste<T>::operator= ----- + +// === FUNCTION ====================================================================== +// Name: main +// Description: +// ===================================================================================== +int main ( int argc, char *argv[] ){ + Ringliste <double> rnglst1(5); + + for(int i = 0; i < 10; i++){ + rnglst1.add(i); + rnglst1.dump(); + cout << "\n"; + + } + + cout << "\n" << rnglst1.last() << "\n\n"; + + Ringliste <string> rnglst2(5); + string werte[10] = {"0","1","2","3","4","5","6","7","8","9"}; + + for(int i = 0; i < 10; i++){ + rnglst2.add(werte[i]); + rnglst2.dump(); + cout << "\n"; + + } + + cout << "\n" << rnglst2.last(); + + return EXIT_SUCCESS; +} // ---------- end of function main ---------- |
