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

时间:2022-02-11 18:56:02
题目:
定义一个表示时间的类Time
a)Time(hours,minutes,seconds)创建一个时间对象;
b)t.hours(),t.minutes(),t.seconds()分别返回时间对象t的小时,分钟和秒值
c)为Time对象定义加法和减法操作(用运算符+和-)
d)定义时间对象的等于和小于关系对象(用运算符==和<)
 #!/usr/bin/env python
# -*- coding:utf-8 -*- """
定义一个表示时间的类Time
a)Time(hours,minutes,seconds)创建一个时间对象;
b)t.hours(),t.minutes(),t.seconds()分别返回时间对象t的小时,分钟和秒值
c)为Time对象定义加法和减法操作(用运算符+和-)
d)定义时间对象的等于和小于关系对象(用运算符==和<) ADT Time: #定义时间的抽象数据类型
Time(self, int hours, int minutes, int seconds) #构造时间对象
+(self, Time t2) #求出本对象加t2的结果
-(self, Time t2) #求出本对象减t2的结果
==(self, Time t2) #本对象是否和t2相等
<(self, Time t2) #本对象小于t2
hours(self) #取出本对象的小时
minutes(self) #取出本对象的分钟
seconds(self) #取出本对象的秒值
"""
"""
Author: Minion Xu
Time:2016-11-21
""" class Time(object):
__slots__ = ('_hours','_minutes','_seconds') #创建时间对象
def __init__(self, hours=0, minutes=0, seconds=0):
#类型判断
if not isinstance(hours, int) or not isinstance(minutes, int) or not isinstance(seconds, int):
raise TypeError if(0<=hours<=23):
self._hours = hours
if (0 <= minutes <= 59):
self._minutes = minutes
if (0 <= seconds <= 59):
self._seconds = seconds
else:
raise ValueError("%d is not valid seconds!" % seconds)
else:
raise ValueError("%d is not valid minutes!" % minutes) else:
raise ValueError("%d is not valid hours!" % hours) # +
def __add__(self, other):
seconds_add = self._seconds + other._seconds
seconds_carry = seconds_add // 60 #超过60秒进位
seconds = seconds_add % 60 #秒值
minutes_add = self._minutes + other._minutes + seconds_carry
minutes_carry = minutes_add // 60 #超过60分进位
minutes = minutes_add % 60 #分钟
hours_add = self._hours + other._hours + minutes_carry
if(hours_add<24):
hours = hours_add
else:
hours = hours_add - 24 #小时
return Time(hours, minutes, seconds)
# -
def __sub__(self, other):
if self._seconds < other._seconds:
self._minutes -= 1
seconds = self._seconds + 60 - other._seconds
else:
seconds = self._seconds - other._seconds
if self._minutes < other._minutes:
self._hours -= 1
minutes = self._minutes + 60 - other._minutes
else:
minutes = self._minutes - other._minutes
if self._hours < other._hours:
hours = self._hours + 24 - other._hours
else:
hours = self._hours - other._hours
return Time(hours, minutes, seconds)
# ==
def __eq__(self, other):
bool1 = False
bool2 = False
bool3 = False
if self._hours == other._hours:
bool1 = True
if self._minutes == other._minutes:
bool2 = True
if self._seconds == other._seconds:
bool3 = True
return bool1 and bool2 and bool3 # <
def __lt__(self, other):
if self._hours < other._hours:
return True
elif self._hours == other._hours:
if self._minutes < other._minutes:
return True
elif self._minutes == other._minutes:
if self._seconds < other._seconds:
return True
else:
return False
else:
return False
else:
return False def hours(self):
return self._hours def minutes(self):
return self._minutes def seconds(self):
return self._seconds def print(self):
print(self._hours,":",self._minutes,":",self._seconds) def __str__(self):
return str(self._hours) + ":" + str(self._minutes) + ":" +str(self._seconds) if __name__ == '__main__':
t = Time(21,31,59)
t1 = Time(18,31,41)
he = t + t1
cha = t - t1
print(he)
print(cha)
print(t==t1)
print(t<t1)
print(he.hours())