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

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

#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; } } // 拷贝构造函数 // d2(d1) Date(const Date& d) { _year = d._year; _month = d._month; _day = d._day; } // 赋值运算符重载 // d2 = d3 -> d2.operator=(&d2, d3) 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]; //如果是闰年的2月, 那么day+1 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;//2020.5.1 + 20 --> 2020.5.21//2020.5.21 + 20 --> 2020.5.41 --> 进位 --> -31 -->月份进位 --> 2020.6.10//2020.12.6 + 90 --> 2020.12.96 --> 进位 --> -31 -->月份进位 --> 2020.13.65 -->年进位 -->// 2021.1.65--> 进位 --> -31 -->月份进位 --> 2021.2.34 --> 进位 --> -28 -->月份进位 --> 2021.3.6 _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); //等价于 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; //判断_day是否为负值或者0, 退位 //2020.5.24 - 30 --> 2020.5.-6 --> 月份退位 --> +30 --> 2020.4.24 while (_day <= 0) { //先让月份退位, 因为_day要加的是上个月的天数 _month--; //考虑年份是否要退位 if (_month == 0) { _month = 12; _year--; } //加上上个月的天数, 如果还是<=0 那么继续循环 _day += getMonthDay(_year, _month); } return *this; } // 前置++ Date& operator++() { //++d: 首先++,返回++之后的值 return *this += 1; } // 后置++ : d++: 本身++, 返回++之前的值 //int: 形参不是一个真正的参数,只是一个标记参数,编译器看到这样的定义,通过语法树搜索,可以解释为后置++ 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) { if (_day > d._day) return true; } } return false; } // ==运算符重载 bool operator==(const Date& d) { if (_year == d._year && _month == d._month && _day == d._day) return true; return false; } // >=运算符重载 inline 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 { //d1 <= d2 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 test3(){ Date d(2020, 5, 24); d.printD(); d -= 30; d.printD(); d -= -30; d.printD(); d -= 3650; d.printD(); d += -3650; d.printD();}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/

你可能感兴趣的文章
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
mysql union orderby 无效
查看>>
mysql v$session_Oracle 进程查看v$session
查看>>
mysql where中如何判断不为空
查看>>
MySQL Workbench 使用手册:从入门到精通
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>