请用循环语句进行程序设计(也可能用到if或switch语句),实现从键盘上输入一个日期,日期格式为:2018-7-9,判断这一天是这一年中的第几天。 注:本题涉及到闰年、平年的判断,已知符合下列条件之一者是润年: (1)能被4整除,但不能被100整除; (2)能被400整除。 输入提示信息:"Please Input the Date:\n" 输入格式:"%d-%d-%d" 输出格式:"Result=%d\n" 如果输入月份不在1到12月份之间输出错误提示信息"Input error!\n" (注:为了便于实现,本题暂不考虑日的合法性)
可以考虑用for循环的switch嵌套语句实现
代码如下:
#include<stdio.h>
int main()
{
int year,month,day,days=0;
scanf("%d-%d-%d",&year,&month,&day);
printf("Please Input the Date:\n");
if(month<=12&&month>=3)
{
for(int i=1;i<month;i++)
{
switch(i)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:days+=31;break;
default:break;
}
switch(i)
{
case 4:case 6:case 9:case 11:days+=30;break;
default:break;
}
}
if((year%4==0&&year%100!=0)||(year%400==0))
{
days+=29+day;
}
else days+=28+day;
printf("Result=%d\n",days);
}
else if(month==1) printf("Result=%d\n",day);
else if(month==2) printf("Result=%d\n",day+31);
else printf("Input error!\n");
return 0;
}
需要注意的是: