python判断三角形类型的代码_c#教程之C#判断三角形的类型

https://www.xin3721.com/eschool/python.html

题目描述:

输入三角形的三条边长,判断是否能构成一个三角形(不考虑退化三角形,即面积为零的三角形),是什么样的三角形(直角、锐角、钝角、等边、等腰)。

函数声明为:byte GetTriangleType(int,int,int)

1. 如何用一个byte来表示各种输出情况?

2. 如果你是一名测试工程师,应该如何写测试用例来完成功能测试呢?

题目解析:

对于如何用一个byte表示各种输出情况,不是很清楚,有待研究。

下面的程序我只是实现了功能,并没有按照给定的函数声明的格式完成,大家可以参考参考

UI:

20151110113428716.png?20151010113450

代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows.Forms;

using System.Text.RegularExpressions;

namespace TriangleTest

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Test_Click(object sender, EventArgs e)

{

//等腰,等边,直角,钝角,锐角。

Dictionary result =new Dictionary();

result.Add("等腰", 0);

result.Add("等边", 0);

result.Add("直角", 0);

result.Add("钝角", 0);

result.Add("锐角", 0);

var t1 = edge1.Text;

var t2 = edge2.Text;

var t3 = edge3.Text;

if (CheckInput(t1, t2, t3))

{

var e1 =double.Parse(edge1.Text);

var e2 =double.Parse(edge2.Text);

var e3 =double.Parse(edge3.Text);

double[] Numbers =new double[] { e1, e2, e3 };

double powSum = Math.Pow(e1, 2) + Math.Pow(e2, 2) + Math.Pow(e3, 2);

double max = Numbers.Max();

if (CheckTriangle(e1, e2, e3))

{

//三角形。

result["等腰"] = CheckEquicrural(e1, e2, e3) ? 1 : 0;

result["等边"] = CheckEquilateral(e1, e2, e3) ? 1 : 0;

result["直角"] = CheckRightAngle(powSum, max) ? 1 : 0;

result["钝角"] = CheckObtuseAngle(powSum, max) ? 1 : 0;

result["锐角"] = CheckAcuteAngle(powSum, max) ? 1 : 0;

string resultTip = result["等腰"] == 1 ?"等腰" :"";

resultTip += result["等边"] == 1 ?"等边" :"";

resultTip += result["直角"] == 1 ?"直角" :"";

resultTip += result["钝角"] == 1 ?"钝角" :"";

resultTip += result["锐角"] == 1 ?"锐角" :"";

resultTip +="三角形";

MessageBox.Show(resultTip);

}

else

{

//不是三角形。

MessageBox.Show("您输入的三边构不成三角形!");

}

}

else

{

//输入非法。

MessageBox.Show("您输入的信息有问题!");

}

}

private bool CheckAcuteAngle(double powSum,double max)

{

return (Math.Pow(max, 2) < powSum - Math.Pow(max, 2)) ?true :false;

}

private bool CheckObtuseAngle(double powSum,double max)

{

return (Math.Pow(max, 2) > powSum - Math.Pow(max, 2)) ?true :false;

}

private bool CheckRightAngle(double powSum,double max)

{

return (Math.Pow(max, 2) == powSum - Math.Pow(max, 2)) ?true :false;

}

private bool CheckEquicrural(double e1,double e2,double e3)

{

return (e1 == e2 && e2 == e3) ?true :false;

}

private bool CheckEquilateral(double e1,double e2,double e3)

{

return (e1 == e2 || e2 == e3 || e3 == e1) ?true :false;

}

private bool CheckTriangle(double edge1,double edge2,double edge3)

{

double[] edges =new double[] { edge1, edge2, edge3 };

double sum = edges[0] + edges[1] + edges[2];

int succFlag = 0;

for (int i = 0; i < edges.Count(); i++)

{

if (edges[i] < sum - edges[i])

{

succFlag++;

}

}

if (succFlag == 3)

{

return true;

}

else

{

return false;

}

}

private bool CheckInput(string edge1,string edge2,string edge3)

{

bool result =false;

Regex reg =new Regex("^[0-9]*$");

if (reg.IsMatch(edge1) && reg.IsMatch(edge2) && reg.IsMatch(edge3))

{

if (Int32.Parse(edge1) > 0 && Int32.Parse(edge2) > 0 && Int32.Parse(edge3) > 0)

{

result =true;

}

}

return result;

}

}

}

Run:

20151110113509532.png?20151010113516

2. 对于功能测试而言:

1)值的类型测试:注意输入值的种类(整形,浮点型,字符串类型等),检查对于非法值类型是否有控制逻辑;

2)值的边界测试:注意输入值的范围(只能为非负数),检查超出范围时是否有控制逻辑;

3)以结果为导向的测试:分别对非三角形,三角形中的等腰、等边、直角、钝角、锐角做出几组符合要求的测试数据,检查Test结果是否正确;

4)值的长度测试:根据需求检查输入值达到最大值长度时,是否能够正常Test。

C#判断三角形的类型基本功能已经是现实,但是并没有按照给定的函数声明的格式完成,还有待于提高,或者是大家有什么好的建议,可以提出,尝试去解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值