解题思路:
       笨方法,即考虑到所有错误的情况
注意事项:

参考代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 30
int main()
{
    char ch[N][N];
    int i,j;
    int m=0;
    int k,p;
    int n;
    int q;
    printf("Input character string:\n");
    gets(ch[m]);
    while(strcmp(ch[m],"End of file")!=0)
    {
        m++;
        gets(ch[m]);
    }
    for(i=0;i<m;i++)
    {
        n=0;
        p=0;
        q=0;
        for(j=0;ch[i][j]!='\0';j++)
        {
            if((ch[i][j]<48||ch[i][j]>57)&&(ch[i][j]!='.'))

           //不是数字也不是‘.’,第一道防线
            {
                printf("N\n");
                n=1;
                break;
            }
        }
        if(n==1)
            continue;
        for(j=0;ch[i][j]!='\0';j++)
        {
            if(ch[i][j]=='.')

         //当为.的时候
            {
                if(ch[i][j+1]=='.')

         //两个‘.’不能连续
                {
                    n=1;
                    printf("N\n");
                    break;
                }
            }
        }
        if(n==1)
            continue;
        for(j=0;ch[i][j]!='\0';j++)
        {
            if(ch[i][j]=='.')//当为.的时候
            {
                p++;
            }
        }
        if(p!=3)

        //‘.’的个数不为3,说明整数个数不为4
            {
                printf("N\n");
            }
        if(p!=3)
            continue;
        if(ch[i][0]=='.')     //第一个元素不能为'.'
        {
            n=1;
        }
        //以下单独讨论第一段整数的情况

        if(ch[i][0]=='0'&&ch[i][1]!='.'&&(ch[i][2]=='.'||ch[i][3]=='.'))

        //前导为0

        {
            n=1;
        }
        while(ch[i][q]!='.')
            {
                q++;
            }
        if(q>3)      //超过3位

        {
            n=1;
        }
        if(q==3)   //正好3位,判断是否超限
        {
            if(ch[i][0]>'2')
                n=1;
            else if(ch[i][0]=='2'&&ch[i][1]>'5')
                n=1;
            else if(ch[i][0]=='2'&&ch[i][1]=='5'&&ch[i][2]>'5')
                n=1;
        }

        //第一段整数讨论完毕  

       //接下来讨论第2至4段整数,每一段整数的前一位都为'.'

        for(j=0;ch[i][j]!='\0';j++)
        {
            k=1;
            if(ch[i][j]=='.')//当为.的时候,下一位应当是整数
            {
                while(ch[i][j+k]!='.'&&ch[i][j+k]!='\0')
                {
                    k++;
                }
                if(k>4||k==1)//整数为4位以上的数,或没有整数
                {
                    n=1;
                    break;
                }
                else if(k<=4)
                {
                    if(k>2)
                    {
                        if(ch[i][j+1]=='0')//前导为0
                        {
                            n=1;
                            break;
                        }
                        if(k==4)//三位数
                        {
                            if(ch[i][j+1]>'2')
                            {
                                n=1;
                                break;
                            }
                            else if(ch[i][j+1]=='2'&&ch[i][j+2]>'5')
                            {
                                n=1;
                                break;
                            }
                            else if(ch[i][j+1]=='2'&&ch[i][j+2]=='5'&&ch[i][j+3]>'5')
                            {
                                n=1;
                                break;
                            }
                        }
                    }
                }
            }
        }
        if(n==1)
        {
            printf("N\n");
        }
        if(n==0)
        {
            printf("Y\n");
        }
    }
    return 0;
}

 

0.0分

0 人评分

  评论区

  • «
  • »