|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
键盘上按照年月日的格式输入年份、月和日期,运行程序以后,判断这一天是这一年的第几天。
输入描述
标准输入,输入三个整数,即年、月、日(都是正数),以空格间隔,不保证输入的年份和日期是正确的。
输出描述
标准输出,输出这一天是这一年的第几天,如果日期不合法,输出“data error!”。#include<stdio.h>
int main()
{
int year,mon,day,days=0;
scanf("%d%d%d",&year,&mon,&day);
if(year>9999||year<1000) printf("date error!");
else
{if(((year%400==0)||(year%34==0&&year%100!=0))&&mon>2)
days++;
switch(mon)
{
case 1:
{
if(day>31) printf("data error!");
else
{days=day;printf("%d",days);}
break;
}
case 2:
{
if(((year%400==0||(year%4==0||year%100!=0))&&mon>29)||((year%400!=0||(year%4!=0||year%100==0))&&mon>28))printf("data error!");
else
{days=day+31;printf("%d",days);}
break;
}
case 3:
{
if(day>31) printf("data error!");
else
{days=day+59;printf("%d",days);}
break;
}
case 4:
{
if(day>30) printf("data error!");
else
{days=day+90;printf("%d",days);}
break;
}
case 5:
{
if(day>31) printf("data error!");
else
{days=day+120;printf("%d",days);}
break;
}
case 6:
{
if(day>30) printf("data error!");
else
{days=day+151;printf("%d",days);}
break;
}
case 7:
{
if(day>31) printf("data error!");
else
{days=day+181;printf("%d",days);}
break;
}
case 8:
{
if(day>31) printf("data error!");
else
{days=day+212;printf("%d",days);}
break;
}
case 9:
{
if(day>30) printf("data error!");
else
{days=day+243;printf("%d",days);}
break;
}
case 10:
{
if(day>31) printf("data error!");
else
{days=day+273;printf("%d",days);}
break;
}
case 11:
{
if(day>30) printf("data error!");
else
{days=day+304;printf("%d",days);}
break;
}
case 12:
{
if(day>31) printf("data error!");
else
{days=day+334;printf("%d",days);}
break;
}
default:printf("date error!");
}
}
}
这段代码有以下问题:
1. 第7行的输出应该是"data error!"而不是"date error!"
2. 第11行的年份除以34应该是除以4
3. 在每个case中都需要加上break语句,否则会继续执行下一个case
4. 在第24行到27行的公式中,应该是闰年的情况下mon>2,而不是mon>29或mon>28
5. 最后的default语句不需要输出任何内容
修改后的代码如下: ++#include <stdio.h>int main() { int year, mon, day, days = 0; scanf("%d%d%d", &year, &mon, &day); if (year > 9999 || year < 1000) printf("data error!"); else { if (((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) && mon > 2) days++; switch (mon) { case 1: { if (day > 31) printf("data error!"); else { days = day; printf("%d", days); } break; } case 2: { if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && day > 29 || (year % 400 != 0 || (year % 4 != 0 || year % 100 == 0)) && day > 28) printf("data error!"); else { days = day + 31; printf("%d", days); } break; } case 3: { if (day > 31) printf("data error!"); else { days = day + 59; printf("%d", days); } break; } case 4: { if (day > 30) printf("data error!"); else { days = day + 90; printf("%d", days); } break; } case 5: { if (day > 31) printf("data error!"); else { days = day + 120; printf("%d", days); } break; } case 6: { if (day > 30) printf("data error!"); else { days = day + 151; printf("%d", days); } break; } case 7: { if (day > 31) printf("data error!"); else { days = day + 181; printf("%d", days); } break; } case 8: { if (day > 31) printf("data error!"); else { days = day + 212; printf("%d", days); } break; } case 9: { if (day > 30) printf("data error!"); else { days = day + 243; printf("%d", days); } break; } case 10: { if (day > 31) printf("data error!"); else { days = day + 273; printf("%d", days); } break; } case 11: { if (day > 30) printf("data error!"); else { days = day + 304; printf("%d", days); } break; } case 12: { if (day > 31) printf("data error!"); else { days = day + 334; printf("%d", days); } break; } default: printf("data error!"); break; } } return 0;}
球一个最佳答案谢谢啦!这对我非常重要!
|
|