diff options
author | Serghei Cebotari <serghei@cebotari.ru> | 2023-11-13 22:40:23 +0300 |
---|---|---|
committer | Serghei Cebotari <serghei@cebotari.ru> | 2023-11-13 22:40:23 +0300 |
commit | 61a4fe90b89cc127444405597a2590f8537474e5 (patch) | |
tree | 44bfa66169cba2a7f2b79d959bf762b5e9bdeaa2 /MindBox.Lib |
Initial commit
Diffstat (limited to 'MindBox.Lib')
-rw-r--r-- | MindBox.Lib/Circle.cs | 37 | ||||
-rw-r--r-- | MindBox.Lib/FlatShape.cs | 10 | ||||
-rw-r--r-- | MindBox.Lib/ITwoDimensional.cs | 6 | ||||
-rw-r--r-- | MindBox.Lib/MindBox.Lib.csproj | 15 | ||||
-rw-r--r-- | MindBox.Lib/Triangle.cs | 44 |
5 files changed, 112 insertions, 0 deletions
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 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>net7.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + <Version>0.0.1</Version> + <Authors>Serghei Cebotari</Authors> + <Product>MindBox Shape Library</Product> + <Description> + Тестовое задание MindBox. Геометрическая библиотка для вычисления площади фигур. + </Description> + </PropertyGroup> + +</Project> 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); + } +} |