Python实验六 面向对象

7-1 sdut-oop-2 Shift Dot(类和对象)

class Point :
    x = 0
    y = 0
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def move(self, x1, y1):
        self.x += x1
        self.y += y1
    def toString(self):
        return "({},{})".format(self.x, self.y)
    
while True:
    try:
        x, y, n = map(int, input().split())
        a = Point(x, y)
        for i in range(n):
            x1, y1 = map(int, input().split())
            a.move(x1, y1)
        print(a.toString())
    except:
        break

7-3 sdut-谁是最强的女汉子

class girls:
    x = 0
    y = 0
    def __init__(self, x, y):
        self.x = x
        self.y = y
ls = []
n = int(input())
Max = 999999
for i in range(n):
    x, y = map(int, input().split())
    a = girls(x, y)
    ls.append(a)
    Max = min(Max, x)
# ls = sorted(ls, key=lambda it:it.x)
sum = 0
cnt = 0
for i in ls:
    if i.x == Max:
        sum += i.y
        cnt += 1
print(cnt, sum)

7-4 sdut-oop-5 计算长方体和四棱锥的表面积和体积(类的继承)

from math import *
class Rect:
    l, h, z = 0, 0, 0
    def __init__(self, l, h, z):
        self.l = max(l, 0)
        self.h = max(h, 0)
        self.z = max(z, 0)
    def length(self):
        return (self.l + self.h) * 2.0
    def area(self):
        return (self.l * self.h)
class Cubic(Rect):
    l, h, z = 0, 0, 0
    def __init__(self, l, h, z):
        super().__init__(l, h, z)
    def area1(self):
        return 2.0 * (self.l * self.h + self.l * self.z + self.h * self.z)
    def volume1(self):
        return self.l * self.h * self.z
class Pyramid(Rect):
    def __init__(self, l, h, z):
        super().__init__(l, h, z)
    def area2(self):
        return self.l * self.h + (self.h * sqrt((self.l / 2) ** 2 + self.z ** 2)) + (self.l * sqrt((self.h/2) ** 2 + self.z ** 2))
    def volume2(self):
        return self.l * self.h * self.z / 3.0
while True :
    try:
        l, h, z = map(float, input().split())
        c = Cubic(l, h, z)
        p = Pyramid(l, h, z)
        print("%.2f %.2f %.2f %.2f" %(c.area1(), c.volume1(), p.area2(), p.volume2()))
    except:
        break

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

class Shape:
    def length(self):
        pass
class Triangle(Shape):
    def __init__(self, a, b, c):
        if a <= 0 or b <= 0 or c <= 0 or (not (a + b > c and a + c > b and b + c > a)):
            self.a, self.b, self.c = 0.0, 0.0, 0.0
        else:self.a, self.b, self.c = a, b, c    
    def length(self):
        return self.a + self.b + self.c
class Rectangle(Shape):
    def __init__(self, a, b):
        if a <= 0 or b <= a:
            self.a, self.b = 0.0, 0.0
        else:
            self.a, self.b = a, b    
    def length(self):
        return 2.0 * (self.a + self.b)
class Circle(Shape):
    def __init__(self, r):
        self.r = max(r, 0)
    def length(self):
        return 2 * 3.14 * self.r
while True:
    try:
        ls = list(map(float, input().split()))
        if len(ls) == 1:
            c = Circle(ls[0])
            print("%.2f" % c.length())
        elif len(ls) == 2:
            r = Rectangle(ls[0], ls[1])
            print("%.2f" % r.length())
        elif len(ls) == 3:
            t = Triangle(ls[0], ls[1], ls[2])
            print("%.2f" % t.length())
    except:
        break
        

7-6 sdut-oop-9 计算长方形的周长和面积(类和对象)

class Rect:
    def __init__(self, l, w):
        if l <= 0 or w <= 0:
            self.l, self.w = 0, 0
        else:
            self.l, self.w = l, w
    def area_1(self):
        return self.l * 4
    def area_2(self):
        return 2 * (self.l + self.w)
    def length_1(self):
        return self.l ** 2
    def length_2(self):
        return self.l * self.w
while 1:
    try:
        ls = list(map(int, input().split()))
        if len(ls) == 1:
            r = Rect(ls[0], 1)
            print(r.l, r.l, r.area_1(), r.length_1())
        elif len(ls) == 2:
            r = Rect(ls[0], ls[1])
            print(r.l, r.w, r.area_2(), r.length_2())
    except:
        break

7-7 sdut-oop-7 答答租车系统(类的继承与多态 面向对象综合练习)

class Che:
    id=0
    name='A'
    maxpeo=0
    weight=0
    meo=0
    def __init__(self,id,name,maxpeo,weight,meo):
        self.id=id
        self.name=name
        self.maxpeo=maxpeo
        self.meo=meo
        self.weight=weight
    def getMaxpeo(self,n):
        return self.maxpeo*n
    def getWeight(self,n):
        return self.weight*n
    def getMeo(self,n):
        return self.meo*n    
class KeChe(Che):
    def __init__(self,id,name,maxpeo,meo):
        self.id=id
        self.name=name
        self.maxpeo=maxpeo
        self.meo=meo
    
class HuoChe(Che):
    def __init__(self,id,name,weight,meo):
        self.id=id
        self.name=name
        self.weight=weight
        self.meo=meo
 
ans=[]
ans.append(KeChe(1,"d",5,800))
ans.append(KeChe(2,"d",5,400))
ans.append(KeChe(3,"d",5,800))
ans.append(KeChe(4,"d",51,1300))
ans.append(KeChe(5,"d",55,1500))
ans.append(Che(6,"d",5,0.45,500))
ans.append(Che(7,"d",5,2.0,450))
ans.append(HuoChe(8,"d",3,200))
ans.append(HuoChe(9,"d",25,1500))
ans.append(HuoChe(10,"d",35,2000))
x=int(input())
if x==1:
    n=int(input())
    sum1=0.0
    sum2=0.0
    sum3=0.0
    for i in range(n):
        #a表示要租车的编号,
        #b表示租用该车型的天数。
        a,b=map(int,input().split())
        sum1+=ans[a-1].getMaxpeo(b)
        sum2+=ans[a-1].getWeight(b)
        sum3+=ans[a-1].getMeo(b)
    print(int(sum1),'%.2f' %sum2,int(sum3))
else:
    print("0 0.00 0")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值