日期类Date

#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;


}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值