7-4 sdut-oop-5 计算长方体和四棱锥的表面积和体积(类的继承) python

Python __ 面向对象基础

import math as m

class Rect:
    l = 0.0
    h = 0.0
    z = 0.0

    def __init__(self, l, h, z):
        self.l = l
        self.h = h
        self.z = z

    def length(self):
        return (self.l + self.h) * 2

    def area(self):
        return self.l * self.h


# 立方体类Cubic
class Cubic(Rect):
    def __init__(self, l, h, z):
        super().__init__(l, h, z)

    def area(self):

        return (self.h * self.l + self.l * self.z + self.h * self.z) * 2

    def v(self):
        return self.h * self.l * self.z


# 四棱锥类Pyramid
class Pyramid(Rect):
    def __init__(self, l, h, z):
        super().__init__(l, h, z)

    def area(self):
        return m.sqrt(self.h*self.h+4*self.z*self.z)*self.l/2+self.h*self.l+m.sqrt(self.l*self.l+4*self.z*self.z)*self.h/2

    def v(self):
        return  self.h * self.l * self.z/3

        


while True:
    try:
        a, b, c = map(float, input().split())
        C = Cubic(a, b, c)
        P = Pyramid(a, b, c)
        if a<=0 or b<=0 or c<=0:
            print("0.00 0.00 0.00 0.00")
        else:

        # print(C.area, C.v, P.area, P.v)
            print("{:.2f} {:.2f} {:.2f} {:.2f}".format(C.area(), C.v(), P.area(), P.v()))
    except:
        break
posted @ 2022-06-01 14:35  kingwzun  阅读(464)  评论(0编辑  收藏  举报