这题不想敲第二遍,居然“-.123”都算数值
class Solution {
public:
bool isNumeric(char* string)
{
int loc = 0;
while(string[loc] !=0){
if((string[loc]>='0'&&string[loc]<='9')||
string[loc]=='+' || string[loc] == '-'||
string[loc]=='.' || string[loc] == 'E'||
string[loc] == 'e'){
loc++;
continue;
}
return false;
}
int cnt = loc = 0;
while(string[loc] != 0){
if(string[loc] == '.') cnt++;
loc++;
}
if(cnt > 1) return false;
cnt = loc = 0;
while(string[loc] != 0){
if(string[loc] == 'e' || string[loc] == 'E') cnt++;
loc++;
}
if(cnt > 1) return false;
cnt = loc = 0;
while(string[loc] != 0){
if(string[loc] == '+' || string[loc] == '-') cnt++;
loc++;
}
if(cnt > 2) return false;
cnt = loc = 0;
while(string[loc] != 0){
if(string[loc] == '+' || string[loc] =='-'){
if(string[loc+1] == 0 || (string[loc+1] != '.' &&
(string[loc+1] <'0' || string[loc+1] > '9')))
return false;
}
if(string[loc] == '.'){
if(string[loc+1] == 0||
(string[loc+1] < '0' || string[loc+1] > '9')){
return false;
}
}
loc++;
}
cnt = loc = 0;
bool flag = false;
while(string[loc] != 0){
if(string[loc] == 'e' || string[loc] == 'E') flag = true;
if(string[loc] == '+' || string[loc] == '-') cnt++;
loc++;
}
if(!flag&&cnt==2) return false;
cnt = loc = 0;
flag = false;
while(string[loc] != 0){
if(string[loc] == 'e' || string[loc] == 'E'){
flag = true;
int a = loc-1;
int b = loc+1;
int cnt1 = 0;
int cnt2 = 0;
while(a >= 0){
if(string[a] == '+' || string[a] == '-'){
if(a!=0) return false;
cnt1++;
}
if(string[a] == '.') cnt2++;
a --;
}
if(cnt2 > 1 || cnt1 > 1) return false;
if(string[b] != '+' && string[b] != '-' &&
(string[b] < '0' || string[b] > '9')){
return false;
}
b++;
while(string[b] != 0){
if(string[b] < '0'||string[a] > '9'){
return false;
}
b++;
}
}
loc ++;
}
cnt = loc = 0;
while(string[loc] != 0){
if(string[loc] == 'e' || string[loc] == 'E'){
if(string[loc+1] == '0') return false;
}
if(string[loc] == '+' || string[loc] == '-'){
if(string[loc+1] == '0'){
if(string[loc+2] == 0||string[loc+2]!='.')
return false;
}
}
loc++;
}
cnt = loc = 0;
if(!flag){
while(string[loc] != 0){
if(string[loc] == '+' || string[loc] == '-'){
if(loc!=0) return false;
}
loc++;
}
}
return true;
}
};