编写一个函数days,实现计算该日在本年中的第几天?(注意闰年问题)要求:
(1) 定义一个结构体变量(包括年、月、日)
(2) 由主函数将年、月、日传递给days函数,计算后将日子数传会主函数输出。
输入: 2014 5 26 输出: 2014--5--26 is the 146th day in 2014.
#include<stdio.h>
int days(int year,int month,int day)
{
int i,sum=0;
for(i=1;i<month;i++)
{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:sum=sum+31;break;
case 4:
case 6:
case 9:
case 11:sum=sum+30;break;
case 2:if(year%4==0&&year%100!=0||year%400==0)
sum=sum+29;
else{
sum=sum+28;
break;
}
}
}
sum=sum+day;
return sum;
}
int main()
{
int year,month,day,total;
scanf("%d %d %d",&year,&month,&day);
total=days(year,month,day);
printf("%d--%d--%d is the %dth day in %d.",year,month,day,total,year);
}