#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> #include<assert.h> using namespace std; class Date { public: void display() { cout << _year << "-" << _month << "-" << _day << endl; } bool IsLeapyear(int year) { return ((year / 400 == 0) || (year / 4 == 0 && year / 100 != 0)); } int GetMonthDay(int year, int month) { if (month > 12 || month < 1) { printf("無此月\n"); return -1; } else { int dayarr[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (IsLeapyear(year) && month == 2) { return 29; } else { return dayarr[month - 1]; } } } Date& operator+(const int day) { if (day < 0) { return *this - (-day); } else { _day += day; while (_day > GetMonthDay(_year, _month)) { _day -= GetMonthDay(_year, _month); if (_month == 12) { _year += 1; _month = 1; } else { _month += 1; } } } return *this; } Date& operator-(int day) { if (day < 0) { return *this + (-day); } else { _day -= day; while (_day <= 0)//注意 等於 的時候 { if (_month == 1) { _year -= 1; _month = 12; } else { _month -= 1; } _day += GetMonthDay(_year, _month); } } return *this; } Date operator++(int day)//後置加加 { Date tem = *this; *this = *this + 1; return tem; } Date& operator++()//前置加加 { *this = *this + 1; return *this; } Date& operator--(int day)//後置減減 { Date tem = *this; *this = *this - 1; return tem; } Date& operator--()//前置減減 { *this = *this - 1; return *this; } Date& operator+=(const int day) { *this = *this + day; return *this; } Date& operator-=(const int day) { *this = *this - day; return *this; } bool operator>(const Date& cur) { if (_year > cur._year) { return 1; } else if (_year == cur._year) { if (_month > cur._month) { return 1; } else if (_month == cur._month) { if (_day > cur._day) { return 1; } } } return 0; } bool operator==(const Date& cur) { if (_year == cur._year && _month == cur._month && _day == cur._day) { return 1; } else { return 0; } } bool operator<(const Date& cur) { if (!(*this > cur || *this == cur)) { return 1; } else { return 0; } } bool operator>=(const Date& cur) { if (*this > cur || *this == cur) { return 1; } else { return 0; } } bool operator<=(const Date& cur) { if (*this < cur || *this == cur) { return 1; } else { return 0; } } int operator-(const Date& cur) { int count = 0; Date max; Date min; if (*this > cur) { max = *this; min = cur; } else if (*this < cur) { max = cur; min = *this; } else { return 0; } while (1) { if (min == max) { return count; } else { min = min + 1; count++; } } } public: Date(int year = 1990, int month = 1, int day = 1) :_year(year) , _month(month) , _day(day) {} Date(const Date& a) :_year(a._year) ,_month(a._month) , _day(a._day) {} ~Date() {} Date& operator=(const Date& cur) { _year = cur._year; _month = cur._month; _day = cur._day; return *this; } private: int _year; int _month; int _day; }; //operator+ operator- //operator++ operator-- //operator+= operator-= void test1() { Date cur(2015, 11, 11); //(cur + 18).display(); //(cur + 19).display(); //(cur + 20).display(); //(cur + (-30)).display(); //(cur - 10).display(); //(cur - 11).display(); //(cur - 12).display(); //(cur - (-18)).display(); } //operator> < == >= <= void test2() { Date cur1(2015, 11, 13); Date cur2(2015, 11, 12); cout << (cur1 > cur2) << endl; cout << (cur1 == cur2) << endl; } //operator-(const Date& cur) void test3() { Date date1(2015, 11, 11); Date date2(2015, 11, 1); Date date3(2015, 11, 22); cout << date1 - date2 << endl; cout << date1 - date3 << endl; } int main() { //test1(); //test2(); test3(); system("pause"); return 0; }