C++日期類(運算子的過載)
阿新 • • 發佈:2018-12-22
日期類(運算子的過載)
在C++裡可以實現運算子的過載,所以可以實現直接對日期類進行加減、比較等操作。
以下是整個Date.h標頭檔案的原始碼:
#pragma once #include<cassert> using namespace std; class Date { public: Date(int year = 1900, int month = 1, int day = 1) :_year(year), _month(month), _day(day) { assert(IsInvalid()); //判斷日期是否合法 } Date(const Date& d) { _year = d._year; _month = d._month; _day = d._day; } ~Date() {} void Display() //列印日期類 { cout << _year << "-" << _month << "-" << _day << endl; } bool IsInvalid() //判斷日期是否合法 { if (_year >= 1900 && _month > 0 && _month < 13 && _day>0 && _day <= GetMonthDays(_year, _month)) { return true; } return false; } int GetMonthDays(int year, int month) //獲取當前月份對應的天數 { static int MonthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month != 2) { return MonthDays[month]; } else { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) //閏年 { return 29; } return 28; } } Date operator+(int day); //過載+ Date& operator+=(int day);//過載++ Date& operator++(); //前置++ Date operator++(int); //後置++ Date operator-(int day); //過載- Date& operator-=(int day);//過載-= Date& operator--(); //前置-- Date operator--(int); //後置-- int operator-(const Date& d); //兩日期類物件相減返回一個整數 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); bool operator!=(const Date& d); private: int _year; int _month; int _day; }; inline Date Date::operator+(int day) { Date tmp(*this); if (day < 0) { return *this - (-day); } tmp._day = _day + day; while (!tmp.IsInvalid()) { int MonthDay = GetMonthDays(tmp._year, tmp._month); tmp._day -= MonthDay; tmp._month++; if (tmp._month == 13) { tmp._year++; tmp._month = 1; } } return tmp; } inline Date& Date::operator+=(int day) { *this = *this + day; //對加法過載的程式碼複用 return *this; } inline Date& Date::operator++() //前置++ { return *this+=1; } inline Date Date::operator++(int) //後置++ { Date tmp(*this); *this += 1; return tmp; } inline Date Date::operator-(int day) { Date tmp(*this); if (day < 0) { return *this + (-day); } tmp._day = _day - day; while (!tmp.IsInvalid()) { if (tmp._month != 1) { tmp._month--; } else //month==1的情況 { tmp._year--; tmp._month = 12; } int MonthDay = GetMonthDays(tmp._year, tmp._month); tmp._day += MonthDay; } return tmp; } inline Date& Date::operator-=(int day) { *this = *this - day; return *this; } inline Date& Date::operator--() //前置-- { return *this -= 1; } inline Date Date::operator--(int) //後置-- { Date tmp(*this); *this -= 1; return tmp; } inline bool Date::operator>(const Date& d) { if (_year > d._year || (_year == d._year && (_month > d._month || (_month == d._month && _day > d._day)))) { return true; } return false; } inline bool Date::operator<(const Date& d) { return !(*this > d || *this == d); } inline bool Date::operator>=(const Date& d) { return (*this > d || *this == d); } inline bool Date::operator<=(const Date& d) { return (*this < d || *this == d); } inline bool Date::operator==(const Date& d) { if (_year == d._year && _month == d._month && _day == d._day) { return true; } return false; } inline bool Date::operator!=(const Date& d) { return !(*this == d); } inline int Date::operator-(const Date& d) //兩日期類物件相減返回一個整數 { int count = 0; Date tmp(*this); Date tmp2(d); if (tmp < tmp2) //第一個日期比第二個小交換順序 { Date x; x = tmp; tmp = tmp2; tmp2 = x; } //處理_day使兩個日期類的_day相同 count = GetMonthDays(tmp2._year, tmp2._month) - tmp2._day + tmp._day; tmp2._day = tmp._day; if (tmp._day >= tmp2._day) //count多加了一個月 { tmp2._month++; if (tmp2._month == 13) { tmp2._year++; tmp2._month = 1; } } //處理_month使兩日期類_month相同 while (tmp._month != tmp2._month) { count = count + GetMonthDays(tmp2._year, tmp2._month); tmp2._month++; if (tmp2._month == 13) { tmp2._year++; tmp2._month = 1; } } //處理兩日期類的_year while (tmp._year > tmp2._year) { //判斷是否為閏年,並且在二月份一下的才能算366天 if (((tmp2._year % 4 == 0 && tmp2._year % 100 != 0) || (tmp2._year % 400 == 0)) && (tmp2._month <= 2)) { count += 366; } else { count += 365; } tmp2._year++; } return count; }