poj 3332 Parsing Real Numbers

发布于 2020-02-11 23:52:06

Write a program that read a line of input and checks if the line contains a valid real number. Real numbers may have a decimal point, an exponent (starting with the character e or E), or both. Additionally, it has the usual collection of decimal digits. If there is a decimal point, there must be at least one digit on each side of the point. There may be a plus or minus sign in front of the number, or the exponent, or both (without any blank characters after the sign). Exponents are integers (not having decimal points). There may be blank characters before or after a number, but not inside it. Note that there is no bound on the range of the numbers in the input, but for the sake of simplicity, you may assume the input strings are not longer than 1000 characters.

输入描述

The first line of the input contains a single integer T which is the number of test cases, followed by T lines each containing the input line for a test case.

输出描述

The output contains T lines, each having a string which is LEGAL or ILLEGAL.

输入例子
2
  1.5e+2
3.
输出例子
LEGAL
ILLEGAL
关注者
0
被浏览
1272
1 个回答
  • 面试哥
    面试哥 2020-02-11
    为面试而生,有面试问题,就找面试哥。
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    #define MAXD 1010
    int n, pat;
    char b[MAXD];
    void init()
    {
        int i, j, k, p;
        gets(b);
        for(i = 0; b[i] == ' '; i ++);
        for(j = strlen(b) - 1; b[j] == ' '; j --);
        for(k = 0, p = i; p <= j; p ++, k ++)
            b[k] = b[p];
        b[k] = '\0';
        n = k;
    }
    void pattern(int i)
    {
        if(pat == 0)
        {
            if(b[i] == '+' || b[i] == '-')
                pat = 1;
            else
                pat = 2;
        }
        else if(pat == 1)
        {
            pat = 2;
        }
        else if(pat == 2)
        {
            if(b[i] == '.')
                pat = 3;
            else if(b[i] == 'e' || b[i] == 'E')
                pat = 5;
        }
        else if(pat == 3)
        {
            pat = 4;
        }
        else if(pat == 4)
        {
            if(b[i] == 'e' || b[i] == 'E')
                pat = 5;
        }
        else if(pat == 5)
        {
            if(b[i] == '+' || b[i] == '-')
                pat = 6;
            else
                pat = 7;
        }
        else if(pat == 6)
        {
            pat = 7;
        }
    }
    int check(int i)
    {
        if(pat == 1)
        {
            if(b[i] == '+' || b[i] == '-')
                return 1;
            else
                return 0;
        }
        else if(pat == 2)
        {
            if(isdigit(b[i]))
                return 1;
            else
                return 0;
        }
        else if(pat == 3)
        {
            if(b[i] == '.')
                return 1;
            else
                return 0;
        }
        else if(pat == 4)
        {
            if(isdigit(b[i]))
                return 1;
            else
                return 0;
        }
        else if(pat == 5)
        {
            if(b[i] == 'e' || b[i] == 'E')
                return 1;
            else
                return 0;
        }
        else if(pat == 6)
        {
            if(b[i] == '+' || b[i] == '-')
                return 1;
            else
                return 0;
        }
        else
        {
            if(isdigit(b[i]))
                return 1;
            else
                return 0;
        }
    }
    int solve()
    {
        int i;
        pat = 0;
        for(i = 0; i < n; i ++)
        {
            pattern(i);
            if(!check(i))
                return 0;
        }
        if(pat == 2 || pat == 4 || pat == 7)
            return 1;
        else
            return 0;
    }
    int main()
    {
        int t;
        gets(b);
        sscanf(b, "%d", &t);
        while(t --)
        {
            init();
            if(solve())
                printf("LEGAL\n");
            else
                printf("ILLEGAL\n");
        }
        return 0;
    }
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看