[cpp] view plaincopy
01./* (程序头部注释开始)
02.* 程序的版权和版本声明部分
03.* Copyright (c) 2011, 烟台大学计算机学院学生
04.* All rights reserved.
05.* 文件名称:renwu.cpp
06.* 作 者:苗向前
07.* 完成日期:2012 年 4 月 11 日
08.* 版 本 号:8.2
09.* 对任务及求解方法的描述部分
10.* 输入描述:略
11.* 问题描述:略
12.* 程序输出:略
13.* 程序头部的注释结束
14.*/
15.#include <iostream>
16.using namespace std;
17.
18.class CTime
19. {
20. private:
21. unsigned short int hour; // 时
22. unsigned short int minute; // 分
23. unsigned short int second; // 秒
24. public:
25. CTime(int h=0,int m=0,int s=0);
26. void setTime(int h,int m,int s);
27. void display();
28. //比较运算符(二目)的重载
29. bool operator > (CTime &t);
30. bool operator < (CTime &t);
31. bool operator >= (CTime &t);
32. bool operator <= (CTime &t);
33. bool operator == (CTime &t);
34. bool operator != (CTime &t);
35. //二目运算符的重载
36. CTime operator+(CTime &c); //返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
37. CTime operator-(CTime &c);//对照+理解
38. CTime operator+(int s);//返回s秒后的时间
39. CTime operator-(int s);//返回s秒前的时间
40. //一目运算符的重载
41. CTime operator++(int);//后置++,下一秒
42. CTime operator++();//前置++,下一秒
43. CTime operator--(int);//后置--,前一秒
44. CTime operator--();//前置--,前一秒
45. //赋值运算符的重载
46. CTime operator+=(CTime &c);
47. CTime operator-=(CTime &c);
48. CTime operator+=(int s);//返回s秒后的时间
49. CTime operator-=(int s);//返回s秒前的时间
50. };
51.
52.CTime::CTime(int h,int m,int s)
53. {
54. hour=h;
55. minute=m;
56. second=s;
57. }
58.
59.void CTime::setTime(int h,int m,int s)
60. {
61. hour=h;
62. minute=m;
63. second=s;
64. }
65.
66.void CTime::display()
67. {
68. cout<<hour<<":"<<minute<<":"<<second<<endl;
69. }
70.//下面实现所有的运算符重载代码。
71.//为简化编程,请注意通过调用已有函数,利用好各函数之间的关系
72.bool CTime::operator > (CTime &t)
73. {
74. if(hour>t.hour)
75. return true;
76. else if(hour<t.hour)
77. return false;
78. else if(minute>t.minute)
79. return true;
80. else if(minute<t.minute)
81. return false;
82. else if(second>t.second)
83. return true;
84. else if(second<t.second)
85. return false;
86. else
87. return false;
88. }
89.
90.bool CTime::operator < (CTime &t)
91. {
92. if(hour<t.hour)
93. return true;
94. else if(hour>t.hour)
95. return false;
96. else if(minute<t.minute)
97. return true;
98. else if(minute>t.minute)
99. return false;
100. else if(second<t.second)
101. return true;
102. else if(second>t.second)
103. return false;
104. else
105. return false;
106. }
107.
108.bool CTime::operator >= (CTime &t)
109. {
110. CTime t1;
111. t1.hour=hour;
112. t1.minute=minute;
113. t1.second=second;
114. if (t1<t)
115. return false;
116. return true;
117. }
118.
119.bool CTime::operator <= (CTime &t)
120. {
121. CTime t1;
122. t1.hour =hour;
123. t1.minute =minute ;
124. t1.second =second ;
125. if (t1>t)
126. return false;
127. return true;
128. }
129.
130.bool CTime::operator == (CTime &t)
131. {
132. CTime t1;
133. t1.hour=hour;
134. t1.minute=minute;
135. t1.second=second;
136. if (t1>t||t1<t)
137. return false;
138. else
139. return true;
140. }
141.
142.bool CTime::operator != (CTime &t)
143. {
144. CTime t1;
145. t1.hour=hour;
146. t1.minute=minute;
147. t1.second=second;
148. if(t1==t)
149. return false;
150. else
151. return true;
152. }
153.
154.CTime CTime::operator+(CTime &c)
155. {
156. CTime t;
157. t.hour=hour+c.hour;
158. t.minute=minute+c.minute;
159. t.second=second+c.second;
160. if (t.second>59)
161. {
162. t.minute=(t.minute+t.second/60);
163. if (t.minute>59)
164. {
165. t.hour=(t.hour+t.minute/60);
166. if (t.hour>23)
167. t.hour=(t.hour%24);
168. t.minute=(t.minute%60);
169. }
170. t.second=(t.second%60);
171. }
172. if (t.minute>59)
173. {
174. t.hour=(t.hour+t.minute/60);
175. if (t.hour>23)
176. t.hour=(t.hour%24);
177. t.minute=(t.minute%60);
178. }
179. if (t.hour>23)
180. t.hour=(t.hour%24);
181. return t;
182. }
183.
184.CTime CTime::operator-(CTime &c)
185. {
186. CTime t;
187. unsigned short int k,h;
188. t.hour=hour;
189. t.minute=minute;
190. t.second=second;
191. if(second<c.second)
192. {
193. k=(-(second-c.second))%60;
194. h=(-(second-c.second))/60;
195. t.second=(60-k);
196. t.minute=(minute-h-1);
197. if(t.minute<0)
198. {
199. t.minute=(-t.minute);
200. k=t.minute%60;
201. h=t.minute/60;
202. t.hour=t.hour-h-1;
203. t.minute=(60-k);
204. if(t.hour<0)
205. {
206. t.hour=(-t.hour);
207. t.hour=(t.hour%24);
208. }
209. }
210. }
211. else
212. {
213. t.second =second-c.second ;
214. }
215. if(t.minute<c.minute )
216. {
217. h=(-(t.minute -c.minute ))%60;
218. k=(-(t.minute -c.minute ))/60;
219. t.minute =(60-k);
220. t.hour=(t.hour-1-h);
221. if(t.hour<0)
222. {
223. t.hour =(-t.hour );
224. t.hour =(t.hour %24);
225. }
226. }
227. else
228. {
229. t.minute =(t.minute-c.minute );
230. }
231. if(t.hour<c.hour )
232. {
233. t.hour =(-(t.hour -c.hour ))%24;
234. }
235. else
236. {
237. t.hour=(t.hour -c.hour );
238. }
239. return t;
240. }
241.
242.CTime CTime::operator+(int s)
243. {
244. CTime t;
245. t.hour =hour;
246. t.minute =minute ;
247. t.second =second ;
248. t.second =(t.second +s);
249. if (t.second >59)
250. {
251. t.minute+=t.second/60;
252. if (t.minute>59)
253. {
254. t.hour+=t.minute/60;
255. if (t.hour>23)
256. t.hour%=24;
257. t.minute%=60;
258. }
259. t.second%=60;
260. }
261. else if (t.minute>59)
262. {
263. t.hour+=t.minute/60;
264. if (t.hour>23)
265. t.hour%=24;
266. t.minute%=60;
267. }
268. else if (t.hour>23)
269. t.hour%=24;
270. return t;
271. }
272.
273.CTime CTime::operator-(int s)
274. {
275. CTime t;
276. unsigned short int k,h;
277. t.hour =hour;
278. t.minute =minute ;
279. t.second =second ;
280. if(t.second <s)
281. {
282. k=(-(second-s) )%60;
283. h=(-(second-s ))/60;
284. t.second =(60-k);
285. t.minute =(minute-h-1);
286. if(t.minute<0)
287. {
288. t.minute =(-t.minute );
289. k=t.minute %60;
290. h=t.minute /60;
291. t.hour=t.hour-h-1;
292. t.minute =(60-k);
293. if(t.hour<0)
294. {
295. t.hour =(-t.hour );
296. t.hour =(t.hour %24);
297. }
298. }
299. }
300. else
301. {
302. t.second =second-s;
303. }
304. return t;
305. }
306.
307.CTime CTime::operator++(int)
308. {
309. CTime temp(*this);
310. second++;
311. if(second>=60)
312. {
313. second-=60;
314. ++minute;
315. }
316. return temp;
317. }
318.
319.CTime CTime::operator++()
320. {
321. if(++second>=60)
322. {
323. second-=60;
324. ++minute;
325. }
326. return *this;
327. }
328.
329.CTime CTime::operator--(int)
330. {
331. CTime temp(*this);
332. second--;
333. if(second<0)
334. {
335. second=59;
336. --minute;
337. }
338. return temp;
339. }
340.CTime CTime::operator--()
341. {
342. if(--second<0)
343. {
344. second=59;
345. --minute;
346. }
347. return *this;
348. }
349.
350.CTime CTime::operator+=(CTime &c)
351. {
352. CTime t1,t;
353. t1.hour =hour;
354. t1.minute =minute;
355. t1.second =second;
356. t=t1+c;
357. hour=t.hour;
358. minute=t.minute;
359. second=t.second;
360. return t;
361. }
362.
363.CTime CTime::operator-=(CTime &c)
364. {
365. CTime t1,t;
366. t1.hour=hour;
367. t1.minute=minute;
368. t1.second=second;
369. t=t1-c;
370. hour=t.hour;
371. minute=t.minute;
372. second=t.second;
373. return t;
374. }
375.
376.CTime CTime::operator+=(int s)
377. {
378. CTime t1,t;
379. t1.hour=hour;
380. t1.minute=minute;
381. t1.second=second;
382. t=t1+s;
383. hour=t.hour;
384. minute=t.minute;
385. second=t.second;
386. return t;
387. }
388.
389.CTime CTime::operator-=(int s)
390. {
391. CTime t1,t;
392. t1.hour=hour;
393. t1.minute=minute;
394. t1.second=second;
395. t=t1-s;
396. hour=t.hour;
397. minute=t.minute;
398. second=t.second;
399. return t;
400. }
401.
402.int main()
403. {
404. CTime t1(8,20,25),t2(11,20,50),t;
405. int n;
406. cout<<"t1为:";
407. t1.display();
408. cout<<"t2为:";
409. t2.display();
410. cout<<"下面比较两个时间大小:\n";
411. if (t1>t2) cout<<"t1>t2"<<endl;
412. if (t1<t2) cout<<"t1<t2"<<endl;
413. if (t1==t2) cout<<"t1=t2"<<endl;
414. if (t1!=t2) cout<<"t1≠t2"<<endl;
415. if (t1>=t2) cout<<"t1≥t2"<<endl;
416. if (t1<=t2) cout<<"t1≤t2"<<endl;
417. cout<<endl;
418. cout<<"t1+t2的时间为:";
419. t=t1+t2;
420. t.display();
421. cout<<"t1-t2的时间为:";
422. t=t1-t2;
423. t.display();
424. cout<<endl;
425. t = t1 + t2;
426. cout << "t1+t2 = ";
427. t.display();
428. t = t1++;
429. cout << "t1++ = ";
430. t.display();
431. t = ++t1;
432. cout << "++t1 = ";
433. t.display();
434. t = t1--;
435. cout << "t1-- = ";
436. t.display();
437. t = --t1;
438. cout << "--t1 = ";
439. t.display();
440. cout<<endl;
441. system("pause");
442. return 0;
443. }
运行结果:
![](https://img-my.csdn.net/uploads/201204/11/1334117844_3311.jpg)