From b08db55701f906ec6c9ef4bcd59eb9880f5599db Mon Sep 17 00:00:00 2001 From: liza-leonova Date: Tue, 24 Dec 2024 00:19:32 +0500 Subject: [PATCH] Update figures.cpp --- task2/figures.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/task2/figures.cpp b/task2/figures.cpp index 3b33880..84caa53 100644 --- a/task2/figures.cpp +++ b/task2/figures.cpp @@ -1,3 +1,69 @@ #include "figures.hpp" +#include -static constexpr double PI = 3.14; +Rect::Rect(double width, double height) : width(width), height(height) {} + +FigureType Rect::Type() const { + return FigureType::RECTANGLE; +} + +double Rect::Perimeter() const { + return 2 * (width + height); +} + +double Rect::Area() const { + return width * height; +} + +Triangle::Triangle(double sideA, double sideB, double sideC) + : sideA(sideA), sideB(sideB), sideC(sideC) {} + +FigureType Triangle::Type() const { + return FigureType::TRIANGLE; +} + +double Triangle::Perimeter() const { + return sideA + sideB + sideC; +} + +double Triangle::Area() const { + double semiPerimeter = Perimeter() / 2; + return std::sqrt(semiPerimeter * (semiPerimeter - sideA) * (semiPerimeter - sideB) * (semiPerimeter - sideC)); +} + +Circle::Circle(double radius) : radius(radius) {} + +FigureType Circle::Type() const { + return FigureType::CIRCLE; +} + +double Circle::Perimeter() const { + return 2 * PI * radius; +} + +double Circle::Area() const { + return PI * radius * radius; +} + +std::unique_ptr
make_figure(FigureType type, double paramA, double paramB, double paramC) { + if (paramA < 0 || paramB < 0 || paramC < 0) { + throw LessThanZeroParam("One or more parameters are less than zero"); + } + + switch (type) { + case FigureType::RECTANGLE: + return std::make_unique(paramA, paramB); + + case FigureType::TRIANGLE: + if (paramA + paramB <= paramC || paramB + paramC <= paramA || paramA + paramC <= paramB) { + throw WrongTriangle("Invalid triangle sides"); + } + return std::make_unique(paramA, paramB, paramC); + + case FigureType::CIRCLE: + return std::make_unique(paramA); + + default: + return nullptr; + } +}