本文是一篇关于数字空格的帖子

    标题:

    Validate if a given string is numeric.

    Some examples:

​"0"​​ => ​​true​

​" 0.1 "​​ => ​​true​

​"abc"​​ => ​​false​

​"1 a"​​ => ​​false​

​"2e10"​​ => ​​true​

    Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

    分析:此题的确很难,难点在于需要斟酌的情况太多,列举如下:

    1、空格不能涌现在数字之间

    2、点和e最多只能涌现一次

    3、涌现e之前必须有数字

    4、正负号要么涌现在数字的最前面,要么涌现在紧接着e前面

    5、e前面必须还得有数字

    6、正负号最多涌现两次

    代码如下:

    //注意:刚涌现的空格不用计数,直到数字、点、e或者正负号涌现后的空格才需要计数。


    每日一道理

当浮华给予我们过多欺骗,现实中的虚假几乎让我们忘却了真的存在,是真情唤回了迷离的心,是真情带给了我们最纯、最真的感觉,它流露的是美的誓言,渗透的是永恒执著的真爱。


        bool isNumber(const char *s) {

        if(s==NULL)return false;

        int num=0,numdot=0,nume=0,numkg=0,numsign=0;//分别表示数字、点、e、空格以及正负号的个数

        bool flag=false;

        while(*s!='\0')

        {

            if(int(*s-'0')>=0&&int(*s-'0')<=9)

            {

                if(numkg>0)

                {

                    return false;

                }

                flag=false;

                num++;

                s++;

            }

            else

            {

                if(*s=='.')

                {

                    if(numdot>0||numkg>0||nume>0)

                    {

                        return false;

                    }

                    numdot++;

                    s++;

                }

                else

                {

                    if(*s==' ')

                    {

                        if(num!=0||numdot!=0||nume!=0||numsign!=0)

                        {

                            numkg++;

                        }

                        s++;

                    }

                    else

                    {

                        if(*s=='e')

                        {

                            if(num<=0||nume>0||numkg>0)

                            {

                                return false;

                            }

                            s++;

                            nume++;

                            flag=true;

                        }

                        else

                        {

                            if(*s=='+'||*s=='-')

                            {

                                if((nume==0&&num>0)||(nume==0&&numsign>0)||(nume==0&&numdot>0))

                                {

                                    return false;

                                }

                                if(nume==1&&*(s-1)!='e')

                                {

                                    return false;

                                }                        

                                s++;

                                numsign++;

                            }

                            else

                            {

                                return false;

                            }

                        }

                    }

                }


            }

        }

        if(num<=0||flag)

        {

            return false;

        }

        return true;

    }

文章结束给大家分享下程序员的一些笑话语录: 手机终究会变成PC,所以ip会比wm更加畅销,但是有一天手机强大到一定程度了就会发现只有wm的支持才能完美享受。就好比树和草,草长得再高也是草,时间到了条件成熟了树就会窜天高了。www.ishuo.cn