hrbust 1555 正确的日期格式

正确的日期格式
Time Limit: 1000 MS Memory Limit: 10240 K
Total Submit: 495(110 users) Total Accepted: 129(89 users) Rating: Special Judge: No
Description
这个题目要你判断题目给的日期格式是否符合格式。

我们先定义日期的格式:yyyy-mm-dd,就是年-月-日,年份占四位,月份占两位,日占两位,不足的用前导0补齐,年月日之间以”-“分隔。

Input
有多组测试数据,每组测试数据占一行,一行除了换行符外,可能有0个字符或者更多字符,但是不超过255个字符。所给字符都是ASCII码表里可显示的字符。

处理到文件结束。

Output
输出一行,如果日期是正确的格式,则输出Yes,否则输出No。

Sample Input
1991-03-05
0-03-05
199999-333-55555
1991:03:05
Sample Output
Yes
No
No
No
Hint
yyyy不应该有”0000”这个情况。

感觉题目表意不清······这里坑点就是没告诉到底要判断到什么程度才算是正确,不只是格式和字符正确,日期也要有正确数值,平润年需要判断,月份和天数需要判断

#include<stdio.h>
#include<string.h>
int main()
{
    char a[300];
    int i;
    while(gets(a))///输入
    {
        bool flag=true;
        int n=strlen(a);
        if(n!=10)///长度不符,pass
        {
            printf("No
");
            continue;
        }
        if(a[4]!='-'||a[7]!='-')///横杠符号不符,pass
        {
            printf("No
");
            continue;
        }
        int sum=0;
        int year=0;
        int month=0;
        int day=0;
        for(i=0; i<10; i++)///分别从字符串中取出年 月 日,用数字存储
        {
            if(i==4)///到达第四位
            {
                 year=sum;///年份
                 sum=0;
                 continue;
            }
            if(i==7)///到达第七位
            {
                month=sum;///月份
                sum=0;
                continue;
            }
            if(a[i]<'0'||a[i]>'9')///判断日期内是否是正确数字字符
            {
                printf("No
");
                flag=false;
                break;
            }
            else
            {
                sum=sum*10+a[i]-'0';///化字符为数字的循环
            }
        }
        day=sum;
        if(year==0||month==0||day==0)///年月日都不能为数字0
        {
            printf("No
");
            continue;
        }
//        printf("%d %d %d
",year,month,day);
        if(flag&&(year%4==0&&year%100!=0||year%400==0))///平年闰年判断
        {
            if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            {
                if(day>=1&&day<=31)///各个月份天数判断
                {
                    printf("Yes
");
                }
                else
                {
                    printf("No
");
                }
            }
            else if(month==2||month==4||month==6||month==9||month==11)
            {
                if(month==2&&day<=29&&day>=1)
                {
                    printf("Yes
");
                }
                else if((month==4||month==6||month==9||month==11)&&day<=30&&day>=1)
                {
                    printf("Yes
");
                }
                else
                {
                    printf("No
");
                }
            }
            else
            {
                printf("No
");
            }
        }
        else if(flag)
        {
            if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            {
                if(day>=1&&day<=31)
                {
                    printf("Yes
");
                }
                else
                {
                    printf("No
");
                }
            }
            else if(month==2||month==4||month==6||month==9||month==11)
            {
                if(month==2&&day<=28&&day>=1)
                {
                    printf("Yes
");
                }
                else if((month==4||month==6||month==9||month==11)&&day<=30&&day>=1)
                {
                    printf("Yes
");
                }
                else
                {
                    printf("No
");
                }
            }
            else
            {
                printf("No
");
            }
        }
    }
    return 0;
}
//首先1:该字符串不能有除数字和 ‘ - ’以外的字符。年数,天数,日数不能全为零
//
//    2:有些月份的天数不能超过31,有些月份的天数不能超过30
//
//    3:平年闰年的区别
//
//    4:输入要用gets(坑点)
原文地址:https://www.cnblogs.com/kuronekonano/p/11794366.html