Implement atoi to convert a string to an integer.
Hint:carefully consider all possible input cases. If you want a challenge, please don't see below and ask yourself what is the possible input cases.
Notes:it is intended-problem to be specified vaguely (ie, no given input specs). You is responsible to gather all the input requirements up front.
Update (2015-02-10):
The signature of the C++
function had been updated. If you still see your function signature accepts a const char *
argument, please click on the reload button to reset your code Definition.
Spoilers alert ... click to show requirements for atoi.
Requirements for Atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, the starting from this character, takes a optional initial plus or minus sign followed by as many numerical digits as P Ossible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which is ignored and has no Effe CT on the behavior of this function.
If the first sequence of non-whitespace characters in STR isn't a valid integral number, or if no such sequence exists be Cause either str is empty or it contains only whitespace characters, no conversion is performed.
If No valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, Int_max (2147483647) or int_min ( -2147483648) is Returne D.
Problem: Converting character channeling to numbers
Analysis: Feel the problem is not difficult, but a lot of details, easy to imagine
1. The number is preceded by a space such as s= "123456"
2. Digital authentication error occurs when unnecessary or more characters appear before the number, output 0 such as s= "b1234", s= "++1233", s= "+-1121"
3. An unnecessary character appears in the number, and the number before the return character is s= "12a12", s= "123 123"
4. The number exceeds the range ( -2147483648--2147483647) if it exceeds the negative output-2147483648 exceeds the positive output 2147483647
In the science of a knowledge point, if a number more than 2147483647 will become negative, and vice versa
1 intMyatoi (Char*str) {2 intSign =1;//set the sign bit, initial setting 1 is positive if there is no symbol in front of it represents a positive number3 Long Longsum =0;//sum range greater than int4 if(str = =NULL)5 return 0;6 while(*str = =' ') str++;//Remove the front space7 if(*str = ='-'){ 8Sign =-1;9str++;Ten}//Take symbol One Else if(*str = ='+'){ ASign =1; -str++; - } the while(*str! =' /'){ - if(*str <'0'|| *str >'9')//cancellations that do not conform to the rules, such as "+-123" - Break; - Else if(*str >='0'&& *str <='9'){ +sum = SUM *Ten+ *str- -; - if(Sum >2147483648)//sum range exceeds + Break; A } atstr++; - } - if(Sign = =1&& sum >2147483647) - return 2147483647; - if(Sign = =-1&& sum >2147483648) - return-2147483648; insum = (int) sum * sign;//Finally, the sum is converted into int type . - returnsum; to}
String to Integer (atoi)