数据结构与算法python语言描述答案_《数据结构与算法Python语言描述》习题第二章第一题(python版)...

1 #!/usr/bin/env python

2 #-*- coding:utf-8 -*-

3

4 """

5 定义一个表示时间的类Time6 a)Time(hours,minutes,seconds)创建一个时间对象;7 b)t.hours(),t.minutes(),t.seconds()分别返回时间对象t的小时,分钟和秒值8 c)为Time对象定义加法和减法操作(用运算符+和-)9 d)定义时间对象的等于和小于关系对象(用运算符==和

11 ADT Time: #定义时间的抽象数据类型12 Time(self, int hours, int minutes, int seconds) #构造时间对象13 +(self, Time t2) #求出本对象加t2的结果14 -(self, Time t2) #求出本对象减t2的结果15 ==(self, Time t2) #本对象是否和t2相等16

21 """

22 Author: Minion Xu23 Time:2016-11-2124 """

25

26 classTime(object):27 __slots__ = ('_hours','_minutes','_seconds')28

29 #创建时间对象

30 def __init__(self, hours=0, minutes=0, seconds=0):31 #类型判断

32 if not isinstance(hours, int) or not isinstance(minutes, int) or notisinstance(seconds, int):33 raiseTypeError34

35 if(0<=hours<=23):36 self._hours =hours37 if (0 <= minutes <= 59):38 self._minutes =minutes39 if (0 <= seconds <= 59):40 self._seconds =seconds41 else:42 raise ValueError("%d is not valid seconds!" %seconds)43 else:44 raise ValueError("%d is not valid minutes!" %minutes)45

46 else:47 raise ValueError("%d is not valid hours!" %hours)48

49

50 #+

51 def __add__(self, other):52 seconds_add = self._seconds +other._seconds53 seconds_carry = seconds_add // 60 #超过60秒进位

54 seconds = seconds_add % 60 #秒值

55 minutes_add = self._minutes + other._minutes +seconds_carry56 minutes_carry = minutes_add // 60 #超过60分进位

57 minutes = minutes_add % 60 #分钟

58 hours_add = self._hours + other._hours +minutes_carry59 if(hours_add<24):60 hours =hours_add61 else:62 hours = hours_add - 24 #小时

63 returnTime(hours, minutes, seconds)64 #-

65 def __sub__(self, other):66 if self._seconds <67 self._minutes>

68 seconds = self._seconds + 60 -other._seconds69 else:70 seconds = self._seconds -other._seconds71 if self._minutes <72 self._hours>

73 minutes = self._minutes + 60 -other._minutes74 else:75 minutes = self._minutes -other._minutes76 if self._hours <77 hours="self._hours" else:79 returntime minutes seconds>

82 def __eq__(self, other):83 bool1 =False84 bool2 =False85 bool3 =False86 if self._hours ==other._hours:87 bool1 =True88 if self._minutes ==other._minutes:89 bool2 =True90 if self._seconds ==other._seconds:91 bool3 =True92 return bool1 and bool2 andbool393

94 #<

95 def __lt__(self, other):96 if self._hours <97 returntrue98 elif self._hours="=other._hours:99" if self._minutes returntrue101 self._seconds returntrue104 else:105 returnfalse106 else:107 returnfalse108 else:109 returnfalse110>

111 defhours(self):112 returnself._hours113

114 defminutes(self):115 returnself._minutes116

117 defseconds(self):118 returnself._seconds119

120 def print(self):121 print(self._hours,":",self._minutes,":",self._seconds)122

123 def __str__(self):124 return str(self._hours) + ":" + str(self._minutes) + ":" +str(self._seconds)125

126 if __name__ == '__main__':127 t = Time(21,31,59)128 t1 = Time(18,31,41)129 he = t +t1130 cha = t -t1131 print(he)132 print(cha)133 print(t==t1)134 print(t

97>77>72>67>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值