C语言编程:给定一个日期(年⼀月⼀日)计算该日期是所在年的第几天。

2024-11-30 14:32:29
推荐回答(1个)
回答1:

//1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊
//情况,闰年且输入月份大于3时需考虑多加一天。
#include

void main(){
int year,month,day,i,leap;
int fleap(int a);
printf("请输入年月日如(2009,1,1):");
scanf("%d,%d,%d",&year,&month,&day);
//printf("%d,%d,%d\n",year,month,day);
switch(month){
case 1:i=0;break;
case 2:i=31;break;
case 3:i=31+28;break;
case 4:i=31+28+31;break;
case 5:i=31+28+31+30;break;
case 6:i=31+28+31+30+31;break;
case 7:i=31+28+31+30+31+30;break;
case 8:i=31+28+31+30+31+30+31;break;
case 9:i=31+28+31+30+31+30+31+31;break;
case 10:i=31+28+31+30+31+30+31+31+30;break;
case 11:i=31+28+31+30+31+30+31+31+30+31;break;
case 12:i=31+28+31+30+31+30+31+31+30+31+30;break;}
leap=fleap(year);
if(leap==1)
{ i=i+day+1;

printf("\n\n\n\n\n %d年是润年\n",year);}
else {i=i+day;printf("\n\n\n\n\n %d年不是润年\n",year);}
printf("%d年%d月%d是%d年的第%d天",year,month,day,year,i);

getch();}

int fleap(int a){
int leap;
if(a%4==0)
{
if(a%100==0)
{
if(a%400==0)
leap=1;
else
leap=0;
}else leap=1;
}
else leap=0;

return(leap);}//判断润年的