#寒假打卡28天第7天
import math
class Round(): #圆形
def __init__(self, R):
self.R = R
def area(self):
return self.R * self.R * 3.14
def girth(self):
return self.R * 2 * 3.14
class Rectangle():#矩形
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def girth(self):
return self.length + self.width
class Triangle():#三角形
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def area(self):
l = (self.a + self.b + self.c)/2
return round(math.sqrt(l * (l - self.a) * (l - self.b) * (l - self.c)), 2) #round保留2为小数
def girth(self):
return self.a + self.c + self.c
class Guadrilateral():#任意四边形
def __init__(self, a, b, c, d, angle):
self.a = a
self.b = b
self.c = c
self.d = d
self.angle = angle
def area(self):
l = (self.a + self.b + self.c + self.d)/2
return round(math.sqrt((l - self.a) * (l - self.b) * (l - self.c) * (l - self.d) -
self.a * self.b * self.c * self.d
* math.pow(math.cos(math.radians(self.angle)), 2)), 2) #pow函数幂次方radians弧度转换为角度
def girth(self):
return self.a + self.b + self.c + self.d
print("----图形计算工具----")
print("*******************")
print("***你可以选择的图形***")
print("********圆形********")
print("********矩形********")
print("*******三角形********")
print("*****任意四边形*******")
target = str(input("请输入你想要计算图形的名称"))
if target == "圆形":
x1 = int(input("请输入半径(厘米):"))
print("圆形的面积为:" + str(Round(x1).area()) + "(平方厘米)!")
print("圆形的周长为:" + str(Round(x1).girth()) + "(厘米)!")
elif target == "矩形":
x1 = int(input("请输入长度(厘米):"))
x2 = int(input("请输入宽度(厘米):"))
print("矩形的面积为:" + str(Rectangle(x1, x2).area()) + "(平方厘米)!")
print("矩形的周长为:" + str(Rectangle(x1, x2).girth()) + "(厘米)!")
elif target == "三角形":
x1 = int(input("请输入边a(厘米):"))
x2 = int(input("请输入边b(厘米):"))
x3 = int(input("请输入边c(厘米):"))
print("三角形的面积为:" + str(Triangle(x1, x2, x3).area()) + "(平方厘米)!")
print("三角形的周长为:" + str(Triangle(x1, x2, x3).girth()) + "(厘米)!")
elif target == "任意四边形":
x1 = int(input("请输入边a(厘米):"))
x2 = int(input("请输入边b(厘米):"))
x3 = int(input("请输入边c(厘米):"))
x4 = int(input("请输入边d(厘米):"))
x5 = int(input("请输入邻对角角度和的一半(°):"))
print("三角形的面积为:" + str(Guadrilateral(x1, x2, x3, x4, x5).area()) + "(平方厘米)!")
print("三角形的周长为:" + str(Guadrilateral(x1, x2, x3, x4, x5).girth()) + "(厘米)!")