Untitled

mail@pastecode.io avatar
unknown
csharp
7 months ago
2.3 kB
8
Indexable
Never
using System;

class Triangle
{
    protected double sideA;
    protected double sideB;
    protected double sideC;

    public Triangle(double a, double b, double c)
    {
        sideA = a;
        sideB = b;
        sideC = c;
    }

    public virtual void SetSides(double a, double b, double c)
    {
        sideA = a;
        sideB = b;
        sideC = c;
    }

    public virtual void PrintSides()
    {
        Console.WriteLine($"Стороны треугольника: {sideA}, {sideB}, {sideC}");
    }

    public virtual double CalculatePerimeter()
    {
        return sideA + sideB + sideC;
    }

    public virtual double CalculateArea()
    {
        double p = CalculatePerimeter() / 2;
        return Math.Sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
    }
}

class RightTriangle : Triangle
{
    public RightTriangle(double a, double b, double c) : base(a, b, c)
    {
    }

    public override void SetSides(double a, double b, double c)
    {
        if (IsRightTriangle(a, b, c))
        {
            base.SetSides(a, b, c);
        }
        else
        {
            Console.WriteLine("Недопустимые стороны для прямоугольного треугольника.");
        }
    }

    public override double CalculateArea()
    {
        return 0.5 * sideA * sideB;
    }

    private bool IsRightTriangle(double a, double b, double c)
    {
        double[] sides = { a, b, c };
        Array.Sort(sides);

        return Math.Pow(sides[0], 2) + Math.Pow(sides[1], 2) == Math.Pow(sides[2], 2);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Triangle triangle = new Triangle(3, 4, 5);
        triangle.PrintSides();
        Console.WriteLine("Периметр: " + triangle.CalculatePerimeter());
        Console.WriteLine("Площадь: " + triangle.CalculateArea());

        RightTriangle rightTriangle = new RightTriangle(3, 4, 5);
        rightTriangle.PrintSides();
        Console.WriteLine("Периметр: " + rightTriangle.CalculatePerimeter());
        Console.WriteLine("Площадь: " + rightTriangle.CalculateArea());

        Console.ReadLine();
    }
}