|
| 1 | +#include <algorithm> |
| 2 | +#include <numeric> |
| 3 | +#include <cmath> |
| 4 | +#include <iostream> |
| 5 | +#include <memory> |
| 6 | +#include <string> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +class Shape { |
| 12 | +public: |
| 13 | + virtual ~Shape() = default; |
| 14 | + virtual double area() const = 0; |
| 15 | + virtual string name() const = 0; |
| 16 | +}; |
| 17 | + |
| 18 | +class Circle : public Shape { |
| 19 | +public: |
| 20 | + explicit Circle(double radiusValue) : radius(radiusValue) {} |
| 21 | + |
| 22 | + double area() const override { |
| 23 | + return 3.141592653589793 * radius * radius; |
| 24 | + } |
| 25 | + |
| 26 | + string name() const override { |
| 27 | + return "Circle"; |
| 28 | + } |
| 29 | + |
| 30 | +private: |
| 31 | + double radius; |
| 32 | +}; |
| 33 | + |
| 34 | +class Rectangle : public Shape { |
| 35 | +public: |
| 36 | + Rectangle(double widthValue, double heightValue) |
| 37 | + : width(widthValue), height(heightValue) {} |
| 38 | + |
| 39 | + double area() const override { |
| 40 | + return width * height; |
| 41 | + } |
| 42 | + |
| 43 | + string name() const override { |
| 44 | + return "Rectangle"; |
| 45 | + } |
| 46 | + |
| 47 | +private: |
| 48 | + double width; |
| 49 | + double height; |
| 50 | +}; |
| 51 | + |
| 52 | +template <typename T> |
| 53 | +void printVector(const vector<T>& values, const string& label) { |
| 54 | + cout << label << ": ["; |
| 55 | + for (size_t i = 0; i < values.size(); ++i) { |
| 56 | + cout << values[i]; |
| 57 | + if (i + 1 < values.size()) { |
| 58 | + cout << ", "; |
| 59 | + } |
| 60 | + } |
| 61 | + cout << "]\n"; |
| 62 | +} |
| 63 | + |
| 64 | +int main() { |
| 65 | + vector<unique_ptr<Shape>> shapes; |
| 66 | + shapes.push_back(make_unique<Circle>(2.0)); |
| 67 | + shapes.push_back(make_unique<Rectangle>(3.0, 4.0)); |
| 68 | + shapes.push_back(make_unique<Circle>(1.5)); |
| 69 | + |
| 70 | + vector<double> areas; |
| 71 | + areas.reserve(shapes.size()); |
| 72 | + |
| 73 | + cout << "Shape areas:\n"; |
| 74 | + for (const unique_ptr<Shape>& shape : shapes) { |
| 75 | + const double value = shape->area(); |
| 76 | + areas.push_back(value); |
| 77 | + cout << "- " << shape->name() << ": " << value << '\n'; |
| 78 | + } |
| 79 | + |
| 80 | + const double totalArea = accumulate(areas.begin(), areas.end(), 0.0); |
| 81 | + const double minArea = *min_element(areas.begin(), areas.end()); |
| 82 | + const double maxArea = *max_element(areas.begin(), areas.end()); |
| 83 | + |
| 84 | + cout << "Total area: " << totalArea << '\n'; |
| 85 | + cout << "Minimum area: " << minArea << '\n'; |
| 86 | + cout << "Maximum area: " << maxArea << '\n'; |
| 87 | + |
| 88 | + vector<int> sampleCounts = {1, 2, 3, 4}; |
| 89 | + printVector(sampleCounts, "Sample counts"); |
| 90 | + printVector(areas, "Computed areas"); |
| 91 | + |
| 92 | + return 0; |
| 93 | +} |
0 commit comments