qq_24371913 于 2014.12.09 20:17 提问

c语言 判断输入的值是否为浮点数

1.可能输入字符串
2.可能输入ip地址

9个回答

habit_reflection   2014.12.09 21:59

wangyaninglm      2014.12.09 20:41
``````

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}

---------------------------------------------

---------------------------------------------

#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;
}

``````
wangyaninglm      2014.12.09 20:41
``````

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}

---------------------------------------------

---------------------------------------------

#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;
}

``````
fenujing   2014.12.09 20:41

eagleyan   2014.12.10 03:42

http://www.lemoda.net/c/unix-regex/

xiangshimoni   2014.12.10 13:25

w810443616   2014.12.10 16:54

xiaohuoxiaoer   2014.12.11 10:44

zxh707wk   2014.12.11 20:05
`````` #include <stdio.h>

int main()
{
float num;
scanf("%f",&num);
if(getchar()!='\n')
{
printf("输入错误\n");
}
else
{
if((int)num*1.0!=num)printf("输入错误\n");
else printf("输入正确\n");
}
return 0;
}

``````