编写一个三角形类,根据三个实数构造三角形对象,如果三个实数不满足三角形的条件则自动构造以最小值为边的等边三角形
时间: 2024-05-19 10:14:49 浏览: 164
class Triangle:
def __init__(self, a, b, c):
if a + b > c and a + c > b and b + c > a:
self.a = a
self.b = b
self.c = c
else:
self.a = self.b = self.c = min(a, b, c)
def is_equilateral(self):
return self.a == self.b == self.c
def is_isosceles(self):
return self.a == self.b or self.b == self.c or self.c == self.a
def is_scalene(self):
return not self.is_equilateral() and not self.is_isosceles()
def perimeter(self):
return self.a + self.b + self.c
def area(self):
s = self.perimeter() / 2
return (s * (s - self.a) * (s - self.b) * (s - self.c)) ** 0.5
阅读全文