「データ構造とアルゴリズムPython言語記述」練習問題第2章第1題(python版)
21784 ワード
:
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 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())
転載先:https://www.cnblogs.com/xautxuqiang/p/6086550.html