Time: Date: timewithdate

Source: Internet
Author: User
Tags date1

Thanks to the party for thanking the motherland for the greatest gratitude of Wang 'er and for helping me identify the mistake t_t)

Then, I don't need to talk nonsense. I need to add code to the question directly.

Question:

1) define a time class, and implement: Comparison of time (= ,! =,>,> =, <, <=), Time increase/decrease several seconds (+ =,-=), time increase/decrease by one second (++ ,--) and the second (-) of the two time differences (-)

2) defines a date class, which can represent: year, month, and day. Design a member function increment for it, which can increase a date by one day.

3) define a time class timewithdate with a date. This class of objects can be compared, added (added to the number of seconds), subtraction (the result is seconds) and other operations

Code:

File List in the header file: Time. h date. h timewithdate. h

File List in the source file: Time. cpp date. cpp timewithdate. cpp

Time. h

# Include <iostream> using namespace STD; class time {PRIVATE: int hour; int minute; int second; public: Time () {hour = 0; minute = 0; second = 0;} Time (int h, int M, int s) {If (h> = 0 & H <= 23) & (M> = 0 & M <= 59) & (S> = 0 & S <= 59) {hour = H; minute = m; second = s;} The else {cout <"time format is incorrect. "<Endl ;}} bool operator == (Time & T); bool Operator! = (Time & T); bool operator> (Time & T); bool operator >=( time & T); bool operator <(Time & T ); bool operator <= (Time & T); time & operator + = (int s); time & operator-= (int s); time & operator ++ (); time & operator -- (); int operator-(Time & T); void Showtime ();};

Date. h

#include <iostream>using namespace std;class Date{private:int year;int month;int day;public:Date(){year = 2000;month = 1;day = 1;}Date(int y,int m,int d);bool operator >(Date &d);bool operator >=(Date &d);bool operator <(Date &d);bool operator <=(Date &d);bool operator ==(Date &d);bool operator !=(Date &d);Date & increment();int operator -(Date &d);void showDate();};

Timewithdate. h

// Define a time class timewithdate with a date. This class of objects can be compared, added (added in seconds), subtraction (result in seconds) and other operations # ifndef date_h # include "date. H "# define date_h # endif # ifndef time_h # include" time. H "# define time_h # endif class timewithdate {PRIVATE: date; time; public: timewithdate () {} timewithdate (INT y, int Mon, int D, int H, int min, int s) {date = Date (Y, Mon, d); time = Time (H, Min, S) ;} bool operator> (timewithdate & TD ); bool operator> = (timewithdate & TD); bool operator = (timewithd Ate & TD); bool Operator! = (Timewithdate & TD); bool operator <= (timewithdate & TD); timewithdate & operator + = (int s ); int operator-(timewithdate & TD); void show ();};

Time. cpp

# Ifndef time_h # include "time. H "# define time_h # endif # include <iostream> using namespace STD; bool time: Operator = (Time & T) {return (hour = T. hour & minute = T. minute & Second = T. second);} bool time: Operator! = (Time & T) {return (hour! = T. Hour | minute! = T. Minute | second! = T. second);} bool time: Operator> (Time & T) {If (hour> T. hour | (hour = T. hour & minute> T. minute) | (hour = T. hour & minute = T. minute & Second> T. second) {return true;} else {return false;} bool time: Operator >=( time & T) {If (hour> T. hour) | (hour = T. hour & minute> T. minute) | (hour = T. hour & minute = T. minute & Second> T. second) | (hour = T. hour & minute = T. minute & Second = T. second) {return true;} else {return false;} bool time: Operator <(Time & T) {If (hour> T. hour | (hour = T. hour & minute> T. minute) | (hour = T. hour & minute = T. minute & Second> T. second) | (hour = T. hour & minute = T. minute & Second = T. second) {return false;} else {return true;} bool time: Operator <= (Time & T) {If (hour> T. hour | (hour = T. hour & minute> T. minute) | (hour = T. hour & minute = T. minute & Second> T. second) {return false;} else {return true ;}} time & time: Operator + = (INT s) {int addhour = s/3600; int addminute = (S-addhour * 3600)/60; int addsecond = s-addhour * 3600-addminute * 60; If (second + addsecond <60) {second = Second + addsecond ;} else {second = Second + addSecond-60; addminute ++;} If (minute + addminute <60) {minute = minute + addminute;} else {minute = minute + addMinute-60; addhour ++;} If (hour + addhour <24) {hour = hour + addhour;} else {hour = hour + addHour-24;} return * This;} Time & time :: operator-= (INT s) {int minushour = s/3600; int minusminute = (S-minushour * 3600)/60; int minussecond = s-minushour * 3600-minusminute * 60; if (second-minussecond> = 0) {second = Second-minussecond;} else {second = Second-minussecond + 60; minusminute = minusminute + 1 ;} if (minute-minusminute> = 0) {minute = minute-minusminute;} else {minute = minute-minusminute + 60; minushour = minushour + 1 ;} if (hour-minushour> = 0) {hour = hour-minushour;} else {hour = hour-minushour + 24;} return * This;} Time & time :: operator ++ () {int addhour = 0; int addminute = 0; int addsecond = 1; if (second + addsecond <60) {second = Second + addsecond ;} else {second = Second + addSecond-60; addminute ++;} If (minute + addminute <60) {minute = minute + addminute;} else {minute = minute + addMinute-60; addhour ++;} If (hour + addhour <24) {hour = hour + addhour;} else {hour = hour + addHour-24;} return * This;} Time & time :: operator -- () {int minushour = 0; int minusminute = 0; int minussecond = 1; if (second-minussecond> = 0) {second = Second-minussecond ;} else {second = Second-minussecond + 60; minusminute = minusminute + 1;} If (minute-minusminute> = 0) {minute = minute-minusminute ;} else {minute = minute-minusminute + 60; minushour = minushour + 1;} If (hour-minushour> = 0) {hour = hour-minushour ;} else {hour = hour-minushour + 24;} return * This;} int time: Operator-(Time & T) {int S = (hour-t.hour) * 3600 + (minute-t.minute) * 60 + second-t.second; return s;} void time: Showtime () {cout 

