按yyyy-mm-dd格式输入一个日期,如2017-10-19,判断这是这一年的第几天。本程序需要进行日期合法性检查。
【输入形式】按yyyy-mm-dd格式输入一个日期,比如2017-10-19
【输出形式】yyyy-mm-dd is the %dth day of yyyy
【样例输入】:Input date like yyyy-mm-dd:2017-10-19
【样例输出】:2017-10-19 is the 292th day of 2017
#include<stdio.h>
int main()
{
int a,b,c,e=0,f,k;
printf("Input date like yyyy-mm-dd:");
scanf("%d-%d-%d",&a,&b,&c);
if(b>=13)
k=9;
else
k=0;
if((b==4)||(b==6)||(b==9)||(b==11))
{ if(c>=31)
k=4;
else
k=0;
}
else if((b==1)||(b==3)||(b==5)||(b==7)||(b==8)||(b==10)||(b==12))
{ if(c>=32)
k=3;
else
k=0;
}
else if(b==2)
{if((a%4==0&&a%100!=0)||(a%400==0))
{if(c>=30)
k=1;
else
k=0;
}
else
{if(c>=29)
k=2;
else
k=0;
}
}
switch(b)
{
case 12:e=e+30;
case 11:e=31+e;
case 10:e=e+30;
case 9:e=e+31;
case 8:e=e+31;
case 7:e=e+30;
case 6:e=e+31;
case 5:e=e+30;
case 4:e=e+31;
case 3:e=e+28;
case 2:e=e+31;
case 1:e=e;
}
f=e+c;
if(((a%4==0&&a%100!=0)||(a%400==0))&&(b>2))
f=f+1;
if(k==0)
printf("%d-%d-%d is the %dth day of %d",a,b,c,f,a);
else
printf("Error date!\n");
return 0;
}