“Калькулятор треугольника” Ответ

Калькулятор треугольника

namespace TriAngle;

internal class MainClass : Program
{
        public static void Main()
        {
            var triangle = new Triangle();
            Triangle.Runner();
        }
    }
 
public class Program : TriangleItems
{
    protected class Triangle : TryExcept
    {
        private double A { get; set; }
        private double B { get; set; }
        private double C { get; set; }

        public void TriangleSides()
        {
              this.A = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("a is " + A);
            this.B = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("b is " + B);
            this.C = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("c is " + C);
        }
        
        public void Perimeter()
        {
            if (A + B > C || B + C > A || A + C > B)
            {
                var perimeter = A + B + C;
                Console.WriteLine("Triangle's perimeter is " + perimeter);
            }
            else if (A == 0 || B == 0 || C == 0)
            {
                Console.WriteLine("Triangle's side can't be zero");
            }
            else
            {
                Console.WriteLine("Sorry! But such triangle doesn't exist!");
            }
        }  
        
        public void Area()
        {
           if (A + B > C || B + C > A || A + C > B)
            {
                var s = (A + B + C) / 2;
                var area = Math.Sqrt(s * (s - A) * (s - B) * (s - C));
                Console.WriteLine("Triangle's area is  " + area);
            }
            else if (A == 0 || B == 0 || C == 0)
            {
                Console.WriteLine("Triangle's side can't be zero");
            }
            else
            {
                Console.WriteLine("Sorry! But such triangle doesn't exist!");
            }
        }
        
        public static DateTime Runner()
        {
            while (true)
            {
                Console.WriteLine("Welcome to Triangle's calculator!");
                Console.WriteLine("Press 1 for start!");

                var input1 = Convert.ToInt32(Console.ReadLine());

                if (input1 == 1)
                {
                    StartProgram = DateTime.Now;
                }
                else
                {
                    throw (new FormatException("You must start program with '1' "));
                }

                Area_try_Except();
                Perimeter_try_Except();

                Console.WriteLine("You can stop program press 2");
                input1 = Convert.ToInt32(Console.ReadLine());

                if (input1 == 2)
                {
                    StopProgram = DateTime.Now;
                }
                else
                {
                    throw (new FormatException("You must stop program with '2' "));
                }
            }
        }
    }
}
 
public class TryExcept : Program
{
    protected static void Area_try_Except()
    {
        try
        {
            var triangle = new Triangle();
            Console.WriteLine("Insert values for triangle's area operation : ");
            triangle.TriangleSides();
            triangle.Area();
        }
        catch
        {
            throw new FormatException("Looks like something went wrong.Triangle's side must be a number!");
        }
    }
  
    protected static void Perimeter_try_Except()
    {
        try
        {
            var triangle = new Triangle();
                Console.WriteLine("You can also calculate triangle's perimeter.It can be same values as before or different.It doesn't affect program functionality!");
                Console.WriteLine("Before calculate perimeter, please enter values again : ");
                triangle.TriangleSides();
                triangle.Perimeter();
        }
        catch
        {
            throw new FormatException("Looks like something went wrong.Triangle's side must be a number!");
        }
    }
}
 
public class TriangleItems
{
    protected static DateTime StartProgram { get; set; }
    protected static DateTime StopProgram { get; set; }

    public static DateTime _start()
    {
        StartProgram = DateTime.Now;
        return StartProgram;
    }

    public static DateTime _stop()
    {
        StopProgram = DateTime.Now;
        return StopProgram;
    }
}
Rovshan Huseynov

Калькулятор треугольника

 namespace TriangleCalc; 
 public class Program
 {
     private double A { get; set; }
     private double B { get; set; }
     private double C { get; set; }
     public void Triangle()
     {
         try
         {
             this.A = Convert.ToDouble(Console.ReadLine());
             Console.WriteLine("a is " + A);
             this.B = Convert.ToDouble(Console.ReadLine());
             Console.WriteLine("b is " + B);
             this.C = Convert.ToDouble(Console.ReadLine());
             Console.WriteLine("c is " + C);
         }  catch
         {
             throw (new Exception("Triangle's side must be a number"));
         }
     }
     
     public void Area()
     {
             if (A + B > C && B + C > A && A + C >B )  
             {
                 var s  = (A+B+C)/2;
                 var result = Math.Sqrt(s * (s - A) * (s - B) * (s - C));
                 Console.WriteLine("The area is " + result );
             }
             else
             {
                 Console.WriteLine("Result is NaN");
             }
     }
     
     public void Perimeter()
     {
         if (A + B > C && A + C > B && B + C > A)
         {
             var perimeter = A + B + C;
             Console.WriteLine("The perimeter is " + perimeter);
         }
         else
         {
             Console.WriteLine("Such triangle doesn't exist");
         }
     } 
 }

public class Runner : Program
{
    public static void Main(string[] args)
    {
        while (true)
        {
            Console.WriteLine("1-Start\n2-Stop");
            var input1 = Convert.ToInt32(Console.ReadLine());

            if (input1 == 1)
            {
                var startProgram = Triangleİtems.Start;
            }
            else
            {
                throw (new Exception("Program must start with 1"));
            }

            var triangle = new Program();
            triangle.Triangle();
            triangle.Area();
            triangle.Perimeter();

            Console.WriteLine("You can end program with 2");
            input1 = Convert.ToInt32(Console.ReadLine());

            if (input1 == 2)
            {
                var stopProgram = Triangleİtems.Stop;
            }

            else
            {
                throw (new Exception("Program must stop with 2"));
            }
        }
    }
}

public static class Triangleİtems
{
    public static DateTime Start;
    public static DateTime Stop;

    public static DateTime StartProgram()
    {
        Start = DateTime.Now;
        return Start;
    }

    public static DateTime StopProgram()
    {
        Stop = DateTime.Now;
        return Stop;
    }
}
Rovshan Huseynov

Ответы похожие на “Калькулятор треугольника”

Смотреть популярные ответы по языку

Смотреть другие языки программирования