my_atoi

 1 #include<iostream>

 2 using namespace std;

 3 int my_atoi(char *s)

 4 {

 5     if(s==NULL) 

 6         return -1;

 7     char *p=s;

 8     int n=0,sum=0;

 9     while(*p++)

10         n++;

11     bool isNegative=false;

12     int j=0;

13     if(s[0]=='-')

14     {

15         isNegative=true;

16         j=1;

17     }

18     for(;j<n;j++)

19         sum=sum*10+s[j]-'0';

20     if(isNegative)

21         sum*=-1;

22     return sum;

23 }

24 int main()

25 {

26     char *s="-123";

27     cout<<my_atoi(s)<<endl;

28     system("pause");

29     return 0;

30 }

 

你可能感兴趣的:(a)