用C/C++實現一個日期類,過載運算子=,==,+,-,++,--,>,>=,
阿新 • • 發佈:2018-12-22
#include<iostream> #include<windows.h> using namespace std; class Date { public: Date(int year, int month, int day) //建構函式 :_year(year) ,_month(month) ,_day(day) {} Date(Date & d) //拷貝構造 :_year(d._year) , _month(d._month) , _day(d._day) {} Date & operator = (const Date &d) //賦值運算子的過載 {//將例項d的所有成員變數值全部賦值給this,這裡不存在指標問題,就不用考慮記憶體問題 _year = d._year; _month = d._month; _day = d._day; return *this; } bool operator == (const Date& d)//過載 == { return this->_year == d._year && this->_month == d._month && this->_day == d._day; } bool operator <(const Date& d) //過載 < { if (_year < d._year)//判斷年 { return true; } else if (_year == d._year)//年相等,就判斷月 { if (_month<d._month) { return true; } else 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)//過載+ { int ret = GetMonthDay(_year, _month);//呼叫函式獲取當年當月天數 Date tmp = *this;//用this建立一個臨時物件 while ((_day + day) > ret)//迴圈條件:當總天數大於一個月的天數時 { if ((tmp._month + 1) > 12)//當月份大於12時 { tmp._year++;//年份加一 tmp._month = 0;//月份置零 } else//否則,月份加一 { tmp._month++; } day -= ret;//總天數減去當月的天數 ret = GetMonthDay(_year, tmp._month);//獲取下一月的天數 } tmp._day += day; return tmp; } Date& operator+= (int day)//過載+=(複用+) { *this = *this + day; return *this; } Date operator- (int day)//過載- { int ret = GetMonthDay(_year, _month); Date tmp = *this; while (day > tmp._day)//當需要減的天>當月的天數時進行迴圈 { while (day > ret) { if (tmp._month > 1) { tmp._month--; } else { tmp._year--; tmp._month = 11; } day -= ret; ret = GetMonthDay(tmp._year, tmp._month); } day -= ret; } tmp._day -= day; return tmp; } Date& operator-= (int day)//過載-=(複用-) { *this = *this - day; return *this; } Date operator++() { return *this += 1; } Date operator++(int) { return *this + 1; } Date operator--() { return *this -= 1; } Date operator--(int) { return *this - 1; } int operator-(const Date& d)//日期間日期 { Date tmp = *this; int day = 0; int ret = 0; if (tmp > d) { while (tmp._year > d._year) { if (tmp._month > 2) { if (IsLeapYear(tmp._year)) { day += 366; tmp._year--; } } else { day += 365; tmp._year--; } } while (tmp._month > d._month) { ret = GetMonthDay(tmp._year, tmp._month); day += ret; } if (tmp._day > d._day) { day += tmp._day - d._day; } else { day -= d._day - tmp._day; } } return day; } void Display() { cout << _year << "\t" << _month << "\t" << _day << endl; } protected: bool IsLeapYear(int year)//判斷是否為閏年 { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return true; } 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; } private: int _year; int _month; int _day; };