#include <iostream> #include <stdexcept> #include <ctime> using namespace std; class Date { private: unsigned year; unsigned month; unsigned day; unsigned days; static bool Isleapyear(unsigned y); static Date Daystodate(unsigned tdays); public: Date(unsigned ,unsigned ,unsigned); unsigned Datetodays(); friend Date operator +(const Date &date,int mdays); friend Date operator +(int mdays,const Date &date); friend int operator -(const Date &date1,const Date &date2); friend Date operator -(const Date &date,int mdays); friend ostream& operator <<(ostream& os,const Date& date); }; inline bool Date::Isleapyear(unsigned y) { return ((y%400==0)||(y%4==0&&y%100!=0)); } inline unsigned Date::Datetodays() { return days; } Date::Date(unsigned y=1,unsigned m=1,unsigned d=1):year(y),month(m),day(d) { if(y<=0||m<=0||d<=0||m>12||d>31) throw range_error("Wrong date"); if(m==2&&Isleapyear(y)&&d>29) throw range_error("Wrong leap year february date"); if(m==2&&!Isleapyear(y)&&d>28) throw range_error("Wrong common year februarydate"); unsigned year_index,month_index,day_index; unsigned commonyear[]={0,31,59,90,120,151,181,212,243,273,304,334}; unsigned leapyear[] ={0,31,60,91,121,152,182,213,244,274,305,335}; for(days=0,year_index=1;year_index!=y;++year_index) { if(Isleapyear(year_index)) days+=366; else days+=365; } if(Isleapyear(year)) days+=leapyear[m-1]+day; else days+=commonyear[m-1]+day; } Date Date::Daystodate(unsigned tdays) { unsigned year_index,month_index,day_index; unsigned commonyear[]={0,31,59,90,120,151,181,212,243,273,304,334}; unsigned leapyear[] ={0,31,60,91,121,152,182,213,244,274,305,335}; Date tdate; tdate.days=tdays; for(year_index=1;tdays>=366;) { if(Isleapyear(year_index)) tdays-=366; else tdays-=365; ++year_index; } if(Isleapyear(year_index)) { for(month_index=1;tdays>leapyear[month_index];++month_index); tdays-=leapyear[month_index-1]; } else { for(month_index=1;tdays>commonyear[month_index];++month_index); tdays-=commonyear[month_index-1]; } tdate.year=year_index; tdate.month=month_index; tdate.day=tdays; return tdate; } Date operator +(const Date &date,int mdays) { return date.Daystodate(date.days+mdays); } Date operator +(int mdays,const Date &date) { return date+mdays; } int operator -(const Date &date1,const Date &date2) { return date1.days-date2.days; } Date operator -(const Date &date,int mdays) { return date+(-mdays); } ostream& operator <<(ostream& os,const Date& date) { return os<<date.year<<"年"<<date.month<<"月"<<date.day <<"日"; } int main(void) { Date d1(1970,1,1),d2,d3(5,2,27); time_t today; struct tm stoday; unsigned durdays; today=time(NULL); stoday=*gmtime(&today);//用标准库验证 cout<<"today: "<<1900+ stoday.tm_year<<"年"<<1+stoday.tm_mon<<"月"<<stoday.tm_mday<<"日"<<endl; durdays=today/86400; d2=durdays+d1; cout<<d1.Datetodays()<<endl; cout<<d2.Datetodays()<<endl; cout<<d2-d1<<endl; cout<<durdays<<endl; cout<<d3.Datetodays()<<endl; cout<<d1<<endl; cout<<d2<<endl; d2=d2-(10); cout<<d2<<endl; durdays=d1-d3; d2=d1-durdays; cout<<d3<<endl; cout<<d2<<endl; system("pause"); return 0; }