zoukankan      html  css  js  c++  java
  • 剑指offer-面试题20-表示数值的字符串-字符串

    /*
    题目:
    	判断字符串是否表示数值。
    */
    /*
    思路:
    	字符串遵循模式A[.[B]][e|EC] ,[+|-].B[e|EC]
    	A、C为可能带正负号的数字串
    	B为数字串
    */
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #include<cmath>
    #include<stdio.h>
    using namespace std;
    
    int index = 0;
    bool scanUnsignedInteger(char *str){
        if(str[index] >= '0' && str[index] <= '9'){
            while(str[index] >= '0' && str[index] <= '9'){
                index++;
            }
            if(str[index] != '.' && str[index] != 'e' && str[index] != '' && str[index] != 'E'){
                return false;
            }
            return true;
        }
        return false;
    
    }
    
    bool scanInteger(char* str){
        if(str[index] == '+' || str[index] == '-'){
            index++;
        }
        return scanUnsignedInteger(str);
    }
    
    bool isNumeric(char* str)
    {
         if(str == nullptr || *str == ''){
            return false;
         }
         bool numeric = true;
    
         if(str[index] != '.'){
            numeric = scanInteger(str);
            if(str[index] == '.'){
                index++;
                if(str[index] != 'e' && str[index] != 'E'){
                    numeric = scanUnsignedInteger(str);
                }
    
            }
         }else {
            index++;
            numeric = scanUnsignedInteger(str);
         }
         if((str[index] == 'e' || str[index] == 'E') && numeric){
            index++;
            numeric = scanInteger(str);
         }
         return numeric && str[index]=='';
    }
    
    
    int main(){
        char* str = "100";
        cout<<isNumeric(str)<<endl;
    }
    

       

  • 相关阅读:
    Django REST framework的解析器与渲染器
    python基础之 数据格式化
    REST framework 之 分页
    Django REST framework 之 认证 权限 限制
    DjangoRestFrameWork 版本控制
    DjangoRESTFrameWork中的视图
    浏览器跨域问题
    初识REST
    vue之生命周期
    vue组件
  • 原文地址:https://www.cnblogs.com/buaaZhhx/p/11892883.html
Copyright © 2011-2022 走看看