Triangle check

Write me a function that receives three integer inputs for the lengths of the sides of a triangle and returns one of four values to determine the triangle type (1=scalene, 2=isosceles, 3=equilateral, 4=error). Generate test cases for the function assuming another developer coded the function

 

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Linq;

   4: using System.Text;

   5:  

   6: namespace Triangle

   7: {

   8:     class Program

   9:     {

  10:         public enum TriangleType

  11:         {

  12:             Scalene = 1,

  13:             Isosceles = 2,

  14:             Equilateral = 3,

  15:             Error = 4,

  16:         }

  17:         private static TriangleType triangle(int a, int b, int c)

  18:         {

  19:             TriangleType answer;

  20:  

  21:             if (a <= 0 || b <= 0 || c <= 0)

  22:             {

  23:                 // negative length -- illegal

  24:                 answer =  TriangleType.Error;

  25:             }

  26:             else if ((a + b <= c) || (b + c <= a) || (a + c <= b))

  27:             {

  28:                 // Sides can't possible connect -- illegal

  29:                 answer = TriangleType.Error;

  30:             }

  31:             else if ((a == b) && (b == c))

  32:             {

  33:                 // by the transitive rule, a == c well

  34:                 answer = TriangleType.Equilateral;

  35:             }

  36:             else if ((a == b) || (b == c) || (a == c))

  37:             {

  38:                 // We've already eliminated quilateral triangles above,

  39:                 // we'll only get here if exactly two sides match

  40:                 answer = TriangleType.Isosceles;

  41:             }

  42:             else

  43:             {

  44:                 // Since, we've eliminated all alternatives, we must have

  45:                 // a scalene triangle if we get here.

  46:                 answer = TriangleType.Scalene;

  47:             }

  48:  

  49:             return answer;

  50:         }

  51:  

  52:         static void Main(string[] args)

  53:         {

  54:             TriangleType result;

  55:             result = triangle(0, 0, 0);

  56:             Console.WriteLine("(0, 0, 0) = {0}", result.ToString());

  57:  

  58:             result = triangle(-1, 2, 3);

  59:             Console.WriteLine("(-1, 2, 3) = {0}", result.ToString());

  60:  

  61:             result = triangle(1, -2, 3);

  62:             Console.WriteLine("(1, -2, 3) = {0}", result.ToString());

  63:  

  64:             result = triangle(1, 2, -3);

  65:             Console.WriteLine("(1, 2, -3) = {0}", result.ToString());

  66:  

  67:             result = triangle(1, 1, 1);

  68:             Console.WriteLine("(1, 1, 1) = {0}", result.ToString());

  69:  

  70:             result = triangle(1, 2, 2);

  71:             Console.WriteLine("(1, 2, 2) = {0}", result.ToString());

  72:  

  73:             result = triangle(4, 5, 6);

  74:             Console.WriteLine("(4, 5, 6) = {0}", result.ToString());

  75:  

  76:             result = triangle(5, 1, 5);

  77:             Console.WriteLine("(5, 1, 5) = {0}", result.ToString());

  78:  

  79:             result = triangle(5, 5, 5);

  80:             Console.WriteLine("(5, 5, 5) = {0}", result.ToString());

  81:  

  82:             result = triangle(-10, -10, -10);

  83:             Console.WriteLine("(-10, -10, -10) = {0}", result.ToString());

  84:         }

  85:     }

  86: }

Advertisements