HELLO WORLD|

kingwzun

园龄:3年6个月粉丝:111关注:0

7-5 sdut-oop-6 计算各种图形的周长(多态)-- python

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

本文作者:kingwzun

本文链接:https://www.cnblogs.com/kingwz/p/16334507.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   kingwzun  阅读(561)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起