日期类(C++实现)
//--------------------------------------------------------------------------
/*
**功能:实现日期的简单操作
**
**
**基本的成员函数:
** 构造函数,拷贝构造函数,析构函数,赋值运算符重载,操作符重载(两个日期间比较大小)
**
**日期类功能函数:
** 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++实现)的更多相关文章
- JAVA基础学习之final关键字、遍历集合、日期类对象的使用、Math类对象的使用、Runtime类对象的使用、时间对象Date(两个日期相减)(5)
1.final关键字和.net中的const关键字一样,是常量的修饰符,但是final还可以修饰类.方法.写法规范:常量所有字母都大写,多个单词中间用 "_"连接. 2.遍历集合A ...
- Java:日历类、日期类、数学类、运行时类、随机类、系统类
一:Calendar类 java.util 抽象类Calendar 1.static Calendar getInstance()使用默认时区和语言环境获得一个日历. 2. int get(int ...
- 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 ...
- Problem B: 时间和日期类(III)
Problem B: 时间和日期类(III) Time Limit: 4 Sec Memory Limit: 128 MBSubmit: 2889 Solved: 1732[Submit][Sta ...
- 日历类和日期类转换 并发修改异常 泛型的好处 *各种排序 成员和局部变量 接口和抽象类 多态 new对象内存中的变化
day07 ==和equals的区别? ==用于比较两个数值 或者地址值是否相同. equals 用于比较两个对象的内容是否相同 String,StringBuffer.StringBuilde ...
- 日期类的使用(java)-蓝桥杯
蓝桥杯日期问题常考,java提供了日期类很方便: //日历类 Calendar c = Calendar.getInstance(); // 获取实例化对象 Date date =c.getTime( ...
- C++实验:时间和日期类
描述 用C++实现日期类CDate和时间类CTime,并在次基础上利用多继承实现日期时间类CDateTime,使其能输出样例信息. 主函数里的代码已经给出,请补充完整,提交时请勿包含已经给出的代码. ...
- 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结
1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018 年月日时分秒 CST代表北 ...
- 背水一战 Windows 10 (44) - 控件(日期类): CalendarView, DatePicker, TimePicker
[源码下载] 背水一战 Windows 10 (44) - 控件(日期类): CalendarView, DatePicker, TimePicker 作者:webabcd 介绍背水一战 Window ...
- day17 包装类、日期类
包装类 作用:1.丰富了基本数据类型只能存放值的问题,还提供了大量的方法或常量. 2.包装类充当了基本数据类型和引用数据类型转换的桥梁. 应用层面:包装类.String.基本数据类型的互相转换. 1. ...
随机推荐
- Proftp设置虚拟用户(转)
首先在proftpd.conf中添加: AuthOrder mod_auth_file.c mod_auth_unix.c AuthUserFile /usr/local/etc/ftp ...
- Amzaon EC2虚拟化技术演进:从 Xen 到 Nitro
今年2月,由光环新网运营的 AWS 中国(北京)区域和由西云数据运营的 AWS 中国 (宁夏)区域发布新的实例类型,新的实例类型包括 C5.C5d.R5.R5d.除了这四种之外,在AWS国外部分区 ...
- distcc加速内核编译
Linux内核编译实在是费时间的事,搞内核移植的时候总要编译,生命有一部分就浪费在等内核编译完成上,有心想买个HP的工作站,看了下Z840的价格,想想还是算了.distcc早就听说过,一直没有去试试, ...
- 机器学习6—SVM学习笔记
机器学习牛人博客 机器学习实战之SVM 三种SVM的对偶问题 拉格朗日乘子法和KKT条件 支持向量机通俗导论(理解SVM的三层境界) 解密SVM系列(一):关于拉格朗日乘子法和KKT条件 解密SVM系 ...
- The Pilots Brothers' refrigerator - poj 2965
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20325 Accepted: 7830 Special Judg ...
- Android 浏览器文本垂直居中问题
问题描述 在开发中,我们常使用 line-height 属性来实现文本的垂直居中,但是在安卓浏览器渲染中有一个常见的问题,就是对于小于12px的字体使用 line-height 属性进行垂直居中的时候 ...
- drupal7 使用(hook_preprocess_HOOK)向各个主题模版里面传递变量
函数地址:hook_preprocess_HOOK 1 首先解释下hook_preprocess_HOOK这个钩子的含义: hook _ preprocess _ H ...
- 安装mingw后,在命令窗体编译c文件
1.编译test.cpp文件 #include<iostream> int main(int argc,char **argv) { std::cout<<"he ...
- centos7.0 activemq的安装
1:下载地址http://activemq.apache.org/activemq-590-release.html 2:wget http://archive.apache.org/dist/act ...
- 【BZOJ1038】[ZJOI2008]瞭望塔 半平面交
[BZOJ1038][ZJOI2008]瞭望塔 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如 ...