您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關C++怎么實現日期類的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
如下所示:
#include<iostream> using namespace std; class Date { public: Date(int year = 1900, int month = 1, int day = 1) //構造 :_year(year) , _month(month) , _day(day) { if (!isInvalidDate(_year, _month, _day)) { _year = 1900; _month = 1; _day = 1; } } Date operator+(int count) { Date tmp(*this); tmp._day += count; ToCorrect(tmp); return tmp; } Date operator-(int count) { Date tmp(*this); tmp._day -= count; ToCorrect(tmp); return tmp; } Date& operator++() //前置++ { (*this)++; return *this; } Date operator++(int) //后置++ { Date tmp(*this); (*this)+=1; return tmp; } Date& operator--() { (*this)--; return *this; } Date operator--(int) { Date tmp(*this); (*this)--; return tmp; } int operator-(const Date &d) { int flag = 1; Date max = *this; Date min = d; if (*this<d) { max = d; min = *this; flag = -1; } int count=0; while (min != max) { ++min; count++; } return count*flag; } Date& operator+=(int day) { *this = *this + day; return *this; } bool operator!=(const Date &d) { return !(*this == d); } bool operator<(const Date &d) { return !((*this>d)||(*this==d)); } bool operator>=(const Date &d) { return !(*this<d); } bool operator>(const Date &d) { return (_year > d._year || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day)); } bool operator==(const Date &d) { return ((_year == d._year) && (_month == d._month) && (_day == d._day)); } public: bool IsLeapYear(int year) { if(year % 400 || (year % 4 && year % 100)) return true; return false; } int YearsOfMonth(int year, int month) { int day; int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; day = days[month]; if (month == 2) day += IsLeapYear(year); return day; } Date ToCorrect(Date &d) { if (d._day>YearsOfMonth(d._year, d._month)) { while (d._day > YearsOfMonth(d._year, d._month)) { d._day -= YearsOfMonth(d._year,d._month); if (d._month == 12) { d._year++; d._month = 1; } else { ++d._month; } } } else { while (d._day <= 0) { if (d._month == 1) { d._year--; d._month = 12; } else { --d._month; } d._day += YearsOfMonth(d._year, d._month); } } return d; } bool isInvalidDate(int year, int month, int day) { if ((year < 1) || (month<0 || month>12) || (day<0 || day>YearsOfMonth(year,month))) return false; return true; } void Display() { cout << _year << "-" << _month << "-" << _day << endl; } friend istream& operator>>(istream& is, Date &d); friend ostream& operator<<(ostream& os, const Date &d); private: int _year; int _month; int _day; }; istream& operator>>(istream& is, Date& d) { cout << "請輸入一個日期" << endl; is >> d._year >> d._month >> d._day; return is; } ostream& operator<<(ostream& os, const Date &d) { cout << d._year << "-" <<d. _month << "-" << d._day << endl; return os; } int main() { /*Date d1(2016,8,18); //d1.Display(); //d1 = d1++; cout << d1 << endl;*/ //Date d1(2015, 12, 3); //(d1++).Display(); //d1.operator++(&d1, 0); //(++d1).Display(); //d1.operator++(&d1); Date d1(2015, 12, 3); Date d2(2015, 11, 1); cout << (d1 - d2) << endl; //Date d1(2015, 12, 3); //Date ret = d1 + 40; //operator+ //ret.Display(); /*Date d1(2015, 12, 3); Date ret = d1 + 40; d1 = ret; ret = d1 - 40; ret.Display();*/ /*Date ret; Date d2(2015, 1, 1); ret = d2 - 1; ret.Display();*/ return 0; }
關于“C++怎么實現日期類的方法”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。