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

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

C++實現(xiàn)日期類(Date類)的方法

2020-05-23 13:55:54
字體:
供稿:網(wǎng)友

如下所示:

#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 << "請輸入一個日期" << 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++實現(xiàn)日期類(Date類)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網(wǎng)。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 聂荣县| 永年县| 武陟县| 嘉定区| 万安县| 海兴县| 科技| 株洲市| 白沙| 宁蒗| 阿克| 蕉岭县| 扎囊县| 马关县| 江安县| 宁南县| 资阳市| 新宾| 苏尼特左旗| 神农架林区| 上林县| 唐河县| 临夏县| 如东县| 封开县| 延长县| 邵阳县| 兴文县| 栾川县| 九龙县| 新密市| 芜湖市| 安阳市| 阜康市| 桦南县| 石嘴山市| 资溪县| 闵行区| 蓝田县| 启东市| 定安县|