日期类

来源:互联网 发布:apriori算法 置信度 编辑:程序博客网 时间:2024/05/22 14:28
//万年历#include<iostream>using namespace std;class Date{public://打印日期void Display(){cout<<"Year:"<<_year<<endl;cout<<"Month:"<<_month<<endl;cout<<"Day:"<<_day<<endl;}// 初始化列表进行初始化。 Date (int year = 1900, int month = 1, int day = 1):_year(year),_month(month),_day(day){if((year < 1900)|| (month < 1 || month > 12)|| (day < 1)||day > GetMonthDay(_year,_month))cout<<"Input Error"<<endl;else;}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;}public://// 比较日期的运算符重载//bool operator == (const Date& d)//为什么可以折磨写{return (_year == d._year&& _month == d._month && _day == d._day);}bool operator != (const Date& d){return !(*this == d);}bool operator > (const Date& d)//else if{if(_year > d._year){return true;}if (_year == d._year ){if (_month >d._month ){return true;}if(_month == d._month ){if(_day > d._day ){return true;}}}return false;}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+(int day){Date tmp(*this);tmp._day += day;while(tmp._day < 0){tmp._day -= GetMonthDay(tmp._year ,tmp._month );if(tmp._month == 1){tmp._year --;tmp._month = 12;}else  tmp._month --;return tmp;}while(tmp._day > GetMonthDay(tmp._year ,tmp._month )){tmp._day -= GetMonthDay(tmp._year ,tmp._month );if(tmp._month == 12){tmp._year ++;tmp._month = 1;}elsetmp._month++;}return tmp;}Date operator-(int day){Date tmp(-day);return tmp;}Date& operator-=(int day){Date *this(-day); }Date& operator+=(int day){Date *this(day);}const Date& operator++()// 前置++{*this += 1;return *this;}Date operator++(int)// 后置++{Date tmp(*this);*this += 1;return tmp;}const Date& operator--()// 前置--{*this -= 1;return *this;}Date operator--(int)// 后置--{Date tmp(*this);*this -= 1;return tmp;}bool IsLeapYear(int year){if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0)){return true;}elsereturn false;}int GetMonthDay(int year,int month){static int DayTab[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};if(IsLeapYear(year))DayTab[2] = 29;else DayTab[2] = 28;return DayTab[month];}friend istream& operator >>(istream& is,const Date& d);friend ostream& operator <<(ostream& os,const Date& d);//// 两个日期相加没有意义// 计算两个日期相减以后的差的天数//int operator-(const Date& d){int flag = 0;Date x1 = *this,x2 = d;if(x1 < x2){flag = -1;x1 = d;x2 = *this;}int count = 0;while(x1 == x2){++x2;count++;}return count*flag;}private:int _year;int _month;int _day;};ostream& operator <<(ostream& os,const Date& d){os<<d._year<<"-"<<d._month<<"_"<<d._day<<endl;return os;}istream& operator >>(istream& is,const Date& d){is>>d._year>>"-">>d._month>>"_">>d._day>>endl;return is;}void TestDate(){}int main(){Date d1(2008,10,26);/*cout<<(++d1)<<endl;*//*cout<<(d1 + 1)<<endl;*//*Date d2(2008,10,27);*///cout<<"d1 == d2?  "<<(d1 == d2)<<endl;//cout<<"d1 != d2?  "<<(d1 != d2)<<endl;//cout<<"d1 > d2?  "<<(d1 > d2)<<endl;//cout<<"d1 >= d2?  "<<(d1 >= d2)<<endl;//cout<<"d1 < d2?  "<<(d1 < d2)<<endl;//cout<<"d1 <= d2?  "<<(d1 <= d2)<<endl;/*d2.Display();Date d3;d3 = d2;d3.Display();*/return 0;}

0 0