forked from coders-school/modern-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
94 lines (78 loc) · 2.45 KB
/
main.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <memory>
#include "Shape.hpp"
#include "Rectangle.hpp"
#include "Square.hpp"
#include "Circle.hpp"
using namespace std;
typedef vector<shared_ptr<Shape>> Collection;
bool sortByArea(shared_ptr<Shape> first, shared_ptr<Shape> second)
{
if(first == nullptr || second == nullptr)
return false;
return (first->getArea() < second->getArea());
}
bool perimeterBiggerThan20(shared_ptr<Shape> s)
{
if(s)
return (s->getPerimeter() > 20);
return false;
}
bool areaLessThan10(shared_ptr<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)
(*it)->print();
}
void printAreas(const Collection& collection)
{
for(vector<shared_ptr<Shape>>::const_iterator it = collection.begin(); it != collection.end(); ++it)
if(*it)
cout << (*it)->getArea() << std::endl;
}
void findFirstShapeMatchingPredicate(const Collection& collection,
bool (*predicate)(shared_ptr<Shape> s),
std::string info)
{
Collection::const_iterator iter = std::find_if(collection.begin(), collection.end(), predicate);
if(*iter != nullptr)
{
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(make_shared<Circle>(2.0));
shapes.push_back(make_shared<Circle>(3.0));
shapes.push_back(nullptr);
shapes.push_back(make_shared<Circle>(4.0));
shapes.push_back(make_shared<Rectangle>(10.0, 5.0));
shapes.push_back(make_shared<Square>(3.0));
shapes.push_back(make_shared<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);
auto square = make_shared<Square>(4.0);
shapes.push_back(square);
findFirstShapeMatchingPredicate(shapes, perimeterBiggerThan20, "perimeter bigger than 20");
findFirstShapeMatchingPredicate(shapes, areaLessThan10, "area less than 10");
return 0;
}