7-5 sdut-oop-6 計算各種圖形的周長(多型)-- python
阿新 • • 發佈:2022-06-01
from abc import ABCMeta, abstractmethod import math from webbrowser import BaseBrowser class Shape: __metaclass__=ABCMeta @abstractmethod def length(self): pass class Triangle(Shape): def __init__(self ,a,b,c): if a<=0 or b<=0 or c<=0: a=0 b=0 c=0 if a+b<=c or c+b<=a or c+a<=b: a=0 b=0 c=0 self.a=a self.b=b self.c=c def length(self): return self.a+self.b+self.c class Rectangle(Shape): def __init__(self ,a,b): if a<=0 or b<=0: a=0 b=0 self.a=a self.b=b def length(self): return 2.0*(self.a+self.b) class Circle(Shape): def __init__(self ,r): if r<=0: r=0 self.r=r def length(self): return (self.r)*2.0*3.14 while True: try: nums=[eval(x) for x in input().split()] n=len(nums) ans=0.0 if n==1: ans=(Circle(nums[0]).length()) elif n==2: ans=(Rectangle(nums[0],nums[1]).length()) else: ans=(Triangle(nums[0],nums[1],nums[2]).length()) print("{:.2f}".format(ans)) except: break