首页 > 其他 > 详细

第44讲:简单定制

时间:2020-09-04 08:32:09      阅读:56      评论:0      收藏:0      [点我收藏+]

一  基本要求

  • 定制一个计时器的类
  • start和stop方法代表启动计时和停止计时
  • 假设计时器对象t1,print(t1)和直接调用t1均显示结果
  • 当计时器未启动或已经停止计时时,调用stop方法会给予温馨提示
  • 两个计时器对象可以进行相加:t1+t2
  • 只能使用提供的有限资源完成

二 需要的资源

三 基本代码

class43_MyTimer.py文件

 1 import time as t
 2 
 3 class MyTimer():
 4     def __init__(self):
 5         self.unit = [,,,小时,分钟,]
 6         self.prompt = "未开始计时!"
 7         self.lasted = []
 8         self.begin = 0
 9         self.end = 0
10     
11     def __str__(self):
12         return self.prompt
13     
14     __repr__ = __str__
15     
16     def __add__(self,other):
17         prompt = "总共运行了"      # 临时使用,方法内部局部变量
18         result = []
19         for index in range(6):
20             result.append(self.lasted[index] + other.lasted[index])
21             if result[index]:
22                 prompt += (str(result[index]) + self.unit[index])
23         return prompt
24     
25     # 开始计时
26     def start(self):
27         self.begin = t.localtime()
28         self.prompt = "提示:请先调用stop()停止计时!"
29         print("计时开始...")
30     
31     # 停止计时
32     def stop(self):
33         if not self.begin:
34             print("提示:请先调用start()进行计时!")
35         else:
36             self.end = t.localtime()
37             self._calc()
38             print("计时结束!")
39     
40     # 内部方法,计算运行时间
41     def _calc(self):
42         self.lasted = []
43         self.prompt = "总共运行了"
44         for index in range(6):
45             self.lasted.append(self.end[index]-self.begin[index])
46             if self.lasted[index]:
47                 self.prompt += (str(self.lasted[index]) + self.unit[index])
48         
49         # 为下一轮计时初始化变量
50         self.begin = 0
51         self.end = 0

运行结果:

 1 >>> import class43_MyTimer  as T
 2 >>> t1 = T.MyTimer()
 3 >>> t1.start()
 4 计时开始...
 5 >>> t1.stop()
 6 总共运行了6秒
 7 计时结束!
 8 >>> t2 = T.MyTimer()
 9 >>> t2
10 未开始计时!
11 >>> t2.stop()
12 提示:请先调用start()进行计时!
13 >>> t2.start()
14 计时开始...
15 >>> t2
16 提示:请先调用stop()停止计时!
17 >>> t2.stop()
18 总共运行了1分钟-44秒
19 计时结束!
20 >>> t1 + t2
21 总共运行了1分钟-38秒
22 >>>

四 课后习题

0. 按照课堂中的程序,如果开始计时的时间是(2022年2月22日16:30:30),停止时间是(2025年1月23日15:30:30),那按照我们用停止时间减开始时间的计算方式就会出现负数,你应该对此做一些转换。

 1 import time as t
 2 
 3 class MyTimer():
 4     def __init__(self):
 5         self.unit = [,,,小时,分钟,]
 6         self.borrow = [0,12,31,24,60,60]
 7         self.prompt = "未开始计时!"
 8         self.lasted = []
 9         self.begin = 0
10         self.end = 0
11     
12     def __str__(self):
13         return self.prompt
14     
15     __repr__ = __str__
16     
17     def __add__(self,other):
18         prompt = "总共运行了"      # 临时使用,方法内部局部变量
19         result = []
20         for index in range(6):
21             result.append(self.lasted[index] + other.lasted[index])
22             if result[index]:
23                 prompt += (str(result[index]) + self.unit[index])
24         return prompt
25     
26     # 开始计时
27     def start(self):
28         self.begin = t.localtime()
29         self.prompt = "提示:请先调用stop()停止计时!"
30         print("计时开始...")
31     
32     # 停止计时
33     def stop(self):
34         if not self.begin:
35             print("提示:请先调用start()进行计时!")
36         else:
37             self.end = t.localtime()
38             self._calc()
39             print("计时结束!")
40     
41     # 内部方法,计算运行时间
42     def _calc(self):
43         self.lasted = []
44         self.prompt = "总共运行了"
45         for index in range(6):
46             temp = self.end[index]-self.begin[index]
47             
48             # 低位不够减,需要向高位借位
49             if temp < 0:
50                 # 测试高位是否有得借,没得借的话再向高位借......
51                 i = 1
52                 while self.lasted[index-i] < 1:
53                     self.lasted[index-i] += self.borrow[index-i] - 1 
54                     self.lasted[index-i-1] -= 1
55                     i += 1
56                 
57                 self.lasted.append(self.borrow[index] + temp)
58                 self.lasted[index-1] -= 1
59             else:
60                 self.lasted.append(temp)
61         
62         # 由于高位随时会被借位,所以打印要放在最后
63         for index in range(6):
64             if self.lasted[index]:
65                 self.prompt += str(self.lasted[index]) + self.unit[index]
66         
67         print(self.prompt)
68         # 为下一轮计时初始化变量
69         self.begin = 0
70         self.end = 0

 

第44讲:简单定制

原文:https://www.cnblogs.com/luoxun/p/13587194.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!