采用的方法是“哨兵位”,浪费了一个空间
int Is_leap_year(int year)
{
if(year < 1)
{
return -1;
}
return (0 ==year % 4 && 0 != year % 100)||(0 == year % 400);//是闰年
}
int Get_month_day(int year,int month)
{
if(month < 1 || month > 12 || -1 == Is_leap_year(year))
{
printf("Get_month_day:error month\n");
return 0;
}
int day_month[13]={29,31,28,31,30,31,30,31,31,30,31,30,31};
if(Is_leap_year(year) && month == 2)
{
month = 0;
}
return day_month[month];
}
int Get_total_day(int year,int month,int day)
{
if(Get_month_day(year,month) == 0 || day < 1 || day > Get_month_day(year,month))
{
printf("Get_total_day:error data\n");
return -1;
}
int total[13]={0,0,31,59,90,120,151,181,212,243,273,304,334};
int sum = 0;
sum = total[month] + day;
if(month > 2 && Is_leap_year(year))
{
sum++;
}
return sum;
}
int main()
{
int year = 0;
int month = 0;
int day = 0;
printf("input year,month,day:");
scanf("%d %d %d",&year,&month,&day);
if(Is_leap_year(year) == -1)
{
printf("error year\n");
}
else if(Is_leap_year(year) == 0)
{
printf("not leap year\n");
}
else
{
int month_day = Get_month_day(year,month);
if(month_day > 0)
{
printf("day is %d\n",month_day);
int total = Get_total_day(year,month,day);
if(total != -1)
{
printf("total is %d\n",total);
}
}
}
return 0;
}
代码已经在VS-2012说明通过测试