判断输入字符串是否为浮点数

浮点数可以由以下的正则表达式确定:
 
white_space_opt        [[:space:]]*
digit_sequence        [[:digit:]]+
fractional_constant      {digit_sequence}\.?|\.{digit_sequence}|{digit_sequence}\.{digit_sequence}
exponent_part          [eE][+-]?{digit_sequence}
floating_constant    {fractional_constant}{exponent_part}*
accepted_input        {white_space_opt}[+-]?{floating_constant}{white_space_opt}
 
---------------------------------------------
如果不接受 1 2 3 这样的数为浮点数, 则fractional_constant应该是  {digit_sequence}\.|.....
如果不接受 浮点数两端加的空格,则 accepted_input 应该是 [+-]?{floating_constant}
---------------------------------------------
你可以用你稀饭的正则表达式库,如posix regex , pcre , boost::regex 啥的做这件事,也可以用其对应的 DFA 做判断, 比如这样
 
#include <stdio.h>
 
int  is_floating_constant(  const  char * input )
{
static  const  int  yy_accept[21] =
     {   0,
         0,    0,    3,    2,    2,    2,    2,    1,    0,    0,
         0,    1,    1,    1,    1,    0,    1,    0,    1,    0
     } ;
 
static  const  int  yy_ec[256] =
     {   0,
         1,    1,    1,    1,    1,    1,    1,    1,    2,    2,
         2,    2,    2,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    2,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    3,    1,    3,    4,    1,    5,    5,    5,
         5,    5,    5,    5,    5,    5,    5,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    6,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
 
         6,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
 
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1
     } ;
 
static  const  int  yy_meta[7] =
     {   0,
         1,    2,    1,    1,    3,    1
     } ;
 
static  const  int  yy_base[24] =
     {   0,
         0,    0,   27,   28,    5,    7,    0,   11,    0,    0,
         0,    0,   16,    0,   21,   20,    0,    0,   19,   28,
        17,   17,   11
     } ;
 
static  const  int  yy_def[24] =
     {   0,
        20,    1,   20,   20,   20,   20,   21,   20,    5,    6,
        21,    8,   20,   22,   13,   20,   15,   23,   15,    0,
        20,   20,   20
     } ;
 
static  const  int  yy_nxt[35] =
     {   0,
         4,    5,    6,    7,    8,    4,    9,   10,   11,   12,
        11,   12,   14,   19,   15,   12,   16,   14,   14,   13,
        13,   16,   18,   19,   19,   17,   20,    3,   20,   20,
        20,   20,   20,   20
     } ;
 
static  const  int  yy_chk[35] =
     {   0,
         1,    1,    1,    1,    1,    1,    5,    5,    5,    5,
         6,    6,    8,   23,    8,    8,    8,   13,   22,   21,
        13,   13,   16,   19,   16,   15,    3,   20,   20,   20,
        20,   20,   20,   20
     } ;
 
     enum  { last_dfa = 19 , jambase = 28 };
 
     int  curr = 1 , c ;
     do  {
         if ( 0 == *input )
             break ;
         c = yy_ec[*(unsigned  char *)input];
         if ( 1 == yy_accept[curr] && 0 == input[1] )
             return  1;
         while ( yy_chk[ yy_base[curr] + c ] != curr )
             if ( (curr=yy_def[curr] ) >= last_dfa + 2 )
                 c = yy_meta[c];
         curr = yy_nxt[ yy_base[curr] + c ];
         ++input;
     }   while ( yy_base[curr] != jambase );
 
     return  0;
}
 
int  main()
{
     char  input[80+1] =  "" ;
     while fgets ( input , 80 , stdin ) )
     {
         if ( is_floating_constant( input ) )
             printf "YES\n"  );
         else
             printf "NO\n"  );
     }
     return  0;
}


///////////////////////////////////////////////////////////////////////

唉,又错了,应该是下面的。。。
 
floating_constant    {fractional_constant}{exponent_part}?
 
对应的DFA表也要改为:
 
     static  const  int  yy_accept[21] =
     {   0,
     0,    0,    3,    2,    2,    2,    2,    1,    0,    0,
     0,    1,    1,    1,    1,    0,    1,    0,    1,    0
     } ;
     
     static  const  int  yy_ec[256] =
     {   0,
     1,    1,    1,    1,    1,    1,    1,    1,    2,    2,
     2,    2,    2,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    2,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    3,    1,    3,    4,    1,    5,    5,    5,
     5,    5,    5,    5,    5,    5,    5,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    6,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     
     6,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
     1,    1,    1,    1,    1
     } ;
     
     static  const  int  yy_meta[7] =
     {   0,
     1,    2,    1,    1,    3,    1
     } ;
     
     static  const  int  yy_base[24] =
     {   0,
     0,    0,   27,   28,    5,    7,    0,   11,    0,    0,
     0,    0,   16,    0,   21,   20,    0,    0,   22,   28,
     17,   17,   11
     } ;
     
     static  const  int  yy_def[24] =
     {   0,
     20,    1,   20,   20,   20,   20,   21,   20,    5,    6,
     21,    8,   20,   22,   13,   20,   15,   23,   23,    0,
     20,   20,   20
     } ;
     
     static  const  int  yy_nxt[35] =
     {   0,
     4,    5,    6,    7,    8,    4,    9,   10,   11,   12,
     11,   12,   14,   19,   15,   12,   16,   14,   14,   13,
     13,   16,   18,   14,   19,   17,   20,    3,   20,   20,
     20,   20,   20,   20
     } ;
     
     static  const  int  yy_chk[35] =
     {   0,
     1,    1,    1,    1,    1,    1,    5,    5,    5,    5,
     6,    6,    8,   23,    8,    8,    8,   13,   22,   21,
     13,   13,   16,   19,   16,   15,    3,   20,   20,   20,
     20,   20,   20,   20
     } ;
 
 
     enum  { last_dfa = 19 , jambase = 28 };


猜你喜欢

转载自blog.csdn.net/a_222850215/article/details/80213840