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

 更新时间:2020年4月25日 17:32  点击:1355

如下所示:

#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类)的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • C++ STL标准库std::vector的使用详解

    vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
  • C++中取余运算的实现

    这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ string常用截取字符串方法

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • C++中四种加密算法之AES源代码

    本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
  • C++ 整数拆分方法详解

    整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ bitset用法

    这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 浅谈C++中的string 类型占几个字节

    本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
  • C++ Eigen库计算矩阵特征值及特征向量

    这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • C++ pair的用法实例详解

    这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
  • VSCode C++多文件编译的简单使用方法

    这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
  • C++中的循环引用

    虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
  • C++随机点名生成器实例代码(老师们的福音!)

    这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++如何删除map容器中指定值的元素详解

    map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
  • C++ 约瑟夫环问题案例详解

    这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15
  • C++中cin的用法详细

    这篇文章主要介绍了C++中cin的用法详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 基于C++中常见编译错误的总结详解

    本篇文章是对C++中的常见编译错误进行了详细的分析介绍,需要的朋友参考下...2020-04-25
  • C++实现递归函数的方法

    在本篇内容里小编给大家分享了关于C++实现递归函数的教学步骤,需要的朋友跟着参考下。...2020-04-25