请大家帮我看看错误在哪!
#include <iostream>using namespace std;
class Date
{
public: Date(int,int,int);
void addMonth(int);
void addYear(int);
void addDay(int);
void redMonth(int);
void redYear(int);
void redDay(int);
void print();
private:
int year,month,day;
};
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void Date::redYear(int yy)
{
year-=yy;
if(!(year%4==0&&year%100!=0||year%400==0))
if(month==2&&day==29)
day=28;
}
void Date::redMonth(int mm)
{
month-=mm;
if(year%4==0&&year%100!=0||year%400==0)
{
if(month==2&&day>29)
day=29;
}
else if(month==2&&day>28)
day=28;
if(month<1)
{
year=year-(-(month-13))/12;
month=12-(-(month-1))%12;
}
if(month==4||month==6||month==9||month==11)
if(day>30)
day=30;
}
void Date::redDay(int dd)
{
day-=dd;
if(day<=0)
month--;
while(day<=0)
{
if(day>=0&&(++month)==13)
month=0;
if(year%4==0&&year%100!=0||year%400==0)
{
if((++month)==2&&day>=0)
day=29-day;
}
else if((++month)==2&&day>=0)
day=28-day;
if((++month)==1||(++month)==3||(++month)==5||(++month)==7||(++month)==8||(++month)==10||(++month)==12)
if(day>=0)
day=31-day;
if((++month)==4||(++month)==6||(++month)==9||(++month)==11)
if(day>=0)
day=30-day;
switch(month)
{
case 12:
if(day<0)
{day+=31;month--;}
else
break;
case 11:
if(day<0)
{day+=30;month--;}
else
break;
case 10:
if(day<0)
{day+=31;month--;}
else
break;
case 9:
if(day<0)
{day+=30;month--;}
else
break;
case 8:
if(day<0)
{day+=31;month--;}
else
break;
case 7:
if(day<0)
{day+=31;month--;}
else
break;
case 6:
if(day<0)
{day+=30;month--;}
else
break;
case 5:
if(day<0)
{day+=31;month--;}
else
break;
case 4:
if(day<0)
{day+=30;month--;}
else
break;
case 3:
if(day<0)
{day+=31;month--;}
else
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0)
{
if(day<0)
{day+=29;month--;}
else
break;
}
else if(day<0)
{day+=28;month--;}
else
break;
default:
if(day<0)
{day+=31;month=12;year--;}
}
}
}
void Date::addDay(int dd)
{
day+=dd;
while(1)
{
if(year%4==0&&year%100!=0||year%400==0)
{
if(month==2&&day<29)
break;
}
else
{
if(month==2&&day<28)
break;
}
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(day<31)
break;
}
else
{
if(day<30)
break;
}
switch(month)
{
case 1:
if(day>31)
{day-=31;month++;}
else
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0)
{if(day>29)
{day-=29;month++;}
else
break;
}
else
{if(day>28)
{day-=28;month++;}
else
break;
}
case 3:
if(day>31)
{day-=31;month++;}
else
break;
case 4:
if(day>30)
{day-=30;month++;}
else
break;
case 5:
if(day>31)
{day-=31;month++;}
else
break;
case 6:
if(day>30)
{day-=30;month++;}
else
break;
case 7:
if(day>31)
{day-=31;month++;}
else
break;
case 8:
if(day>31)
{day-=31;month++;}
else
break;
case 9:
if(day>30)
{day-=30;month++;}
else
break;
case 10:
if(day>31)
{day-=31;month++;}
else
break;
case 11:
if(day>30)
{day-=30;month++;}
else
break;
default:
if(day>31)
{day-=31;month=1;year++;}
}
}
}
void Date::addMonth(int mm)
{
month+=mm;
if(year%4==0&&year%100!=0||year%400==0)
{
if(month==2&&day>29)
day=29;
}
else if(month==2&&day>28)
day=28;
if(month>12)
{
year=year+month/12;
month%=12;
}
if(month==4||month==6||month==9||month==11)
if(day>30)
day=30;
}
void Date::addYear(int yy)
{
year+=yy;
if(!(year%4==0&&year%100!=0||year%400==0))
if(month==2&&day==29)
day=28;
}
void Date::print()
{
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
int main()
{
Date date1(2012,1,10);
date1.redDay(20);
date1.print();
return 0;
}最后输出不是我想要的数据。。。