C++實(shí)現(xiàn)日期類(Date類)的方法
如下所示:
#include<iostream> using namespace std; class Date { public: Date(int year = 1900, int month = 1, int day = 1) //構(gòu)造 :_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 << "請(qǐng)輸入一個(gè)日期" << 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++實(shí)現(xiàn)日期類(Date類)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持我們。
上一篇:總結(jié)C/C++面試中可能會(huì)碰到的字符串指針題
欄 目:C語言
下一篇:C++如何實(shí)現(xiàn)簡單的計(jì)時(shí)器詳解
本文標(biāo)題:C++實(shí)現(xiàn)日期類(Date類)的方法
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1807.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)