atol实现【转】
1 int my_atoi(const char *str) 2 { 3 assert(str != NULL); 4 int flag = 1; 5 int result = 0; 6 while(*str == ' ' || *str == '\n' || *str == '\t') 7 { 8 ++str; 9 } 10 if(*str == '-') 11 { 12 flag = -1; 13 ++str; 14 } 15 else if(*str == '+') 16 { 17 ++str; 18 } 19 while(*str != '\0') 20 { 21 if(*str >= '0' && *str <= '9') 22 { 23 result = result*10 + *str - '0'; 24 } 25 else 26 { 27 return flag * result; 28 } 29 ++str; 30 } 31 return flag * result; 32 }