日期类

来源:互联网 发布:gnome和x windows 编辑:程序博客网 时间:2024/06/15 08:38
#include <iostream>using namespace std;class Date{public:Date(int year = 2017, int month = 1 , int day = 1)  //构造函数:_year(year),_month(month),_day(day){if(!(_year > 0 && (_month > 0 && _month < 13 ) && (_day > 0 && _day <= GetDaysInMonth(_year,_month)))){_year = 2017;_month = 1;_day = 1;}}Date(const Date& d)   //拷贝构造函数:_year(d._year),_month(d._month),_day(d._day){}    Date& operator=(const Date& d)  //赋值运算符重载{if(this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;}bool IsLeap(int _year)    //判断是否闰年{if((0 == _year%4 && 0!= _year%100) || _year%400 == 0){return true;}return false;}bool operator<(const Date& d){if(_year < d._year){return true;}else if(_year > d._year){return false;}if(_month < d._month){return true;}else if(_month > d._month){return false;}if(_day < d._day){return true;}elsereturn false;}bool operator>(const Date& d){if(_year > d._year){return true;}else if(_year < d._year){return false;}if(_month > d._month){return true;}else if(_month < d._month){return false;}if(_day > d._day){return true;}elsereturn false;}bool operator<=(const Date& d){if(_year < d._year){return true;}else if(_year > d._year){return false;}if(_month < d._month){return true;}else if(_month > d._month){return false;}if(_day < d._day){return true;}else if(_day > d._day){return false;}return true;}bool operator>=(const Date& d){if(_year > d._year){return true;}else if(_year < d._year){return false;}if(_month > d._month){return true;}else if(_month < d._month){return false;}if(_day > d._day){return true;}else if(_day < d._day){return false;}return true;}bool operator!=(const Date& d){if((_year == d._year) && (_month == d._month) && (_day == d._day)){return false;}return true;}bool operator==(const Date& d){if((_year == d._year) && (_month == d._month) && (_day == d._day)){return true;}return false;}   // 求当前日期day天后的日期Date operator+(int day){Date temp(*this); temp._day += day;while(temp._day <= 0){if(temp._month == 1){temp._year -= 1;temp._month = 12;}else{temp._month -= 1;}temp._day += GetDaysInMonth(temp._year,temp._month);}while(temp._day > GetDaysInMonth(temp._year,temp._month)){if(temp._month == 12){temp._year += 1;temp._day -= GetDaysInMonth(temp._year,temp._month);temp._month = 1;}else{temp._day -= GetDaysInMonth(temp._year,temp._month);temp._month += 1;}}return temp;}        // 求当前日期day天前的日期Date operator-(int day){if(day < 0){return (*this)+(0-day);}Date temp(*this); temp._day -= day;while(temp._day <= 0){if(temp._month == 1){temp._year -= 1;temp._month = 12;}else{temp._month -= 1;}temp._day += GetDaysInMonth(temp._year,temp._month);}return temp;}        // 求两个日期之间差多少天 int operator-(const Date& d){Date latter = (*this>d)?(*this):d; Date former = (*this<d)?(*this):d; int count = 0; while(latter != former) { former++; count++; } return count; }// 前置++Date& operator++(){_day += 1;if(_day > GetDaysInMonth(this->_year,this->_month)){if(_month == 12){_year += 1;_month = 1;}else{_month += 1;}_day = 1;}return *this;}// 后置++Date operator++(int){Date temp(*this); operator++(); return temp;}// 前置--Date& operator--(){_day -= 1;if(_day == 0){if(_month == 1){_year -= 1;_month = 12;}else{_month -= 1;}_day = GetDaysInMonth(this->_year,this->_month);}return *this;}// 后置--Date operator--(int){Date temp(*this); operator--();return temp;}~Date(){}private:int _year;int _month;int _day;int GetDaysInMonth(int year,int month)   //获得当前月份的天数{int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};if(IsLeap(year)){days[2] = 29;}return days[month];}};int main(){Date d1(2016,12,29);Date d2(d1);Date d3;d3 = d2;//bool ret = (d3 >= d2);d3 = d3 + 428;//--d3;int ret = d3 - d2;return 0;}


                                             
0 0