1. 程式人生 > 其它 >7-4 sdut-oop-5 計算長方體和四稜錐的表面積和體積(類的繼承) python

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