# 面试题:编写一个合法的三角形类

0

``` 1     public class Triangel
2     {
3         public double a;
4         public double b;
5         public double c;
6         public Triangel(double a, double b, double c)
7         {
8             Boolean result1 = a > 0 && b > 0 && c > 0;
9             Boolean result2 = a + b > c && a + c > b && b + c > a;
10             Boolean result3 = Math.Abs(a - b) < c && Math.Abs(a - c) < b &&
11                               Math.Abs(b - c) < a;
12             if (result1 && result2 && result3)
13             {
14                 this.a = a;
15                 this.b = b;
16                 this.c = c;
17             }
18             else
19             {
20                 throw new Exception("构造三角形的参数非法~!");
21             }
22         }
23
24         public double GetZhouchang()
25         {
26             return a + b + c;
27         }
28     }
29
30     public class RightAngelTrigel : Triangel
31     {
32         public RightAngelTrigel(double a, double b, double c)
33             : base(a, b, c)//基类不包含零个参数的构造函数.
34         {
35         }
36         public double GetMianji()
37         {
38             double max = GetMax(a, b, c);
39             if (max == a)
40             {
41                 return b * c / 2;
42             }
43             else if (max == b)
44             {
45                 return a * c / 2;
46             }
47             else
48             {
49                 return a * b / 2;
50             }
51         }
52
53         public double GetMax(double a, double b, double c)
54         {
55             double max = 0;
56             if (a > max)
57             {
58                 max = a;
59             }
60             if (b > max)
61             {
62                 max = b;
63             }
64             if (c > max)
65             {
66                 max = c;
67             }
68             return max;
69         }
70
71     }```

3

```    class Triangle
{
public Triangle(decimal a, decimal b, decimal c)
{
SortedEdges = new[] { a, b, c };
Array.Sort(SortedEdges);

if (SortedEdges[0] <= 0 || SortedEdges[0] + SortedEdges[1] <= SortedEdges[2])
{
throw new ArgumentException("invalid edge length for a triangle");
}
}

public decimal[] SortedEdges { get; private set; }

public decimal GetPerimeter()
{
return SortedEdges.Sum();
}
}

class RightAngledTriangle : Triangle
{
public RightAngledTriangle(decimal a, decimal b, decimal c)
: base(a, b, c)
{
if (SortedEdges[0] * SortedEdges[0]
+ SortedEdges[1] * SortedEdges[1]
!= SortedEdges[2] * SortedEdges[2])
{
throw new ArgumentException("invalid edge length for a triangle");
}
}

public decimal GetArea()
{
return 0.5m * SortedEdges[0] * SortedEdges[1];
}
}```

（1）只要检查最短的边是否大于0

（2）只要检查是否2条较短边相加大于最长边

（3）判断直角三角形时很容易知道哪2条是直角边

0
```public class Triangel
{
protected double a { get; set; }
protected double b { get; set; }
protected double c { get; set; }
public Triangel(double a, double b, double c)
{
验证三角形...
}

public double GetZhouchang()
{
return a + b + c;
}

public virtual double GetMianji()//virtual 关键
{
...
}
}

public class RightAngelTrigel : Triangel
{

public RightAngelTrigel(double a, double b):
base(a,b,Math.Sqrt(Math.Pow(a,2)+Math.Pow(b,2)))//关键
{
}

public override double GetMianji()
{
return a * b / 2;//关键
}
}```

0

jason2013 | 园豆：1998 (小虾三级) | 2012-09-19 07:52

您需要登录以后才能回答，未注册用户请先注册