Prove safety offer53: a numerical value string, the string "+100", "5e2", "- 123", "3.1416" and "-1E-16" shows the value. But "12e", "1a3.14", "1.2.3", "+ - 5" and "12e + 4.3" is not

1 Title Description

  Implement function used to determine whether a string represents a value (including integer and fractional). For example, the string "+100", "5e2", "- 123", "3.1416" and "-1E-16" shows the value. But "12e", "1a3.14", "1.2.3", "+ - 5" and "12e + 4.3" neither.

2 ideas and methods

  (1) follows the pattern string representing the value A [[B].] [ E | EC] or [B]. [E | EC] , where A is the integer part of the value, B is the value of the fractional part after the decimal point, C followed by 'e' and 'E' is the value of exponent part. Note: There can be no number before the decimal point numbers.

    The entire three-step process

    A) the integer part of the value of the first scan; the first sign is first taken, the integer portion of the scan A

    2) If you experience decimal point '.', Begins scanning fractional part B

    3) If you have 'e' and 'E', directly under the portion C starts scanning

  (2) Note that rule represents a numeric string to follow; may have a numerical value before the "+" or "-" followed by the digits 0 to 9 denotes the integer part of the value, if the value is a decimal, then after the decimal point there may be a plurality of digits 0 to 9, the value of the fractional part. If expressed in scientific notation, followed by an 'e' or 'E', and followed by an integer (which may have a sign) indicates the index.

3 C ++ core code

(1)

. 1  class Solution {
 2  public :
 . 3      int index = 0 ;
 . 4      BOOL that isNumeric ( char * String )
 . 5      {        
 . 6          IF (! String ) return  to false ;
 . 7          // digital format can be A [e | [[B] .] EC] or .B [e | EC], where a and C are
 8          // integers (sign can have, or may not), and B is an unsigned integer 
. 9          BOOL In Flag scanInteger = ( String );
 10          IF (index <strlen ( String ) && String[index] == ' . ' ) {
 . 11              index ++ ;
 12 is              In Flag scanUnsignedInteger = ( String ) || In Flag; // . || reasons used here, the latter can not follow the foregoing figures. Note: This flag must be placed back ||! ! 
13 is          }
 14          IF (index <strlen ( String ) && ( String [index] == ' E ' || String [index] == ' E ' )) {
 15              index ++ ;
 16              In Flag && scanInteger In Flag = ( String ); / /&& is used here reason, e | E must have a front of the back number 
. 17          }
 18 is          return In Flag == index && strlen ( String ); // NOTE: index == strlen (string) must be added !! counterexample 1a123 
. 19      }
 20 is      BOOL scanInteger ( char * String ) { // scan + or - or empty initial digits 0-9, corresponding to a, C 
21 is          IF (index <strlen ( String ) && ( String [index] == ' + ' || String [index] == ' - ' ))
 22 is              index ++ ;
 23 is          returnscanUnsignedInteger ( String ); // to true indicates the presence of A or C 
24      }
 25      BOOL scanUnsignedInteger ( char * String ) { // scanning of digits 0-9, corresponding to B 
26 is          int Start = index;
 27          the while (index <strlen ( String ) && String [index]> = ' 0 ' && String [index] <= ' . 9 ' )
 28              index ++ ;
 29          return index> Start; // to true indicating the presence of B 
30     }
31 
32 };
View Code

(2)

1  // Note represents a numeric string to follow the rules;
 2  // before value may have a "+" or "-" followed by the digits 0 to 9 denotes the integer part of the value, if the value is a decimal, then after the decimal point may have several digits 0 to 9
 3  // fractional part of the value. If expressed in scientific notation, followed by an 'e' or 'E', and followed by an integer (which may have a sign) indicates the index. 
. 4  class Solution {
 . 5  public :
 . 6      BOOL that isNumeric ( char * String )
 . 7      {
 . 8          IF ( String == NULL or * String == ' \ 0 ' )
 . 9              return  to false ;
 10          IF (* String=='+'||*string=='-')
11             string++;
12         int dot=0,num=0,nume=0;
13         while(*string != '\0'){
14             if(*string>='0' && *string<='9'){
15                 string++;
16                 num =one ;
17              }
 18              else  if (* string == ' . ' ) {
 19                  if (dot> 0 || name> 0 )
 20                      return  false ;
21                  string ++ ;
22                  dot = 1 ;
23              }
 24              else  if (* string == ' e ' || * string == ' E ' ) {
 25                 if (name> 0 || num == 0 )
 26                      return  false ;
27                  string ++ ;
28                  name ++ ;
29                  if (* string == ' + ' || * string == ' - ' )
 30                      string ++ ;
31                  if (* string == ' \ 0 ' )
 32                      return  false ;
33              }
 34             else
35                 return false;
36         }
37         return true;
38     }
39 };
View Code

Reference material

https://blog.csdn.net/fuqiuai/article/details/88198099

https://blog.csdn.net/u012477435/article/details/83351659#_873

Guess you like

Origin www.cnblogs.com/wxwhnu/p/11429020.html