#include <iostream>
using namespace std;
//日期是否合法
//日期比较
//两个日期中间差的天数
//日期加上或减去一定的天数后的日期
class Date
{
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
public:
Date(int year = 1, int month = 1, int day = 1)
{
if(year>0 && month>0 && month<13 && day>0 && day<32)
{
_year = year;
_month = month;
_day = day;
}
else
cout<<"日期不合法"<<endl;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
void Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
~Date()
{
}
bool operator==(const Date& d)
{
if( _year == d._year && _month == d._month && _day == d._day)
return true;
else
return false;
}
bool operator>(const Date& d)
{
if(_year > d._year)
return false;
else if(_year==d._year && _month>d._month )
return false;
else if(_year==d._year && _month==d._month && _day>d._day)
return false;
else
return true;
}
bool operator>=(const Date& d)
{
return (*this > d) || (*this == d);
}
bool operator<(const Date& d)
{
return !(*this >= d);
}
bool operator<=(const Date& d)
{
return !(*this > d);
}
Date& operator=(const Date &d)
{
if (this != &d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
}
Date operator+(int days)
{
if(days < 0)
{
return operator-(-days);
}
int a = 0;
int sumDays = _day + days;
while(sumDays > (a = GetDays(_year, _month)))
{
sumDays -= GetDays(_year, _month);
_month++;
if(_month > 12)
{
_year++;
_month -= 12;
}
else
{
_day = sumDays;
}
}
_day = sumDays;
return *this;
}
Date& operator+=(int day)
{
*this = operator+(day);
return *this;
}
Date operator-(int days)
{
if(days < 0)
{
return operator+(-days);
}
while(days >= _day)
{
days -= _day;
if(_month == 1)
{
_year--;
_month = 12;
}
else
{
_month--;
}
_day = GetDays(_year, _month);
}
_day -= days;
return *this;
}
Date& operator-=(int day)
{
*this = operator-(day);
return *this;
}
Date& operator++()
{
++_day;
if(_day > GetDays(_year, _month))
{
_day -= GetDays(_year, _month);
++_month;
if(_month > 12)
{
_month = 1;
_year++;
}
}
return *this;
}
Date operator++(int)
{
Date temp = *this;
*this = operator++();
return temp;
}
Date& operator--()
{
if (_day > 1)
{
--_day;
}
else
{
if (_month == 1)
{
--_year;
_month = 12;
_day = GetDays(_year, _month);
}
else
{
--_month;
_day = GetDays(_year, _month);
}
}
return *this;
}
Date operator--(int)
{
Date temp = *this;
*this = operator--();
return temp;
}
int Day_day(Date& d)
{
int days = 0;
int balance = 0;
if(*this > d)
{
Date temp = *this;
*this = d;
d = temp;
}
if(_year==d._year && _month==d._month)
{
balance = _day - d._day;
}
else if(_year==d._year)
{
balance = (GetDays(d._year, d._month) - d._day) + _day;
}
else
{
d._day = GetDays(d._year, d._month) - d._day;//计算大的日期距离当年年底的天数
while((++d._month)<=12)
d._day += GetDays(d._year, d._month);
while(--_month)//计算小的年份距离年初的天数
_day += GetDays(_year, _month);
while( (++d._year)<_year )//计算两个日期间隔的年份的天数
{
if(IsLeapYear(d._year))
days += 366;
else
days += 365;
}
balance = d._day + _day + days; //间隔天数
}
}
return balance;
}
private:
bool IsLeapYear(int year)
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return true;
else
return false;
}
int GetDays(int year, int month)
{
int month_Array[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = month_Array[month - 1];
if (month == 2 && IsLeapYear(year))
{
day += 1;
}
return day;
}
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout<<d._year<<"-"<<d._month<<"-"<<d._day;
return _cout;
}
istream& operator>>(istream& _cin, Date& d)
{
_cin>>d._year>>d._month>>d._day;
return _cin;
}
void FunTest()
{
//Date d1(2016, 0, 23 );
//d1.Display();
//Date d2;
//cin>>d2;
//cout<<d2<<endl;
/*
Date d1(2016, 10, 23 );
Date d2(2016, 10, 1 );
bool b = d1 == d2;
b = d1>d2;
b = d1<=d2;
cout<<b<<endl;
Date d3;
d3 = d1;
d3.Display();
*/
/*Date d4(2016, 10, 23 );
Date d5 = d4+10;
d5 = d4++;
d5 = ++d4;
d5.Display();*/
/*
Date d4(2016, 10, 23 );
Date d5 = d4-30;
d5 = d4--;
d5 = --d4;
d5.Display();
*/
/*
Date d4(2016, 10, 23 );
d4 += 10;
d4 -= 20;
d4.Display();
*/
Date d6(2016, 10, 24 );
Date d7(2015, 10, 24 );
cout<<d6.Day_day(d7)<<endl;
}
int main()
{
FunTest();
system("pause");
return 0;
}
using namespace std;
//日期是否合法
//日期比较
//两个日期中间差的天数
//日期加上或减去一定的天数后的日期
class Date
{
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
public:
Date(int year = 1, int month = 1, int day = 1)
{
if(year>0 && month>0 && month<13 && day>0 && day<32)
{
_year = year;
_month = month;
_day = day;
}
else
cout<<"日期不合法"<<endl;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
void Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
~Date()
{
}
bool operator==(const Date& d)
{
if( _year == d._year && _month == d._month && _day == d._day)
return true;
else
return false;
}
bool operator>(const Date& d)
{
if(_year > d._year)
return false;
else if(_year==d._year && _month>d._month )
return false;
else if(_year==d._year && _month==d._month && _day>d._day)
return false;
else
return true;
}
bool operator>=(const Date& d)
{
return (*this > d) || (*this == d);
}
bool operator<(const Date& d)
{
return !(*this >= d);
}
bool operator<=(const Date& d)
{
return !(*this > d);
}
Date& operator=(const Date &d)
{
if (this != &d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
}
Date operator+(int days)
{
if(days < 0)
{
return operator-(-days);
}
int a = 0;
int sumDays = _day + days;
while(sumDays > (a = GetDays(_year, _month)))
{
sumDays -= GetDays(_year, _month);
_month++;
if(_month > 12)
{
_year++;
_month -= 12;
}
else
{
_day = sumDays;
}
}
_day = sumDays;
return *this;
}
Date& operator+=(int day)
{
*this = operator+(day);
return *this;
}
Date operator-(int days)
{
if(days < 0)
{
return operator+(-days);
}
while(days >= _day)
{
days -= _day;
if(_month == 1)
{
_year--;
_month = 12;
}
else
{
_month--;
}
_day = GetDays(_year, _month);
}
_day -= days;
return *this;
}
Date& operator-=(int day)
{
*this = operator-(day);
return *this;
}
Date& operator++()
{
++_day;
if(_day > GetDays(_year, _month))
{
_day -= GetDays(_year, _month);
++_month;
if(_month > 12)
{
_month = 1;
_year++;
}
}
return *this;
}
Date operator++(int)
{
Date temp = *this;
*this = operator++();
return temp;
}
Date& operator--()
{
if (_day > 1)
{
--_day;
}
else
{
if (_month == 1)
{
--_year;
_month = 12;
_day = GetDays(_year, _month);
}
else
{
--_month;
_day = GetDays(_year, _month);
}
}
return *this;
}
Date operator--(int)
{
Date temp = *this;
*this = operator--();
return temp;
}
int Day_day(Date& d)
{
int days = 0;
int balance = 0;
if(*this > d)
{
Date temp = *this;
*this = d;
d = temp;
}
if(_year==d._year && _month==d._month)
{
balance = _day - d._day;
}
else if(_year==d._year)
{
balance = (GetDays(d._year, d._month) - d._day) + _day;
}
else
{
d._day = GetDays(d._year, d._month) - d._day;//计算大的日期距离当年年底的天数
while((++d._month)<=12)
d._day += GetDays(d._year, d._month);
while(--_month)//计算小的年份距离年初的天数
_day += GetDays(_year, _month);
while( (++d._year)<_year )//计算两个日期间隔的年份的天数
{
if(IsLeapYear(d._year))
days += 366;
else
days += 365;
}
balance = d._day + _day + days; //间隔天数
}
}
return balance;
}
private:
bool IsLeapYear(int year)
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return true;
else
return false;
}
int GetDays(int year, int month)
{
int month_Array[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = month_Array[month - 1];
if (month == 2 && IsLeapYear(year))
{
day += 1;
}
return day;
}
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout<<d._year<<"-"<<d._month<<"-"<<d._day;
return _cout;
}
istream& operator>>(istream& _cin, Date& d)
{
_cin>>d._year>>d._month>>d._day;
return _cin;
}
void FunTest()
{
//Date d1(2016, 0, 23 );
//d1.Display();
//Date d2;
//cin>>d2;
//cout<<d2<<endl;
/*
Date d1(2016, 10, 23 );
Date d2(2016, 10, 1 );
bool b = d1 == d2;
b = d1>d2;
b = d1<=d2;
cout<<b<<endl;
Date d3;
d3 = d1;
d3.Display();
*/
/*Date d4(2016, 10, 23 );
Date d5 = d4+10;
d5 = d4++;
d5 = ++d4;
d5.Display();*/
/*
Date d4(2016, 10, 23 );
Date d5 = d4-30;
d5 = d4--;
d5 = --d4;
d5.Display();
*/
/*
Date d4(2016, 10, 23 );
d4 += 10;
d4 -= 20;
d4.Display();
*/
Date d6(2016, 10, 24 );
Date d7(2015, 10, 24 );
cout<<d6.Day_day(d7)<<endl;
}
int main()
{
FunTest();
system("pause");
return 0;
}