Skip to content

Commit

Permalink
Update figures.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
liza-leonova authored Dec 23, 2024
1 parent 88c1655 commit b08db55
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion task2/figures.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,69 @@
#include "figures.hpp"
#include <cmath>

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<Figure> 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<Rect>(paramA, paramB);

case FigureType::TRIANGLE:
if (paramA + paramB <= paramC || paramB + paramC <= paramA || paramA + paramC <= paramB) {
throw WrongTriangle("Invalid triangle sides");
}
return std::make_unique<Triangle>(paramA, paramB, paramC);

case FigureType::CIRCLE:
return std::make_unique<Circle>(paramA);

default:
return nullptr;
}
}

0 comments on commit b08db55

Please sign in to comment.