回复 4楼 xufan
能帮忙改下这个么?运行出来是第二行有错
#include<iostream.h>
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 year,month,day
Int leap(int);
Int dton(Date&)
Date ntod(int)
public:
Date()
Date (int y,int mint d) l year = y;month = m;day = d;
void setday(int d) day = d;
void setmonth(int m) month = m;
void setyear(int y) year = y;
int getday() return day;
int getmonth() return month;
int getyear() return year;
Date operator + (int days)
static Date date;
int number = dton(*this)+days
date = ntod(number)
return date;
Date operator - (int days)
staffs Date date;
int number = dton(*this)
number - = days;
date = ntod(number)
return date;
int operator - (Date &b)
int days = dton(*this)-(dton(b)-1);
return days;
void disp()
cout<<year<<""<<month<<""<<day<<endl;
int Date: :leap(int year)
if year%4 == 0 && year%1O0! = 0||year%400 = 0
return 1;
else
return 0;
int Date: :dton(Date &b)
inty,m,days = 0;
for(y = 1;y <= d.year;y++)
if leap(y) days + = 365;
else days + = 365;
for(m = 0;m < d.month-1;m++)
if leap(d.year)
days + = day_tab[1][m];
else
days + = day_tab[0][m];
days + = d.day;
return days;
Date Date::ntod(intn)
Int y = 1,m = 1,d rest = n lp;
while(1)
if(leap(y))
if(rest < = 366)break;
else rest - = 366;
else
if(rest = 365)berak;
else rest - = 365;
y++;
y--;
lp = leap(y);
while(1)
if(lp)
if(rest > day_tab[1][m-1])rest -= day_ tab[1][m-1];
else break;
else
if(rest > day_tab[0][m-1])rest -= day_ tab[0][m-1];
else break;
m++;
d= rest;
reyurn Date(y,m,d);
void main()
Date now(2003,10,1),then(2005,6,5)
cout < <"now:";now.disp();
cout < <"then:"; then.disp();
cout < <"相差天数:"< <(then-now)< < endl;
Date dl = now + 1000.d2 = now - 1000;
cout < <"now + 1000:";d1.disp();
cout < <"now + 1000:";d2.disp();