問題三:
輸入三個正整數,分別代表一個日期的年、月、日,計算並在屏幕上顯示輸入日期後一天的年、月、日。例如:
輸入:2007 2 28
輸出:2007 3 1
1)檢查年月日的合法性:如月份在[1, 12]范圍內取值,每個月份的日取值范圍為:
1, 3, 5, 7, 8, 10, 12: [1, 31]
4, 6, 9, 11: [1, 30]
2: 閏年:[1, 29],不是閏年:[1, 28]
2) 能正確處理邊界日期-每個月的最後一天
#include <stdio.h>
int isDate(int year,int month,int day)
{
if(month<1 || month>12 || day < 1 || day > 31 )
{
return 0;
}
if(month>7 && month%2==1 && day>30)
{
return 0;
}
if(month<7 && month%2==0 && day>30)
{
return 0;
}
if(!(year % 4 ==0 && year % 100 !=0 || year % 400==0))
{
if(month ==2 && day > 28)
{
return 0;
}
}
return 1;
}
void returnTomorrowDate(int year,int month,int day)
{
int newYear=year;
int newMonth=month;
int newDay=day;
if(isDate)
{
if(day<28)
{
newDay+=1
}
if(month>7 && month %2 ==0 && day <31) ||
(month < 7 && month % 2 == 1 && day <31 )
{
newDay +=1;
}
if((year % 4 ==0 && year % 100 !=0 || year % 400==0) && (month =2 && day <29)
{
newDay +=1;
}
if(month<11)
{
newMonth +=1;
newDay=1;
}
else
{
year+=1;
newMonth=1;
newDay=1;
}
}
printf("%d %d %d\n",newYear,newMonth,newDay);
}
void inputDate()
{
int year;
int month;
int day;
printf("plealse input your date\n");
scanf("%d %d %d",&year,&month,&day);
returnTomorrowDate(year,month,day);
}
void main()
{
inputDate()
}