编写日期类,要求按照xxxx-xx-xx的格式输出日期,实现加一天的操作。
#include <iostream>
#include<string.h>
using namespace std;
int daytab[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
bool IsLeapYear(int year)
{
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
else
return 0;
}
int main()
{
int year,month,day;
int number;//记录是第几天
int caseNumber;
scanf("%d",&caseNumber);
while(caseNumber--){
number=0;
scanf("%d%d%d",&year,&month,&day);
if(month==12&&day==31)//省去判断年
{
year++;month=1;day=1;
}
else{
int row=IsLeapYear(year);
for(int i=0;i<month;i++)
{
number+=daytab[row][i];
}
number+=day;
number++;
month=0;
while(number>daytab[row][month])
{
number-=daytab[row][month];
month++;
}
day=number;
}
printf("%04d-%02d-%02d",year,month,day);
}
}