#include
class date
{
int year;
int month;
int day;
public:
date(int y,int m,int d)
{
year=y;month=m;day=d;
}
void disp()
{
cout << year << "." << month << "." << day << endl;
}
friend int count_day(date &d,int);
friend int leap(int year);
friend int subs(date d1,date d2);
};
int count_day(date &d,int flag)
{
static int day_tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
int p,i,s;
if(leap(d.year))
p=1;
else
p=0;
if(flag)
{
s=d.day;
for(i=1;i
}
else
{
s=day_tab[p][d.month]-d.day;
for (i=d.month+1;i<=12;i++)
s+=day_tab[p][i-1];
}
return(s);
}
int leap(int year)
{
if(year%4==0 && year%100!=0 || year%400==0)
return 1;
else
return 0;
}
int subs(date d1,date d2)
{
int days,day1,day2,y;
if(d1.year
days=count_day(d1,0);
for(y=d1.year+1;y
days+=366L;
else
days+=365L;
days+=count_day(d2,1);
}
else if (d1.year==d2.year)
{
day1=count_day(d1,1);
day2=count_day(d2,1);
days=day2-day1;
}
else
{
days=count_day(d2,0);
for(y=d2.year+1;y
days+=366L;
else
days+=365L;
days+=count_day(d1,1);
}
return days;
}
void main()
{
date d1(2000,1,1),d2(2005,10,1);
int ds1=subs(d1,d2),ds2=subs(d2,d1);
cout << "日期d1:";d1.disp();
cout << "日期d2:";d2.disp();
cout << "d1和d2相距" << ds1 << "天" << endl;
cout << "d2和d1相距" << ds2 << "天" << endl;
}