C++实现日期类(Date)

 更新时间:2018年09月10日 08:35:16   作者:WangJ_F_  
这篇文章主要为大家详细介绍了C++实现日期类的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现日期类的具体代码,供大家参考,具体内容如下

#include<iostream>
#include<stdlib.h>
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(const Date& d)
 : _year(d._year)
 , _month(d._month)
 , _day(d._day)
 {}
 
 //析构函数
 ~Date()
 {}
 
 //判断是不是闰年
 bool IsLeapYear(int year)
 {
  if ((year % 400 == 0) ||
  ((year % 4 == 0) && (year % 100 != 0)) )
  {
  return true;
  }
  return false;
 }
 //判断是不是合法日期
 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;
 }
 //判断当前月份多少天
 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 && IsLeapYear(year))
  {
  day += 1;
  }
  return day;
 }
 //修正日期
 Date ToCorrect(Date &d)
 {
  while (d._day > YearsOfMonth(d._year, d._month) || d._day <= 0)
  {
  if(d._day <= 0)
  {
   d._day += YearsOfMonth(d._year,( d._month - 1));
   if (d._month == 1)
   {
   d._month = 12;
   d._year--;
   }
   else
   {
   d._month--;
   }
  }
  else
  {
   d._day -= YearsOfMonth(d._year, d._month);
   if (d._month == 12)
   {
   d._year++;
   d._month = 1;
   }
   else
   {
   d._month++;
   }
  }
  }
  return d;
 }
 // 当前日期days天后是什么日期? 
 Date operator+(int days)
 {
  Date tmp(*this);
  tmp._day += days;
  ToCorrect(tmp);
  return tmp;
 }
 
 // 当前日期days天前是什么日期? 
 Date operator-(int days)
 {
  Date tmp(*this);
  tmp._day -= days;
  ToCorrect(tmp);
  return tmp;
 }
 
 // 日期比大小 
 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 ||
  (_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));
 }
 bool operator!=(const Date& d)
 {
 return !(*this == d);
 }
 bool operator>=(const Date &d)
 {
 return !(*this<d);
 }
 bool operator<=(const Date &d)
 {
 return !(*this>d);
 }
 
 // 重载取地址符号 
 Date* operator&()
 {
 
 }
 
 // 前置++ 
 Date& operator++()
 {
 (*this)++;
 return *this;
 }
 
 // 后置++ 
 Date operator++(int)//通过返回值和传参区别前置和后置++
 {
 Date tmp(*this);
 (*this) = (*this) + 1;
 return tmp;
 }
 
 // 前置-- 
 Date& operator--()
 {
 (*this)--;
 return *this;
 }
 
 // 后置-- 
 Date operator--(int)
 {
 Date tmp(*this);
 (*this)--;
 return tmp;
 }
 void Display()
 {
 cout << _year << "-" << _month << "-" << _day << endl;
 }
private:
 int _year;
 int _month;
 int _day;
};
 
int main()
{
 Date d(2018, 9, 9);
 d.Display();
 Date d1 = d + 50;
 d1.Display();
 d1 =d1 - 50;
 d1.Display();
 
 cout << "------"<<endl;
 cout << "前置++" << endl;
 d1.Display();
 (++d1).Display();
 d1.Display();
 cout << "后置++" << endl;
 d1.Display();
 (d1++).Display();
 d1.Display();
 cout << "------" << endl;
 
 
 system("pause");
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C/C++编程语言中的指针(pointer)你了解吗

    C/C++编程语言中的指针(pointer)你了解吗

    这篇文章主要为大家详细介绍了C/C++编程语言中的指针,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • C语言共用体union作用使用示例教程

    C语言共用体union作用使用示例教程

    这篇文章主要为大家介绍了C语言共用体union作用的使用示例教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-02-02
  • Qt 10进制和16进制转换的使用示例

    Qt 10进制和16进制转换的使用示例

    在编程过程中,处理16进制字符串与10进制数字之间的转换是很常见的需求,本文主要介绍了Qt 10进制和16进制转换的使用示例,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • C++多线程编程详解

    C++多线程编程详解

    这篇文章主要介绍了c语言多线程编程使用示例,小编觉得这篇文章写的还不错,需要的朋友可以参考下,希望能够给你带来帮助
    2021-09-09
  • C语言的动态内存管理的深入了解

    C语言的动态内存管理的深入了解

    这篇文章主要为大家详细介绍了语言C的动态内存管理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • C++实现四叉树效果(附源码下载)

    C++实现四叉树效果(附源码下载)

    这篇文章主要介绍了C++实现四叉树效果(附源码下载),非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-03-03
  • c语言中单引号和双引号的区别(顺利解决从字符串中提取IP地址的困惑)

    c语言中单引号和双引号的区别(顺利解决从字符串中提取IP地址的困惑)

    c语言中的单引号和双引号可是有很大区别的,使用之前一定要了解他们之间到底有什么不同,下面小编就给大家详细的介绍一下吧,对此还不是很了解的朋友可以过来参考下
    2013-07-07
  • C++读写.mat文件的方法

    C++读写.mat文件的方法

    本文介绍了“C++读写.mat文件的方法”,需要的朋友可以参考一下
    2013-03-03
  • C++实现LeetCode(74.搜索一个二维矩阵)

    C++实现LeetCode(74.搜索一个二维矩阵)

    这篇文章主要介绍了C++实现LeetCode(74.搜索一个二维矩阵),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • strcat函数实现简单示例

    strcat函数实现简单示例

    这篇文章主要介绍了strcat函数实现简单示例,需要的朋友可以参考下
    2014-03-03

最新评论