日期计算器(date类)
//日期计算器
#include
using namespace std;
#includeclass Date
{
public:Date (int year = 2015, int month = 11, int day = 15)//构造函数:_year(year),_month(month) //初始化列表进行初始化,_day(day){}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;}void Display1(){cout<<"日期:"<<_year<<"-"<<_month<<"-"<<_day< (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){return *this > d || *this == d;}bool operator<(const Date& d){return !(*this >= d);}bool operator<=(const Date& d){return !(*this > d);}bool Judge_ymd(int year=2015,int month =11,int day=15)//判断有效性{if(_year<1||_month<0||_month>12||_day<1||_day>Judge_day(_year,_month))return false;return true;}bool IsLeapYear (int year)//闰年{if ((year % 4 == 0 && year % 100 != 0)|| year % 400 == 0)return true;return false;}int YearMonth(Date& d)//月数 ++ {++d._month;if(d._month==13){d._month=1;++d._year;}else{d._month;}return d._month;}int Judge_day(int year,int month)//每月天数{int MonthDay[12]={31,28,31,30,31,30,31,31,30,31,30,31};if(IsLeapYear(year)){return MonthDay[month-1]+1;}return MonthDay[month-1];}public:void CorrectDate()//转换日期{while(_day<=0){if(_month==1){_year--;_month=12;}else{_month--;}_day+=Judge_day(_year,_month);}while(_day>=Judge_day(_year,_month)){if(_month==12){_year++;_month=1;}else{_month++;}_day-=Judge_day(_year,_month);}}//计算:日期前后n天的日期Date operator+(int day){cout<_day+=day;this->CorrectDate();return *this;}Date& operator-=(int day){this->_day-=day;this->CorrectDate();return *this;}Date& operator++()//前置++{*this+=1;return *this;}Date operator++(int)//后置++{Date tmp(*this);*this+=1;return tmp;}//计算:两日期之间天数int operator-(Date& d){int days=0;Date d1=*this;Date d2=d;cout<<"两日期间天数:"<>(istream& in, Date& d);friend ostream& operator<<(ostream& out, Date& d);
private:int _year;int _month;int _day;
};istream& operator>>(istream& in, Date& d)
{in>>d._year;in>>d._month;in>>d._day;return in;
}
ostream& operator<<(ostream& out, Date& d)
{out<>input;switch(input){case 1:{next1:int k=0;cout<<"请输入日期:"<> d1;if(d1.Judge_ymd()==false){cout<<"非法日期,请重新输入"<>k;(d1+k).Display1();break;}case 2:{next2:int days=0;cout<<"请输入日期1:"<> d1;if(d1.Judge_ymd()==false){cout<<"非法日期,请重新输入"<> d2;if(d2.Judge_ymd()==false){cout<<"非法日期,请重新输入"< 转载于:https://blog.51cto.com/zxtong/1752749
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
