计算该日是该年的第几天
/*
Date: 07/03/19 21:10
Description: 主要是闰年的判断和2月份的天数的处理
*/
1 #include<stdio.h> 2 int sumday(int month,int day); 3 int leap(int year); 4 int main(void) 5 { 6 int day,month,year,days; 7 printf("Please input date:\n"); 8 printf("year-month-day\n"); 9 scanf("%d-%d-%d",&year,&month,&day); 10 days=sumday(month,day); 11 if(leap(year)&&month>=3)//是闰年,且超过三月 12 days=days+1; 13 printf("%d-%d-%d is the %dth day in this year.\n",year,month,day,days); 14 return 0; 15 } 16 int leap(int year) 17 { 18 int leap=0; 19 if(year%4==0&&year/100!=0||year%400==0) 20 leap=1;//是闰年返回1 21 return leap; 22 } 23 int sumday(int month,int day) 24 { 25 int day_table[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 26 for(int i=1;i<month;i++) 27 day+=day_table[i]; 28 return day; 29 }
运行结果: