From 61a4fe90b89cc127444405597a2590f8537474e5 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Mon, 13 Nov 2023 22:40:23 +0300 Subject: Initial commit --- MindBox.Lib/Circle.cs | 37 +++++++++++++++++++++++++++++++++++ MindBox.Lib/FlatShape.cs | 10 ++++++++++ MindBox.Lib/ITwoDimensional.cs | 6 ++++++ MindBox.Lib/MindBox.Lib.csproj | 15 ++++++++++++++ MindBox.Lib/Triangle.cs | 44 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 MindBox.Lib/Circle.cs create mode 100644 MindBox.Lib/FlatShape.cs create mode 100644 MindBox.Lib/ITwoDimensional.cs create mode 100644 MindBox.Lib/MindBox.Lib.csproj create mode 100644 MindBox.Lib/Triangle.cs (limited to 'MindBox.Lib') diff --git a/MindBox.Lib/Circle.cs b/MindBox.Lib/Circle.cs new file mode 100644 index 0000000..5793a26 --- /dev/null +++ b/MindBox.Lib/Circle.cs @@ -0,0 +1,37 @@ +namespace MindBox.Lib; + +public class Circle : FlatShape +{ + private readonly double _radius; + + public required double Radius + { + get + { + return _radius; + } + init + { + if (value <= 0.0) + { + throw new ArgumentException($"Radius cannot be non-positive: {value}"); + } + else + { + _radius = value; + } + } + } + public override double GetArea() + { + if (_area != null) + { + return _area.Value; + } + else + { + _area = Math.PI * _radius * _radius; + return _area.Value; + } + } +} \ No newline at end of file diff --git a/MindBox.Lib/FlatShape.cs b/MindBox.Lib/FlatShape.cs new file mode 100644 index 0000000..aa43312 --- /dev/null +++ b/MindBox.Lib/FlatShape.cs @@ -0,0 +1,10 @@ +namespace MindBox.Lib; + +public abstract class FlatShape : ITwoDimensional +{ + protected double? _area; + public virtual double GetArea() + { + throw new NotImplementedException(); + } +} diff --git a/MindBox.Lib/ITwoDimensional.cs b/MindBox.Lib/ITwoDimensional.cs new file mode 100644 index 0000000..776e74f --- /dev/null +++ b/MindBox.Lib/ITwoDimensional.cs @@ -0,0 +1,6 @@ +namespace MindBox.Lib; + +public interface ITwoDimensional +{ + public double GetArea(); +} diff --git a/MindBox.Lib/MindBox.Lib.csproj b/MindBox.Lib/MindBox.Lib.csproj new file mode 100644 index 0000000..c513307 --- /dev/null +++ b/MindBox.Lib/MindBox.Lib.csproj @@ -0,0 +1,15 @@ + + + + net7.0 + enable + enable + 0.0.1 + Serghei Cebotari + MindBox Shape Library + + Тестовое задание MindBox. Геометрическая библиотка для вычисления площади фигур. + + + + diff --git a/MindBox.Lib/Triangle.cs b/MindBox.Lib/Triangle.cs new file mode 100644 index 0000000..54e8a85 --- /dev/null +++ b/MindBox.Lib/Triangle.cs @@ -0,0 +1,44 @@ +namespace MindBox.Lib; + +public class Triangle : FlatShape +{ + private readonly double[] _sides; + + public Triangle(double a, double b, double c) + { + _sides = new[] { a, b, c }; + if (_sides.Any(side => side <= 0)) + { + throw new ArgumentException($"Side(s) cannot be non-positive: {string.Join(" ;", _sides.Where(side => side <= 0))}"); + } + if (a >= b + c || b >= c + a || c >= a + b) + { + throw new ArgumentException($"Sides lengths are not valid: {a}, {b}, {c}"); + } + } + public override double GetArea() + { + if (_area != null) + { + return _area.Value; + } + else + { + double semiPerimeter = _sides.Sum() / 2; + _area = Math.Sqrt(semiPerimeter * + (semiPerimeter - _sides[0]) * + (semiPerimeter - _sides[1]) * + (semiPerimeter - _sides[2])); + return _area.Value; + } + } + public bool IsRight() + { + var sorted = _sides.OrderByDescending(x => x); + + double hypotenuse = sorted.First(); + var catheti = sorted.Skip(1); + + return hypotenuse * hypotenuse == catheti.Sum(x => x * x); + } +} -- cgit v1.2.3