国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++實現日期類(Date)

2020-01-26 13:38:54
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C++實現日期類的具體代碼,供大家參考,具體內容如下

#include<iostream>#include<stdlib.h>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(const Date& d) : _year(d._year) , _month(d._month) , _day(d._day) {}  //析構函數 ~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; } //判斷當前月份多少天 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; } // 當前日期days天后是什么日期?  Date operator+(int days) {  Date tmp(*this);  tmp._day += days;  ToCorrect(tmp);  return tmp; }  // 當前日期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)//通過返回值和傳參區別前置和后置++ { 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;}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 承德市| 汉阴县| 彝良县| 达州市| 拉孜县| 临潭县| 安宁市| 山东| 信宜市| 富源县| 平顺县| 灌南县| 萨迦县| 荣昌县| 旬阳县| 石泉县| 沁阳市| 舒城县| 乌兰浩特市| 阜南县| 新巴尔虎右旗| 井冈山市| 喀什市| 邵阳市| 龙井市| 嘉义市| 湖南省| 临西县| 霍山县| 望城县| 莱州市| 扎鲁特旗| 呼玛县| 兰坪| 金沙县| 陆良县| 共和县| 凌源市| 焦作市| 金华市| 射阳县|