1 #include "stdio.h"
2 #include "math.h"
3 long int atoi(char * str)
4 {
5 unsigned long int intNum = 0;
6 unsigned long int maxNum = (long int)pow(2,(int)(8 * sizeof(long int) -1 )) -1 ;
7 int sign = 1;
8 if(NULL == str)
9 {
10 return 0;
11 }
12 while (' ' == *str || '\t' == *str)
13 {
14 str++;
15 }
16 if( '-' == *str )
17 {
18 sign = -1;
19 str++;
20 }
21 else if( '+' == *str)
22 {
23 str++;
24 }
25 while(*str >= '0' && *str <= '9' )
26 {
27 intNum = intNum *10 + (*str - '0');
28 if(-1 == sign && intNum > maxNum + 1)
29 {
30 return sign*(maxNum+1);
31 }
32 else if(1 == sign && intNum > maxNum )
33 {
34 return sign*maxNum;
35 }
36 else
37 {
38 str++;
39 }
40
41 }
42 return sign * intNum;
43 }
44 int main()
45 {
46 char str[] = " -01234567989999999x99999999999999";
47 printf("%d",atoi(str));
48 return 0;
49 }