C++完成日期类
来源:互联网 发布:上海译文 知乎 编辑:程序博客网 时间:2024/05/18 02:50
#include <iostream>using namespace std;class Date { public: Date(int year = 2017, int month = 9, int day = 10) : _year(year) , _month(month) , _day(day) {} Date& operator=(const Date& d); // 前置++ Date& operator++(); // 后置++ Date operator++(int); Date& operator--(); Date operator--(int); //days天之后的日期 Date operator+(int days); // days天之前的日期 Date operator-(int days); // 两个日期之间的距离 int operator-(const Date& d); bool operator==(const Date& d); bool operator!=(const Date& d); bool operator>(const Date& d); bool operator<(const Date& d); private: int _year; int _month; int _day; }; Date& Date::operator=(const Date& d){ if(this!=&d) { _year=d._year; _month=d._month; _day=d._day; } return *this;}Date& Date::operator++(){ ++_day;//日期加1 switch(_month)//日期向月进位 { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if(32==_day)//大月 { _day=1; ++_month; } break; case 2: if((0==_year%4)&&(0!=_year%100)||(0==_year%400))//润年 { if(30==_day) { _day=1; ++_month; } } else//平年 { if(29==_day) { _day=1; ++_month; } } break; default://小月 if(31==_day) { _day=1; ++_month; } break; } if(13==_month)//月向年进位 { _month=1; ++_year; } return *this;}Date Date::operator++(int){ Date tmp(*this); ++(*this); return tmp;}Date& Date::operator--(){ --_day; if(0==_day)//日期为0向月借位 switch(--_month)//月份-1,再具体分情况看日期置为多少 {//--_month:0-11 case 0://月份为0向年借位 --_year; _month=12; _day=31; break; case 1: case 3: case 5: case 7: case 8: case 10://大月 _day=31; break; case 2: if((0==_year%4)&&(0!=_year%100)||(0==_year%400))//润年 _day=29; else//平年 _day=28; break; default://小月 _day=30; } return *this;}Date Date::operator--(int){ Date tmp(*this); --(*this); return tmp;}Date Date::operator+(int days){ if(days<0)//输入天数为负数,改为算|days|天之前的日期 return (*this)-(-days); while(days--) { ++(*this); } return *this;}Date Date::operator-(int days){ if(days<0)//输入天数为负数,改为算|days|天之后的日期 return (*this)+(-days); while(days--) { --(*this); } return *this;}bool Date::operator==(const Date& d){ return (_year==d._year)&&(_month==d._month)&&(_day==d._day);}bool Date::operator!=(const Date& d){ return (_year!=d._year)||(_month!=d._month)||(_day!=d._day);}bool Date::operator>(const Date& d){ if(_year!=d._year)//比较年份 return _year>d._year ; else {//年份相同比较月份 if(_month!=d._month) return _month>d._month; else//月份不同比较日 return _day>d._day; }}bool Date::operator<(const Date& d){ if(_year!=d._year)//比较年份 return _year<d._year ; else {//年份相同比较月份 if(_month!=d._month) return _month<d._month; else//月份不同比较日 return _day<d._day; }}int Date::operator-(const Date& d){ int count=0; Date tmp(d);//防止d的值被改变 if(*this>d)//第一个日期大于第二个 while(*this!=tmp) { ++tmp; ++count; } else while(*this!=tmp)//第一个日期小于第二个 { --tmp; --count; } return count;}int main(){ Date a(2017,10,25); Date b(1996,3,4); int c=a-b; printf("%d\n",c); return 0;}
阅读全文
0 0
- C++完成日期类
- 完成日期类的实现
- C/C++完成两个日期之间相隔天数的计算
- C++完成日期类的实现--Date
- 【C++】日期类+日期万年历+日期计算器
- 完成按照日期排列
- C++【类:日期类】
- c#-日期类
- 【C++】日期类
- 【C++】日期类
- 【C++】 日期类Date
- C++完成日期类的实现——Date
- 使用c语言库函数-完成加减天数数后自动转换成相关日期
- MySQL日期类型 待完成
- PublicConst常用日期类(C#)
- C#.NET 日期操作类
- 【C++】c++日期类Date
- C++:日期类的实现
- C 输出当前时间的几种方式
- Android_Fragment+ViewPager滑动
- 协方差与相关系数
- Qt获取控件位置,坐标总结
- Maven搭建web项目
- C++完成日期类
- PCB 丝印规范及要求
- OpenGL FrameBufferCopy相关Api比较(glCopyPixels,glReadPixels,glCopyTexImage2D,glFramebufferTexture2D)
- mysql客户端navicat安装详解 图解
- checkbox选中
- 视频信号
- 简单的实现一下自定义圆然后平移的小动画
- Java 的发展史
- mysql日记1