-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
13 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,14 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <stdexcept> | ||
#include <cmath> | ||
|
||
enum class FigureType | ||
{ | ||
TRIANGLE, | ||
CIRCLE, | ||
RECTANGLE, | ||
}; | ||
|
||
class Figure { | ||
public: | ||
virtual FigureType Type() const = 0; | ||
virtual double Perimeter() const = 0; | ||
virtual double Area() const = 0; | ||
virtual ~Figure() = default; // Виртуальный деструктор | ||
}; | ||
|
||
class Rect : public Figure { | ||
public: | ||
Rect(double width, double height) : width(width), height(height) {} | ||
|
||
FigureType Type() const override { return FigureType::RECTANGLE; } | ||
double Perimeter() const override { return 2 * (width + height); } | ||
double Area() const override { return width * height; } | ||
|
||
private: | ||
double width; | ||
double height; | ||
}; | ||
|
||
class Triangle : public Figure { | ||
public: | ||
Triangle(double a, double b, double c) : a(a), b(b), c(c) { | ||
if (a <= 0 || b <= 0 || c <= 0) { | ||
throw LessThanZeroParam("Sides must be greater than zero"); | ||
} | ||
if (a + b <= c || a + c <= b || b + c <= a) { | ||
throw WrongTriangle("Invalid triangle sides"); | ||
} | ||
} | ||
|
||
FigureType Type() const override { return FigureType::TRIANGLE; } | ||
double Perimeter() const override { return a + b + c; } | ||
double Area() const override { | ||
double s = Perimeter() / 2; | ||
return std::sqrt(s * (s - a) * (s - b) * (s - c)); | ||
#include "figures.hpp" | ||
|
||
std::unique_ptr<Figure> make_figure(FigureType type, double a, double b, double c) { | ||
switch (type) { | ||
case FigureType::TRIANGLE: | ||
return std::make_unique<Triangle>(a, b, c); | ||
case FigureType::CIRCLE: | ||
return std::make_unique<Circle>(a); | ||
case FigureType::RECTANGLE: | ||
return std::make_unique<Rect>(a, b); | ||
default: | ||
throw std::invalid_argument("Unknown figure type"); | ||
} | ||
|
||
private: | ||
double a; | ||
double b; | ||
double c; | ||
}; | ||
|
||
class Circle : public Figure { | ||
public: | ||
Circle(double radius) : radius(radius) { | ||
if (radius <= 0) { | ||
throw LessThanZeroParam("Radius must be greater than zero"); | ||
} | ||
} | ||
|
||
FigureType Type() const override { return FigureType::CIRCLE; } | ||
double Perimeter() const override { return 2 * PI * radius; } | ||
double Area() const override { return PI * radius * radius; } | ||
|
||
private: | ||
double radius; | ||
}; | ||
|
||
std::unique_ptr<Figure> make_figure(FigureType type, double a, double b = 0, double c = 0); | ||
|
||
class WrongTriangle : public std::invalid_argument { | ||
public: | ||
explicit WrongTriangle(const std::string& message) : std::invalid_argument(message) {} | ||
}; | ||
|
||
class LessThanZeroParam : public std::invalid_argument { | ||
public: | ||
explicit LessThanZeroParam(const std::string& message) : std::invalid_argument(message) {} | ||
}; | ||
} |