日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

日期类的实现

發布時間:2023/11/30 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 日期类的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class Date { public:// 1.無參的構造函數// Date()// {// }// 2.缺省構造函數Date(int year = 1900, int month = 1, int day = 1){if(year < 1900 || month < 0 || month > 12 || day < 0 || day > 31){assert(0);}_year = year;_month = month;_day = day;}//3. 帶參的構造函數// Date (int year, int month, int day)// {// _year = year;// _month = month;// _day = day;// }// 4. 拷貝構造函數,創建一個對象Date(Date& d){_year = d._year;_month = d._month;_day = d._day;}// 5. 析構函數~Date(){}// 6. 初始化列表 # if 0Date (int year = 1900, int month = 1, int day = 1):_year(year),_month(month),_day(day){cout << "初始化列表" << endl;} # endif// 7. 賦值運算符重載// d2 = d3;// d2.operator(*this, d3) ==> this 指向d2, d代表的是d3Date& operator = (const Date& d){this -> _year = d._year;this -> _month = d._month;this -> _day = d._day;return *this;}// d2 == d3// d2.operator(*this, d3)bool operator == (const Date& d) {if(this -> _year == d._year && this -> _month == d._month && this -> _day == _day){return true;}return false;}bool operator != (const Date d){if(!(*this == d)){return true;}return false;}//d1 > d2//d1.operator > (this, d2)bool operator > (const Date& d) {if(this -> _year > d._year){return true;}if(this -> _year == d._year){if(this -> _month > d._month){return true;}}if(this -> _month == d._month){if(this -> _day > d._day){return true;}}return false;}//d2 < d3//d2.operator(this, d3)bool operator < (const Date& d) {if(*this == d || *this > d){return false;}return true;}// d2 >= d3bool operator >= (const Date& d){if(*this > d || *this == d){return true;}return false;}//d2 <= d3bool operator <= (const Date& d){if(*this < d || *this == d){return true;}return false;}//d2++Date& operator ++ () // 前置 {++( this -> _day );if(this -> _day > GetMonthDay(this -> _year, this -> _month)){this -> _day = 1;++(this -> _month);if((this -> _month) > 12){++(this -> _year);this -> _month = 1;}}return *this;}//d2++Date operator ++ (int) // 后置 {Date ret = (*this);++( *this );return ret;}//2018-1-1Date& operator -- (){--(this -> _day);if(this -> _day == 0){(this -> _month)--;if(this -> _month == 0){this -> _year -= 1;this -> _month = 12;}(this -> _day) += GetMonthDay(this -> _year, this -> _month);}return *this;}Date operator -- (int)//后置{Date ret = *this;--(*this);return ret;}Date& operator += (int day){this -> _day = this -> _day + day;while(this -> _day > GetMonthDay(this -> _year, this -> _month)){this -> _day = this -> _day - GetMonthDay(this -> _year, this -> _month);this -> _month ++;if(this -> _month > 12){this -> _month = 1;this -> _year ++;}}return *this;}//d1 + 30//d1.operator(this, day)Date operator + (int day) {Date ret = *this;ret += day;return ret;}Date& operator -= (int day){this -> _day -= day;while(this -> _day <= 0){this -> _month --;if(this -> _month < 1){this -> _month = 12;this -> _year --;}this -> _day += GetMonthDay(this -> _year, this -> _month);}return *this;}Date operator - (int day){Date ret = *this;ret -= day;return ret;}// 2018-1-31 - 2018-1-1// d1.operator(this, d2)int operator - (Date& d){int count = 0;//記錄天數int flag = 1;if(*this < d ){swap(*this, d);flag = -1;}while(*this > d){--(*this);++count;}count =flag * count;if(flag == -1){swap(*this, d);}return count;}//取地址運算符重載const Date* operator & () const{return this;}Date* operator & (){return this;}friend ostream& operator << (ostream& out, const Date& d){out << "year:" <<d._year << endl;out << "month:" << d._month << endl;out << "day: " << d._day << endl;return out;}friend istream& operator >> (istream& in, Date& d){cout << "請分別輸入年月日" << endl;in >> d._year;in >> d._month;in >> d._day;return in;}void Display()//展示日期類{cout<<_year<<"-"<<_month<<"-"<<_day<<endl;}int GetMonthDay(int year, int month){int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if(( year %4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 )){day[month] = day[2] + 1;}return day[month];}private:int _year;int _month;int _day; };

總結

以上是生活随笔為你收集整理的日期类的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。