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

实验任务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     def withdraw(self,amount):
10         if self._balance < amount:
11             print('余额不足')
12             return
13 
14         self._balance -= amount
15     def info(self):
16         print('持卡人姓名', self._name)
17         print('持卡人账号', self._card_no)
18         print('持卡人账户余额', self._balance)
19     def get_balance(self):
20         return self._balance
21 
22 def main():
23     print('测试账户1: '.center(30,'*'))
24     a1 = Account('Bob','5002311',20000)
25     a1.deposit(5000)
26     a1.withdraw(4000)
27     a1.info()
28 
29     print()
30 
31     print('测试账户2: '.center(30,'*'))
32     a2 = Account('Joe','5006692',20000)
33     a2.withdraw(10000)
34     a2.withdraw(5000)
35     a2.info()
36 
37 if __name__ == '__main__':
38     main()
View Code

 实验任务2

 1 class Shape:
 2     def info(self):
 3         pass
 4     def area(self):
 5         pass
 6     def preimeter(self):
 7         pass
 8 class Rect(Shape):#矩形类
 9     def __init__(self, x = 0, y = 0, length = 2, width = 1):
10         self._x = x
11         self._y = y
12         self._width = width
13         self._length = length
14     def info(self):#打印图片信息
15         print(f'矩形左上角顶点坐标: ({self._x}, {self._y})')
16         print(f'矩形长: {self._length}')
17         print(f'矩形宽: {self._width}')
18     def area(self):#面积
19         return self._length * self._width
20     def perimeter(self):#周长
21         return (self._length + self._width) * 2
22 class Circle(Shape):
23     def __init__(self, x = 0, y = 0, radius = 1):
24         self._x = x
25         self._y = y
26         self._r = radius
27     def info(self):
28         print(f'圆心: ({self._x}, {self._y})')
29         print(f'半径: {self._r}')
30     def area(self):
31         return 3.14 * self._r * self._r
32     def perimeter(self):
33         return 2 * 3.14 * self._r
34 class Triangle(Shape):
35     def __init__(self, a = 1, b = 1, c = 1):
36         self._a, self._b, self._c = a, b, c
37     def info(self):
38         print(f'三角形三边长: ({self._a}, {self._b}, {self._c})')
39     def area(self):
40         s = (self._a + self._b + self._c) / 2
41         ans = (s*(s - self._a)*(s - self._b)*(s - self._c)) ** 0.5
42         return ans
43     def perimeter(self):
44         return (self._a + self._b + self._c)
45 def main():
46     print('测试1:'.center(40, '*'))
47     shapes_lst1 = [Circle(), Rect(), Triangle()]
48     for t in shapes_lst1:
49         t.info()
50         print(f'面积: {t.area():.2f}')
51         print(f'周长: {t.perimeter():.2f}')
52         print()
53     print('测试2:'.center(40, '*'))
54     shapes_lst2 = [Circle(x = 2, y = 2, radius = 10),
55                    Rect(x = 50, y = 50, length = 10, width = 5),
56                    Triangle(a = 3, b = 4, c = 5)]
57     for t in shapes_lst2:
58         t.info()
59         print(f'面积: {t.area():.2f}')
60         print(f'周长: {t.perimeter():.2f}')
61         print()
62 if __name__ == '__main__':
63     main()
View Code

 实验任务3

 1 from math import*
 2 
 3 def func(x):
 4     m = 0
 5     s = 2
 6     a = 1/(s* (2*pi)**0.5)
 7     b = exp(-0.5* (x/s)**2)
 8     return a*b
 9 
10 for i in [1,3,5,7,9]:
11     print(f'x = {i}, f = {func(i):.8f}')
View Code

 实验任务4

 1 from random import choice
 2 class RandomWalk():
 3     '''一个生成随机漫步数据的类'''
 4     def __init__(self, num_points = 5000):
 5         '''初始化随机漫步的属性'''
 6         self.num_points = num_points
 7         # 所有的随机漫步都始于(0,0)
 8         self.x_values = [0]
 9         self.y_values = [0]
10     def fill_walk(self):
11         '''计算随机漫步包含的所有点'''
12         # 不断漫步,直到列表到达指定长度
13         while len(self.x_values) < self.num_points:
14             # 决定前进方向,以及,沿这个方向前进的距离
15             x_direction = choice([1, -1])
16             x_distance = choice([0, 1, 2, 3, 4])
17             x_step = x_direction * x_distance
18             y_direction = choice([1, -1])
19             y_distance = choice([0, 1, 2, 3, 4])
20             y_step = y_direction * y_distance
21             # 据绝原地踏步
22             if x_step == 0 and y_step == 0:
23                 continue
24             # 计算下一个点的x和y值
25             next_x = self.x_values[-1] + x_step
26             next_y = self.y_values[-1] + y_step
27             self.x_values.append(next_x)
28             self.y_values.append(next_y)
29 def main():
30     '''测试随机游走类'''
31 
32 
33     rw = RandomWalk(5)
34     rw.fill_walk()
35     print(rw.x_values)
36     print(rw.y_values)
37 if __name__ == '__main__':
38     main()
View Code
 1 from matplotlib import pyplot as plt
 2 from random_walk import RandomWalk
 3 from time import sleep
 4 # 代码范例中,模拟两次随机漫步过程
 5 n = 0
 6 while n < 2:
 7     n += 1
 8 # 创建一个RandomWalk对象,调用其方法生成随机游走坐标
 9 rw = RandomWalk(50000)
10 rw.fill_walk()
11 # 用散点图进行可视化绘制
12 plt.figure(figsize = (10, 6), dpi = 128)
13 point_numbers = list(range(rw.num_points))
14 plt.scatter(rw.x_values, rw.y_values, c=point_numbers,
15 cmap=plt.cm.Blues, edgecolor='none', s=1)
16 # 终点和起点
17 plt.scatter(0, 0, c = 'grey', edgecolors='none', s=100)
18 plt.scatter(rw.x_values[-1], rw.y_values[-1], c = 'red',
19 edgecolors='none', s=100)
20 # 隐藏坐标轴
21 plt.axis('off')
22 plt.show()
View Code

 

posted @ 2023-06-13 16:34  ‘_‘  阅读(26)  评论(0)    收藏  举报