《数据结构与算法Python语言描述》习题第二章第一题,python版

题目:


定义一个表示时间的类Time


a)Time(hours,minutes,seconds)创建一个时间对象;


b)t.hours(),t.minutes(),t.seconds()分别返回时间对象t的小时,分钟和秒值


c)为Time对象定义加法和减法操作(用运算符+和-)


d)定义时间对象的等于和小于关系对象(用运算符==和<)


  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 
  4 """
  5 定义一个表示时间的类Time
  6 a)Time(hours,minutes,seconds)创建一个时间对象;
  7 b)t.hours(),t.minutes(),t.seconds()分别返回时间对象t的小时,分钟和秒值
  8 c)为Time对象定义加法和减法操作(用运算符+和-)
  9 d)定义时间对象的等于和小于关系对象(用运算符==和<)
 10 
 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     <(self, Time t2)                                #本对象小于t2
 17     hours(self)                                     #取出本对象的小时
 18     minutes(self)                                   #取出本对象的分钟
 19     seconds(self)                                   #取出本对象的秒值
 20 """
 21 """
 22 Author: Minion Xu
 23 Time:2016-11-21
 24 """
 25 
 26 class Time(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 not isinstance(seconds, int):
 33             raise TypeError
 34 
 35         if(0<=hours<=23):
 36             self._hours   = hours
 37             if (0 <= minutes <= 59):
 38                 self._minutes = minutes
 39                 if (0 <= seconds <= 59):
 40                     self._seconds = seconds
 41                 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._seconds
 53         seconds_carry = seconds_add // 60    #超过60秒进位
 54         seconds = seconds_add % 60   #秒值
 55         minutes_add = self._minutes + other._minutes + seconds_carry
 56         minutes_carry = minutes_add // 60    #超过60分进位
 57         minutes = minutes_add % 60   #分钟
 58         hours_add = self._hours + other._hours + minutes_carry
 59         if(hours_add<24):
 60             hours = hours_add
 61         else:
 62             hours = hours_add - 24       #小时
 63         return Time(hours, minutes, seconds)
 64     # -
 65     def __sub__(self, other):
 66         if self._seconds < other._seconds:
 67             self._minutes -= 1
 68             seconds = self._seconds + 60 - other._seconds
 69         else:
 70             seconds = self._seconds - other._seconds
 71         if self._minutes < other._minutes:
 72             self._hours -= 1
 73             minutes = self._minutes + 60 - other._minutes
 74         else:
 75             minutes = self._minutes - other._minutes
 76         if self._hours < other._hours:
 77             hours = self._hours + 24 - other._hours
 78         else:
 79             hours = self._hours - other._hours
 80         return Time(hours, minutes, seconds)
 81     # ==
 82     def __eq__(self, other):
 83         bool1 = False
 84         bool2 = False
 85         bool3 = False
 86         if self._hours == other._hours:
 87             bool1 = True
 88         if self._minutes == other._minutes:
 89             bool2 = True
 90         if self._seconds == other._seconds:
 91             bool3 = True
 92         return bool1 and bool2 and bool3
 93 
 94     # <
 95     def __lt__(self, other):
 96         if self._hours < other._hours:
 97             return True
 98         elif self._hours == other._hours:
 99             if self._minutes < other._minutes:
100                 return True
101             elif self._minutes == other._minutes:
102                 if self._seconds < other._seconds:
103                     return True
104                 else:
105                     return False
106             else:
107                 return False
108         else:
109             return False
110 
111     def hours(self):
112         return self._hours
113 
114     def minutes(self):
115         return self._minutes
116 
117     def seconds(self):
118         return self._seconds
119 
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 + t1
130     cha = t - t1
131     print(he)
132     print(cha)
133     print(t==t1)
134     print(t<t1)
135     print(he.hours())