Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _02_Experiment
- {
- class Program
- {
- static public void Main()
- {
- Console.Write("What figure you want to work with (Triangle / Rectangle / Circle / Square) ");
- string figure = Console.ReadLine().ToLower();
- if (figure == "triangle")
- {
- Triangle triangle = new Triangle();
- }
- else if (figure == "rectangle")
- {
- Rectangle rectangle = new Rectangle();
- }
- else if (figure == "circle")
- {
- Circle circle = new Circle();
- for (int i = 0; i < 1; i++)
- {
- Console.Write("Your input is a (Radius / Diameter) ");
- string input = Console.ReadLine().ToLower();
- double diaOrRad = 0;
- if (input == "diameter")
- {
- try
- {
- Console.Write("Diameter's values is: ");
- diaOrRad += double.Parse(Console.ReadLine());
- if (Verification.TooSmallValues(diaOrRad))
- {
- i--;
- continue;
- }
- else
- {
- circle.Radius = diaOrRad / 2;
- }
- circle.Choose();
- }
- catch (FormatException)
- {
- Console.WriteLine("Please enter numbers for the element's value!\n");
- i--;
- continue;
- }
- }
- else if (input == "radius")
- {
- try
- {
- Console.Write("Radius' values is: ");
- diaOrRad += double.Parse(Console.ReadLine());
- if (Verification.TooSmallValues(diaOrRad))
- {
- i--;
- continue;
- }
- else
- {
- circle.Radius = diaOrRad;
- }
- circle.Choose();
- }
- catch
- {
- Console.WriteLine("Please enter numbers for the element's value!\n");
- i--;
- continue;
- }
- }
- else
- {
- Console.WriteLine("\nPlease choose for your input one of the " +
- "options we have presented you in the upper line!\n");
- i--;
- continue;
- }
- }
- }
- else if (figure == "square")
- {
- Square square = new Square();
- for (int i = 0; i < 1; i++)
- {
- Console.Write("Your input is a (Side / Diagonal) ");
- string input = Console.ReadLine().ToLower();
- double sideOrDia = 0;
- if (input == "side")
- {
- try
- {
- Console.Write("Side's values is: ");
- sideOrDia += double.Parse(Console.ReadLine());
- if (Verification.TooSmallValues(sideOrDia))
- {
- i--;
- continue;
- }
- else
- {
- square.Side = sideOrDia;
- }
- square.Choose();
- }
- catch (FormatException)
- {
- Console.WriteLine("Please enter numbers for the element's value!\n");
- i--;
- continue;
- }
- }
- else if (input == "diagonal")
- {
- try
- {
- Console.Write("Diagonal's value is: ");
- sideOrDia += double.Parse(Console.ReadLine());
- if (Verification.TooSmallValues(sideOrDia))
- {
- i--;
- continue;
- }
- else
- {
- square.Diagonal = sideOrDia;
- }
- square.Choose();
- }
- catch
- {
- Console.WriteLine("Please enter numbers for the element's value!\n");
- i--;
- continue;
- }
- }
- else
- {
- Console.WriteLine("\nPlease choose for your input one of the " +
- "options we have presented you in the upper line!\n");
- i--;
- continue;
- }
- }
- }
- }
- }
- static class Verification
- {
- static public bool FirstUnequalityTheorem(double side1, double side2, double side3)
- {
- if (side1 < side2 + side3 && side2 < side1 + side3 && side3 < side1 + side2)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- static public bool TooSmallValues(double value)
- {
- if (value <= 0)
- {
- Console.WriteLine("\nYou have entered too small values! Please enter again the type of your input with a correct value!\n");
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- class Triangle
- {
- private double area;
- private double perimeter;
- private static int count = 0;
- public Triangle()
- {
- Console.Write("You want to find (Area / Perimeter / Both) of a triangle: ");
- string faceOrArea = Console.ReadLine().ToLower();
- if (faceOrArea == "area")
- {
- Area();
- }
- else if (faceOrArea == "perimeter")
- {
- Perimeter();
- }
- else if (faceOrArea == "both")
- {
- AreaAndPerimeter();
- }
- else
- {
- Console.WriteLine("\nPlease choose for your input one of the options we have presented you in the upper line!\n");
- }
- }
- private void AreaAndPerimeter()
- {
- string[] order = new string[] { "a", "b", "c" };
- double[] values = new double[3];
- Console.WriteLine("\nEnter the values of the three sides of the triangle:");
- for (int i = 0; i < 3; i++)
- {
- Console.Write($"{order[i]} = ");
- values[i] = double.Parse(Console.ReadLine());
- if (Verification.TooSmallValues(values[i]))
- {
- i--;
- continue;
- }
- }
- if (Verification.FirstUnequalityTheorem(values[0], values[1], values[2]))
- {
- perimeter = values[0] + values[1] + values[2];
- area = HeronFormula(values[0], values[1], values[2]);
- Console.WriteLine("\nThe perimeter of that triangle is: {0:N2} and his area is {1:N2}", perimeter, area);
- }
- else
- {
- Console.WriteLine("\nIt is impossible to exist such a triangle according to the UNEQUALITY THEOREM ONE!\n");
- StartAgain();
- }
- }
- private void Perimeter()
- {
- string[] order = new string[] { "a", "b", "c" };
- double[] values = new double[3];
- Console.Write("\nEnter the values of the three sides of the triangle: \n");
- for (int i = 0; i < 3; i++)
- {
- Console.Write($"{order[i]} = ");
- values[i] = double.Parse(Console.ReadLine());
- if (Verification.TooSmallValues(values[i]))
- {
- i--;
- continue;
- }
- }
- if (Verification.FirstUnequalityTheorem(values[0], values[1], values[2]))
- {
- perimeter = values[0] + values[1] + values[2];
- Console.WriteLine("\nThe perimeter of that triangle is: {0:N2}", perimeter);
- }
- else
- {
- Console.WriteLine("\nIt is impossible to exist such a triangle according to the UNEQUALITY THEOREM ONE!\n");
- StartAgain();
- }
- }
- private void Area()
- {
- string[] order = new string[] { "first", "second", "third" };
- string[] elements = new string[3];
- double[] values = new double[3];
- for (int i = 0; i < 3; i++)
- {
- Console.Write($"Your {order[i]} input is a (Side / Height / Angle) ");
- elements[i] = Console.ReadLine().ToLower();
- //Wrong element input;
- if (!(elements[i] == "side" || elements[i] == "height" || elements[i] == "angle"))
- {
- Console.WriteLine("\nPlease choose for your input one of the options we have presented you in the upper line!\n");
- i--;
- continue;
- }
- Console.Write("Its vaue is ");
- values[i] = double.Parse(Console.ReadLine());
- //Wrong value input;
- if (Verification.TooSmallValues(values[i]))
- {
- i--;
- continue;
- }
- //Wrong angle value input;
- else if (elements[i] == "angle" && (values[i] <= 0 || values[i] >= 180))
- {
- Console.WriteLine("\nYou have impossible values for angle in a triangle! Please enter again the type of your input with a correct value!\n");
- i--;
- continue;
- }
- //Area found by a base side and its height:
- if ((elements[0] == "side" || elements[1] == "side") && (elements[0] == "height" || elements[1] == "height"))
- {
- area = BaseAndHeight(values[Array.IndexOf(elements, "side")], values[Array.IndexOf(elements, "height")]);
- if (area != 0)
- {
- Console.WriteLine("The area of that triangle is: {0:N2}", area);
- break;
- }
- else
- {
- Console.Write("Do you know the base side of that triangle (Yes / No) ");
- string ifKnow = Console.ReadLine().ToLower();
- if (ifKnow == "yes")
- {
- Console.Write("Its vaue is ");
- double sideIfKnow = double.Parse(Console.ReadLine());
- area = BaseAndHeight(sideIfKnow, values[Array.IndexOf(elements, "height")]);
- Console.WriteLine("The area of that triangle is: {0:N2}", area);
- break;
- }
- else
- {
- Console.WriteLine("There is no such a formula to calculate the triangle's area using your input data.");
- StartAgain();
- }
- }
- }
- }
- //Finding out how many the sides in the triangle are:
- int numOfSides = 0;
- foreach (string element in elements)
- {
- if (element == "side")
- {
- numOfSides++;
- }
- }
- //Area of a rectangular triangle:
- //Area found by an angle and the other two sides:
- bool predict = elements.Contains("angle");
- if (predict)
- {
- //Area of a rectangular triangle:
- double angle = values[Array.IndexOf(elements, "angle")];
- if (angle == 90 && numOfSides == 2)
- {
- area = RectangularTriangle(values[Array.IndexOf(elements, "side")], values[Array.LastIndexOf(elements, "side")]);
- Console.WriteLine("The area of that triangle is: {0:N2}", area);
- }
- //Area found by an angle and the other two sides:
- else if (angle != 90 && numOfSides == 2)
- {
- area = AngleAndTwoSides(values[Array.IndexOf(elements, "angle")], values[Array.IndexOf(elements, "side")], values[Array.LastIndexOf(elements, "side")]);
- Console.WriteLine("The area of that triangle is: {0:N2}", area);
- }
- }
- //Area found using The Heron's formula and checking the first unequality theorem:
- else
- {
- if (numOfSides == 3)
- {
- if (Verification.FirstUnequalityTheorem(values[0], values[1], values[2]))
- {
- area = HeronFormula(values[0], values[1], values[2]);
- Console.WriteLine("The area of that triangle is: {0:N2}", area);
- }
- else
- {
- Console.WriteLine("\nIt is impossible to exist such a triangle according to the UNEQUALITY THEOREM ONE!\n");
- StartAgain();
- }
- }
- else if (numOfSides != 3 && area == 0)
- {
- Console.WriteLine("There is no way to calculate the area of the triangle!");
- StartAgain();
- }
- }
- }
- private double BaseAndHeight(double side, double height)
- {
- Console.Write("Is this the base side of the height (Yes / No) ");
- string yesOrNo = Console.ReadLine().ToLower();
- if (yesOrNo == "yes")
- {
- return side * height / 2;
- }
- else if (yesOrNo == "no")
- {
- return 0.0;
- }
- else
- {
- Console.WriteLine("You entered incorrect values");
- StartAgain();
- return 0.0;
- }
- }
- private double RectangularTriangle(double side1, double side2)
- {
- Console.WriteLine("Do these two sides form the right angle (Yes / No) ");
- string yesOrNo = Console.ReadLine().ToLower();
- if (yesOrNo == "yes")
- {
- return side1 * side2 / 2;
- }
- else
- {
- StartAgain();
- return 0.0;
- }
- }
- private double AngleAndTwoSides(double angle, double side1, double side2)
- {
- Console.Write("Is this angle between the two other sides (Yes / No) ");
- string yesOrNo = Console.ReadLine().ToLower();
- if (yesOrNo == "yes")
- {
- return side1 * side2 * Math.Abs(Math.Sin(angle * Math.PI / 180)) / 2;
- }
- else
- {
- StartAgain();
- return 0.0;
- }
- }
- private double HeronFormula(double side1, double side2, double side3)
- {
- double halfPerimeter = (side1 + side2 + side3) / 2;
- return Math.Sqrt(halfPerimeter * (halfPerimeter - side1) * (halfPerimeter - side2) * (halfPerimeter - side3));
- }
- static public void StartAgain()
- {
- Console.Write("Do you want to start from the beginning (Yes / No) ");
- string startAgain = Console.ReadLine().ToLower();
- if (startAgain == "yes")
- {
- Console.Clear();
- Program.Main();
- }
- else if (startAgain == "no")
- {
- Console.WriteLine("Thank you for the time spent.");
- }
- else
- {
- Console.WriteLine("\nPlease choose for your input one of the options we have presented you in the upper line!\n");
- StartAgain();
- }
- }
- private void End(bool end)
- {
- if (end == true)
- {
- Console.WriteLine("\nThank you for your participation!");
- }
- }
- ~Triangle()
- {
- bool end = true;
- if (count > 0)
- {
- end = false;
- }
- End(end);
- count++;
- }
- }
- class Square
- {
- private double area;
- private double perimeter;
- private double diagonal;
- private double side;
- public double Side
- {
- get
- {
- return side;
- }
- set
- {
- side += value;
- }
- }
- public double Diagonal
- {
- get
- {
- return diagonal;
- }
- set
- {
- diagonal += value;
- }
- }
- public void Choose()
- {
- for (int i = 0; i < 1; i++)
- {
- Console.Write("You want to find (Area / Perimeter / Both) of a circle: ");
- string faceOrArea = Console.ReadLine().ToLower();
- if (faceOrArea == "area")
- {
- Area();
- }
- else if (faceOrArea == "perimeter")
- {
- Perimeter();
- }
- else if (faceOrArea == "both")
- {
- AreaAndPerimeter();
- }
- else
- {
- Console.WriteLine("\nPlease choose for your input one of the options we have presented you in the upper line!\n");
- }
- }
- }
- private void Area()
- {
- area = Math.Pow(side, 2);
- Console.WriteLine(area);
- }
- private void Perimeter()
- {
- perimeter = 4 * side;
- Console.WriteLine(perimeter);
- }
- private void AreaAndPerimeter()
- {
- area = Math.Pow(side, 2);
- perimeter = 4 * side;
- Console.WriteLine("The area of the square is: {0:N2}", area);
- Console.WriteLine("The perimeter of the square is: {0:N2}", perimeter);
- }
- }
- class Rectangle
- {
- private static double area;
- private static double perimeter;
- private static int count = 0;
- public Rectangle()
- {
- Console.Write("You want to find (Area / Perimeter / Both) of a rectangle: ");
- string faceOrArea = Console.ReadLine().ToLower();
- for (int i = 0; i < 1; i++)
- {
- if (faceOrArea == "area")
- {
- Area();
- }
- else if (faceOrArea == "perimeter")
- {
- Perimeter();
- }
- else if (faceOrArea == "both")
- {
- AreaAndPerimeter();
- }
- else
- {
- Console.WriteLine("\nPlease choose for your input one of the options we have presented you in the upper line!\n");
- i--;
- continue;
- }
- }
- }
- private void Area()
- {
- string[] order = new string[] { "first", "second" };
- string[] elements = new string[2];
- double[] values = new double[2];
- for (int i = 0; i < 2; i++)
- {
- Console.Write($"Your {order[i]} input is a (Side / Perimeter / Diagonal) ");
- elements[i] = Console.ReadLine().ToLower();
- //Wrong element input;
- if (!(elements[i] == "side" || elements[i] == "perimeter" || elements[i] == "diagonal"))
- {
- Console.WriteLine("\nPlease choose for your input one of the options we have presented you in the upper line!\n");
- i--;
- continue;
- }
- Console.Write("Its vaue is ");
- try
- {
- values[i] = double.Parse(Console.ReadLine());
- //Wrong value input;
- if (Verification.TooSmallValues(values[i]))
- {
- i--;
- continue;
- }
- }
- catch (FormatException)
- {
- Console.WriteLine("Please enter numbers for the element's value!\n");
- i--;
- continue;
- }
- }
- if (elements[0] == "side" && elements[1] == "side")
- {
- area = AreaOfTwoSides(values[0], values[1]);
- }
- else if ((elements[0] == "perimeter" || elements[1] == "perimeter") && (elements[0] == "side" || elements[1] == "side"))
- {
- area = AreaOfPerimeterAndSide(values[Array.IndexOf(elements, "perimeter")], values[Array.IndexOf(elements, "side")]);
- }
- else if ((elements[0] == "diagonal" || elements[1] == "diagonal") && (elements[0] == "side" || elements[1] == "side"))
- {
- area = AreaOfDiagonalAndSide(values[Array.IndexOf(elements, "diagonal")], values[Array.IndexOf(elements, "side")]);
- }
- else
- {
- Console.WriteLine("There is no such a formula to calculate the rectangle's area using your input data.");
- }
- if (area != 0)
- {
- Console.WriteLine("The area of the rectangle is: {0:N2}", area);
- }
- else
- {
- Console.WriteLine("Something with your input was wrong!");
- StartAgain();
- }
- }
- private void Perimeter()
- {
- string[] order = new string[] { "a", "b" };
- double[] values = new double[2];
- Console.Write("\nEnter the values of two adjacent sides of the rectangle:\n");
- for (int i = 0; i < 2; i++)
- {
- try
- {
- Console.Write($"{order[i]} = ");
- values[i] = double.Parse(Console.ReadLine());
- if (Verification.TooSmallValues(values[i]))
- {
- i--;
- continue;
- }
- }
- catch (FormatException)
- {
- Console.WriteLine("Please enter numbers for the element's value!\n");
- i--;
- continue;
- }
- }
- perimeter = PerimeterOfTwoSides(values[0], values[1]);
- if (perimeter != 0)
- {
- Console.WriteLine("The perimeter of the rectangle is: {0:N2}", perimeter);
- }
- else
- {
- Console.WriteLine("Something with your input was wrong!");
- StartAgain();
- }
- }
- private void AreaAndPerimeter()
- {
- string[] order = new string[] { "a", "b" };
- double[] values = new double[2];
- Console.Write("\nEnter the values of two adjacent sides of the rectangle:\n");
- for (int i = 0; i < 2; i++)
- {
- try
- {
- Console.Write($"{order[i]} = ");
- values[i] = double.Parse(Console.ReadLine());
- if (Verification.TooSmallValues(values[i]))
- {
- i--;
- continue;
- }
- }
- catch (FormatException)
- {
- Console.WriteLine("Please enter numbers for the element's value!\n");
- i--;
- continue;
- }
- }
- area = AreaOfTwoSides(values[0], values[1]);
- if (area != 0)
- {
- perimeter = 2 * (values[0] + values[1]);
- Console.WriteLine("The area of the rectangle is: {0:N2}", area);
- Console.WriteLine("The perimeter of the rectangle is: {0:N2}", perimeter);
- }
- }
- private double AreaOfDiagonalAndSide(double d, double side)
- {
- double faceRect = side * Math.Sqrt(Math.Pow(d, 2) - Math.Pow(side, 2));
- if (d > side)
- {
- return faceRect;
- }
- else
- {
- Console.WriteLine("\nYou have entered impossible values for the diagonal!");
- StartAgain();
- return 0.0;
- }
- }
- private double AreaOfPerimeterAndSide(double p, double side)
- {
- if (p <= 2 * side)
- {
- Console.WriteLine("You have entered an incorrect input!");
- return 0.0;
- }
- else
- {
- return (p * side - 2 * Math.Pow(side, 2)) / 2;
- }
- }
- private double AreaOfTwoSides(double side1, double side2)
- {
- if (side1 == side2)
- {
- Console.Write("\nYou entered two equal values. Do you agree with your input (Yes / No) ");
- string square = Console.ReadLine().ToLower();
- if (square == "yes")
- {
- return side1 * side2;
- }
- else
- {
- StartAgain();
- return 0.0;
- }
- }
- else
- {
- return side1 * side2;
- }
- }
- private double PerimeterOfTwoSides(double side1, double side2)
- {
- if (side1 == side2)
- {
- Console.Write("\nYou entered two equal values. Do you agree with your input (Yes / No) ");
- string square = Console.ReadLine().ToLower();
- if (square == "yes")
- {
- return 2 * (side1 + side2);
- }
- else
- {
- StartAgain();
- return 0.0;
- }
- }
- else
- {
- return 2 * (side1 + side2);
- }
- }
- static public void StartAgain()
- {
- Console.Write("\nDo you want to start from the beginning (Yes / No) ");
- string startAgain = Console.ReadLine().ToLower();
- if (startAgain == "yes")
- {
- Console.Clear();
- Program.Main();
- }
- else if (startAgain == "no")
- {
- Console.WriteLine("Thank you for the time spent.");
- }
- else
- {
- Console.WriteLine("\nPlease choose for your input one of the options we have presented you in the upper line!\n");
- StartAgain();
- }
- }
- private void End(bool end)
- {
- if (end == true)
- {
- Console.WriteLine("\nThank you for your participation!");
- }
- }
- ~Rectangle()
- {
- bool end = true;
- if(count > 0)
- {
- end = false;
- }
- End(end);
- count++;
- }
- }
- class Circle
- {
- private double area;
- private double perimeter;
- private double radius;
- private static int count = 0;
- public double Radius
- {
- get
- {
- return radius;
- }
- set
- {
- radius += value;
- }
- }
- public void Choose()
- {
- for (int i = 0; i < 1; i++)
- {
- Console.Write("You want to find (Area / Perimeter / Both) of a circle: ");
- string faceOrArea = Console.ReadLine().ToLower();
- if (faceOrArea == "area")
- {
- Area();
- }
- else if (faceOrArea == "perimeter")
- {
- Perimeter();
- }
- else if (faceOrArea == "both")
- {
- AreaAndPerimeter();
- }
- else
- {
- Console.WriteLine("\nPlease choose for your input one of the options we have presented you in the upper line!\n");
- }
- }
- }
- private void Area()
- {
- area = Math.PI * Math.Pow(radius, 2);
- if (area != 0)
- {
- Console.WriteLine("The area of the circle is: {0:N2}", area);
- }
- else
- {
- Console.WriteLine("Something with your input was wrong!");
- StartAgain();
- }
- }
- private void Perimeter()
- {
- perimeter = 2 * Math.PI * radius;
- if (perimeter != 0)
- {
- Console.WriteLine("The perimeter of the circle is: {0:N2}", perimeter);
- }
- else
- {
- Console.WriteLine("Something with your input was wrong!");
- StartAgain();
- }
- }
- private void AreaAndPerimeter()
- {
- area = Math.PI * Math.Pow(radius, 2);
- perimeter = 2 * Math.PI * radius;
- if (area != 0 && perimeter != 0)
- {
- Console.WriteLine("The area of the circle is: {0:N2}", area);
- Console.WriteLine("The perimeter of the circle is: {0:N2}", perimeter);
- }
- else
- {
- Console.WriteLine("\nSomething with your input was wrong!");
- StartAgain();
- }
- }
- static public void StartAgain()
- {
- Console.Write("Do you want to start from the beginning (Yes / No) ");
- string startAgain = Console.ReadLine().ToLower();
- if (startAgain == "yes")
- {
- Console.Clear();
- Program.Main();
- }
- else if (startAgain == "no")
- {
- Console.WriteLine("Thank you for the time spent.");
- }
- else
- {
- Console.WriteLine("\nPlease choose for your input one of the options we have presented you in the upper line!\n");
- StartAgain();
- }
- }
- private void End(bool end)
- {
- if (end == true)
- {
- Console.WriteLine("\nThank you for your participation!");
- }
- }
- ~Circle()
- {
- bool end = true;
- if (count > 0)
- {
- end = false;
- }
- End(end);
- count++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment