//判断year是否是闰年
bool isLeapYear(int year)
{
return (year%400==0 || (year%4==0 && year%100!=0));
}
//日期类
class Date
{
private:
int year;
int month;
int day;
public:
Date()
{}
Date(int year, int month, int day)
{
this->year = year;
this->month = month;
this->day = day;
}
//设置日期
void setDate(int year, int month, int day)
{
this->year = year;
this->month = month;
this->day = day;
}
//在当前日期的基础上增加一天
void addOneDay()
{
day++;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
if(day > 31)
{
day -= 31;
month++;
}
break;
}
case 4:
case 6:
case 9:
case 11:
{
if(day > 30)
{
day -= 30;
month++;
}
break;
}
case 2:
{
if(isLeapYear(year))
{
if(day > 29)
{
day -= 29;
month++;
}
}
else
{
if(day > 28)
{
day -= 28;
month++;
}
}
break;
}
default:
{
break;
}
}
}
//输出函数,输出格式为:XXXX年XX月XX日,如2010年4月11日
void display()
{
cout<
};
//输入函数,输入格式为:XXXX XX XX,如2010 4 13
Date InputDate()
{
int year, month, day;
Date now = Date();
cin>>year>>month>>day;
now.setDate(year, month, day);
return now;
}
void main()
{
Date now = Date();
cout<<"请输入日期(XXXX XX XX)";
now = InputDate();
cout<<"日期:";
now.display();
now.setDate(2011, 5, 31);
cout<<"新日期:";
now.display();
now.addOneDay();
cout<<"一天后:";
now.display();
}