7-4 sdut-oop-5 计算长方体和四棱锥的表面积和体积(类的继承) 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
本文作者:kingwzun
本文链接:https://www.cnblogs.com/kingwz/p/16334162.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
python习题
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步