//--------------------------------------------------------------------------/***名称:日期的简单操作******类函数:构造函数,拷贝构造函数,析构函数,操作符重载函数****日期类操作函数: 1:计算一个日期加上多少天数后的日期       **                2:把该日期改为加上指定数目的天数后的日期**                3:一个日期减上多少天数后的日期 **                4:该日期加1(前置++)(后置++)**                5:该日期减1(前置--)(后置--)**                6:计算某日期到未来某日期间隔的天数******日期:2016/1/13*/
//---------------------------------------------------------------------------
/*----------------------------------------头文件--------------------------------------*/# ifndef __DATA_H__
# define __DATA_H__# define _CRT_SECURE_NO_WARNINGS 1# include <iostream>
# include <stdlib.h>
# include <assert.h>using namespace std;class Date
{
public://打印函数void Display();
public://构造函数Date(size_t year, size_t month, size_t day);//拷贝构造函数Date(const Date & d);//析构函数~Date();//操作符重载bool operator==(const Date & d);bool operator>(const Date & d);bool operator<(const Date & d);bool operator>=(const Date & d);bool operator<=(const Date & d);Date operator=(const Date & d);//日期类操作Date operator+ (size_t day);Date& operator+= (size_t day);Date operator- (size_t day);Date& operator-= (size_t day);Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);int Date::operator-(const Date & d);private:bool _IsLeapYear(size_t year);size_t _GetMonthDay(size_t year, size_t month);private:size_t _year;size_t _month;size_t _day;
};# endif   //__DATA_H__
/*------------------------------------功能函数-----------------------------------------*/# include "Date.h"//打印函数
void Date::Display()
{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}//构造函数
Date::Date(size_t year = 1900, size_t month = 1, size_t day = 1)
{if (year > 0 && month > 0 && month<13 && day>0 && day <= _GetMonthDay(year, month)){_year = year;_month = month;_day = day;}else{cout << "初始化数据非法" << endl;_year = 1900;_month = 1;_day = 1;}
}//拷贝和构造函数
Date::Date(const Date & d)
{_year = d._year;_month = d._month;_day = d._day;
}//析构函数
Date::~Date()
{//do_nothing
}/*操作符重载*/
bool Date::operator==(const Date & d)
{return (_year == d._year)&& (_month == d._month)&& (_day == d._day);
}bool Date::operator>(const Date & d)
{if (_year > d._year){return true;}else if (_year == d._year && _month > d._month){return true;}else if (_year == d._year && _month == d._month && _day > d._day){return true;}return false;
}bool Date::operator<(const Date & d)
{return !(*this == d) && !(*this > d);
}bool Date::operator>=(const Date & d)
{return (*this == d) || (*this > d);
}bool Date::operator<=(const Date & d)
{return !(*this > d);
}Date Date::operator=(const Date & d)
{_year = d._year;_month = d._month;_day = d._day;return *this;
}/*日期类操作*/
//判断是否为闰年
bool Date::_IsLeapYear(size_t year)
{return ((0 == year % 4 && 0 != year % 100) || (0 == year % 400));
}//返回指定月的天数
size_t Date::_GetMonthDay(size_t year, size_t month)
{size_t MonthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };size_t MonthDays = MonthArray[month];bool ret;if ((2 == month) && (ret = _IsLeapYear(year))){MonthDays = 29;}return MonthDays;
}//一个日期加上多少天数后的日期
Date Date::operator+ (size_t day)
{size_t MonthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };Date tmp(*this);size_t SumDays = tmp._day + day;while (true){if (SumDays <= _GetMonthDay(tmp._year, tmp._month)){tmp._day = SumDays;return tmp;}else{SumDays -= _GetMonthDay(tmp._year, tmp._month);tmp._month++;if (tmp._month > 12){tmp._year++;tmp._month %= 12;}}}
}//把该日期改为加上指定数目的天数后的日期
Date& Date::operator+= (size_t day)
{*this = operator+(day);return *this;
}//一个日期减上多少天数后的日期
Date Date::operator- (size_t day)
{size_t MonthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };Date tmp(*this);size_t SumDays = tmp._day + day;while (true){if (day < tmp._day){tmp._day -= day;return tmp;}else{day -= tmp._day;if (1 == tmp._month){tmp._year--;tmp._month = 12;}else{tmp._month--;}tmp._day = _GetMonthDay(tmp._year, tmp._month);}}
}//把该日期改为减上指定数目的天数后的日期
Date& Date::operator-= (size_t day)
{*this = operator-(day);return *this;
}//该日期加1(前置++)
Date& Date::operator++()
{++_day;if (_day > _GetMonthDay(_year, _month)){_day %= _GetMonthDay(_year, _month);++_month;if (_month > 12){++_year;_month %= 12;}}return *this;
}//日期加1(后置++)
Date Date::operator++(int)
{Date tmp(*this);*this = operator++();return tmp;
}//该日期减1(前置--)
Date& Date::operator--()
{if (_day > 1){--_day;}else{if (1 == _month){--_year;_month = 12;_day = _GetMonthDay(_year,_month);}else{--_month;_day = _GetMonthDay(_year, _month);}}return *this;
}//该日期减1(后置--)
Date Date::operator--(int)
{Date tmp(*this);*this = operator--();return tmp;
}//返回指定日期减去该日期后的天数
int Date::operator-(const Date & d)
{//异常:该日期比被减日期小if (d._year < _year){return -1;}else if (d._year == _year && d._month < _month){return -1;}else if (d._year == _year && d._month == _month && d._day < _day){return -1;}//正常情况Date tmp_d(d);Date tmp_d_day(*this);int ret = 0;while (!(tmp_d == tmp_d_day)){tmp_d_day.operator++();ret++;}return ret;
}   
/*------------------------------------部分测试用例-------------------------------------*/# include "Date.h"//测试operator<=()
void Test7()
{//falseDate d1(2016, 1, 12);Date d2(2016, 1, 11);bool ret = d1 <= d2;d1.Display();d2.Display();cout << ret << endl;//falseDate d3(2016, 1, 12);Date d4(2015, 1, 12);ret = d3 <= d4;d3.Display();d4.Display();cout << ret << endl;//trueDate d5(2016, 1, 12);Date d6(2016, 1, 13);ret = d5 <= d6;d5.Display();d6.Display();cout << ret << endl;//falseDate d7(2016, 1, 12);Date d8(2016, 1, 12);ret = d7 <= d8;d7.Display();d8.Display();cout << ret << endl;}
//测试operator>=()
void Test6()
{//trueDate d1(2016, 1, 12);Date d2(2016, 1, 11);bool ret = d1 >= d2;d1.Display();d2.Display();cout << ret << endl;//trueDate d3(2016, 1, 12);Date d4(2015, 1, 12);ret = d3 >= d4;d3.Display();d4.Display();cout << ret << endl;//falseDate d5(2016, 1, 12);Date d6(2016, 1, 13);ret = d5 >= d6;d5.Display();d6.Display();cout << ret << endl;//trueDate d7(2016, 1, 12);Date d8(2016, 1, 12);ret = d7 >= d8;d7.Display();d8.Display();cout << ret << endl;
}
//测试operator<()
void Test5()
{//falseDate d1(2016, 1, 12);Date d2(2016, 1, 11);bool ret = d1 < d2;d1.Display();d2.Display();cout << ret << endl;//falseDate d3(2016, 1, 12);Date d4(2015, 1, 12);ret = d3 < d4;d3.Display();d4.Display();cout << ret << endl;//trueDate d5(2016, 1, 12);Date d6(2016, 1, 13);ret = d5 < d6;d5.Display();d6.Display();cout << ret << endl;
}//测试operator>()
void Test4()
{//trueDate d1(2016, 1, 12);Date d2(2016, 1, 11);bool ret = d1 > d2;d1.Display();d2.Display();cout << ret << endl;//falseDate d3(2016, 1, 12);Date d4(2016, 1, 12);ret = d3 > d4;d3.Display();d4.Display();cout << ret << endl;//falseDate d5(2016, 1, 12);Date d6(2016, 1, 13);ret = d5 > d6;d5.Display();d6.Display();cout << ret << endl;}//测试operator==
void Test3()
{//falseDate d1(2016, 1, 12);Date d2(2016, 1, 11);bool ret = d1 == d2;d1.Display();d2.Display();cout << ret << endl;//trueDate d3(2016, 2, 12);Date d4(2016, 1, 12);ret = d3 == d4;d3.Display();d4.Display();cout << ret << endl;//falseDate d5(2016, 1, 12);Date d6(2016, 1, 13);ret = d5 == d6;d5.Display();d6.Display();cout << ret << endl;}//测试Date(const Date & d)
void Test2()
{Date d1(2016, 1, 12);d1.Display();Date d2(d1);d2.Display();Date d3 = d1;d3.Display();
}//测试Date(size_t year = 0, size_t month = 0, size_t day = 0)
void Test1()
{Date d(2016, 1, 12);d.Display();
}//测试operator+ (size_t day)
void Test()
{Date d1(2016, 1, 13);d1.Display();cout << "+366天" << endl;Date ret = d1.operator+ (366);ret.Display();
}//测试operator+= (size_t day)
void Test8()
{Date d1(2016, 1, 13);d1.Display();cout << "+10天" << endl;Date ret = d1.operator+= (18);ret.Display();
}//测试operator- (size_t day)
void Test9()
{Date d1(2016, 1, 10);d1.Display();cout << "+375天" << endl;Date ret = d1.operator- (375);ret.Display();
}//测试operator++()/operator++(int)
void Test10()
{Date d1(2016, 2, 29);d1.operator++();d1.Display();Date d2(2016, 3, 3);d2.operator--();d2.Display();
}//测试operator--(int)/operator--()
void Test11()
{Date d1(2016, 12, 31);Date ret = d1.operator++(int());ret.Display();Date d2(2016, 12, 31);ret = d2.operator--(int());ret.Display();
}//测试operator-(const Date & d)
void Test12()
{Date d1(2016, 1, 13);Date d2(1970, 1, 1);int ret = d2.operator-(d1);cout << "起始日期" << endl;d2.Display();cout << "截止日期" << endl;d1.Display();cout<<"间隔"<< ret << "天" << endl;
}int main()
{//Test();//Test1();//Test2();//Test3();//Test4();//Test5();//Test6();//Test7();//Test8();//Test9();//Test10();//Test11();Test12();system("pause");return 0;
}

转载于:https://blog.51cto.com/814193594/1734719

【C++入门】简单的日期类操作相关推荐

  1. java 8时间操作_Java8 时间日期类操作

    Java8 时间日期类操作 Java8的时间类有两个重要的特性 线程安全 不可变类,返回的都是新的对象 显然,该特性解决了原来java.util.Date类与SimpleDateFormat线程不安全 ...

  2. C++类与对象入门实践(日期类的实现)

    日期类 class Date { public://成员函数private:int _year;int _month;int _day; }; 日期类成员对象:  年.月.日 实现功能:  成员函数 ...

  3. java当中日期类的相关操作(学习笔记)

    一:引言 Calendar类是日历类,提供操作日历字段的方法,其中有常用操作 get 和 set 方法还有 add方法 详细用法请看码 二:上码 package cn.wyj.one;import j ...

  4. Python 基础 之 jupyter notebook 中机器学习的简单入门书写数字识别 demo 操作学习

    Python 基础 之 jupyter notebook 中机器学习的简单入门书写数字识别 demo 操作学习 目录 Python 基础 之 jupyter notebook 中机器学习的简单入门书写 ...

  5. 计算机简单的操作是什么知识,简单的电脑计算机操作使用入门知识!(实用快键键)...

    很多人觉得自己都是电脑高手了,只是很多最基础的知识我们都知道吗? 下面就来看看,这些很简单的计算机入门知识,或许很多操作你都不是很懂哦! 1.重命名文件或者文件夹的方法: 1)选中--右键--重命名 ...

  6. date日期相减 java_03时间日期类

    Java8 在 java.time 包中增加了时间日期相关的API,弥补了 Java8 以前对日期.时间处理的不足. 在介绍Java8新的时间日期API前,先看看 java8 以前我们操作日期时间常用 ...

  7. 蓝桥杯日期计算java_日期类的使用(java)-蓝桥杯

    蓝桥杯日期问题常考,java提供了日期类很方便: //日历类 Calendar c = Calendar.getInstance(); // 获取实例化对象 Date date =c.getTime( ...

  8. 国产化之路-统信UOS /Nginx /Asp.Net Core+ EF Core 3.1/达梦DM8实现简单增删改查操作

    引言 经过前期的准备工作,.net core 3.1的运行环境和WEB服务器已经搭建完毕,这里需要注意一下,达梦DM8数据库对于Entity Framework Core 3.1 的驱动在NuGet官 ...

  9. python自己做个定时器_技术图文:如何利用 Python 做一个简单的定时器类?

    原标题:技术图文:如何利用 Python 做一个简单的定时器类? 背景 今天在B站上看有关 Python 最火的一个教学视频 -- "零基础入门学习 Python",这也是我们 P ...

最新文章

  1. 磁盘加密软件TrueCrypt知识大全(三)之加密非系统分区/设备
  2. python生成器函数_【python】生成器和生成器函数
  3. switch里能不能用continue?
  4. Android项目笔记整理(1)
  5. Linux 本地密码正确无法登录,记一次无法正常本地登陆Linux服务器(确定密码正确)...
  6. Lowest Common Ancestor of a Binary Search Tree a Binary Tree
  7. 精雕道路怎么遍弧形_有网友私信我问郑州融信奥体世纪这个楼盘怎么样他今天来...
  8. 玩转 Java8 Stream,常用方法大合集
  9. WinServer2008R2搭建和授权DHCP服务器详解
  10. 模拟最大黑客组织 FIN6 的行为,MITRE 免费助力网络安全防御
  11. 二叉树的基本操作(C)
  12. 球球大作战代点链接源码c语言,最新球球大作战代点源码无加密开源-球球大作战一天刷100个棒棒糖和刷龙蛋200个源码下载免费版-西西软件下载...
  13. 如何使用vs进行代码比较
  14. 特洛伊木马 (计算机木马程序)
  15. 镁光闪存颗粒对照表_内存颗粒型号识别
  16. 使用CityBuilder搭建智慧城市3D可视化模型
  17. 七彩虹将星X15 AT评测
  18. 清北学堂2019.8.7
  19. pdf文档打不开是怎么回事?
  20. 什么是wildfly

热门文章

  1. linux 使用ssr客户端_【第一期】基于 @vue/cli3 与 koa 创建 ssr 工程
  2. java 保留字符串,如何在Java中保留字符串而不使用反转功能
  3. edges2shoes数据集下载_edges2cats
  4. Eclipse继承HttpServlet出现:HttpServlet cannot be resolved to a type的错误的解决方案
  5. EntityManager的使用
  6. RestTemplate发送请求并携带header信息
  7. Spring3 整合 Hibernate4实现数据库操作(1)
  8. php js 比较,PHP与JS的比较
  9. linux常用命令--开发调试篇
  10. java jive歌词_Java Jive_Manhattan Transfer with Phil Collins_高音质在线试听_Java Jive歌词|歌曲下载_酷狗音乐...