Date. cpp

// Defines a date type, which can represent year, month, and day. Design a member function increment for it, which can increase a date by one day. # Ifndef date_h # include "date. H "# define date_h # endif # include <iostream> using namespace STD; date: Date (INT y, int M, int d) {year = y; month = m; day = D;} Date & date: increment () {If (Year % 400 = 0) | (Year % 4 = 0) & (Year % 100! = 0) {If (month = 2) {If (day <= 28) {day ++;} else {month = 3; Day = 1 ;}} else if (month = 1 | month = 3 | month = 5 | month = 7 | month = 8 | month = 10) {If (day <= 30) {day ++;} else {month ++; Day = 1 ;}} else if (month = 12) {If (day <= 30) {day ++;} else {month = 1; Day = 1; year ++ ;}} else {If (day <= 29) {day ++;} else {month ++; Day = 1 ;}}} else {If (month = 2) {If (day <= 27) {day ++;} else {month = 3; Day = 1 ;}} else if (month = 1 | month = 3 | month = 5 | month = 7 | month = 8 | month = 10) {If (day <= 30) {day ++ ;} else {month ++; Day = 1 ;}} else if (month = 12) {If (day <= 30) {day ++;} else {month = 1; day = 1; year ++ ;}}else {If (day <=29) {day ++ ;}else {month ++; Day = 1 ;}}} return * This;} void Date: showdate () {cout <year <"year" <month <"month" <day <"day. "<Endl;} bool Date: Operator> (Date & D) {return (Year> D. year) | (year = D. year & month> D. month) | (year = D. year & month = D. month & day> D. day);} bool Date: Operator >=( Date & D) {return (Year> D. year) | (year = D. year & month> D. month) | (year = D. year & month = D. month & day> = D. day);} bool Date: Operator <(Date & D) {return (year <D. year) | (year = D. year & month <D. month) | (year = D. year & month = D. month & day <D. day);} Boo L date: Operator <= (Date & D) {return (year <D. year) | (year = D. year & month <D. month) | (year = D. year & month = D. month & day <= D. day);} bool Date: Operator = (Date & D) {return (year = D. year & month = D. month & day = D. day);} bool Date: Operator! = (Date & D) {return (year! = D. Year | month! = D. Month | day! = D. day) ;}int Date: Operator-(Date & D) {int n = 0; If (year = D. year & month = D. month & day = D. day) {n = 0;} else if (Year> D. year) | (year = D. year & month> D. month) | (year = D. year & month = D. month & day> = D. day) {While (year! = D. Year | month! = D. Month | day! = D. Day) {d. increment (); N ++ ;}} else {While (year! = D. Year | month! = D. Month | day! = D. Day) {increment (); n -- ;}} return N ;}

Timewithdate. cpp

// Define a time class timewithdate with a date. This class of objects can be compared, added (added in seconds), subtraction (result in seconds) and other operations # ifndef timewithdate_h # include "timewithdate. H "# define timewithdate_h # endif # include <iostream> using namespace STD; bool timewithdate: Operator> (timewithdate & TD) {return (date> TD. date) | (date = TD. date & time> TD. time);} bool timewithdate: Operator >=( timewithdate & TD) {return (date> TD. date) | (date = TD. date & time> = TD. time);} Bo Ol timewithdate: Operator <(timewithdate & TD) {return (date <TD. date) | (date = TD. date & time <TD. time);} bool timewithdate: Operator <= (timewithdate & TD) {return (date <TD. date) | (date = TD. date & time <= TD. time);} bool timewithdate: Operator = (timewithdate & TD) {return (date = TD. date) & (time = TD. time);} bool timewithdate: Operator! = (Timewithdate & TD) {return (date! = TD. Date) | (time! = TD. time);} timewithdate & timewithdate: Operator + = (INT s) {int addday = s/86400; S = s-addday * 86400; For (INT I = 0; I <addday; I ++) {date. increment ();} Time + = (s); return * This;} int timewithdate: Operator-(timewithdate & TD) {int daygap = date-TD. date; int secondgap = Time-TD. time; int S = daygap * 86400 + secondgap; return s;} void timewithdate: Show () {date. showdate (); time. showtime ();}

Main. cpp

# Ifndef timewithdate_h # include "timewithdate. H "# define timewithdate_h # endif # include <iostream> using namespace STD; void main () {timewithdate TD1 (,); timewithdate td2, ); cout <(td1-td2) <Endl; td1.show (); If (TD1> td2) {cout <"La la";} Time time1 (, 45 ); time time2 (, 43); int S1 = time1-time2; cout <S1 <Endl; Date date1 (, 23); Date date2 (, 18 ); int S2 = date1-date2; cout <S2 <Endl ;}

I finally wrote it. It's been a long time since I wrote it.

Wang 'er real home exam is a must and a variety of bugs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.