//--------------------------------------------------------------------------
/*
**功能:实现日期的简单操作
**
**
**基本的成员函数:
** 构造函数,拷贝构造函数,析构函数,赋值运算符重载,操作符重载(两个日期间比较大小)
**
**日期类功能函数:
** 1:计算一个日期加上多少天数后的日期
** 2:把该日期改为加上指定数目的天数后的日期
** 3:一个日期减上多少天数后的日期
** 4:把该日期改为减去指定数目的天数后的日期
** 5:该日期加1(前置++)(后置++)
** 6:该日期减1(前置--)(后置--)
** 7:计算某日期到未来某日期间隔的天数
**
**
** By :Lynn-Zhang
**
*/
//--------------------------------------------------------------------------- #define _CRT_SECURE_NO_WARNINGS 1 #include<assert.h>
#include <iostream>
using namespace std; //在实现日期之间的运算之前,要先进行日期是否非法的检查
//实现日期间大小的比较
//计算两个日期间相差的天数
//计算一个日期加或减上day天后的日期 class Date
{
public:
Date(int year,int month,int day) //构造函数
{
if (year >= && month > && month< && day> < GetMonthDay(year, month)) //判断日期是否非法
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "日期非法" << endl;
assert(false);
}
} Date(const Date& d) //拷贝构造
{
_year = d._year;
_month = d._month;
_day = d._day;
} void Display() // 打印日期
{
cout << _year << "-" << _month << "-" << _day << endl;
} ~Date() //析构函数
{
//cout << "~Date()" << endl;
} //运算符重载(两个日期间比较大小)
bool operator==(const Date &d) //判断两个日期是否相等
{
return (_year == d._year) && (_month == d._month) && (_day == d._day);
} bool operator>(const Date &d) //判断本日期是否大于日期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;
}
else
return false;
} bool operator>=(const Date &d) //判断本日期是否大于或等于日期d
{
return (*this > d) || (*this == d);
} bool operator<(const Date &d) //判断本日期是否小于日期d
{
return !(*this >= d);
}
bool operator<=(const Date &d) //判断本日期是否小于等于日期d
{
return !(*this > d);
} //赋值运算符重载
Date operator=(const Date &d)
{
if (this != &d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
} private:
bool IsLeapYear(int year) //判断是否闰年
{
if (((year % == ) && (year % != )) || (year % == ))
return true;
else
return false;
} int GetMonthDay(int year, int month) //根据已知年月获取该月的天数
{
int monthArray[] = { , , , , , , , , , , , , }; int day = monthArray[month]; if (month == && IsLeapYear(year))
{
day += ;
} return day;
} public:
Date operator+(int day) //给一个日期加day天
{
if (day < )
{
return operator-(-day);
}
Date tmp = *this;
int sumDays = tmp._day + day;
while (sumDays > GetMonthDay(tmp._year, tmp._month))
{
sumDays -= GetMonthDay(tmp._year, tmp._month);
tmp._month++;
if (tmp._month > )
{
tmp._year++;
tmp._month %= ;
}
else
{
tmp._day = sumDays;
}
}
return tmp;
} Date & operator+=(int day) //加上相应的天数后还要进行赋值
{
*this = operator+(day);
return *this;
} Date operator-(int day) //给一个日期减去day天
{
if (day < )
{
return operator+(-day);
}
Date tmp = *this;
while (day >= tmp._day)
{
day -= tmp._day;
if (tmp._month == )
{
tmp._year--;
tmp._month = ;
}
else
{
tmp._month--;
}
tmp._day = GetMonthDay(tmp._year, tmp._month);
}
tmp._day -= day;
return tmp;
} Date & operator-=(int day) //减去相应的天数后赋值
{
*this = operator-(day);
return *this;
}
Date & operator++() //日期加1(前置++)
{
++_day;
if (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month > )
{
++_year;
_month = ;
}
}
return *this;
} Date & operator++(int) //后置++
{
Date tmp = *this;
*this = operator++();
return tmp;
} Date & operator--() // 日期减1 (前置--)
{
if (_day > )
{
--_day;
}
else
{
if (_month == )
{
--_year;
_month = ;
_day = GetMonthDay(_year, _month);
}
else
{
--_month;
_day = GetMonthDay(_year, _month);
}
}
return *this;
} Date & operator--(int) //后置--
{
Date tmp = *this;
*this = operator--();
return tmp;
} //计算两个日期相差的天数
int operator-(Date & d)
{
if (_year <d. _year)
{
Date tmp =* this;
*this = d;
d = tmp;
}
else if (_year == d._year&&_month < d._month)
{
Date tmp = *this;
*this = d;
d = tmp;
}
else if (_year ==d. _year&&_month == d._month&&_day < d._day)
{
Date tmp = *this;
*this = d;
d = tmp;
}
Date tmp1(*this);
Date tmp2(d);
int ret = ;
while (!(tmp2 == tmp1))
{
tmp2.operator++();
ret++;
}
return ret;
} private:
int _year;
int _month;
int _day;
}; void Test1() //测试用例
{
/*Date d1(2016, 1, 15);
d1.Display();
Date d2 = d1;
d2.Display();
Date d3;
d3 = d1;
d2.Display();*/ Date d1(, , );
Date d2(, , );
d1.Display();
d2.Display();
// == 0
//bool ret = d1 == d2;
//cout << ret << endl;
// > 1
//bool ret = d1 >d2;
//cout << ret << endl;
// < 0
//bool ret = d1 < d2;
//cout << ret << endl;
// >= 1
//bool ret = d1 >= d2;
//cout << ret << endl;
// <= 0
//bool ret = d1 <= d2;
//cout << ret << endl;
// =
//d1 = d2;
//d1.Display();
//d2.Display();
}
void Test2()
{
Date d1(, ,);
d1.Display();
//Date ret = d1+1;
//ret.Display();
//Date ret=d1+70;
//ret.Display();
//Date ret=d1-25;
//ret.Display();
////Date ret = d1 += 70;
////ret.Display();
////d1.Display();
//Date ret = d1 -= 70;
//ret.Display();
//d1.Display();
//d1++;
//d1.Display();
//++d1;
//d1.Display();
//--d1;
//d1.Display();
//d1--;
//d1.Display();
Date d2(, , );
d2.Display();
int ret = d1 - d2;
cout << ret << endl;
}
int main()
{
//Test1();
Test2();
system("pause");
return ;
}

