Date calculation --- C ++

Source: Internet
Author: User

Date calculation --- C ++

# Define _ CRT_SECURE_NO_WARNINGS 1 # include <iostream> # include <assert. h> using namespace std; class Date {public: void display () {cout <_ year <"-" <_ month <"-" <_ day <endl;} bool IsLeapyear (int year) {return (year/400 = 0) | (year/4 = 0 & year/100! = 0);} int GetMonthDay (int year, int month) {if (month> 12 | month <1) {printf ("None this month \ n "); return-1;} else {int dayarr [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31 }; if (IsLeapyear (year) & month = 2) {return 29;} else {return dayarr [month-1] ;}} Date & operator + (const int day) {if (day <0) {return * this-(-day);} else {_ day + = day; while (_ day> GetMonthDay (_ year, _ month )) {_ day-= GetMonthDay (_ yea R, _ month); if (_ month = 12) {_ year + = 1; _ month = 1;} else {_ month + = 1 ;}}} return * this;} Date & operator-(int day) {if (day <0) {return * this + (-day);} else {_ day-= day; while (_ day <= 0) // when the value is equal to {if (_ month = 1) {_ year-= 1; _ month = 12 ;} else {_ month-= 1;} _ day + = GetMonthDay (_ year, _ month) ;}} return * this;} Date operator ++ (int day) // Add {Date tem = * this; * this = * this + 1; return tem;} Date & operator ++ ()/ /Add {* this = * this + 1; return * this;} Date & operator -- (int day) to the front minus {Date tem = * this; * this = * this-1; return tem;} Date & operator -- () // minus {* this = * this-1; return * this ;} date & operator + = (const int day) {* this = * this + day; return * this;} Date & operator-= (const int day) {* this = * this-day; return * this;} bool operator> (const Date & cur) {if (_ year> cur. _ year) {return 1;} else if (_ year = cur. _ year ){ If (_ month> cur. _ month) {return 1;} else if (_ month = cur. _ month) {if (_ day> cur. _ day) {return 1 ;}}return 0 ;}bool operator ==( const Date & cur) {if (_ year = cur. _ year & _ month = cur. _ month & _ day = cur. _ day) {return 1 ;}else {return 0 ;}} bool operator <(const Date & cur) {if (! (* This> cur | * this = cur) {return 1 ;}else {return 0 ;}} bool operator >=( const Date & cur) {if (* this> cur | * this = cur) {return 1 ;}else {return 0 ;}} bool operator <= (const Date & cur) {if (* this <cur | * this = cur) {return 1 ;}else {return 0 ;}} int operator-(const Date & cur) {int count = 0; Date max; Date min; if (* this> cur) {max = * this; min = cur;} else if (* this <cur) {max = cur; min = * this;} else {return 0 ;}while (1) {if (min = max) {return count ;}else {min = min + 1; count ++ ;}} public: date (int year = 1990, int month = 1, int day = 1): _ year (year), _ month (month), _ day (day) {} Date (const Date & a): _ year (. _ year), _ month (. _ month), _ day (. _ day ){}~ Date () {} Date & operator = (const Date & cur) {_ year = cur. _ year; _ month = cur. _ month; _ day = cur. _ day; return * this;} private: int _ year; int _ month; int _ day ;}; // operator + operator-// operator ++ operator -- // operator + = operator-= void test1 () {Date cur (2015, 11, 11 ); // (cur + 18 ). display (); // (cur + 19 ). display (); // (cur + 20 ). display (); // (cur + (-30 )). display (); // (cur-10 ). display (); // (cur-11 ). display (); // (cur-12 ). display (); // (cur-(-18 )). display () ;}// operator ><===void test2 () {Date cur1 (2015, 11, 13); Date cur2 (2015, 11, 12); cout <(cur1> cur2) <endl; cout <(cur1 = cur2) <endl ;}// operator-(const Date & cur) void test3 () {Date date1 (2015, 11, 11); Date date2 (2015, 11, 1); Date date3 (2015, 11, 22 ); cout <date1-date2 <endl; cout <date1-date3 <endl;} int main () {// test1 (); // test2 (); test3 (); system ("pause"); return 0 ;}

 

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.