编程实现输入某年某月某日,计算这一天是这一年的第几天。
#include "stdio.h"
int getDays(int year ,int month,int date)
{
int months[13]={0,31,0,31,30,31,30,31,31,30,31,30,31} ,i,days=0;
if((year % 4 == 0 && year % 100 !=0)||(year % 400 ==0 )) /*判断是否是闰年*/
months[2]=29;
else
months[2]=28;
if(month <=0 || month >12 || date>months[month])
{
printf("It is error\n");
return -1;
} /*参数的有效性判断*/
for(i=1;i<month;i++) { /*计算天数*/
days = days + months[i];
}
days = days + date;
return days;
}
main()
{
printf("The days of 2th Dec 2015 is %d\n",getDays(2015,12,2));
getchar();
}