题目:
implement atoi to convert a string to an integer. hint: carefully consider all possible input cases. if you want a challenge, please do not see below and ask yourself what are the possible input cases. notes: it is intended for this problem to be specified vaguely (ie, no given input specs). you are responsible to gather all the input requirements up front. requirements for atoi: the function first discards as many whitespace characters as necessary until the first non-whitespace character is found. then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. the string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. if the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because 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 returned.
解析:注意前后中间(或全是)的空格,注意正负号,注意溢出,(题目要求中没有:注意特殊字符出现)
代码也许还待优化:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<code>//#define int_max 2147483647</code>
<code>//#define int_min -2147483648</code>
<code>double</code>
<code>noheadblank(</code><code>const</code>
<code>char</code> <code>*str){</code>
<code> </code><code>double</code>
<code>val = 0;</code>
<code> </code><code>if</code><code>(str[0] ==</code><code>‘-‘</code>
<code>|| str[0] ==</code><code>‘+‘</code><code>){</code>
<code> </code><code>int</code>
<code>i = 1;</code>
<code> </code><code>while</code><code>(str[i] ==</code><code>‘0‘</code><code>) ++i;</code>
<code> </code><code>if</code><code>(str[i] ==</code><code>‘\0‘</code><code>)</code><code>return</code>
<code>0;</code>
<code> </code><code>else</code>
<code>if</code><code>(str[i] ></code><code>‘0‘</code>
<code>&& str[i] <=</code><code>‘9‘</code><code>) {val = str[i] -</code><code>‘0‘</code><code>; ++i;}</code>
<code>return</code> <code>0;</code>
<code> </code><code>for</code><code>(; str[i] !=</code><code>‘\0‘</code>
<code>&& str[i] >=</code><code>‘0‘</code>
<code>&& str[i] <=</code><code>‘9‘</code><code>; ++i)</code>
<code> </code><code>val = val * 10 + (str[i] -</code><code>‘0‘</code><code>);</code>
<code> </code><code>if</code><code>(str[0] ==</code><code>‘-‘</code><code>) val *= (-1);</code>
<code> </code><code>}</code><code>else</code>
<code>if</code><code>(str[0] ></code><code>‘0‘</code>
<code>&& str[0] <=</code><code>‘9‘</code><code>) {</code>
<code> </code><code>val = str[0] -</code><code>‘0‘</code><code>;</code>
<code> </code><code>int</code>
<code> </code><code>for</code><code>(; str[i] !=</code><code>‘\0‘</code>
<code>&& str[i] <=</code><code>‘9‘</code><code>; ++i){</code>
<code> </code><code>val = val * 10 + (str[i] -</code><code>‘0‘</code><code>);</code>
<code> </code><code>}</code>
<code>if</code><code>(str[0] ==</code><code>‘0‘</code><code>){</code>
<code> </code><code>while</code><code>(str[i] ==</code><code>‘0‘</code><code>) ++i;</code>
<code> </code><code>if</code><code>(str[i] ==</code><code>‘\0‘</code><code>)</code><code>return</code>
<code> </code><code>else</code>
<code> </code><code>}</code>
<code> </code><code>return</code>
<code>val;</code>
<code>}</code>
<code>class</code>
<code>solution {</code>
<code>public</code><code>:</code>
<code> </code><code>int</code>
<code>atoi</code><code>(</code><code>const</code>
<code>char</code> <code>*str) {</code>
<code> </code><code>double</code>
<code> </code><code>if</code><code>(str == null)</code><code>return</code>
<code> </code><code>if</code><code>(str[0] ==</code><code>‘ ‘</code><code>){</code>
<code> </code><code>while</code><code>(str[i] ==</code><code>‘ ‘</code><code>) ++i;</code>
<code>val = noheadblank(str+i);</code>
<code>val = noheadblank(str);</code>
<code> </code><code>if</code><code>(val > (</code><code>double</code><code>)int_max)</code><code>return</code>
<code>int_max;</code>
<code>if</code> <code>(val < (</code><code>double</code><code>)int_min)</code><code>return</code>
<code>int_min;</code>
<code>return</code> <code>(</code><code>int</code><code>)val;</code>
<code> </code><code>}</code>
<code>};</code>