牛客OJ:判断字符串是否为数值

这题不想敲第二遍,居然“-.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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值