/* * Aresh Saharkhiz * saharkiz@gmail.com * Associate Professor / Mapua Institute of Technology / Philippines */ using System; using System.IO; // Introduction to Software Testing // Authors: Paul Ammann & Jeff Offutt // Chapter 3, section 3.3, page 121 // Jeff Offutt--Java version Feb 2003 // Classify triangles internal class trityp { private static string[] triTypes = new string[] { "", "scalene", "isosceles", "equilateral", "not a valid triangle" }; // Ignore 0. private static string instructions = "This is the ancient TriTyp program.\nEnter three integers that represent the lengths of the sides of a triangle.\nThe triangle will be categorized as either scalene, isosceles, equilateral\nor invalid.\n"; public static void Main(string[] argv) { // Driver program for trityp int A, B, C; int T; Console.WriteLine(instructions); Console.WriteLine("Enter side 1: "); A = N; Console.WriteLine("Enter side 2: "); B = N; Console.WriteLine("Enter side 3: "); C = N; T = Triang(A, B, C); Console.WriteLine("Result is: " + triTypes[T]); } // ==================================== // The main triangle classification method private static int Triang(int Side1, int Side2, int Side3) { int triOut; // triOut is output from the routine: // Triang = 1 if triangle is scalene // Triang = 2 if triangle is isosceles // Triang = 3 if triangle is equilateral // Triang = 4 if not a triangle // After a quick confirmation that it's a valid // triangle, detect any sides of equal length if (Side1 <= 0 || Side2 <= 0 || Side3 <= 0) { triOut = 4; return (triOut); } triOut = 0; if (Side1 == Side2) { triOut = triOut + 1; } if (Side1 == Side3) { triOut = triOut + 2; } if (Side2 == Side3) { triOut = triOut + 3; } if (triOut == 0) { // Confirm it's a valid triangle before declaring // it to be scalene if (Side1 + Side2 <= Side3 || Side2 + Side3 <= Side1 || Side1 + Side3 <= Side2) { triOut = 4; } else { triOut = 1; } return (triOut); } // Confirm it's a valid triangle before declaring // it to be isosceles or equilateral if (triOut > 3) { triOut = 3; } else if (triOut == 1 && Side1 + Side2 > Side3) { triOut = 2; } else if (triOut == 2 && Side1 + Side3 > Side2) { triOut = 2; } else if (triOut == 3 && Side2 + Side3 > Side1) { triOut = 2; } else { triOut = 4; } return (triOut); } // end Triang // ==================================== // Read (or choose) an integer private static int N { get { int inputInt = 1; string inStr; try { inStr = Console.ReadLine(); inputInt = Convert.ToInt32(inStr); } catch (IOException) { Console.WriteLine("Could not read input, choosing 1."); } catch (FormatException) { Console.WriteLine("Entry must be a number, choosing 1."); } return (inputInt); } } // end getN } // end trityp class