程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Valid Number

Valid Number

編輯:C++入門知識

題目:


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


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved