实验七

发布时间 2023-06-14 00:22:45作者: Wangchengxu

1.实验任务1

 1 class Account:
 2     def __init__(self,name,account_number,initial_amount = 10):
 3         self._name = name
 4         self._card_no = account_number
 5         self._balance = initial_amount
 6 
 7     def deposit(self,amount):
 8         self._balance += amount
 9 
10     def withdraw(self,amount):
11         if self._balance < amount:
12             print('余额不足')
13             return
14 
15         self._balance -= amount
16 
17     def info(self):
18         print('持卡人姓名:',self._name)
19         print('持卡人账号:',self._card_no)
20         print('持卡人账户余额:',self._balance)
21 
22     def get_balance(self):
23         return self._balance
24 
25 def main():
26     print('测试账户1:'.center(30,'*'))
27     a1 = Account('Bob','5002311',20000)
28     a1.deposit(5000)
29     a1.withdraw(4000)
30     a1.info()
31 
32     print()
33 
34     print('测试账户2:'.center(30,'*'))
35     a2 = Account('Joe','5006692',20000)
36     a2.withdraw(10000)
37     a2.withdraw(5000)
38     a2.info()
39 
40 
41 if __name__ == '__main__':
42     main()

2.实验任务2

shape.py

 1 class Shape:
 2     def info(self):
 3         pass
 4 
 5     def area(self):
 6         pass
 7 
 8     def perometer(self):
 9         pass
10 
11 class Rect(Shape):
12     def __init__(self,x = 0,y = 0,length = 2,width = 1):
13         self._x = x
14         self._y = y
15         self._width = width
16         self._length = length
17 
18     def info(self):
19         print(f'矩形左上角顶点坐标:({self._x},{self._y})')
20         print(f'矩形长:{self._length}')
21         print(f'矩形宽:{self._width}')
22 
23 
24     def area(self):
25         return self._length * self._width
26 
27     def perimeter(self):
28         return (self._length + self._width) * 2
29 
30 
31 class Circle(Shape):
32     def __init__(self, x = 0,y = 0,radius = 1):
33         self._x = x
34         self._y = y
35         self._r = radius
36 
37     def info(self):
38         print(f'圆心:({self._x},{self._y})')
39         print(f'半径:{self._r}')
40 
41     def area(self):
42         return 3.14 * self._r * self._r
43 
44     def perimeter(self):
45         return 2 * 3.14 * self._r
46 
47 
48 class Triangle(Shape):
49     def __init__(self,a = 0,b = 0,c = 1):
50         self._a,self._b,self._c = a,b,c
51 
52     def info(self):
53         print(f'三角形三边长:({self._a},{self._b},{self._c})')
54 
55     def area(self):
56         s = (self._a + self._b + self._c) / 2
57         ans = (s*(s - self._a)*(s - self._b)*(s - self._c)) ** 0.5
58 
59         return ans
60 
61     def perimeter(self):
62         return(self._a + self._b + self._c)
63 
64 
65 def main():
66     print('测试1:'.center(40,'*'))
67 
68     shapes_lst1 = [Circle(),Rect(),Triangle()]
69 
70     for t in shapes_lst1:
71         t.info()
72         print(f'面积:{t.area():.2f}')
73         print(f'周长:{t.perimeter():.2f}')
74         print()
75 
76     print('测试2:'.center(40,'*'))
77 
78     shapes_lst2 = [Circle(x = 2,y = 2,radius = 10),
79                    Rect(x = 50,y = 50,length = 10,width = 5),
80                    Triangle(a = 3,b = 4,c = 5)]
81 
82     for t in shapes_lst2:
83         t.info()
84         print(f'面积:{t.area():.2f}')
85         print(f'周长:{t.perimeter():.2f}')
86         print()
87 
88 
89 if __name__ == '__main__':
90     main()

task2.py

1 from shape import Rect,Circle
2 
3 shape_lst = [Rect(5,5,10,5),Circle(),Circle(1,1,10)]
4 
5 for i in shape_lst:
6     i.info()
7     print(f'面积:{i.area():.2f}')
8     print(f'周长:{i.perimeter():.2f}')
9     print()

task3.py

 1 import math
 2 
 3 def func(x):
 4     m = 0
 5     s = 2
 6     f = math.exp(((x - m)/s)**2/-2)/s/math.sqrt(2*math.pi)
 7     return f
 8 
 9 print(f'x = 1, f = {func(1):.8f}')
10 print(f'x = 3, f = {func(3):.8f}')
11 print(f'x = 5, f = {func(5):.8f}')
12 print(f'x = 7, f = {func(7):.8f}')
13 print(f'x = 9, f = {func(9):.8f}')