进行日期的比较、加减天数及两个日期的时间差等运算。
代码如下:
#include<iostream> #include<assert.h> using namespace std; class Date { public: void Display() { cout << _year << "-" << _month << "-" << _day << endl; } public: Date(int year, int month, int day) {//应判断日期是否有效 //cout << "Date(int year, int month, int day)" << endl; if (year >= 2000 && month > 0 && month<13 && day>0 && day <= GetMonthDay(year, month)) { _year = year; _month = month; _day = day; } else { cout << "日期非法!" << endl; assert(false); } } Date (const Date& d) { //cout << "Date (const Date& d)" << endl; this->_year = d._year; this->_month = d._month; this->_day = d._day; } ~Date() { //cout << "~Date()" << endl; } public://两个日期相比较 bool operator == (const Date& d); bool operator < (const Date& d); bool operator <= (const Date& d); bool operator > (const Date& d); bool operator >= (const Date& d); public://日期计算器 Date operator +(int day); Date& operator +=(int day); Date operator -(int day); Date& operator -=(int day); Date& operator++();//前置++ Date operator++(int);//后置++ Date& operator--();//前置-- Date operator--(int);//后置-- int GetMonthDay(int year, int month); int operator -(const Date& d);//两个日期间相差的天数 private: int _year; int _month; int _day; }; bool Date::operator == (const Date& d) { return this->_year == d._year&&this->_month == d._month&&this->_day == d._day; } bool Date::operator < (const Date& d) { return this->_year < d._year || (this->_year == d._year&&this->_month < d._month) || (this->_year == d._year&&this->_month == d._month&&this->_day < d._day); } bool Date::operator <= (const Date& d) {//复用代码 return *this<d || *this == d; } bool Date::operator > (const Date& d) { return this->_year > d._year || (this->_year == d._year&&this->_month > d._month) || (this->_year == d._year&&this->_month == d._month&&this->_day > d._day); } bool Date::operator >= (const Date& d) {//复用代码 return *this>d || *this == d; } //日期计算器 int Date::GetMonthDay(const int year, const int month)//获得具体某年某月的总天数 { int day; if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) day = 31; else if (month == 2) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) day = 29; else day = 28; } else day = 30; return day; } Date Date::operator +(int day)//日期加天数 { if (day < 0) return *this - (-day); Date tmp = *this; tmp._day += day; while (tmp._day >(GetMonthDay(tmp._year, tmp._month))) { tmp._day -= GetMonthDay(tmp._year, tmp._month); if (tmp._month == 12) { tmp._year += 1; tmp._month = 1; } else tmp._month += 1; } return tmp; } Date& Date::operator +=(const int day) { return *this = *this + day; } Date Date::operator -(const int day)//日期减天数 { if (day < 0) return *this + (-day); Date tmp = *this; tmp._day -= day; while (tmp._day <= 0) { if (tmp._month == 1) { --tmp._year; tmp._month = 12; } else tmp._month--; tmp._day += GetMonthDay(tmp._year, tmp._month); } return tmp; } Date& Date::operator -=(const int day) { return *this = *this - day; } Date& Date::operator++()//前置++ { *this += 1; return *this; } Date Date::operator++(int)//后置++ { Date tmp = *this; *this += 1; return tmp; } Date& Date::operator--()//前置-- { *this -= 1; return *this; } Date Date::operator--(int)//后置-- { Date tmp = *this; *this -= 1; return tmp; } int Date::operator -(const Date& d)//两个日期间相差的天数 {//通过较小的日期不断自加,直到等于较大的日期,统计出自加次数 int flag = 1; int days = 0; Date max = *this; Date min = d; if (max < min) {//调用C++库函数中的swap(std::swap()) std::swap(max._year, min._year); std::swap(max._month, min._month); std::swap(max._day, min._day); flag = -1; } while(!(min == max)) { min++; days++; } return flag*days; } void Test1() {//比较日期大小 Date p1(2016, 1, 20); Date p2(2016, 1, 25); p1.Display(); p2.Display(); bool ret; ret = p1 == p2; cout << "---------ret = " << ret << endl; ret = p1 > p2; cout << "---------ret = " << ret << endl; ret = p1 < p2; cout << "---------ret = " << ret << endl; } void Test2() {//日期加减天数 Date p1(2016, 2, 20); Date p2(2016, 1, 25); p1.Display(); p1 = p1 + 41; p1.Display(); cout << endl; ++p1; p1.Display(); p1++; p1.Display(); cout << endl; p2.Display(); p2 = p2 - 56; p2.Display(); cout << endl; --p2; p2.Display(); p2--; p2.Display(); } void Test3() {//两个日期间相差的天数 Date p1(2016, 2, 20); Date p2(2016, 1, 25); int days = p1 - p2; cout << days << endl; } int main() { Test1(); Test2(); Test3(); system("pause"); return 0; }
时间: 2024-10-11 06:30:17