Date Counter-c++ implementation __c++

Source: Internet
Author: User

Date Counter implementation: Define the date class in the program: Class date{}; The main use of operator overload knowledge in which to implement: date plus or minus a certain number of days, date minus the date difference days, the two dates of various comparisons and date input and output functions.

Specific code implementation:

#include <iostream> #include <cstdlib> using namespace std;
	Class Data {friend ostream& operator<< (ostream& _cout, const data& D);
Friend istream& operator>> (istream& _cin, data& D); Public:data (const int YEAR=1900,CONST int Month=1,const int day=1): _year (year), _month (month), _day (day) {} Dat
		A (const data& Data) {_year=data._year;
		_month=data._month;
	_day=data._day;    ~data () {} int getdaysinmonth (int year,int month);       Gets the number of days per month in bool Isleapyear (int year);       Judging leap year bool Isvalidata ();    
	Determine whether the date is legal data & operator = (const data &d);
	Data operator+ (int day);
	Data operator-(int day);
	int operator-(const Data &d);
	Data &operator-();
	Data operator-(int);
	Data &operator + + ();
	Data operator + + (int);
	BOOL operator > (const Data &d);
	BOOL operator < (const Data &d);
	BOOL operator >= (const Data &d);
	BOOL operator <= (const Data &d); BOOL OperAtor = = (const Data &d);
BOOL operator!= (const Data &d);
	Private:int _year;
	int _month;
int _day;

}; BOOL Data::isleapyear (int year)//judging leap years {return (year%4==0) && (year%100!=0)) | |
(year%400==0) True:false; BOOL Data::isvalidata ()//Determine whether the date is valid {if (_year>=1900) && ((_month>0) && (_month<13) &am
	p;& (_day<=getdaysinmonth (_year,_month)) && (_day>0)) {return true;
return false;
	int data::getdaysinmonth (int year,int month)//Get the number of days per month {int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	if (Isleapyear (year) && (month==2)) {days[month]++;
return Days[month]; } ostream& operator<< (ostream& _cout, const data& d) {_cout<<d._year<< "-" <<d._
	month<< "-" <<d._day;
return _cout;
	} istream& operator>> (istream& _cin, data& d) {_cin>>d._year>>d._month>>d._day;
return _cin; Data & Data:: operator = (CONST Data &d) {if (this!= &d) {_year=d._year;
		_month=d._month;
	_day=d._day;
return *this;
		} Data data::operator+ (int day) {if (day<0) {day=0-day;
	return *this-day;
	} Data tmp (*this);
	Tmp._day+=day;
		while (Tmp._day>getdaysinmonth (Tmp._year,tmp._month)) {tmp._day-=getdaysinmonth (tmp._year,tmp._month);
		tmp._month++;
			if (tmp._month==13) {tmp._month=1;
		tmp._year++;
} return TMP;
		Data data:: operator-(int day) {if (day<0) {day=0-day;
	return *this+day;
	} Data tmp (*this);
	Tmp._day-=day;
		while (tmp._day<=0) {tmp._month--;
			if (tmp._month==0) {tmp._month=12;
		tmp._year--;
	} tmp._day+=getdaysinmonth (Tmp._year,tmp._month);
return TMP;
	int Data::operator-(const Data &d) {int count=0;
		if (*this>d) {Data tmp=d;
			while (tmp!=*this) {tmp++;
		count++;
	return count;
		else {Data tmp=*this;
			while (Tmp!=d) {tmp++;
		count++;
	return count; } Data & DaTa::operator + + ()//predecessor + + {_day+=1;
		if (_day>getdaysinmonth (_year,_month)) {_month++;
			if (_month==13) {_year++;
		_month=1;
	} _day=1;
return *this;
	} data Data::operator + + (int)//post + + {data tmp=*this;
	_day+=1;
		if (_day>getdaysinmonth (_year,_month)) {_month++;
			if (_month==13) {_year++;
		_month=1;
	} _day=1;
return TMP;
	Data & Data::operator-()//front---{_day-=1;
		if (_day==0) {_month--;
			if (_month==0) {_year--;
		_month=12;
	} _day=getdaysinmonth (_year,_month);
return *this;
	} Data Data::operator-(int)//post--{data tmp=*this;
	_day-=1;
		if (_day==0) {_month--;
			if (_month==0) {_year--;
		_month=12;
	} _day=getdaysinmonth (_year,_month);
return TMP;
	BOOL Data::operator > (const Data &d) {if (_year>d._year) {return true;
		' Else if (_year==d._year) {if (_month>d._month) {return true;
else if (_month==d._month) {if (_day>d._day) {				return true;
}} return false;
	BOOL Data::operator < (const Data &d) {if (_year<d._year) {return true;
		' Else if (_year==d._year) {if (_month<d._month) {return true;
			' Else if (_month==d._month) {if (_day<d._day) {return true;
}} return false; BOOL Data::operator >= (const Data &d) {return! (
*THIS&LT;D); BOOL Data::operator <= (const Data &d) {return! (
*THIS&GT;D); BOOL Data::operator = = (Const Data &d) {return (_year==d._year) && (_month==d._month) && (_day==d._
Day); BOOL Data::operator!= (const Data &d) {return! (
*this==d);

 }
The test function void Test1 () {Data D1 (2016,9,30);
	Data D2 (2016,10,6);
	D1=D2;
	d2=d2+999;
	d1=d1-999;
	Data d=--d2;
	d=d2--;
	Data D3 (2016,9,30);
	Data D4 (2001,10,6);
	int ret=d3-d4;
cout<<ret<<endl;
	} void Test2 () {Data D1 (2016,11,8);
	Data D2 (2016,11,7);
	if (d1>d2) {cout<< "d1>d2" <<endl;
	} Data D3 (2016,11,6);
	Data D4 (2016,11,7);
	if (d3<d4) {cout<< "d3<d4" <<endl;
	} Data d5 (2016,11,9);
	Data d6 (2016,11,7);
	if (d5>=d6) {cout<< "D5>=d6" <<endl;
	} Data D7 (2016,10,6);
	Data D8 (2016,10,21);
	if (D7&LT;=D8) {cout<< "D7<=d8" <<endl;
	} Data D9 (2016,11,6);
	Data D0 (2016,11,6);
	if (d9==d0) {cout<< "d9==d0" <<endl;
	Data da (2016,11,6);
	Data db (2016,11,7);
	if (da!=db) {cout<< "da!=db" <<endl;
	} void Test3 () {Data D1 (2016,3,2);
	cin>>d1;
cout<<d1<<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.