Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

triangle

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;
    }
}
 
PREVIOUS NEXT
Tagged: #triangle
ADD COMMENT
Topic
Name
1+9 =