這篇文章主要介紹了C++如何實(shí)現(xiàn)日期類,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計(jì)、成都網(wǎng)站建設(shè)、微信開發(fā)、微信小程序、集團(tuán)成都定制網(wǎng)頁設(shè)計(jì)等服務(wù)項(xiàng)目。核心團(tuán)隊(duì)均擁有互聯(lián)網(wǎng)行業(yè)多年經(jīng)驗(yàn),服務(wù)眾多知名企業(yè)客戶;涵蓋的客戶類型包括:成都集裝箱等眾多領(lǐng)域,積累了大量豐富的經(jīng)驗(yàn),同時(shí)也獲得了客戶的一致贊許!
具體內(nèi)容如下
#include<iostream> #include<stdlib.h> using namespace std; class Date { public: //構(gòu)造函數(shù) 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; } } //拷貝函數(shù) Date(const Date& d) : _year(d._year) , _month(d._month) , _day(d._day) {} //析構(gòu)函數(shù) ~Date() {} //判斷是不是閏年 bool IsLeapYear(int year) { if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ) { return true; } return false; } //判斷是不是合法日期 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; } //判斷當(dāng)前月份多少天 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 && IsLeapYear(year)) { day += 1; } return day; } //修正日期 Date ToCorrect(Date &d) { while (d._day > YearsOfMonth(d._year, d._month) || d._day <= 0) { if(d._day <= 0) { d._day += YearsOfMonth(d._year,( d._month - 1)); if (d._month == 1) { d._month = 12; d._year--; } else { d._month--; } } else { d._day -= YearsOfMonth(d._year, d._month); if (d._month == 12) { d._year++; d._month = 1; } else { d._month++; } } } return d; } // 當(dāng)前日期days天后是什么日期? Date operator+(int days) { Date tmp(*this); tmp._day += days; ToCorrect(tmp); return tmp; } // 當(dāng)前日期days天前是什么日期? Date operator-(int days) { Date tmp(*this); tmp._day -= days; ToCorrect(tmp); return tmp; } // 日期比大小 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 || (_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)); } bool operator!=(const Date& d) { return !(*this == d); } bool operator>=(const Date &d) { return !(*this<d); } bool operator<=(const Date &d) { return !(*this>d); } // 重載取地址符號 Date* operator&() { } // 前置++ Date& operator++() { (*this)++; return *this; } // 后置++ Date operator++(int)//通過返回值和傳參區(qū)別前置和后置++ { Date tmp(*this); (*this) = (*this) + 1; return tmp; } // 前置-- Date& operator--() { (*this)--; return *this; } // 后置-- Date operator--(int) { Date tmp(*this); (*this)--; return tmp; } void Display() { cout << _year << "-" << _month << "-" << _day << endl; } private: int _year; int _month; int _day; }; int main() { Date d(2018, 9, 9); d.Display(); Date d1 = d + 50; d1.Display(); d1 =d1 - 50; d1.Display(); cout << "------"<<endl; cout << "前置++" << endl; d1.Display(); (++d1).Display(); d1.Display(); cout << "后置++" << endl; d1.Display(); (d1++).Display(); d1.Display(); cout << "------" << endl; system("pause"); return 0; }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C++如何實(shí)現(xiàn)日期類”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
網(wǎng)頁標(biāo)題:C++如何實(shí)現(xiàn)日期類
文章地址:http://chinadenli.net/article8/ppgjip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、標(biāo)簽優(yōu)化、網(wǎng)站制作、做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)