01. /* 02. * copyright and version Declaration of the program part 03. * Copyright (c) 2013, Yantai University Computer college student 04. * All rightsreserved. 05. * file name: ctime. cpp 06. * Author: Zhao guanzhe 07. * Completion Date: April 8, May 7, 2013. * version: v1.0 09. * input Description: 10. * Problem description: 11. */# include <iostream> using namespace std; class CTime {private: unsigned short int hour; // unsigned short int minute; // unsigned short int second; // seconds public: CTime () {} friend istream & operator> (istream &, CTime &); Friend ostream & operator <(ostream &, CTime &); // reload bool operator> (CTime & t) of the comparison operator (object ); bool operator <(CTime & t); bool operator >=( CTime & t); bool operator <= (CTime & t); bool operator = (CTime & t ); bool operator! = (CTime & t); // CTime operator + (CTime & c ); // return the time CTime operator-(CTime & c) after the hour, minute, and second specified by c; // Control + understand CTime operator + (int s ); // return the time CTime operator-(int s) after s seconds; // return the time before s seconds // reload CTime operator ++ (int) of the target operator ); // post ++, CTime operator ++ () Next second; // front ++, next CTime operator -- (int); // post --, previous second CTime operator -- (); // front --, previous second // reload CTime operator + = (CTime & c) of the value assignment operator ); CTime operator-= (CTime & c); CTime operator ++ = (int s );/ /Return the time CTime operator-= (int s) after s; // return the time before s}; // all operators are implemented below to overload the code. // To simplify programming, you must call existing functions to take advantage of the relationships between functions istream & operator> (istream & input, CTime & t) {cout <"Enter the hour, minute, and second (h m s):" <endl; input> t. hour> t. minute> t. second; if (t. hour <0 | t. hour> 59 | t. minute <0 | t. minute> 59 | t. second <0 | t. second> 59) {t. hour = 0, t. minute = 0, t. second = 0; cout <"invalid time! "<Endl; exit (0);} return input;} ostream & operator <(ostream & output, CTime & t) {output <t. hour <":" <t. minute <":" <t. second; return output;} bool CTime: operator> (CTime & t) {if (hour> t. hour) return true; else if (hour = t. hour & minute> t. minute) return true; else if (minute = t. minute & second> t. second) return true; else return false;} bool CTime: operator <(CTime & t) {I F (hour <t. hour) return true; else if (hour = t. hour & minute <t. minute) return true; else if (minute = t. minute & second <t. second) return true; else return false;} bool CTime: operator >=( CTime & t) {if (hour> t. hour) return true; else if (hour = t. hour & minute> t. minute) return true; else if (minute = t. minute & second> = t. second) return true; else return false;} bool CTime: operat Or <= (CTime & t) {if (hour <t. hour) return true; else if (hour = t. hour & minute <t. minute) return true; else if (minute = t. minute & second <= t. second) return true; else return false;} bool CTime: operator = (CTime & t) {if (hour = t. hour & minute = t. minute & second = t. second) return true; else return false;} bool CTime: operator! = (CTime & t) {if (hour = t. hour & minute = t. minute & second = t. second) return false; else return true;} CTime: operator + (CTime & c) {CTime ct (* this); if (ct. second + c. second> = 60) {ct. second = ct. second + c. second-60; ++ ct. minute;} else ct. second = ct. second + c. second; if (ct. minute + c. minute> = 60) {ct. minute = ct. minute + c. minute-60; ++ ct. hour;} else ct. minute = ct. minute + C. minute; if (ct. hour + c. hour> = 60) ct. hour = ct. hour + c. hour-60; else ct. hour = ct. hour + c. hour; return ct;} CTime: operator-(CTime & c) {CTime ct (* this); if (ct. second <c. second) {-- ct. minute; ct. second = ct. second + 60-c. second;} else ct. second = ct. second-c. second; if (ct. minute <c. minute) {-- ct. hour; ct. minute = ct. minute + 60-c. minute;} else ct. minute = ct. minute-c. Minute; if (ct. hour <c. hour) ct. hour = ct. hour + 24-c. hour; else ct. hour = ct. hour-c. hour; return ct;} CTime: operator + (int s) {CTime ct; ct. hour = s/3600; s = s % 3600; ct. minute = s/60; ct. second = s % 60; return (* this + ct);} CTime: operator-(int s) {CTime ct; ct. hour = s/3600; s = s % 3600; ct. minute = s/60; ct. second = s % 60; return (* this-ct);} CTime: operator ++ (Int) {CTime tp (* this); * this = * this + 1; return tp;} CTime: operator ++ () {return (* this = * this + 1);} CTime: operator -- (int) {CTime tp (* this); * this = * this-1; return tp;} CTime: operator -- () {return (* this = * this-1); // you need to change the value} CTime :: operator + = (CTime & c) {return (* this = * this + c);} CTime: operator-= (CTime & c) {return (* this = * this-c);} CTime: operator + = (Int s) {return (* this = * this + s);} CTime: operator-= (int s) {return (* this = * this-s);} void main () {// CTime t1 (8, 20, 25), t2 (11, 20, 50 ), t; CTime t1, t2, t; cin> t1> t2; cout <"t1:" <t1 <endl; cout <"t2: "<t2 <endl; cout <" the following two time sizes are compared: \ n "; if (t1> t2) cout <" t1> t2 "<endl; if (t1 <t2) cout <"t1 <t2" <endl; if (t1 = t2) cout <"t1 = t2" <endl; if (t1! = T2) cout <"t1 =t2" <endl; if (t1> = t2) cout <"t1 ≥t2" <endl; if (t1 <= t2) cout <"t1 ≤ t2" <endl; cout <endl; // The following is a self-designed test of heavy loads of other operators t = t1 + t2; cout <"t1 + t2 =" <t <endl; t = t1-t2; cout <"t1-t2 =" <t <endl; t2 = t2-365; cout <"t2 advances 365 seconds. T2-365 = "<t2 <endl; t1 = t1 + 3; cout <" t1 3 s. T1 + 3 = "<t1 <endl; cout <" ++ t1 = "<++ t1 <endl; cout <"t1 =" <t1 <endl; cout <"-- t1 =" <-- t1 <endl; cout <"t1 =" <t1 <endl; cout <"t2 ++ =" <t2 ++ <endl; cout <"t2 =" <t2 <endl; cout <"t2 -- =" <t2 -- <endl; cout <"t2 =" <t2 <endl ;}
Running result: