From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog2/SortedList2/Compare.h | 20 +++++++++ Bachelor/Prog2/SortedList2/SL_test.cpp | 75 +++++++++++++++++++++++++++++++++ Bachelor/Prog2/SortedList2/SortedList.h | 63 +++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 Bachelor/Prog2/SortedList2/Compare.h create mode 100644 Bachelor/Prog2/SortedList2/SL_test.cpp create mode 100644 Bachelor/Prog2/SortedList2/SortedList.h (limited to 'Bachelor/Prog2/SortedList2') diff --git a/Bachelor/Prog2/SortedList2/Compare.h b/Bachelor/Prog2/SortedList2/Compare.h new file mode 100644 index 0000000..336cf08 --- /dev/null +++ b/Bachelor/Prog2/SortedList2/Compare.h @@ -0,0 +1,20 @@ +// Function-object to compare two objects. +// This template needs operator<() for objects of type T, but it is possible to +// specialize it for any given type so that this template is not used. +// Author: U.Breymann / H.P.Weber +// Date: 05.05.05 + +#ifndef COMPARE_H +#define COMPARE_H + +template +class Compare { +public: + bool operator()( const T& a, const T& b ) const + { + return a < b; + } +}; + +#endif + diff --git a/Bachelor/Prog2/SortedList2/SL_test.cpp b/Bachelor/Prog2/SortedList2/SL_test.cpp new file mode 100644 index 0000000..f836972 --- /dev/null +++ b/Bachelor/Prog2/SortedList2/SL_test.cpp @@ -0,0 +1,75 @@ +// Driver for SortedList +// Author: U.Breymann / H.P.Weber +// Date: 05.05.05 + +#include +using std::cout; +using std::endl; + +#include +using std::setw; + +#include // std::copy + +#include // sin() +#include // abs(int) + +#include "SortedList.h" + +template <> +class Compare< int > { // specialization +public: + enum sortCriterion{ increasing, decreasing, absoluteValue }; + Compare( sortCriterion s = increasing ) : howToSort( s ) { } // Constructor + bool operator()( const int& a, const int& b ) const + { + bool result; + switch( howToSort ) { + case increasing: result = a < b; break; + case decreasing: result = a > b; break; + case absoluteValue: result = abs(a) < abs(b); break; + default: throw "Compare.operator()(): default darf nicht erreicht werden"; + } + return result; + } +private: + sortCriterion howToSort; +}; + +int main() +{ + Compare< int > absolute( Compare< int >::absoluteValue ); + SortedList< int > sortedList, sortedListAbsolute( absolute ); + std::ostream_iterator< int > output( cout, "\t" ); + + cout << "Werte mit verschiedenen Vorzeichen einfuegen:\n"; + for( int i = 0; i < 7; ++i ) { + int value = int( 100.0 * sin( static_cast< float >( i ) ) ); + cout << *sortedList.insert( value ) << endl; + sortedListAbsolute.insert( value ); + } + + cout << "\nincreasing sortiert( default ):\n"; + std::copy( sortedList.begin(), sortedList.end(), output ); + + cout << "\n\nnach Absolutbetrag sortiert:\n"; + std::copy( sortedListAbsolute.begin(), sortedListAbsolute.end(), output ); + + cout << "\n\nWert 90 finden" << endl; + SortedList< int >::Iterator search = sortedList.find( 90 ); + if( search == sortedList.end() ) + cout << "nicht gefunden\n"; + else + cout << *search << " gefunden" << endl; + + cout << "\nWert 91 finden" << endl; + search = sortedList.find( 91 ); + if( search == sortedList.end() ) + cout << "nicht gefunden\n\n"; + else + cout << *search << " gefunden" << endl << endl; + + return 0; +} + + diff --git a/Bachelor/Prog2/SortedList2/SortedList.h b/Bachelor/Prog2/SortedList2/SortedList.h new file mode 100644 index 0000000..300bd74 --- /dev/null +++ b/Bachelor/Prog2/SortedList2/SortedList.h @@ -0,0 +1,63 @@ +// Template for sorted list +// Author: U.Breymann / H.P.Weber +// Date: 05.05.05 + +#ifndef SORTEDLIST_H +#define SORTEDLIST_H + +#include +#include // find + +#include "compare.h" + +template< class T > +class SortedList +{ +public: + typedef std::list< T >::iterator Iterator; + typedef std::list< T >::const_iterator ConstIterator; + SortedList( const Compare< T >& cmp = Compare< T >() ) : comp( cmp ) { } + virtual ~SortedList() { } + + bool empty() const { return myList.empty(); } + int size() const { return myList.size(); } + + // take first or last element + void pop_front() { myList.pop_front(); } + void pop_back() { myList.pop_back(); } + + // read first or last element + T& front() { return myList.front(); } + const T& front() const { return myList.front(); } + T& back() { return myList.back(); } + const T& back() const { return myList.back(); } + + Iterator begin() { return myList.begin(); } + ConstIterator begin() const { return myList.begin(); } + Iterator end() { return myList.end(); } + ConstIterator end() const { return myList.end(); } + + void erase( Iterator& pos ) { myList.erase( pos ); } + + // find element + Iterator find( const T& value ) + { + return std::find( myList.begin(), myList.end(), value ); + } + + // sorted insert + Iterator insert( const T& value ) + { + // find place to insert + Iterator temp( begin() ); + while( temp != end() && comp( *temp, value ) ) ++temp; + return myList.insert( temp, value ); + } + +private: + std::list< T > myList; + Compare< T > comp; +}; + +#endif + -- cgit v1.2.3