bool IsNumber(const char *s)
{
bool num = false;
bool dot = false;
bool exp = false;
bool spa = false;
while(*s != '\0' && *s == ' ' ) ++s;
if (*s!='\0' && (*s == '+' || *s == '-'))++s;
while(*s != '\0')
{
if(*s == ' ') spa = true;
else if(spa) return false;
else if (*s >= '0' && *s <= '9')
num = true;
else if(*s == 'e')
{
if(exp || !num) return false;
exp = true;
num = false;
}
else if(*s == '.')
{
if(exp || dot) return false;
dot = true;
}
else if(*s == '-' || *s == '+')
{
if (*(s-1) != 'e') return false;
}
else
return false;
++s;
}
return num;
}
#include <stdio.h>
int main(int argc, char** argv)
{
char s1[] = "abc";
char s2[] = " +1.e-2 ";
char s3[] = " .0e200";
char s4[] = "0+00";
printf("%s\n", IsNumber(s1)?"true":"false");
printf("%s\n", IsNumber(s2)?"true":"false");
printf("%s\n", IsNumber(s3)?"true":"false");
printf("%s\n", IsNumber(s4)?"true":"false");
return 0;
}