![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream> 2 using namespace std; 3 int atoi1(char *s) 4 { 5 int sign=1,num=0; 6 if(*s=='-') 7 sign=-1; 8 else if(*s=='+') 9 sign=1; 10 s++; 11 while((*s)!='\0') 12 { 13 num=num*10+(*s-'0'); 14 s++; 15 } 16 return num*sign; 17 } 18 19 int main() 20 { 21 char *s="-123567890"; 22 cout<<atoi1(s); 23 system("pause"); 24 } 25 26 27 下面是微软对该函数的实现 28 long atol(const char *nptr) 29 { 30 int c; 31 long total; 32 int sign; 33 34 35 while ( isspace((int)(unsigned char)*nptr) ) 36 ++nptr;//吸收空字符 37 38 c = (int)(unsigned char)*nptr++; 39 sign = c; 40 if (c == '-' || c == '+') 41 c = (int)(unsigned char)*nptr++; 42 43 total = 0; 44 45 while (isdigit(c)) { 46 total = 10 * total + (c - '0'); 47 c = (int)(unsigned char)*nptr++; 48 } 49 50 if (sign == '-') 51 return -total; 52 else 53 return total; 54 }