所需头函数:
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
定义一个存储年月日的结构体
structdate {
intmonth;
intday;
intyear;
} ;
完整代码呈现
boolisLeap(structdated);
intnumberOfDays(structdated);
intmain(intargc, charconst *argv[])
{
structdatetoday, tomorrow;
printf("Enter today's date (mm dd yyyy):");
scanf("%i%i%i", &today.month, &today.day, &today.year);
通过三个判断语句来一点一点分析
if( today.day != numberOfDays(today) ) {
tomorrow.day = today.day+1;
tomorrow.month = today.month;
tomorrow.year = today.year;
} elseif (today.month == 12 ) {
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = today.year + 1;
} else {
tomorrow.day = 1;
tomorrow.month = today.month+1;
tomorrow.year = today.year;
}
printf("Tomorrow's date is %i-%i-%i.\n", tomorrow.year, tomorrow.month, tomorrow.day);
return0;
}
intnumberOfdays(structdated)
{
intdays;
constintdaysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ( d.month ==2 && isLeap(d) )
{
days = 29;
}else
{
days = daysPerMonth[d.month-1];
}
returndays;
}
boolisLeap(structdated)
{
boolleap = false;
if( (d.year %4 == 0 && d.year %100 !=0 ) || d.year%400 == 0 )
leap = true;
returnleap;
}