// 获得三角形类型描述 public static string GetTriangleType(TriangleDefinition triangle) { TriangleType type = TriangleType.CommonTriangle; // 判断类型 if (!IsTriangle(triangle)) { type = TriangleType.InvalidTriangle; } else if (IsRightTriangle(triangle)) { type = TriangleType.RightTriangle; } else if (IsEquilateralTriangle(triangle)) { type = TriangleType.EquilateralTriangle; } else if (IsoscelesTriangle(triangle)) { type = TriangleType.IsoscelesTriangle; } // 返回类型描述字符串 return(type.GetDescription()); }
public void Given_Valid_Input_Output_Right_TypeDescription() { // 期望结果 Arrange string common_triangle = "一般三角形"; string isosceles_triangle = "等腰三角形"; string right_triangle = "直角三角形"; string equilateral_triangle = "等边三角形"; // 测试用例 TriangleDefinition com_triangle = new TriangleDefinition(2, 3, 4); TriangleDefinition iso_triangle = new TriangleDefinition(1, 2, 2); TriangleDefinition rig_triangle = new TriangleDefinition(3, 4, 5); TriangleDefinition equ_triangle = new TriangleDefinition(6, 6, 6); // 运行判定 Act and Assert TestContext.WriteLine(Evaluator.GetTriangleType(com_triangle)); Assert.AreEqual(Evaluator.GetTriangleType(com_triangle), common_triangle); TestContext.WriteLine(Evaluator.GetTriangleType(iso_triangle)); Assert.AreEqual(Evaluator.GetTriangleType(iso_triangle), isosceles_triangle); TestContext.WriteLine(Evaluator.GetTriangleType(rig_triangle)); Assert.AreEqual(Evaluator.GetTriangleType(rig_triangle), right_triangle); TestContext.WriteLine(Evaluator.GetTriangleType(equ_triangle)); Assert.AreEqual(Evaluator.GetTriangleType(equ_triangle), equilateral_triangle); }
// 判断是否是等边三角形 public static bool IsEquilateralTriangle(TriangleDefinition triangle) { if (triangle.A == triangle.B && triangle.B == triangle.C) { return(true); } return(false); }
// 判断是否是直角三角形 public static bool IsRightTriangle(TriangleDefinition triangle) { if (Math.Pow(triangle.A, 2) + Math.Pow(triangle.B, 2) == Math.Pow(triangle.C, 2) || Math.Pow(triangle.A, 2) + Math.Pow(triangle.C, 2) == Math.Pow(triangle.B, 2) || Math.Pow(triangle.B, 2) + Math.Pow(triangle.C, 2) == Math.Pow(triangle.A, 2)) { return(true); } return(false); }
public void CheckTriangle() { double a = 5; double b = 4; double c = 3; double expected = 6; TriangleDefinition triangle = new TriangleDefinition(a, b, c); Figure figure = triangle.shapeDefinition(); Assert.AreEqual(expected, figure.getSquare()); }
// 判断是否是三角形 public static bool IsTriangle(TriangleDefinition triangle) { if (triangle.A <= 0 || triangle.B <= 0 || triangle.C <= 0 || triangle.A + triangle.B <= triangle.C || triangle.A + triangle.C <= triangle.B || triangle.B + triangle.C <= triangle.A) { return(false); } return(true); }
// 判断是否是等腰三角形 public static bool IsoscelesTriangle(TriangleDefinition triangle) { if (triangle.A == triangle.B || triangle.B == triangle.C || triangle.A == triangle.C && !IsEquilateralTriangle(triangle)) { return(true); } return(false); }