實現(xiàn)一個CDate類
目前累計服務客戶數(shù)千家,積累了豐富的產(chǎn)品開發(fā)及服務經(jīng)驗。以網(wǎng)站設計水平和技術實力,樹立企業(yè)形象,為客戶提供網(wǎng)站制作、網(wǎng)站建設、網(wǎng)站策劃、網(wǎng)頁設計、網(wǎng)絡營銷、VI設計、網(wǎng)站改版、漏洞修補等服務。創(chuàng)新互聯(lián)建站始終以務實、誠信為根本,不斷創(chuàng)新和提高建站品質,通過對領先技術的掌握、對創(chuàng)意設計的研究、對客戶形象的視覺傳遞、對應用系統(tǒng)的結合,為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進步。class CDate { public: CDate(int year = 1900, int month = 1, int day = 1) : _year(year) , _month(month) , _day(day) { if (!(_year >= 1900 && (_month > 0 && _month < 13) && (_day > 0 && _day <= _GetMonthDay(_year, _month)))) { _year = 1900; _month = 1; _day = 1; } } bool IsLeap(int year)//判斷閏年 { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { return true; } return false; } private: int _GetMonthDay(int year, int month) { int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (IsLeap(year) && month == 2)//閏年并且月份為2 { days[month] += 1; } return days[month]; } private: int _year; int _month; int _day; };
完成CDate類的幾個函數(shù)
#includeusing namespace std; class CDate { public: CDate(int year = 1900, int month = 1, int day = 1) : _year(year) , _month(month) , _day(day) { if (!(_year >= 1900 && (_month > 0 && _month < 13) && (_day > 0 && _day <= _GetMonthDay(_year, _month)))) { _year = 1900; _month = 1; _day = 1; } } //比較日期 bool operator<(const CDate& d) { if (_year < d._year) { return true; } else if (_year == d._year && _month < d._month) { return true; } else if (_year == d._year && _month == d._month && _day (const CDate& d) { return (!(*this < d || *this == d)); } //日期加法 CDate operator+(int day) { if (day < 0) { return *this - (0 - day); } CDate temp(*this); temp._day += day; while (temp._day > _GetMonthDay(temp._year, temp._month)) { temp._day -= _GetMonthDay(temp._year, temp._month); if (temp._month == 12) { temp._year++; temp._month = 1; } else { temp._month++; } } return temp; } //日期減法 CDate operator-(int day) { if (day < 0) { return *this + (0 - day); } CDate temp(*this); temp._day -= day; while (temp._day <= 0) { if (temp._month == 1) { temp._year--; temp._month = 12; } else { temp._month--; } temp._day += _GetMonthDay(temp._year, temp._month); } return temp; } //前置加加 CDate& operator++() { *this = *this + 1; return *this; } //后置加加 CDate operator++(int) { CDate temp(*this); *this = *this + 1; return temp; } //前置減減 CDate& operator--() { *this = *this - 1; return *this; } //后置減減 CDate operator--(int) { CDate temp(*this); *this = *this - 1; return temp; } //兩者作差 int operator-(const CDate& d) { CDate min(*this); CDate max(d); if (min > max) { min = d; max = *this; } int count = 0; while (min < max) { min = min + 1; count++; } return count; } bool IsLeap(int year) { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { return true; } return false; } private: int _GetMonthDay(int year, int month) { int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (IsLeap(year) && month == 2)//閏年并且月份為2 { days[month] += 1; } return days[month]; } friend ostream& operator<<(ostream& _cout, const CDate& d) { _cout << d._year << "-" << d._month << "-" << d._day; return _cout; } private: int _year; int _month; int _day; }; int main() { CDate d(1992, 1, 26); cout << d + 4 << endl; cout << d + 34 << endl; cout << d + 10000 << endl; cout << d - 26 << endl; CDate d1(1992, 1, 1); cout << d - d1 << endl; cout << d1 - d << endl; cout << d1++ << endl; cout << ++d1 << endl; system("pause"); return 0; }
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡助力業(yè)務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調度,確保服務器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務器買多久送多久。