c语言中给定日期求该天是当年的第几天的程序 注意闰年
发布网友
发布时间:2024-10-05 14:10
我来回答
共1个回答
热心网友
时间:2024-10-29 23:30
#include <stdio.h>
int main()
{
int y, m, d,n;
printf("Please enter the year, month, day: \n");
scanf("%d %d %d",&y, &m, &d);
if ((m > 12)||(d > 31)||(y < 0)||(m < 0)||(d < 0))
printf("Error input!!");
else
{
switch (m)
{
case 1:
n = d;
break;
case 2:
n = 31 + d;
break;
case 3:
n = 31 + 28 + d;
break;
case 4:
n = 31 + 28 + 31 + d;
break;
case 5:
n = 31 + 28 + 31 + 30 + d;
break;
case 6:
n = 31 + 28 + 31 + 30 + 31 + d;
break;
case 7:
n = 31 + 28 + 31 + 30 + 31 + 30 + d;
break;
case 8:
n = 31 + 28 + 31 + 30 + 31 + + 30 + 31 + d;
break;
case 9:
n = 31 + 28 + 31 + 30 + 31 + + 30 + 31 + 31 + d;
break;
case 10:
n = 31 + 28 + 31 + 30 + 31 + + 30 + 31 + 31 + 30 + d;
break;
case 11:
n = 31 + 28 + 31 + 30 + 31 + + 30 + 31 + 31 + 30 + 31 + d;
break;
default :
n = 31 + 28 + 31 + 30 + 31 + + 30 + 31 + 31 + 30 + 31+ 30 + d;
break;
}
if ((m>2)&&(y%4==0)&&(y%100!=0)||(y%400==0))
printf("%d月%d日是%d年的第%d天\n",m, d, y, n+1);
else
printf("%d月%d日是%d年的第%d天 \n",m, d, y, n);
}
return 0;
}
记得打赏啊 嘿嘿追问要用结构体呢?