[求助]帮忙改正一道程序
这是判断一个的日期为该年第几天的程序原程序是一网友写的,但我发现有一些错误码,改动还是有一点问题,就是当你输入一个不存在的日期时,
它也依然会运行。如输入2006.11.31 会输出335。请高手帮忙改正,并说明为什么!
#include "stdio.h"
main ()
{
int year, month, day;
int total;
int leap;
printf ("enter year.month.day\n");
scanf ("%d.%d.%d.", &year, &month, &day);
if (month>12||day>31)
printf ("error");
else{
switch (month)
{
case 1: total=0+day; break;
case 2: total=31+day; break;
case 3: total=59+day; break;
case 4: total=90+day; break;
case 5: total=120+day; break;
case 6: total=151+day; break;
case 7: total=181+day; break;
case 8: total=212+day; break;
case 9: total=243+day; break;
case 10: total=273+day; break;
case 11: total=304+day; break;
case 12: total=334+day; break;
}
leap=year%4==0&&year%100!=0||year%400==0;
if (leap&&month>2)
total++;
printf ("%d", total);
}
}