用函数调用判断年月日是否合法
#include<stdio.h>
int run(int y)
{
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) //判断是否为闰年
return 1;
return 0;
}
int f(int y, int m, int d)
{
if (y < 0 || m <= 0 || m > 12 || d <= 0||d>31 )
return 0;
if ((1 == m || 3 == m || 5 == m || 7 == m || 8 == m || 10 == m || 12 == m)&&(d<=31)) //区分30天和31天的月份
return 1;
if ((2 == m || 4 == m || 6 == m || 9 == m || 11 == m) &&(d==31))
return 0;
if (run(y))
{
if (2 == m && ( 30 == d || 31 == d))
return 0;
}
else if (2 == m && (29 == d || 30 == d || 31 == d)) //判断闰年二月份的天数
return 0;
}
int main()
{
int y = 0;
int m = 0;
int d = 0;
printf("请输入年月日,空格隔开: ");
scanf("%d %d %d", &y, &m, &d);
printf("%s\n", 1 == run(y) ? "是闰年" : "不是闰年");
printf("%s\n", 1 == f(y, m, d) ? "合法" : "不合法");
return 0;
}