程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
one
#include
{
int year,month,day,days; int month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11; month1=31;month2=month1+28,month3=month2+31;month4=month3+30;month5=month4+31;month6=month5+30; month7=month6+31;month8=month7+31;month9=month8=30;month10=month9+31,month11=month10+30; printf("Please enter your birthday:"); scanf("%d.%d.%D",&year,&month,&day); switch(month) { case 1 : days = day; break; case 2 : days = month1 + day; break; case 3 : days = month2 + day; break; case 4 : days = month3 + day; break; case 5 : days = month4 + day; break; case 6 : days = month5 + day; break; case 7 : days = month6 + day; break; case 8 : days = month7 + day; break; case 9 : days = month8 + day; break; case 10 : days = month9 + day; break; case 11 : days = month10 + day; break; case 12 : days = month11 + day; break; default :printf("I can't solve it"); break;
}
if((year%4==0&&year%100!=0)||year%400==0)
{
if(month>=3)
days++;
}
printf("your birthday is %d",days of the year”,days);
return 0;
}
two:
#include
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)/*如果是闰年且月份大于2,总天数应该加一天*/sum++;
printf("It is the %dth day.",sum);
return 0;
}