blob: f836972181bed147c1490e82b09e8db0e6b415f8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
// Driver for SortedList
// Author: U.Breymann / H.P.Weber
// Date: 05.05.05
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <algorithm> // std::copy
#include <cmath> // sin()
#include <cstdlib> // 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;
}
|