using namespace std;
class Solution {
public:
bool scanUnsignedInteger(char** str){
const char* before = *str;
while(**str!='\0' && **str >= '0' && **str <= '9')
++(*str);
//当str中存在若干0-9的数字时,返回true
return *str > before;
}
bool scanInteger(char** str){
if(**str=='+' || **str == '-')
++(*str);
return scanUnsignedInteger(str);
}
bool isNumeric(char* string)
{
if(string == NULL)
return false;
bool numeric = scanInteger(&string);
//如果出现‘.’则接下来是数字的小数部分
if(*string=='.'){
++string;
//下面一行代码用||的原因
//1.小数后面可以没有数字,如.123等于0.123
//2. 2. 等于2.0
//3. 1.2
numeric = scanUnsignedInteger(&string) || numeric;
}
//如果出现‘e’或者'E',则接下来是数字部分
if(*string == 'e' || *string == 'E'){
++string;
//下面代码用&&的原因是:
//1.当e或者E前面没有数字时,整个字符串不能表示数字,如.e1、e1
//2.当e或者E后面没有整数时,整个字符串不能表示数字,如12e、12e+5.4
numeric = numeric && scanInteger(&string);
}
return numeric && *string == '\0';//是数字,且字符串结束。才能返回true
}
};
class Solution {
public:
bool scanUnsignedInteger(char** str){
const char* before = *str;
while(**str!='\0' && **str >= '0' && **str <= '9')
++(*str);
//当str中存在若干0-9的数字时,返回true
return *str > before;
}
bool scanInteger(char** str){
if(**str=='+' || **str == '-')
++(*str);
return scanUnsignedInteger(str);
}
bool isNumeric(char* string)
{
if(string == NULL)
return false;
bool numeric = scanInteger(&string);
//如果出现‘.’则接下来是数字的小数部分
if(*string=='.'){
++string;
//下面一行代码用||的原因
//1.小数后面可以没有数字,如.123等于0.123
//2. 2. 等于2.0
//3. 1.2
numeric = scanUnsignedInteger(&string) || numeric;
}
//如果出现‘e’或者'E',则接下来是数字部分
if(*string == 'e' || *string == 'E'){
++string;
//下面代码用&&的原因是:
//1.当e或者E前面没有数字时,整个字符串不能表示数字,如.e1、e1
//2.当e或者E后面没有整数时,整个字符串不能表示数字,如12e、12e+5.4
numeric = numeric && scanInteger(&string);
}
return numeric && *string == '\0';//是数字,且字符串结束。才能返回true
}
};