#include <stdio.h> #include <assert.h> #include <stdlib.h> int my_atoi(const char *str) { assert(str); int flag = 1; int ret = 0; while (isspace(*str)) { str++; } if (*str == ‘-‘) { flag = -1; } if (*str == ‘+‘ || *str == ‘-‘) { str++; } while (*str) { ret = ret * 10 + (*str - ‘0‘); str++; } return ret*flag; } int main() { char *str = "-12384"; int ret = my_atoi(str); printf("%d\n", ret); return 0; }
原文地址:http://10706198.blog.51cto.com/10696198/1760045