Title: Enter a String representing an integer to convert the string to an integer and output. For example, the input string "345", the output integer 345.
Analysis: Although the problem is not difficult to learn, C + + language generally can achieve basic functions, but different programmers on the problem written code has a very big difference, it can be said that the problem can well reflect the programmer's thinking and programming habits, it has been included in Microsoft, including a number of companies as a face test. It is recommended that readers write their own code before looking down and compare the code they write to the reference code below.
First we analyze how to complete the basic function, that is, how to correctly convert a string representing an integer to an integer. or "345" as an example. When we scan to the first character of the string ' 3 ', we don't know how many bits are in the back, just know that this is the first bit, so the number we get is 3. When scanning to the second number ' 4 ', at this point we already know that there is a 3, and then add a number 4, the preceding 3 is equivalent to 30, so the resulting number is 3*10+4=34. Then we scan to the character ' 5 ', we already know that ' 5 ' is already 34, because the following to add a 5, the front 34 is equivalent to 340, so the number is 34*10+5=345.
Analysis here, we can not come up with a conversion idea: each scan to a character, we put the previously obtained number multiplied by 10 plus the current character represented by the number. This idea is not difficult to achieve with loops.
Since integers may not only contain numbers, it is also possible to start with ' + ' or '-' to indicate the positive or negative of an integer. So we need to take the first character of this string for special processing. If the first character is ' + ', then no action is required, and if the first character is '-' it indicates that the integer is a negative number, and at the end we have to turn the resulting value into a negative number.
Then we try to handle the illegal input. Since the input is a pointer, the first thing we need to do before using the pointer is to determine if the pointer is empty. If you try to access a null pointer, it will inevitably cause the program to crash. In addition, the input string may contain characters that are not numeric. Whenever these illegal characters are encountered, there is no need to continue the conversion. The last issue to consider is the overflow problem. Since the input number is entered as a string, it is possible to enter a large numeric conversion that will overflow beyond the largest integer that can be represented.
Now that the analysis is almost over, start thinking about writing code. First we consider how to declare this function. Since it is the conversion of strings into integers, it is natural that we think of:
int strtoint (const char* str);
This statement seems to have no problem. But what value should be returned when the input string is a null pointer or contains an illegal character? How about 0? So how to distinguish between illegal input and the string itself is the "0" of the two cases?
Next we consider another way of thinking. We can return a Boolean value to indicate whether the input is valid, and put the converted integer in the argument list as a reference or as a pointer. So we can declare the following:
BOOL Strtoint (const char *STR, int& num);
This approach solves the previous problem. But the user of this function will not feel very convenient when using this function, because he can't directly assign the integer value to other shaping variables, which is not intuitive enough.
The first statement above is straightforward. How to inform the user when the illegal input is met under the premise of ensuring the intuition? One solution is to define a global variable that, whenever it encounters an illegal input, marks the global variable. After calling this function, the user can examine the global variable to determine if the conversion was successful.
enumStatus {kvalid =0, kinvalid};intG_nstatus =Kvalid;/////////////////////////////////////////////////////////////////////////Convert A string to an integer///////////////////////////////////////////////////////////////////////intStrtoint (Const Char*str) {G_nstatus=Kinvalid; Long Longnum =0; if(str! =NULL) { Const Char* Digit =str; //The first char in the string maybe ' + ' or '-' BOOLminus =false; if(*digit = ='+') Digit++; Else if(*digit = ='-') {digit++; Minus=true; } //The remaining chars in the string while(*digit! =' /') { if(*digit >='0'&& *digit <='9') {num= num *Ten+ (*digit-'0'); //Overflow if(Num > std::numeric_limits<int>:: Max ()) {num=0; Break; } Digit++; } //if the char is not a digit, invalid input Else{num=0; Break; } } if(*digit = =' /') {G_nstatus=Kvalid; if(minus) Num=0-num; } } returnstatic_cast<int>(num);}
Original: http://zhedahht.blog.163.com/
Programmer Interview 50 Questions (4)-Convert string to integer [algorithm]