c++实现日期类,日历计算器
在写日期类日期计算器之前先实现了一个简单的复数类//引用做参数,1.swap--在函数内部改变参数,2.Bigdata提高效率//内联函数必须在定义处加上inline//定义在类内部的函数默认为内联函数//c++中尽量使用const,枚举,内联去替代宏//宏没有类型安全的检查,在预处理的时候替换了所以无法调试,宏安全性不高
classComplex{public:Complex(doublereal=0.0,doublep_w_picpath=0.0)//定义了一个全缺省的构造函数:_real(real),_p_w_picpath(p_w_picpath){cout<<"Complex()"<<endl;//这样可以在屏幕上输出构造函数的信息,方便分析构造函数的调用}Complex(constComplex&c)//拷贝构造函数{cout<<"Complex(constComplex&c)"<<endl;_real=c._real;_p_w_picpath=c._p_w_picpath;}~Complex()//析构函数{cout<<"~Complex()"<<endl;}Complex&operator=(constComplex&c)//Complex返回值支持连续的赋值{cout<<"Complex&operator=()"<<endl;if(this!=&c){_real=c._real;_p_w_picpath=c._p_w_picpath;}return*this;}voidDisplay(){cout<<_real<<"-"<<_p_w_picpath<<this-="">_real++;this->_p_w_picpath++;return*this;}Complexoperator++(int)//后置++,返回加之前的值,int用来占位,形成重载{Complexret(*this);this->_real++;this->_p_w_picpath++;returnret;}booloperator==(constComplex&c){IsEqual(c);}boolIsEqual(constComplex&c){return_p_w_picpath==c._p_w_picpath&&_real==c._real;}booloperator>(constComplex&c){MoreThan(c);}boolMoreThan(constComplex&c){if(_real>c._real){returntrue;}elseif(_real==c._real&&_p_w_picpath>c._p_w_picpath){returntrue;}returnfalse;}Complexoperator+(constComplex&c){/*Complexret;ret._real=_real+c._real;ret._p_w_picpath=_p_w_picpath+c._p_w_picpath;*/Complexret(*this);ret._real+=c._real;ret._p_w_picpath+=c._p_w_picpath;returnret;}Complex&operator+=(constComplex&c){_real+=c._real;_p_w_picpath+=c._p_w_picpath;return*this;}private:double_real;double_p_w_picpath;};
classDate{public:/*Date(){cout<<"Date()"<<endl;}*/Date(intyear=1900,intmonth=1,intday=1)//使用初始化列表更高效//必须使用初始化列表初始化的成员变量//1.常量成员变量//2.引用类型成员变量//3.没有缺省的构造函数的类成员变量{//cout<<"Date(intyear=1900,intmonth=1,intday=1)"<<if=""year="">1900&&month>0&&month<13day="">0&&day<=GetMonthDay(year,month)){_year=year;_month=month;_day=day;}else{cout<<"非法日期"<<endl;}}Date(constDate&d){//cout<<"Date(constDate&d)"<<endl;_year=d._year;_month=d._month;_day=d._day;}Date&operator=(constDate&d){//cout<<"Date&operator=(constDate&d)"<<endl;if(this!=&d){_year=d._year;_month=d._month;_day=d._day;}return*this;}~Date(){//cout<<"~Date"<<void=""-="">constDate*this{cout<<_year<<"-"<<_month<<"-"<<_day<<public:=""bool=""operator=""return=""_year="="d._year=""_month="="d._month=""_day="="const=""this="=">(constDate&d){//if(_year>d._year)//{//returntrue;//}//else//{//if(_year==d._year)//{//if(_month>d._month)//returntrue;//else//{//if(_month==d._month)//{//if(_day>d._day)//returntrue;//}//}//}//}//returnfalse;return(_year>d._year)||((_year==d._year)&&(_month>d._month))||((_year==d._year)&&(_month==d._month)&&(_day>d._day));}booloperator>=(constDate&d){return(*this>d)||(*this==d);}booloperator<(constreturn=""this="">=d);}booloperator<=(constreturn=""this="">d);}//日期计算器Dateoperator+(intday){if(day<0)return=""this=""-=""date=""tmp._day=""while="">GetMonthDay(tmp._year,tmp._month)){tmp._day-=GetMonthDay(tmp._year,tmp._month);if(tmp._month==12){tmp._year++;tmp._month=1;}else{++tmp._month;}}returntmp;}Date&operator+=(intday){/*if(day<0){return*this-(-day);}_day+=day;while(_day>GetMonthDay(_year,_month)){_day-=GetMonthDay(_year,_month);if(_month==12){_year++;_month=1;}else{++_month;}}return*this;*/*this=*this+day;return*this;}{if(day<0){return*this+(-day);}Dateoperator-(intday)Datetmp(*this);tmp._day-=day;while(tmp._day<=0){if(tmp._month==1){tmp._year--;tmp._month=12;}else{tmp._month--;}tmp._day+=GetMonthDay(tmp._year,(tmp._month));}returntmp;}Date&operator-=(intday){/*if(day<0){return*this+(-day);}_day-=day;while(_day<0){_day+=GetMonthDay(_year,(--_month));if(_month==1){_year--;_month=12;}else{_month--;}}return*this;*/*this=*this-day;return*this;}Dateoperator++()//前置++{*this+=1;return*this;}Dateoperator++(int)//后置++{/*Datetmp(*this);tmp._day+=1;while(_day>GetMonthDay(tmp._year,tmp._month)){tmp._day-=GetMonthDay(tmp._year,tmp._month);if(tmp._month==12){tmp._year++;tmp._month=1;}else{tmp._month++;}}returntmp;*/Datetmp(*this);*this+=1;return*this;}Dateoperator--()//前置--{*this-=1;return*this;}Dateoperator--(int)//后置--{/*Datetmp(*this);tmp._day-=1;while(tmp._day<0){tmp._day+=GetMonthDay(tmp._year,(--tmp._month));if(tmp._month==1){tmp._year--;tmp._month=12;}else{tmp._month--;}}returntmp;*/Datetmp(*this);*this-=1;return*this;}intoperator-(constDate&d){intflag=1;Datemax=*this;Datemin=d;if(max<min){swap(max._year,min._year);swap(max._month,min._month);swap(max._day,min._day);flag=-1;}intdays=0;while(min!=max){++min;++days;}returndays*flag;}private:bool_IsLeapyear(intyear){if((year%4==0&&year%100!=0)||year%400==0)returntrue;}intGetMonthDay(intyear,intmonth){intmonthArray[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};intday=monthArray[month];if(month==2&&_IsLeapyear(year)){day+=1;}returnday;}private:int_year;int_month;int_day;};
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。