forked from ziobron/modern_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_formatted.cpp
More file actions
94 lines (78 loc) · 2.29 KB
/
main_formatted.cpp
File metadata and controls
94 lines (78 loc) · 2.29 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "Circle.hpp"
#include "Rectangle.hpp"
#include "Shape.hpp"
#include "Square.hpp"
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<Shape *> Collection;
bool sortByArea(Shape *first, Shape *second) {
if (first == NULL || second == NULL) {
return false;
}
return (first->getArea() < second->getArea());
}
bool perimeterBiggerThan20(Shape *s) {
if (s) {
return (s->getPerimeter() > 20);
}
return false;
}
bool areaLessThan10(Shape *s) {
if (s) {
return (s->getArea() < 10);
}
return false;
}
void printCollectionElements(const Collection &collection) {
for (Collection::const_iterator it = collection.begin();
it != collection.end(); ++it) {
if (*it != NULL) {
(*it)->print();
}
}
}
void printAreas(const Collection &collection) {
for (vector<Shape *>::const_iterator it = collection.begin();
it != collection.end(); ++it) {
if (*it != NULL) {
cout << (*it)->getArea() << std::endl;
}
}
}
void findFirstShapeMatchingPredicate(const Collection &collection,
bool (*predicate)(Shape *s),
std::string info) {
Collection::const_iterator iter =
std::find_if(collection.begin(), collection.end(), predicate);
if (*iter != NULL) {
cout << "First shape matching predicate: " << info << endl;
(*iter)->print();
} else {
cout << "There is no shape matching predicate " << info << endl;
}
}
int main() {
Collection shapes;
shapes.push_back(new Circle(2.0));
shapes.push_back(new Circle(3.0));
shapes.push_back(NULL);
shapes.push_back(new Circle(4.0));
shapes.push_back(new Rectangle(10.0, 5.0));
shapes.push_back(new Square(3.0));
shapes.push_back(new Circle(4.0));
printCollectionElements(shapes);
cout << "Areas before sort: " << std::endl;
printAreas(shapes);
std::sort(shapes.begin(), shapes.end(), sortByArea);
cout << "Areas after sort: " << std::endl;
printAreas(shapes);
Square *square = new Square(4.0);
shapes.push_back(square);
findFirstShapeMatchingPredicate(shapes, perimeterBiggerThan20,
"perimeter bigger than 20");
findFirstShapeMatchingPredicate(shapes, areaLessThan10, "area less than 10");
return 0;
}