日期类(Date)_运算符重载简单应用
来源:互联网 发布:淘宝手机店铺装修尺寸 编辑:程序博客网 时间:2024/06/16 09:41
面对日期类里面的加加减减涉及到的进位和借位,我更倾向使用一个保存了每个月天数的数组代替,虽然很占内存,但是方法简化了,而且不需要遍历数组,只需要拿到月份的数字,用月份的数字当下标就可以得到这个月的天数(哈希表的简单应用),这种方法很好用,尤其是在需要不断的遍历数组的时候,可以用这种方法代替遍历.
在写代码的时候,还需要注意代码的复用,这样写代码的效率会变高,不会把时间都浪费在写很多类似的逻辑上,比如在本文的重载>,<,==,!=,>=,<=的时候就体现了出来.
#include<iostream>using namespace std;class Date{public: Date(int year = 1900, int month = 1, int day = 1) :_year(year) , _month(month) , _day(day) { if (_year<1900 || _month<0 || _month>12 || _day<0 || _day>calenter[_month]) exit(-1); } bool operator > (const Date &d) const { 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)const { if (_year == d._year&&_month == d._month&&_day == d._day) return true; else return false; } bool operator >=(const Date&d)const { return (*this > d || *this == d); } bool operator <=(const Date&d)const { return !(*this > d); } bool operator >(const Date&d)const { return !(*this > d || *this == d); } bool operator !=(const Date&d)const { return!(*this == d); } bool IsLeapYear(Date d) { if ((d._year % 100 != 0 && d._year % 4 == 0) || (d._year % 400 == 0)) { d.calenter[2] = 29; return true; } d.calenter[2] = 28; return false; } Date operator+(int day)const//+ 不是+=,只需要操作,不能给this改变值!!! { if (day<0) { *this - (-day); } else { Date tmp = *this; tmp._day += day; while (tmp._day > calenter[_month]) { tmp._day -= calenter[_month]; tmp._month++; if (tmp._month > 12) { tmp._year++; tmp._month = 1; IsLeapYear(tmp); } } return tmp; } } Date operator+=(int day) { return *this = *this + day; } Date operator-(int day)const { if(day<0) { return *this + (-day); } Date tmp = *this; tmp._day -= day; while (tmp._day < 0) { if (tmp._month == 1) { tmp._year--; IsLeapYear(tmp); tmp._month = 12; tmp._day += 31; } else { tmp._day += calenter[_month - 1]; tmp._month++; } } return tmp; } Date operator-=(int day) { return *this = *this - day; } Date operator++()//前置 { return ((*this) + 1); } Date operator++(int)//后置 { Date tmp = *this; *this=*this+1; return tmp; } Date operator--() { return *this = *this - 1; } Date operator--(int) { Date tmp = *this; *this = *this - 1; return tmp; } int operator-(const Date& d) { Date tmp1; Date tmp2; int count = 0; if (*this > d) { tmp1 = d; tmp2 = *this; } else { tmp1 = *this; tmp2 = d; } while (tmp1!=tmp2) { tmp1++; count++; } return count; } ostream operator <<(ostream&os,const Date &d) { out<<d._yeaar<<-<<d._month<<-<<d.day<<endl; return out; }private: int _year; int _month; int calenter[13]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int _day;};
阅读全文
0 0
- 日期类(Date)_运算符重载简单应用
- 运算符重载的应用:日期类
- Date类重载“+”运算符
- C++日期类的运算符重载和简单边界条件
- 运算符重载----日期类
- 运算符重载--日期类
- 定义日期类Date,并重载运算符实现几种操作
- 重载_,“+, -, --, ++”运算符
- 【7.11】为Date类重载“+”运算符,实现在某一个日期上(月、日、年)加一个天数。
- 作业11: 类_运算符重载
- c++_类的运算符重载
- 字符串类_运算符重载
- C++_运算符重载
- C++_运算符重载
- C++_运算符重载
- C++_运算符重载
- C++_运算符重载
- C++_运算符重载
- Oracle中如何获取当天时间的最开始的时间和最结尾的时间
- for语句的简单练习 打印三角 九九乘法表 累加
- java Swing 时间选择器控件
- tp3.2报错(T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING
- Web自动化之Headless Chrome概览
- 日期类(Date)_运算符重载简单应用
- numpy 常用基本库
- spark性能优化
- Android apk安全监测及加固方案
- SSH 在本地执行远程机器上的命令
- solr6.6环境搭建
- python 实现Hadoop的partitioner和二次排序
- SpringMVC的拦截器问题
- android 自定义纯数字键盘输入