Skip to content

Commit 8154aa7

Browse files
less.h + greater.h -> bug in greater
1 parent ebb63e8 commit 8154aa7

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

cpp4j/cpp4j.pro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ SOURCES += rationalnumber.cpp \
1414
HEADERS += \
1515
rationalnumber.h \
1616
rationalnumberarray.h \
17-
pair.h
17+
pair.h \
18+
less.h \
19+
greater.h
1820

cpp4j/greater.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef GREATER_H
2+
#define GREATER_H
3+
4+
#include "less.h"
5+
6+
template <class T> class Greater {
7+
8+
public :
9+
10+
bool operator ()(const T &left, const T &right){
11+
Less<T> less;
12+
// nicht kleiner && nicht gleich
13+
bool lesser = less(left, right);
14+
bool equal = !less(left, right) && !less(right,left);
15+
16+
return !lesser && !equal;
17+
}
18+
19+
};
20+
21+
22+
#endif // GREATER_H

cpp4j/less.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef LESS_H
2+
#define LESS_H
3+
4+
5+
template <class T> class Less {
6+
7+
public :
8+
9+
bool operator ()(const T &left, const T &right){
10+
return left < right;
11+
}
12+
13+
};
14+
15+
16+
#endif // LESS_H

cpp4j/testTemplates.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ using namespace std;
1414

1515
// include your own header files here...
1616
#include "pair.h"
17+
#include "less.h"
18+
#include "greater.h"
1719
//#include "order.h"
1820
//#include "tree.h"
1921
//#include "map.h"
@@ -79,6 +81,12 @@ int testTemplates(void)
7981
cout << "2<3 == " << lessInt(2,3) << endl;
8082
cout << "4<3 == " << lessInt(4,3) << endl;
8183

84+
Greater<int> greaterInt;
85+
assert ( greaterInt(4,3) == 1 );
86+
assert ( greaterInt(3,4) == 0 );
87+
assert ( greaterInt(3,3) == 0 );
88+
89+
8290
#if 0 // move this line down while your implementation proceeds...
8391
/////////////////////////////////////////
8492
// TEST PAIR ORDER

0 commit comments

Comments
 (0)