我的第一万天是多少?(日期计算器)
2017-03-23 17:39
477 查看
当我们回想往事时会不会感叹时间流逝,往事如烟,暮然回首,物是人非……
额……不扯了,现在进入主题,当别人问你多大的时候,你总是会说:我22了!然后别人会说,都奔三了,结婚了没有啊,难道你会在回一句,我连女朋友都没有……过。真是悲催呀!额……又扯远了。
好了,为了避免尴尬,别人问你多大的时候,你可以跟他说,我已经七千多天了,哈哈哈。那么这个换算是如何进行的呢?
接下来我来告诉你:
一万天也是一个具有有重要意义的日子,快来计算你的一万天吧!
额……不扯了,现在进入主题,当别人问你多大的时候,你总是会说:我22了!然后别人会说,都奔三了,结婚了没有啊,难道你会在回一句,我连女朋友都没有……过。真是悲催呀!额……又扯远了。
好了,为了避免尴尬,别人问你多大的时候,你可以跟他说,我已经七千多天了,哈哈哈。那么这个换算是如何进行的呢?
接下来我来告诉你:
#include<iostream> using namespace std; class Date { public: Date(int year = 1990, int month = 1, int day = 1) :_year(year) , _month(month) , _day(day) { //如果日期规格不正确,那么定义一个日期 if (!(_year > 0 && _month > 0 && _month<13 && _day>0 && _day < GetMonthDays(_year, _month))) { _year = 1990; _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) { _year = d._year; _month = d._month; _day = d._day; } return *this; } //日期加上天数,返回日期 Date operator+(const int days) { if (days<0)//如果天数为负数,直接转为减法 { return (*this) - (0 - days); } Date temp(*this);//定义一个临时变量, temp._day += days;//将天数加到临时变量的天上面 int _daysinmonth = 0; while (temp._day>(_daysinmonth=GetMonthDays(temp._year, temp._month))) { temp._day -= _daysinmonth; if (12 == temp._month)//如果当前月份为12,那么年加1,月置于1 { temp._year += 1; temp._month = 1; } else { temp._month += 1; } } return temp;//处理完成之后,返回这个日期 } //日期减去天数,返回日期 Date operator-(const int days) { if (days < 0)//如果天数是负数,直接转为加法 { return (*this) + days; } Date temp(*this); temp._day -= days; while (temp._day < 0) { if (1 == temp._day)//如果当前月份为1月,那么年减1,月份置于12 { temp._year -= 1; temp._month = 12; } else { temp._month -= 1; } temp._day += GetMonthDays(temp._year, temp._month); } return temp; } //日期之间的减法,返回相差的天数 int operator-(const Date& d) { Date _mindate(*this); Date _maxdate(d); if (_mindate > _maxdate) { _mindate = _maxdate; _maxdate = *this; } int count = 0; while (_mindate < _maxdate) { ++_mindate;//让小的日期加1的同时将计数器加1 ++count; } return count; } Date&operator++() { *this = *this+1; return *this; } Date&operator++(int) { Date temp = *this; *this = *this + 1; return temp; } Date&operator--() { *this = *this - 1; return *this; } Date&operator--(int) { Date temp = *this; *this = *this - 1; return temp; } bool operator>(const Date& d) { if (_year > d._year || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _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; } bool operator<(const Date& d) { return !(*this>d || *this == d); } bool operator!=(const Date& d) { return !(*this == d); } bb40 private: //获得每月的天数 int GetMonthDays(int year,int month) { int DaysInMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (IsLeap(year) && month == 2) { DaysInMonth[month] += 1; } return DaysInMonth[month]; } //判断是不是瑞年 bool IsLeap(int year) { if (((0 == year % 4) && (0 != year % 100)) || (0 == year % 400)) { return true; } return false; } //重载输出操作符 friend ostream& operator << (ostream&_cout, const Date&d) { _cout << d._year << "-" << d._month << "-" << d._day; return _cout; } private: int _year; int _month; int _day; }; int main() { //Date d; //Date d1(2017, 3, 22); //Date d2(2017, 2, 30); Date d3(1995, 7, 11); cout << d3 + 10000<< endl;//我的第一万天是多少 cout << d3 - 100 << endl;//出生前百天日期 Date d4(2017, 3, 23); cout << d4 - d3 << endl;//我活了多少天 return 0; }
一万天也是一个具有有重要意义的日子,快来计算你的一万天吧!
相关文章推荐
- 鸡兔共笼,共有30个头和90只脚,求鸡兔各有多少?
- jquery分页组件(每页显示多少条)
- 1024!结尾有多少个0
- 转:你对存储性能了解多少?
- n个节点的二叉树有多少种形态
- 最多有多少人参加比赛呢?
- 程序员这个职业赚钱吗?能赚多少钱?
- Java知多少(110)数据库之插入记录
- 人脸识别技术的应用场景,你了解多少?
- 位操作:一个整数的二进制表示中 有多少个1?
- Java知多少(完结篇)
- 【科普】人眼到底等于多少像素
- 新款ipad你了解多少?
- 整数的二进制表示中有多少个1的问题
- 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?
- 我的成长--从足球到篮球有多少路要走(三)
- 赛门铁克新版备份恢复方案Backup Exec 15 知多少
- 创建一个将某个数分配为多少份的随机数的表值函数
- TCP 、UDP、IP包的最大长度是多少?
- MyBatis知多少(9)不同类型的数据库