博客
关于我
日期类
阅读量:229 次
发布时间:2019-03-01

本文共 4928 字,大约阅读时间需要 16 分钟。

#include 
using namespace std;class Date {public: Date(int year = 1, int month = 1, int day = 1) { if (year > 0 && month > 0 && month <= 12 && day > 0 && day <= getMonthDay(year, month)) { _year = year; _month = month; _day = day; } else { cout << "日期不合法 : " << year << "-" << month << "-" << day << endl; cout << "重置为默认值 : 2000-1-1" << endl; _year = 2000; _month = 1; _day = 1; } } Date(const Date& d) { _year = d._year; _month = d._month; _day = d._day; } Date& operator=(const Date& d) { if (this == &d) return *this; this->_year = d._year; this->_month = d._month; this->_day = d._day; return *this; } ~Date() { } int getMonthDay(int year, int month) { static int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int day = days[month]; if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) day++; return day; } Date& operator+=(int day) { if (day < 0) return *this -= -day; _day += day; while (_day > getMonthDay(_year, _month)) { _day -= getMonthDay(_year, _month); _month++; if (_month == 13) { _month = 1; _year++; } } return *this; } Date operator+(int day) { Date ret(*this); ret += day; return ret; } Date operator-(int day) { Date ret = *this; ret -= day; return ret; } Date& operator-=(int day) { if (day < 0) return *this += -day; _day -= day; while (_day <= 0) { _month--; if (_month == 0) { _month = 12; _year--; } _day += getMonthDay(_year, _month); } return *this; } Date& operator++() { return *this += 1; } Date operator++(int) { Date ret(*this); *this += 1; return ret; } Date operator--(int) { Date ret(*this); *this -= 1; return ret; } Date& operator--() { return *this -= 1; } bool operator>(const Date& d) { if (_year > d._year) return true; else if (_year == d._year) { if (_month > d._month) return true; else if (_month == d._month) { return _day > d._day; } } return false; } bool operator==(const Date& d) { return _year == d._year && _month == d._month && _day == d._day; } bool operator>= (const Date& d) { return (*this > d) || (*this == d); } bool operator< (const Date& d) { return !(*this > d); } bool operator<= (const Date& d) { return !(*this > d); } bool operator!= (const Date& d) { return !(*this == d); } int operator-(const Date& d) { Date d1(*this); Date d2(d); int num = 0; if (d1 > d2) { while (d1 > d2) { d2++; num++; } return num; } else { while (d1 < d2) { d1++; num++; } return -num; } } void printD() { cout << _year << "-" << _month << "-" << _day << endl; }private: int _year; int _month; int _day;};void test() { Date d(2020, 5, 20); d.printD(); d += 10; d.printD(); d += 10; d.printD(); Date d2(2020, 12, 6); d2.printD(); d2 += 90; d2.printD(); d2 += 3650; d2.printD(); ++d2; d2.operator++(); d2.printD(); d2.operator++(0); d2.printD(); d2++; d2.printD();}void test2() { Date d1(2020, 5, 24); d1.printD(); d1 -= 30; d1.printD(); d1 -= -30; d1.printD(); d1 -= 3650; d1.printD(); d1 += -3650; d1.printD();}void test3() { Date d1(2020, 5, 25); Date d2(d1); Date d3(d1); Date d4(2020, 5, 23); cout << (d1 > d4) << endl; cout << (d1 < d4) << endl; cout << (d1 <= d4) << endl; cout << (d3 > d1) << endl; cout << (d1 >= d1) << endl; cout << (d1 == d2) << endl; cout << (d1 != d2) << endl;}void test4() { Date d1(2020, 5, 25); Date d2(2020, 5, 25); Date d3(2020, 5, 26); Date d4(2020, 5, 23); cout << (d1 > d4) << endl; cout << (d1 < d4) << endl; cout << (d1 <= d4) << endl; cout << (d3 > d1) << endl; cout << (d1 >= d1) << endl; cout << (d1 == d2) << endl; cout << (d1 != d2) << endl;}void test5() { Date d1(2020, 5, 25); Date d2 = d1 + 3650; cout << (d1 - d2) << endl; cout << (d2 - d1) << endl; Date d3 = d2 + 189; cout << (d2 - d3) << endl; cout << (d3 - d2) << endl;}int main() { test(); test2(); //test3(); //test4(); //test5(); return 0;}

转载地址:http://aunv.baihongyu.com/

你可能感兴趣的文章
opencv——图像缩放1(resize)
查看>>
Opencv——模块介绍
查看>>
OpenCV与AI深度学习 | 2024年AI初学者需要掌握的热门技能有哪些?
查看>>
OpenCV与AI深度学习 | CIB-SE-YOLOv8: 优化的YOLOv8, 用于施工现场的安全设备实时检测 !
查看>>
OpenCV与AI深度学习 | OpenCV图像拼接--Stitching detailed使用与参数介绍
查看>>
OpenCV与AI深度学习 | OpenCV快速傅里叶变换(FFT)用于图像和视频流的模糊检测(建议收藏!)
查看>>
OpenCV与AI深度学习 | SAM2(Segment Anything Model 2)新一代分割一切大模型介绍与使用(步骤 + 代码)
查看>>
OpenCV与AI深度学习 | YOLO11介绍及五大任务推理演示(目标检测,图像分割,图像分类,姿态检测,带方向目标检测)
查看>>
OpenCV与AI深度学习 | YOLOv11来了:将重新定义AI的可能性
查看>>
OpenCV与AI深度学习 | YOLOv8自定义数据集训练实现火焰和烟雾检测(代码+数据集!)
查看>>
OpenCV与AI深度学习 | YOLOv8重磅升级,新增旋转目标检测,又该学习了!
查看>>
OpenCV与AI深度学习 | 使用OpenCV轮廓检测提取图像前景
查看>>
OpenCV与AI深度学习 | 使用Python和OpenCV实现火焰检测(附源码)
查看>>
OpenCV与AI深度学习 | 使用PyTorch进行小样本学习的图像分类
查看>>
OpenCV与AI深度学习 | 使用YOLO11实现区域内目标跟踪
查看>>
OpenCV与AI深度学习 | 使用YOLOv8做目标检测、实例分割和图像分类(包含实例操作代码)
查看>>
OpenCV与AI深度学习 | 使用单相机对已知物体进行3D位置估计
查看>>
OpenCV与AI深度学习 | 初学者指南 -- 什么是迁移学习?
查看>>
OpenCV与AI深度学习 | 十分钟掌握Pytorch搭建神经网络的流程
查看>>
OpenCV与AI深度学习 | 基于GAN的零缺陷样本产品表面缺陷检测
查看>>