感觉原代码太墨迹,我这样写的
#include <stdio.h>
int main()
{
/* 定义需要计算的日期 */
int year = 2008;
int month = 8;
int day = 8;
int sum=0;
for(int i=1;i<month;i++){
if(i==2){
if(year%400==0 || (year%4==0 && year%100!=0)){
sum+=29;
}else{
sum+=28;
}
}else{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:sum+=31;break;
default:sum+=30;break;
}
}
}
printf("%d年%d月%d日是该年的第%d天",year,month,day,sum+day);
return 0;
}