日期类
2015-09-13 15:12
211 查看
//万年历 #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; } else tmp._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; } else return 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; }
相关文章推荐
- YARN源码分析(四)-----Journalnode
- Uva-1220 - Party at Hali-Bula
- 15级新同学风面会
- Android中的动画资源
- java算法系列之一:堆排序算法
- 快速汉字转拼音
- js创建对象的几个常用方式
- 内存泄露检测工具——LeakCanary
- Spring MVC ParameterMethodNameResolver example
- 屏幕适配布局
- 基于 REST 的 Web 服务:基础
- 自动化测试与DevOps以及持续集成的关系。
- c++数组操作
- 复数类
- 华为上机考试题----删除重复字符串和约瑟夫环
- hdu 5441 长春区域赛网络赛 1005 Travel(并查集)
- c++数组操作
- Java记录 -26- StringPool和字面赋值
- HightChart颜色
- android-saripaar:Android UI输入验证框架