问题描述
判
问题解析
接口设计:
int daysOfyymmdd(int year, int month, int day);
方法1:
int daysOfyymmdd(int year, int month, int day)
{
int sum=0; //利用switch语句的穿透性质
switch(month) //先计算某月以前月份的总天数
{
case 12:
sum=sum+30;
case 11:
sum=sum+31;
case 10:
sum=sum+30;
case 9:
sum=sum+31;
case 8:
sum=sum+31;
case 7:
sum=sum+30;
case 6:
sum=sum+31;
case 5:
sum=sum+30;
case 4:
sum=sum+31;
case 3:
if(IsLeapYear(year)==1)
{
sum=sum+29;
}
else
{
sum=sum+28;
}
case 2:
sum=sum+31;
case 1:
sum=sum+0;
default:
printf("data error");
}
sum=sum+day; //再加上某天的天数
return sum;
}
方法2:
int daysOfyymmdd(int year, int month, int day)
{
int sum=0;
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
for(int i=0;i<month-1;i++)
{
sum=sum+days[i];
}
if(IsLeapYear(year)==1&&month>2)//如果是闰年且月份大于2,总天数应该加一天
{
sum++;
}
sum=sum+day; //再加上某天的天数
return sum;
}
方法3:
int daysOfyymmdd(int year, int month, int day)
{
int sum=0;
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(IsLeapYear(year)==1)//判断是不是闰年
{
days[1]++;
}
for(int i=0;i<month-1;i++)
{
sum=sum+days[i];
}
sum=sum+day; //再加上某天的天数
return sum;
}
引入接口:
int IsLeapYear(int year);
方法1:
int IsLeapYear(int year)
{
int leap;
if(year%400==0||(year%4==0&&year%100!=0))//判断是不是闰年
{
leap=1;
}
else
{
leap=0;
}
return leap;
}
方法2:
int IsLeapYear(int year)
{
int leap=0;
if(year%400==0||(year%4==0&&year%100!=0))//判断是不是闰年
{
leap=1;
}
return leap;
}