日期类(C++实现)的更多相关文章

  1. JAVA基础学习之final关键字、遍历集合、日期类对象的使用、Math类对象的使用、Runtime类对象的使用、时间对象Date(两个日期相减)(5)

    1.final关键字和.net中的const关键字一样,是常量的修饰符,但是final还可以修饰类.方法.写法规范:常量所有字母都大写,多个单词中间用 "_"连接. 2.遍历集合A ...

  2. Java:日历类、日期类、数学类、运行时类、随机类、系统类

    一:Calendar类 java.util 抽象类Calendar   1.static Calendar getInstance()使用默认时区和语言环境获得一个日历. 2. int get(int ...

  3. HDOJ(HDU) 2133 What day is it(认识下Java的Calendar类---日期类)

    Problem Description Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me wha ...

  4. Problem B: 时间和日期类(III)

    Problem B: 时间和日期类(III) Time Limit: 4 Sec  Memory Limit: 128 MBSubmit: 2889  Solved: 1732[Submit][Sta ...

  5. 日历类和日期类转换 并发修改异常 泛型的好处 *各种排序 成员和局部变量 接口和抽象类 多态 new对象内存中的变化

    day07 ==和equals的区别? ==用于比较两个数值 或者地址值是否相同.  equals 用于比较两个对象的内容是否相同   String,StringBuffer.StringBuilde ...

  6. 日期类的使用(java)-蓝桥杯

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

  7. C++实验:时间和日期类

    描述 用C++实现日期类CDate和时间类CTime,并在次基础上利用多继承实现日期时间类CDateTime,使其能输出样例信息. 主函数里的代码已经给出,请补充完整,提交时请勿包含已经给出的代码. ...

  8. 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结

    1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018             年月日时分秒    CST代表北 ...

  9. 背水一战 Windows 10 (44) - 控件(日期类): CalendarView, DatePicker, TimePicker

    [源码下载] 背水一战 Windows 10 (44) - 控件(日期类): CalendarView, DatePicker, TimePicker 作者:webabcd 介绍背水一战 Window ...

  10. day17 包装类、日期类

    包装类 作用:1.丰富了基本数据类型只能存放值的问题,还提供了大量的方法或常量. 2.包装类充当了基本数据类型和引用数据类型转换的桥梁. 应用层面:包装类.String.基本数据类型的互相转换. 1. ...

随机推荐

  1. Session和Cookie之间存在的区别与联系

    一. 概念理解 你可能有留意到当你浏览网页时,会有一些推送消息,大多数是你最近留意过的同类东西,比如你想买桌子,上淘宝搜了一下,结果连着几天会有各种各样的桌子的链接.这是因为 你浏览某个网页的时候,W ...

  2. 从头认识Spring-2.4 基于java的标准注解装配-@Inject-限定器@Named

    这一章节我们来讨论一下基于java的标准注解装配标签@Inject的限定器@Named. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_s ...

  3. pwd 命令

    Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...

  4. 搭建redis集群遇到的坑

    搭建redis集群遇到的坑 #!/bin/bash # 作者: tuhooo # 日期: 2017.4.23 20.15 # 用途: 通过ruby脚本启动redis伪集群 if [ $2 == &qu ...

  5. DMP

    1.dmp-data mabagement platform数据管理平台 数据赋能,营销智变 2.定义 把分散的第一,第三方异构.多源数据进行整合,然后纳入统一技术平台中,并对这些数据进行标准化和细分 ...

  6. window安装redis

    1.redis简介redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...

  7. RecyclerView加载更多用notifyDataSetChanged()刷新图片闪烁

    首先来看看对比ListView看一下RecyclerView的Adapter主要增加了哪些方法: notifyItemChanged(int position) 更新列表position位置上的数据可 ...

  8. 动态对象创建(二)重载new和delete

    动态对象创建(二)重载new和delete 前言 上文我简单介绍了一下动态对象创建的方法,这一篇文章的内容主要是对重载new和delete做一些讲解,也希望能够得到博友们的指点,在这里谢过大家. 通常 ...

  9. hdu3579(线性同余方程组)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  10. devexpress gridcontrol如何遍历每一行

    List<Medicine> medicinelist = new List<Medicine>(); foreach (GridViewRow row in GridView ...