C + + Implementation Date Class

Source: Internet
Author: User

#include <iostream>using namespace std;class date{public:date (int year,int month , Int day): _year (year), _month (month), _day (day) {    if (year<1900| | month<1| | month>12| | day<1| | Day>getmonthday (year,month)) cout<< "Invalid date" &LT;&LT;ENDL;} Bool isleapyear (Int year)//To determine whether it is a leap year {   return  (year%4==0&&year%100!=0| | year%400==0);}     int getmonthday (int year,int month)//Gets the number of days per month {int montharr[12]={ 31,28,31,30,31,30,31,31,30,31,30,31};if (Isleapyear (year) &&month==2) {    return  montharr[month-1]+1;} ELSERETURN&NBSP;MONTHARR[MONTH-1];} date operator+ (Int day)//date + days {  date tmp (*this);  tmp._day +=day;   while (Tmp._day >getmonthday (tmp._year ,tmp._month ))   {       tmp._day -=getmonthday (Tmp._year ,tmp._month );   if (tmp._month ==12)   {       tmp._year  +=1;   tmp._month =1;  }  else  {       tmp._month +=1;  }  }  return tmp;} date operator-(Int day)//date-days {  date tmp (*this);  tmp._day -=day;   while (tmp._day <0)   {      tmp._day = Getmonthday (tmp._year ,tmp._month )-(-tmp._day)  ;  if (tmp._month ==1)    {       tmp._year -=1;   tmp._month =12;   }  else  {      tmp._month -=1;   }  }  return tmp;} int operator-(CONST&NBSP;DATE&AMP;&NBSP;D)//date minus date {     int countday=0;  int&nbsP;countmonth=0; int countyear=0; date tmp (*this);  if (_day<d._day )  {      countday=_day+getmonthday (D._year,d._month)-d._day ; _month=_month-1;  } else {      countday=_day-d._day ; }  if (_ month<d._month )  {     while (_month)  {      countmonth+=getmonthday (_year,_month); _month--; } int n=12-d._month; while (n)  {  date tmp (*this);       countmonth+=getmonthday (d._year ,d._month+ n-1 );  n -- ; } _year=_year-1; }  else  {  Int n=_month-d._month;     while (n)  {       Countmonth+=getmonthday (d._year ,d._month+n-1 );  n--; }  }  if ( Isleapyear (d._year))  {&nbsP;   countyear= (_year-d._year ) *366; } else {       countyear= (_year-d._year ) *365; }return countday+countmonth+countyear;} Void display () {    cout<<_year<< "-" <<_month<< "-" <<_day <<endl;} private:int _year;int _month;int _day;}; Void test1 () {    date d1 (2015,1,16);d 1. display  ();//(D1+34). display  ();(d 1-30). display  ();} Void test2 () {   date d1 (2015,11,13);    d1. display  ();    date d2 (2015,10,13);    d2. display  ();     cout<<d1-d2<<endl;//Date-Date}int main () {Test2 (); System (" Pause ");     return 0;}


C + + Implementation Date Class

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.