Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public interface IFigure
- {
- double CalculateArea();
- }
- public class Circle : IFigure
- {
- public double Radius { get; }
- public Circle(double radius)
- {
- if (radius <= 0)
- {
- throw new ArgumentException("Радиус должен быть положительным числом.");
- }
- Radius = radius;
- }
- public double CalculateArea()
- {
- return Math.PI * Math.Pow(Radius, 2);
- }
- }
- public class Triangle : IFigure
- {
- public double SideA { get; }
- public double SideB { get; }
- public double SideC { get; }
- public Triangle(double sideA, double sideB, double sideC)
- {
- if (!IsValidTriangle(sideA, sideB, sideC))
- {
- throw new ArgumentException("Невозможно создать треугольник с заданными сторонами.");
- }
- SideA = sideA;
- SideB = sideB;
- SideC = sideC;
- }
- public double CalculateArea()
- {
- double s = (SideA + SideB + SideC) / 2;
- return Math.Sqrt(s * (s - SideA) * (s - SideB) * (s - SideC));
- }
- public bool IsRightTriangle()
- {
- double[] sides = { SideA, SideB, SideC };
- Array.Sort(sides);
- return Math.Pow(sides[0], 2) + Math.Pow(sides[1], 2) == Math.Pow(sides[2], 2);
- }
- private bool IsValidTriangle(double a, double b, double c)
- {
- return a + b > c && a + c > b && b + c > a;
- }
- }
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- [TestClass]
- public class FigureTests
- {
- [TestMethod]
- public void CircleAreaCalculation()
- {
- Circle circle = new Circle(5);
- double expectedArea = Math.PI * Math.Pow(5, 2);
- Assert.AreEqual(expectedArea, circle.CalculateArea(), 0.0001);
- }
- [TestMethod]
- public void TriangleAreaCalculation()
- {
- Triangle triangle = new Triangle(3, 4, 5);
- double expectedArea = 6;
- Assert.AreEqual(expectedArea, triangle.CalculateArea(), 0.0001);
- }
- [TestMethod]
- public void RightTriangleCheck()
- {
- // Это часть будто не правильно работает…
- Triangle rightTriangle = new Triangle(3, 4, 5);
- Assert.IsTrue(rightTriangle.IsRightTriangle());
- Triangle nonRightTriangle = new Triangle(1, 2, 3);
- Assert.IsFalse(nonRightTriangle.IsRightTriangle());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement