任务05-05-13 日期识别3
怎么写,快崩溃了,不知道从哪下手。碰过类似的题,前面的题还好,到这里不会了。
供参考:
#include<stdio.h>
int is_leap_year(int y) // 判断是否闰年
{
return ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0);
}
int DaysOfMonth(int y, int m) // 某月的天数
{
int days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if (is_leap_year(y) && m == 2)
return days[m - 1] + 1;
return days[m - 1];
}
int is_valid_date(int y, int m, int d) //判断日期是否合法
{
if (y < 1 || y > 9999 || m < 1 || m > 12 || d < 1 || d > 31)
return 0;
return d <= DaysOfMonth(y, m);
}
void compare(int* a, int* b, int* c)
{
int t;
if (*a > *b) t = *a, * a = *b, * b = t;
if (*a > *c) t = *a, * a = *c, * c = t;
if (*b > *c) t = *b, * b = *c, * c = t;
}
int main()
{
int a, b, c, num = 0, valid_date[6][3] = { 0 };
scanf("%d%*c%d%*c%d", &a, &b, &c);
compare(&a, &b, &c);
if (is_valid_date(a, b, c)) {
valid_date[num][0] = a;
valid_date[num][1] = b;
valid_date[num][2] = c;
num++;
}
if (is_valid_date(a, c, b) && b != c) {
valid_date[num][0] = a;
valid_date[num][1] = c;
valid_date[num][2] = b;
num++;
}
if (is_valid_date(b, a, c) && a != b) {
valid_date[num][0] = b;
valid_date[num][1] = a;
valid_date[num][2] = c;
num++;
}
if (is_valid_date(b, c, a) && c != b) {
valid_date[num][0] = b;
valid_date[num][1] = c;
valid_date[num][2] = a;
num++;
}
if (is_valid_date(c, a, b) && a != c && b != c)
{
valid_date[num][0] = c;
valid_date[num][1] = a;
valid_date[num][2] = b;
num++;
}
if (is_valid_date(c, b, a) && a != c && a != b) {
valid_date[num][0] = c;
valid_date[num][1] = b;
valid_date[num][2] = a;
num++;
}
if (num == 0)
printf("Invalid Date!");
else {
printf("%d\n", num);
for (num--; num >= 0; num--) {
printf("%04d-%02d-%02d", valid_date[num][0], valid_date[num][1], valid_date[num][2]);
if (num) printf("\n");
}
}
return 0;
}