实验七 面向对象编程与内置模块

task1

实验源码

 1 class Account:
 2     def __init__(self, name, account_number, initial_amount = 10):
 3         self._name = name
 4         self._cord_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         self._balance -= amount
15 
16     def info(self):
17         print('持卡人姓名' , self._name)
18         print('持卡人账号', self._cord_no)
19         print('持卡人账户余额', self._balance)
20 
21     def get_balance(self):
22         return self._balance
23 
24 def main():
25     print('测试账户1:'.center(30, '*') )
26     a1 = Account('Bob', '5002311', 20000)
27     a1.deposit(5000)
28     a1.withdraw(114000)
29     a1.info()
30     a1.get_balance()
31 
32     print()
33 
34     print('测试账户:'.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()

 

运行截图

 

task2

实验源码

shape.py

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

 

运行截图

shape

 

task2

 

task3

实验源码

 1 from math import *
 2 
 3 
 4 def func(x):
 5     return exp(-pow(x/2, 2)/2)/pow(2*pi, 0.5)/2
 6 
 7 
 8 def main():
 9     for i in range(1, 9, 2):
10         print(f'x = {i}, f = {func(i):.8f}')
11 
12 
13 main()

 

运行截图

 

task4

实验源码

random_walk.py

 1 """
 2 简易版二维随机游走类
 3 """
 4 
 5 from random import choice
 6 
 7 
 8 class Randomwalk:
 9     def __init__(self, num_points = 7):
10         self.num_point = num_points
11         self.x_values = [0]
12         self.y_values = [0]
13 
14     def fill_walk(self):
15         while len(self.x_values) < self.num_point:
16             x_direction = choice([1, -1])
17             x_distance = choice([0, 1, 2, 3, 4])
18             x_step = x_direction * x_distance
19 
20             y_direction = choice([1, -1])
21             y_distance = choice([0, 1, 2, 3, 4])
22             y_step = y_direction * y_distance
23 
24             if x_distance == 0 and y_distance == 0:
25                 continue
26 
27             x_next = self.x_values[-1] + x_step
28             self.x_values.append(x_next)
29             y_next = self.y_values[-1] + y_step
30             self.y_values.append(y_next)
31 
32 
33 def main():
34     rw = Randomwalk(5)
35     rw.fill_walk()
36     print(rw.x_values)
37     print(rw.y_values)
38 
39 
40 if __name__ == '__main__':
41     main()

task4

 1 """
 2 模拟多次漫步
 3 """
 4 
 5 from matplotlib import pyplot as plt
 6 from random_walk import Randomwalk
 7 from time import sleep
 8 
 9 
10 n = 0
11 while n < 2:
12     n += 1
13 
14     rw = Randomwalk(50000)
15     rw.fill_walk()
16 
17     plt.figure(figsize=(10, 6), dpi=128, facecolor='none')
18     point_number = list(range(rw.num_point))
19     plt.scatter(rw.x_values, rw.y_values, c=point_number, edgecolors='none', s=1)
20 
21     plt.scatter(0, 0, c='grey', edgecolors='none', s=100)
22     plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolors='none', s=100)
23 
24     plt.axis('off')
25 
26     plt.show()

 

运行截图

random_walk.py

 task4

 

t.info()
print(f'面积{t.area():.2f}')
print(f'周长{t.perimeter():.2f}')
print()
posted @ 2023-06-14 01:49  理理理  阅读(26)  评论(0)    收藏  举报