//--------------------------------------------------------------------------
/*
**功能:实现日期的简单操作
**
**
**基本的成员函数:
** 构造函数,拷贝构造函数,析构函数,赋值运算符重载,操作符重载(两个日期间比较大小)
**
**日期类功能函数:
** 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. day14—JQuery编程基础

    JQuery 1.什么是jQuery jQuery是一个优秀的JavaScript框架.一个轻量级的JavaScript类库.jQuery的核心理念是Write less.Do more. 使用jQu ...

  2. Redis 的学习和使用

    安装Redis 官方网站:http://redis.io/ 官方下载:http://redis.io/download 可以根据需要下载不同版本 windows版:https://github.com ...

  3. MyEclipse配置输出控制台信息至文本文件里

    有时会遇到这种情况.输出的信息过多,console控制台显示不全然.这是就须要将输出的信息输出到文本文件里,既能够查看也能够备份. 1.右击须要执行的项目->Run As->Run Con ...

  4. Android自定义action与permission!!! (转)

    原文地址:http://blog.csdn.net/android_tutor/article/details/6310418#reply 大家好,今天给大家简单分享一下Android中自定义acti ...

  5. IEnumerable, IEnumerator接口

    IEnumerable接口 // Exposes the enumerator, which supports a simple iteration over a non-generic collec ...

  6. java Collection-Map 之 TreeMap

    TreeMap 内部定义了一个类  static final class Entry<K,V> implements Map.Entry<K,V>,(自平衡红黑二叉树)作为数据 ...

  7. mysql-proxy做客户端连接转发【外网访问内网mysql】

    功能 用于外网客户端连接内网的MySQL,将此工具安装在中转服务器上. 软件版本 mysql-proxy-0.8.1-linux-rhel5-x86-64bit.tar.gz 简单的配置过程 解压后有 ...

  8. php扩展安装phpize

    安装php(fastcgi模式)的时候,常常有这样一句命令:/usr/local/webserver/php/bin/phpize 一.phpize是干嘛的? phpize是什么东西呢?php官方的说 ...

  9. Android JNI开发之NDK环境搭建

    参考:http://www.cnblogs.com/yejiurui/p/3476565.html 谷歌改良了ndk的开发流程,对于Windows环境下NDK的开发,如果使用的NDK是r7之前的版本, ...

  10. Android设备各种使用尺寸整理

    // 获取屏幕的宽度.高度 Display defDip = getWindowManager().getDefaultDisplay(); int disWidth = defDip.getWidt ...