匿名用户
1级
2015-05-10 回答
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from math import pi
import logging
class Geometrie(object):
"""docstring for Geometrie"""
def __init__(self):
pass
def say(self):
print self.__class__.__name__
def compute_area(self):
pass
def compute_circumference(self):
pass
def say_cirumfrerence(self):
print "%s 's cirumfrerence is: %f" % (self.__class__.__name__, self.compute_circumference())
def say_area(self):
print "%s 's cirumfrerence is: %f" % (self.__class__.__name__, self.compute_area())
class Ellipse(Geometrie):
"""docstring for Ellipse"""
def __init__(self,major_axis, minor_axis):
"""
major_axis is a
minor_axis is b
"""
super(Ellipse, self).__init__()
if not (isNum(major_axis) and isNum(minor_axis)):
raise Exception("TypeError: Please make sure the major:\
{0!r} and minor {1!r} axis are correct.".format(major_axis, minor_axis))
else:
self.a=major_axis
self.b=minor_axis
def compute_circumference(self):
q=self.a+self.b
h=(abs((self.a-self.b)/(self.a-self.b)))**2
m=22/(7*pi)-1
n=(abs((self.a-self.b)/self.a))**(33.397)
return pi*q*(1+3*h/(10+(4-3*h)**(0.5)))*(1+m*n)
def compute_area(self):
return self.a*self.b*pi
class Square(Geometrie):
"""
docstring for Square"Geometrie
"""
def __init__(self, length, width):
super(Square,self).__init__()
if not (isNum(length) and isNum(width)):
raise Exception("TypeError: Please make sure the length:\
{0!r} and width {1!r} axis are correct.".format(length, width))
else:
self.a = length
self.b = width
def compute_circumference(self):
return 2*(self.a+self.b)
def compute_area(self):
return self.a*self.b
class Circle(Geometrie):
"""docstring for Circle"""
def __init__(self, radius):
super(Circle, self).__init__()
if not (isNum(radius)):
raise Exception("TypeError: Please make sure the radius:\
{0!r} is correct.".format(radius))
else:
self.r = radius
def compute_circumference(self):
return (2*self.r)*pi
def compute_area(self):
return pi*(self.r**2)
def isNum(value):
try:
value + 1
except TypeError:
return False
else:
return True
def main():
"""
docstring for main
"""
Es = Ellipse(2,1)
Es.say_cirumfrerence()
Es.say_area()
Sq = Square(2,1)
Sq.say_cirumfrerence()
Sq.say_area()
Cr = Circle(4)
Cr.say_cirumfrerence()
Cr.say_area()
if __name__ == '__main__':
main()