您的位置:首页 > 其它

if语句和switch语句 关于时间的三个问题

2012-11-28 03:38 447 查看
1 判断是否闰年

/*

*判断是否闰年。如果是,返回1;如果不是,返回0

*/

int checkyear(int year)

{

if(year%4!=0) return 0;

else

{

if(year%100==0)

{

if(year%400==0) return 1;

else return 0;

}

return 1;

}

}

2 判断一个月最大天数

/*

*获取每月最大天数,返回最大天数。如果该年是闰年,leap=1;否则leap=0

*/

int maxday(int leap,int mouth)

{

int max_day;

switch (mouth)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:max_day=31;break;

case 2:{

if(leap==1) max_day=29;

else max_day=28;

break;

}

default:max_day=30;

}

return max_day;

}

3 计算一天是一年中的第几天

int fun(int leap,int mouth,int date)

{

int n,sum=0;

for(n=1;n<=mouth;n++)

{

if(mouth==n)

{

sum=sum+date;

}

else

{

sum=sum+maxday(leap,n);

}

}

return sum;

}

int maxday(int leap,int mouth)

{

int max_day;

switch (mouth)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:max_day=31;break;

case 2:{

if(leap==1) max_day=29;

else max_day=28;

break;

}

default:max_day=30;

}

return max_day;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: