189 8069 5689

C++怎么实现日期类的方法

这篇文章将为大家详细讲解有关C++怎么实现日期类的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联-专业网站定制、快速模板网站建设、高性价比错那网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式错那网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖错那地区。费用合理售后完善,十载实体公司更值得信赖。

如下所示:

#include
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 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 (*thisd)||(*this==d));
  }
  bool operator>=(const Date &d)
  {
    return !(*this(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 << "-" <

关于“C++怎么实现日期类的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


网页标题:C++怎么实现日期类的方法
标题URL:http://cdxtjz.cn/article/gdseod.html

其他资讯