# C语言习题 计算该日在本年中是第几天

C语言习题 计算该日在本年中是第几天

``2000 12 31``

``366``

``````#include<stdio.h>
struct y_m_d
{
int year;
int month;
int day;
};

int days(struct y_m_d date)
{
int day=0;
switch(date.month)
{
case 12:
day+=31;
case 11:
day+=30;
case 10:
day+=31;
case 9:
day+=30;
case 8:
day+=31;
case 7:
day+=31;
case 6:
day+=30;
case 5:
day+=31;
case 4:
day+=30;
case 3:
day+=31;
case 2:
if(date.year%400==0||(date.year%100!=0&&date.year%4==0))
day+=29;
else day+=28;
case 1:
day+=date.day;
}
return day;
}
int main()
{
y_m_d date;
int days(y_m_d);
int day_sum;
cin>>date.year>>date.month>>date.day;
day_sum=days(date);
cout<<day_sum<<endl;
return 0;
}
``````

``````#include <stdio.h>
struct y_m_d
{
int year;
int month;
int day;
};

int days(struct y_m_d date)        /* 定义days函数 */
{
int day_sum,i;
int day_tab[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
day_sum=0;
for (i=1; i<date.month; i++)
day_sum+=day_tab[i];
day_sum+=date.day;
if ((date.year%4==0 && date.year%100!=0 || date.year%4==0) && date.month>=3)
day_sum+=1;
return(day_sum);
}

int main()
{
struct y_m_d date;
int days(struct y_m_d);
int day_sum;
scanf("%d%d%d",&date.year,&date.month,&date.day);
day_sum=days(date);
printf("%d\n",day_sum);
return 0;
}``````