case12
好像用不到
对以上程序,如果输入2004-1-4日会怎么样。是不是多算了一天?SB才会问2004-4-31
main()
{
int day,month,year,sum,leap;
printf("\\nplease input year,month,day\\n");
scanf("%d,%d,%d",&year,&month,&day);
switch(month){
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error");break;
}
sum=sum+day;
if(year%400==0||(year%4==0&&year%100!=0))
leap=1;
else
leap=0;
if(leap==1&&month>2)
sum++;
printf("It is the %dth day.",sum);}
^_^ case 12 是多余的....
if ((x.y%4==0)&&((x.y%400==0)||(x.y%100==1)==0))
润年的条件是符合下列条件之一..
1.能被4整除且被400整除.
2.能被4整除且不被100整除.
i替换成j ,,^_^ ...的确可以.
#define leep(year) (!(year%400)||(!(year%4)&&year%100))
main()
{
int sum;
struct { int y;
int m;
int d;
}sc;
printf ("Please Input Date (YYYY-MM-DD) : ");
scanf ("%d-%d-%d",&sc.y,&sc.m,&sc.d);
switch(sc.m) /*先计算某月以前月份的总天数*/
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59+leep(sc.y);break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error");break;
}
sum=sum+sc.d; /*再加上某天的天数*/
printf("It is the %dth day.",sum);
getch();
}
没有考虑输入数据的准确性等问题呵呵
还有那个使用次数问题我也没加进去运行通过了