实验七

实验1

task1.py

实验源码:

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

实验结果:

 task2.py

实验源码:

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

实验结果:

 

posted @ 2023-06-12 17:17  showdownx  阅读(35)  评论(0)    收藏  举报