Topic:
implements Atoi, converting a string to an integer. The space character in the string needs to be removed before the first non-null character is found. If the first non-null character is a plus or minus sign, select the symbol and combine it with as many consecutive digits as possible, which is the value of the integer. If the first non-null character is a number, it is directly combined with successive numeric characters to form an integer. Strings can include extra characters after the characters that form integers, which can be ignored and have no effect on the function. When the first non-null character sequence in a string is not a valid integer, or the string is empty, or the string contains only white space characters, no conversion is made. If the function cannot perform a valid conversion, return0. Description: Assume that our environment can only store +A bit signed integer whose value range is [?231,231?1]。 If the value exceeds the range that can be represented, the Int_max is returned (231?1) or Int_min (?231). Example1: Enter:" the"Output: theExample2: Enter:"-42"Output:- theExplanation: The first non-whitespace character is'-', it is a minus sign. We try to combine the minus sign with all the successive numbers in the back, and we end up getting- the. Example3: Enter:"4193 with words"Output:4193Explanation: Conversion due to numbers'3', because its next character is not a number. Example4: Enter:"Words and 987"Output:0Explanation: The first non-null character is'W', but it is not a number or a positive, minus sign. Therefore, a valid conversion cannot be performed. Example5: Enter:"-91283472332"Output:-2147483648Explanation: Numbers"-91283472332"More than +a bit signed integer range. So return to Int_min (?231) 。
1, problem-solving ideas:
This is not particularly difficult, but be sure to note the removal of the problem, as well as illegal characters. There is no error in this write-up, and the problem of overflow in the test case is not easy.
#defineInt_max 2147483647#defineint_min-2147483648BOOLIsdigital (Charp) { return(p>='0'&& p<='9')?true:false;}BOOLIsSpace (Charp) { returnp = =' '?true:false;}BOOLIsflag (Charp) { return(p = ='-'|| p = ='+')?true:false;}intMyatoi (Char*str) { Char*p =str; intNumber =0; CharFlag =0; BOOLIscal =false; if(p = =NULL) { return 0; } //printf ("%s\n\r", str); while((*p)! =' /'){ if(Isdigital (*p))) {printf ("Digital is%c\n\r",*p); Iscal=true; if(Number >214748364){ if(Flag = =1|| Flag = =0){ returnInt_max; } if(Flag = =-1){ returnint_min; } } if(Number = =214748364){ if(Flag = =1|| Flag = =0){ if((*p) >'7'){ returnInt_max; } } if(Flag = =-1){ if((*p) >'8'){ returnint_min; } }} number= number*Ten+ (*P)-'0'; }Else if(IsSpace (*p)) { if(number!=0){ Break; }Else{ if(iscal| | flag!=0){ Break; } } while((*p) = =' ') {p++; } Continue; }Else if(Isflag (*p)) { if(Flag = =0){ if((*p) = ='-') {flag= -1; }Else{flag=1; } }Else{ Break; } }Else{ Break; } P++; } if(Flag = =-1) { number= number * (-1); } returnNumber ;}
"Simple Algorithm" 17. String to Integer (atoi)