日期类的问题
谁能给我写一个日期类啊 要明确点的 谢谢了
#include <iostream.h>
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<d.month;i++)
s+=day_tab[p][i-1];
}
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<d2.year)
{
days=count_day(d1,0);
for(y=d1.year+1;y<d2.year;y++)
if(leap(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<d1.year;y++)
if(leap(y))
days+=366L;
else
days+=365L;
days+=count_day(d1,1);
}
return days;
}
void main()
{
date d1(2010,3,24),d2(2008,5,12);
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;
}
//这是关于计算日期的类