650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/75/80/wKioL1Y69G2wAJ1RAAES17fVPxM685.jpg "style=" float: none; "title=" qq20151105141427.jpg "alt=" Wkiol1y69g2waj1raaes17fvpxm685.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/75/83/wKiom1Y69C6ArY3hAADKarnOkf4580.jpg "style=" float: none; "title=" qq20151105141452.jpg "alt=" Wkiom1y69c6ary3haadkarnokf4580.jpg "/>
On line 8th, the pointer ptr points to the first address of the string and uses a const modifier to indicate that PTR is a constant pointer and can only point to a constant, and the content it points to cannot be changed. Here it is convenient to determine whether the first character of the string is '-' in the back, so that the final conversion result is positive or negative.
Line 10th, determine whether the first character of the string is ' + ' or '-', and if it is one, you should start the conversion from the second character of the string.
Line 12th, while loop starts, first determine whether each character is a number, if not, then jump out; Yes, the value is calculated.
The 19th line determines whether the first character of the string is '-', and if so, takes the value calculated above as negative.
Combined with the previous article, "Do not use library functions to convert numbers to strings", you can summarize the steps:
Numeric to String:
1, take the absolute value of the number;
2. Determine if the address allocated for storing the target string is empty;
3, each number is stripped out, and converted to characters, stored in the buf[] array (reverse order);
4, according to the number of positive and negative, determine the length of the target string;
5, the buf[] array in reverse order of the numeric character is copied back to the target str[] array;
6. If negative, the first character of the str[] array must be '-'.
String to Number:
1, storing the first character of the string, used to judge the positive and negative numbers;
2, if the first character of the string is ' + ' or '-', it is necessary to start the conversion from the second character;
3, convert each character to a number, and add up;
4, according to whether the first character of the string is '-', to determine the positive or negative number;
5, return the number.
Convert a string to a number without using a library function