剑指offer---表示数值的字符串

class Solution {  
public:  
    bool isNumeric(char* string)  
    {  
        if(string == NULL)  
            return false;  
        if(*string == '+' || *string == '-')  
            string++;  
        if(*string == '\0')  
            return false;  
        scanDigit(&string);  
        bool flag = true;  
        if(*string != '\0')  
        {  
            if(*string == '.')  
            {      
                string++;  
                scanDigit(&string);  
                if(*string == 'e' || *string == 'E')  
                    flag = isE(&string);  
            }  
            else  if(*string == 'e' || *string == 'E')      //整数  
                    flag = isE(&string);  
            else   
                flag = false;  
                  
        }  
         return flag && (*string == '\0');             //此处考虑1.2.3这个例子就知道为什么要加后一项  
          
    }  
    void scanDigit(char **string)  
    {  
        while(**string != '\0' && **string >='0' && **string <='9')  
            ++(*string);  
    }  
    bool isE(char **string)  
    {  
        ++(*string);  
        if(**string == '+' || **string == '-')  
            ++(*string);  
        if(**string == '\0')  
            return false;  
        scanDigit(string);  
        return (**string == '\0') ? true : false;  
          
    }  
};  

 

posted @ 2017-08-04 16:05  双马尾是老公的方向盘  阅读(174)  评论(0编辑  收藏  举报