#include""classDate{private: intyear,month,day;public: Date(){year=0;month=0;day=0;}//默认构造函数 Date(intx,inty,intz){year=x;month=y;day=z;}//重载构造函数 voidadd(intm);//加天数 voiddel(intm);//减天数 intdiff(Date&q);//日期差 voiddisplay();//显示};voidDate::add(intm){ intd[12]={31,28,31,30,31,30,31,31,30,31,30,31},temp,f;//d为12个月每个月天数的数组 f=0; while(m>0) { if(year%100==0) d[1]=(year%400==0?29:28);//如果是闰年,2月天数为29 else d[1]=(year%4==0?29:28);//如果是闰年,2月天数为29 temp=d[month-1]-day; if(temp>=m)//如果所加天数未超过当月剩余天数 { day=day+m; break; } m-=temp; day=0; month++; for(;month=m) { day+=m; break; } else { m-=d[month-1]; } } if(month0) { if(year%100==0) d[1]=(year%400==0?29:28); else d[1]=(year%4==0?29:28); if(day>=m) { day=day-m; break; } m-=day; month--; for(;month>=1;month--) { if(d[month-1]>=m) { day=d[month-1]-m; break; } else { m-=d[month-1]; } } if(month>=1) break; month=12; year--; day=31; }}intDate::diff(Date&q){ intdifferece; intd[12]={31,28,31,30,31,30,31,31,30,31,30,31},m,i; Datetemp; Date*pSmall,*pBig; if(year==)//如果年相同 { if(month==)//若月相同 { differece=; } else//月不同 { if(month>)//让pSmall指针指向小日期,pBig指针指向大日期 { pSmall=&q; pBig=this; } else { pSmall=this; pBig=&q; } >month; >day; for(i=1;imonth&&>day) { differece=i; break; } } differece=(month>); } } else//年份不同 { if(year>)//让pSmall指针指向小日期,pBig指针指向大日期 { pSmall=&q; pBig=this; } else { pSmall=this; pBig=&q; } >year; >month; >day; m=pBig->year-pSmall->year; for(i=(m-1)*365;imonth&&>day) { differece=i-(m-1)*365+1; break; } } differece=(year>); } returndifferece;}voidDate::display(){ printf("%d-%d-%d\n",year,month,day);}//主函数演示voidmain(){ Dated(2013,5,21); printf("原日期t1:"); (); (73); printf("t2=t1+73:"); (); (73); printf("t3=t2-73:"); (); printf("2013-5-21与2014-5-21日相差%d天\n",(Date(2014,5,21)));}主函数演示结果写了挺久,里面也用到一些比较巧妙的处理法,程序里也作了适当的注释,有问题可以问我,没问题的话望采纳!