-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfigures.cpp
More file actions
141 lines (124 loc) · 2.4 KB
/
Copy pathfigures.cpp
File metadata and controls
141 lines (124 loc) · 2.4 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <vector>
#include <memory>
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
class Figure
{
public:
Figure(const string &name) : name_(name) {}
string Name() const
{
return name_;
}
virtual double Perimeter() const = 0;
virtual double Area() const = 0;
protected:
const string name_;
};
class Triangle : public Figure
{
public:
Triangle(int a, int b, int c) : Figure("TRIANGLE")
{
a_ = a;
b_ = b;
c_ = c;
}
double Perimeter() const override
{
return a_ + b_ + c_;
}
double Area() const override
{
double p = (a_ + b_ + c_) / 2.0;
return sqrt(p * (p - a_) * (p - b_) * (p - c_));
}
private:
int a_, b_, c_;
};
class Rect : public Figure
{
public:
Rect(int a, int b) : Figure("RECT")
{
a_ = a;
b_ = b;
}
double Perimeter() const override
{
return 2 * (a_ + b_);
}
double Area() const override
{
return a_ * b_;
}
private:
int a_, b_;
};
class Circle : public Figure
{
public:
Circle(int r) : Figure("CIRCLE")
{
r_ = r;
}
double Perimeter() const override
{
return 2 * 3.14 * r_;
}
double Area() const override
{
return 3.14 * r_ * r_;
}
private:
int r_;
};
template <typename S>
shared_ptr<Figure> CreateFigure(S &stream)
{
string name;
stream >> name;
int a, b, c;
if (name == "TRIANGLE")
{
stream >> a >> b >> c;
return make_shared<Triangle>(a, b, c);
}
else if (name == "RECT")
{
stream >> a >> b;
return make_shared<Rect>(a, b);
}
else
{
stream >> a;
return make_shared<Circle>(a);
}
}
int main()
{
vector<shared_ptr<Figure>> figures;
for (string line; getline(cin, line); )
{
istringstream is(line);
string command;
is >> command;
if (command == "ADD")
{
figures.push_back(CreateFigure(is));
}
else if (command == "PRINT")
{
for (const auto ¤t_figure : figures)
{
cout << fixed << setprecision(3)
<< current_figure->Name() << " "
<< current_figure->Perimeter() << " "
<< current_figure->Area() << endl;
}
}
}
return 0;
}