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++ 浅析构造函数和析构函数的相关资料,帮助大家入门c++ 编程,感兴趣的朋友可以了解下
    2020-08-08
  • C语言 详细讲解接续符和转义符的使用

    C语言 详细讲解接续符和转义符的使用

    接续符是用来告诉编译器行为的符号,那编译器遇到接续符是什么行为呢,就是去掉接续符,然后把下一行连接到现在这行上面,转义符是主要用于表示无回显字符,也用于表示常规字符,转义符必须放在单引号或者双引号里面
    2022-04-04
  • C++实现迷宫游戏

    C++实现迷宫游戏

    这篇文章主要为大家详细介绍了C++实现迷宫游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • VS Code远程连接Linux服务器调试C程序的操作方法

    VS Code远程连接Linux服务器调试C程序的操作方法

    这篇文章主要介绍了VS Code远程连接Linux服务器调试C程序的操作方法,打开远程 Linux 服务器上的文件夹本文以 /root/ 为例,给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2023-12-12
  • OpenMP task construct 实现原理及源码示例解析

    OpenMP task construct 实现原理及源码示例解析

    这篇文章主要为大家介绍了OpenMP task construct 实现原理及源码示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • C语言中位运算符"|"的5种高级用法总结

    C语言中位运算符"|"的5种高级用法总结

    这篇文章主要为大家详细介绍了C语言中位运算符"|"的5种高级用法,文中的示例代码讲解详细,具有一定的参考价值,需要的可以参考一下
    2023-04-04
  • c语言基于stdarg.h的可变参数函数的用法

    c语言基于stdarg.h的可变参数函数的用法

    本篇文章主要介绍了c语言基于stdarg.h的可变参数函数的用法,详细的介绍了可变参数函数的详细用法和源码实例,有兴趣的可以了解一下
    2017-07-07
  • C/C++实现日期计算器的示例代码

    C/C++实现日期计算器的示例代码

    本篇文章主要介绍了C/C++实现日期计算器的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • C++可执行文件绝对路径值与VS安全检查详解

    C++可执行文件绝对路径值与VS安全检查详解

    这篇文章主要给大家介绍了关于C++可执行文件绝对路径值与VS安全检查的相关资料,文中通过图文以及实例代码介绍的非常详细,对大家学习或者使用C++具有一定的参考学习价值,需要的朋友可以参考下
    2023-01-01
  • C++中template方法undefined reference to的问题解决

    C++中template方法undefined reference to的问题解决

    Undefined reference to 错误:这类错误是在连接过程中出现的,本文就来介绍一下C++中template方法undefined reference to的问题解决,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03

最新评论