Advertisement

Valid Number用于检测数字有效性

阅读量:

问题:

验证一个给定的字符串是否为数字。

输入:字符串

输出:布尔类型的变量,用于表示该字符串是否为数字。

特殊案例:输入字符串"1 "," 0"," ."。

思路:需要明确数字的正确表达方式,并详尽地列举出所有可能的分支情况。通过调试过程中遇到的问题案例,对代码中的条件判断部分进行了优化与完善。

代码如下:

复制代码
     /** * @param s: the string that represents a number
    
      * @return: whether the string is a valid number
    
      */
    
     bool isNumber(string &s) {
    
     // write your code here
    
     bool pd = 1, pe = 1; 
    
     //pd, decimal point can occur? 1:0。 
    
     //pe, exponent symbol(e) can occur? 1:0。
    
     int sl = s.size(); // string length
    
     int i = 1; // in order to process front spaces

全部评论 (0)

还没有任何评论哟~