结构化编程解决三角形问题
import math
class Triangle:
REL_TOL = 1e-9
def __new__(cls, *args, **kwargs):
if cls.is_triangle(*args):
instance = super().__new__(cls)
return instance
else:
return None
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def get_perimeter(self):
return self.a + self.b + self.c
def get_area(self):
p = self.get_perimeter() / 2
area = (p * (p - self.a) * (p - self.b) * (p - self.c)) ** 0.5
return area
#@property装饰器会将方法转换为相同名称的只读属性
@property
def perimeter(self):
return self.a + self.b + self.c
@property
def area(self):
p = self.get_perimeter / 2
area = (p * (p - self.a) * (p - self.b) * (p - self.c)) ** 0.5
return area
def is_right_angled(self):
return (math.isclose(self.a ** 2 + self.b ** 2, self.c ** 2, rel_tol=Triangle.REL_TOL)
or math.isclose(self.a ** 2 + self.c ** 2, self.b ** 2, rel_tol=Triangle.REL_TOL)
or math.isclose(self.b ** 2 + self.c ** 2, self.a ** 2, rel_tol=Triangle.REL_TOL))
def is_acute_angled(self):
if not self.is_right_angled():
if self.a ** 2 + self.b ** 2 > self.c ** 2 and self.a ** 2 + self.c ** 2 > self.b ** 2 and self.b ** 2 + self.c ** 2 > self.a ** 2:
return True
return False
def is_obtuse_angled(self):
if not self.is_right_angled():
if self.a ** 2 + self.b ** 2 < self.c ** 2 or self.a ** 2 + self.c ** 2 < self.b ** 2 or self.b ** 2 + self.c ** 2 < self.a ** 2:
return True
return False
def equal_to(self, other):
#类型判断isinstance,此处判断other是否是Triangle的实例化对象
if isinstance(other, Triangle):
A = sorted((self.a, self.b, self.c))
B = sorted((other.a, other.b, other.c))
return A[0] == B[0] and A[1] == B[1] and A[2] == B[2]
return -1
def not_equal_to(self, other):
if isinstance(other, Triangle):
A = sorted((self.a, self.b, self.c))
B = sorted((other.a, other.b, other.c))
return A[0] != B[0] and A[1] != B[1] and A[2] != B[2]
return -1
def greater_than(self, other):
if isinstance(other, Triangle):
return self.get_area() > other.get_area()
return -1
def greater_equal(self, other):
if isinstance(other, Triangle):
return self.get_area() >= other.get_area()
return -1
def less_than(self, other):
if isinstance(other, Triangle):
return self.get_perimeter() < other.get_perimeter()
return -1
def less_equal(self, other):
if isinstance(other, Triangle):
return self.get_perimeter() <= other.get_perimeter()
return -1
#**************************************************************************************
# 魔术方法
#**************************************************************************************
#相等
def __eq__(self, other):
if isinstance(other, Triangle):
A = sorted((self.a, self.b, self.c))
B = sorted((other.a, other.b, other.c))
return A[0] == B[0] and A[1] == B[1] and A[2] == B[2]
return -1
#不等
def __ne__(self, other):
if isinstance(other, Triangle):
A = sorted((self.a, self.b, self.c))
B = sorted((other.a, other.b, other.c))
return A[0] != B[0] and A[1] != B[1] and A[2] != B[2]
return -1
#面积大于
def __gt__(self, other):
if isinstance(other, Triangle):
return self.get_area() > other.get_area()
return -1
#面积大于等于
def __ge__(self, other):
if isinstance(other, Triangle):
return self.get_area() >= other.get_area()
return -1
#周长小于
def __lt__(self, other):
if isinstance(other, Triangle):
return self.get_perimeter() < other.get_perimeter()
return -1
#周长小于等于
def __le__(self, other):
if isinstance(other, Triangle):
return self.get_perimeter() <= other.get_perimeter()
return -1
#类方法
@classmethod
def is_valid(cls, num):
# print(cls.REL_TOL)
return type(num) in {int, float} and num > 0
#静态方法
@staticmethod
def is_triangle(a, b, c):
# print(Triangle.REL_TOL)
return Triangle.is_valid(a) and Triangle.is_valid(b) and Triangle.is_valid(c) and (a + b > c) and (
b + c > a) and (a + c > b)
if __name__ == '__main__':
print("Triangle类....")
Triangle类....
t = Triangle(3,4,5)
#周长
t.get_perimeter()
12
#面积
t.get_area()
6.0
#是否是直角三角行
t.is_right_angled()
True
#两个三角是否相等
s=Triangle(3,4,5)
t.equal_to(s)
True
#两个三角面积是否相等
t==s
True
#t三角面积是否大于s三角面积
#t>s
t.greater_than(s)
False
#t三角面积是否大于等于s三角面积
#t>=s
t.greater_equal(s)
True
#t三角周长是否小于等于s三角周长
#t<=s
t.less_equal(s)
True
isinstance() 函数
语法
isinstance(object, classinfo)
参数
object -- 实例对象。
classinfo -- 可以是直接或间接类名、基本类型或者由它们组成的元组。
返回值
如果对象的类型与参数二的类型(classinfo)相同则返回 True,否则返回 False。