VS2013
#define _CRT_SECURE_NO_WARNINGS 1 #include<assert.h> #include <iostream> using namespace std; //在实现日期之间的运算之前,要先进行日期是否非法的检查 class Date { public: Date(int year,int month,int day) //构造函数 { if (year >= 2000 && month > 0 && month<13 && day>0 < GetMonthDay(year, month)) //判断日期是否非法 { _year = year; _month = month; _day = day; } else { cout << "日期非法" << endl; assert(false); } } Date(const Date& d) //拷贝构造 { _year = d._year; _month = d._month; _day = d._day; } void Display() { cout << _year << "-" << _month << "-" << _day << endl; } ~Date() { //cout << "~Date()" << endl; } //运算符重载 bool operator==(const Date &d) { return (_year == d._year) && (_month == d._month) && (_day == d._day); } bool operator>(const Date &d) { if (_year > d._year) { return true; } else if (_year == d._year&&_month > d._month) { return true; } else if (_year == d._year&&_month == d._month&&_day > d._day) { return true; } else 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=(const Date &d) //赋值运算符重载 { if (this != &d) { this->_year = d._year; this->_month = d._month; this->_day = d._day; } return *this; } private: 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) //根据已知年月获取该月的天数 { int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int day = monthArray[month]; if (month == 2 && IsLeapYear(year)) { day += 1; } return day; } public: Date operator+(int day) //给一个日期加day天 { if (day < 0) { return operator-(-day); } Date tmp = *this; int sumDays = tmp._day + day; while (sumDays > GetMonthDay(tmp._year, tmp._month)) { sumDays -= GetMonthDay(tmp._year, tmp._month); tmp._month++; if (tmp._month > 12) { tmp._year++; tmp._month %= 12; } else { tmp._day = sumDays; } } return tmp; } Date & operator+=(int day) //加上相应的天数后还要进行赋值 { *this = operator+(day); return *this; } Date operator-(int day) //给一个日期减去day天 { if (day < 0) { return operator+(-day); } Date tmp = *this; while (day >= tmp._day) { day -= tmp._day; if (tmp._month == 1) { tmp._year--; tmp._month = 12; } else { tmp._month--; } tmp._day = GetMonthDay(tmp._year, tmp._month); } tmp._day -= day; return tmp; } Date & operator-=(int day) //减去相应的天数后赋值 { *this = operator-(day); return *this; } Date & operator++() //日期加1 { ++_day; if (_day > GetMonthDay(_year, _month)) { _day -= GetMonthDay(_year, _month); ++_month; if (_month > 12) { ++_year; _month = 1; } } return *this; } Date & operator++(int) //后置++ { Date tmp = *this; *this = operator++(); return tmp; } Date & operator--() { if (_day > 1) { --_day; } else { if (_month == 1) { --_year; _month = 12; _day = GetMonthDay(_year, _month); } else { --_month; _day = GetMonthDay(_year, _month); } } return *this; } Date & operator--(int) { Date tmp = *this; *this = operator--(); return tmp; } //计算两个日期相差的天数 int operator-(Date & d) { if (_year <d. _year) { Date tmp =* this; *this = d; d = tmp; } else if (_year == d._year&&_month < d._month) { Date tmp = *this; *this = d; d = tmp; } else if (_year ==d. _year&&_month == d._month&&_day < d._day) { Date tmp = *this; *this = d; d = tmp; } Date tmp1(*this); Date tmp2(d); int ret = 0; while (!(tmp2 == tmp1)) { tmp2.operator++(); ret++; } return ret; } private: int _year; int _month; int _day; }; void Test1() { /*Date d1(2016, 1, 15); d1.Display(); Date d2 = d1; d2.Display(); Date d3; d3 = d1; d2.Display();*/ Date d1(2016, 1, 15); Date d2(2016, 1, 16); d1.Display(); d2.Display(); // == 0 //bool ret = d1 == d2; //cout << ret << endl; // > 1 //bool ret = d1 >d2; //cout << ret << endl; // < 0 //bool ret = d1 < d2; //cout << ret << endl; // >= 1 //bool ret = d1 >= d2; //cout << ret << endl; // <= 0 //bool ret = d1 <= d2; //cout << ret << endl; // = //d1 = d2; //d1.Display(); //d2.Display(); } void Test2() { Date d1(2016, 2,1); d1.Display(); //Date ret = d1+1; //ret.Display(); //Date ret=d1+70; //ret.Display(); //Date ret=d1-25; //ret.Display(); ////Date ret = d1 += 70; ////ret.Display(); ////d1.Display(); //Date ret = d1 -= 70; //ret.Display(); //d1.Display(); //d1++; //d1.Display(); //++d1; //d1.Display(); //--d1; //d1.Display(); //d1--; //d1.Display(); Date d2(2016, 2, 29); d2.Display(); int ret = d1 - d2; cout << ret << endl; } int main() { //Test1(); Test2(); system("pause"); return 0; }
时间: 2024-11-05 15:52:30