C++实现日期类(Date类)的方法

 更新时间:2017年01月11日 08:48:56   投稿:jingxian  
下面小编就为大家带来一篇C++实现日期类(Date类)的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

如下所示:

#include<iostream>
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 (*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++实现日期类(Date类)的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • C语言文件读写操作介绍与简单示例

    C语言文件读写操作介绍与简单示例

    这篇文章主要给大家介绍了关于C语言文件读写操作的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • C语言实现消消乐游戏的代码分享

    C语言实现消消乐游戏的代码分享

    本章我们将编写十字消除游戏,用户点击空白方块,沿其上下左右方向寻找第一个彩色方块,如果有两个或两个以上颜色一致,就将其消除,感兴趣的可以了解一下
    2023-02-02
  • C语言深入讲解函数的使用

    C语言深入讲解函数的使用

    各位小伙伴们,今天YU同学给大家带来的是与函数相关的知识,本篇将会带着大家初步认识和调用函数来解决一些简单的问题
    2022-04-04
  • C语言异常处理机制案例讲解

    C语言异常处理机制案例讲解

    这篇文章主要介绍了C语言异常处理机制案例讲解,本文讲解了异常处理机制所用的函数和具体的代码实现等,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • 关于win32 gettimeofday替代方案

    关于win32 gettimeofday替代方案

    下面小编就为大家带来一篇关于win32 gettimeofday替代方案。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • C语言指针超详细讲解下篇

    C语言指针超详细讲解下篇

    指针提供了对地址操作的一种方法,因此,使用指针可使得 C 语言能够更高效地实现对计算机底层硬件的操作。另外,通过指针可以更便捷地操作数组。在一定意义上可以说,指针是 C 语言的精髓
    2022-04-04
  • c与c++之间的相互调用及函数区别示例详解

    c与c++之间的相互调用及函数区别示例详解

    这篇文章主要为大家介绍了c与c++相互调用的使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • C语言动态顺序表实例代码

    C语言动态顺序表实例代码

    大家好,本篇文章主要讲的是C语言动态顺序表实例代码,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • opencv求解区域的内接矩形

    opencv求解区域的内接矩形

    这篇文章主要为大家详细介绍了opencv求解区域的内接矩形,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • C语言实现绘制可爱的橘子钟表

    C语言实现绘制可爱的橘子钟表

    这篇文章主要为大家详细介绍了如何利用C语言实现绘制可爱的橘子钟表,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的可以了解一下
    2022-12-12

最新评论