atoi函数的实现

来源:互联网 发布:poker适合编程吗 编辑:程序博客网 时间:2024/06/06 14:12

原帖地址:

http://www.cppblog.com/Cunch/archive/2011/03/13/141711.html


     今天看见大家在讨论atoi这个函数,我也凑个热闹,学习下atoi函数的用法,记得当时学C语言,以为就只能将全是数字的字符串转化成整数,现在明白了远不至如此。

1、能够将从当前开始的字符(数字或+、-)到第一个不是数字的字符结束的数字字符串,转化成整数;

2、需要注意的指针在这个过程中的指向发生了改变.

我的atoi实现代码:

[cpp] view plain copy
 print?
  1. int atoi(const char * str)  
  2. {  
  3.     if(str == NULL) return 0;  
  4.   
  5.     int result = 0;  
  6.     int sign = 1;  
  7.     if( ('0'<= str[0] && str[0] <= '9') ||(str[0]=='-') || str[0]=='+')  
  8.     {  
  9.         if(str[0] == '+' || str[0] == '-')  
  10.         {  
  11.             if(str[0]=='-')  
  12.             {  
  13.                 sign = -1;  
  14.             }  
  15.             str++;  
  16.         }  
  17.     }  
  18.     else  
  19.     {  
  20.         return 0;  
  21.     }  
  22.   
  23.     while('0'<= *str && *str <= '9')  
  24.     {  
  25.         result = result*10 + (*str++ - '0');  
  26.     }  
  27.   
  28.     return result;  
  29. }  

修改版:

[cpp] view plain copy
 print?
  1. int atoi(const char * str)  
  2. {  
  3.     if(str == NULL) return 0;  
  4.   
  5.     int result = 0;  
  6.     int sign = 1;  
  7.     if( ('0'<= str[0] && str[0] <= '9') ||(str[0]=='-') || str[0]=='+')  
  8.     {  
  9.         if(str[0] == '+' || str[0] == '-')  
  10.         {  
  11.             if(str[0]=='-')  
  12.             {  
  13.                 sign = -1;  
  14.             }  
  15.             str++;  
  16.         }  
  17.     }  
  18.       
  19.       
  20.     while('0'<= *str && *str <= '9' )  
  21.     {  
  22.         result = result*10 + (*str++ - '0');  
  23.     }  
  24.   
  25.     return result * sign;  
  26. }  

后面说我没溢出判断:
我仔细想了想:我认为没必有,由于我保证了传进来的str是有意义的:
无外乎这几种情况: {'\0'}, {'+', '\0'}, {'1', '2', '\0'} , {'a', 'b', '1', '2', '\0'},这些都应该没有问题
,由于while('0' <= *str && *str <= '9'),等于间接就判断了。

修改版二:
现在发现我对溢出的理解是错误,下面给出代码

[cpp] view plain copy
 print?
  1. #define INT_MAX (2147483647)  
  2. #define INT_MIN (-2147483648)  
  3.   
  4. int atoi(const char* str)  
  5. {  
  6.     if(!str)  
  7.     {  
  8.         return 0;  
  9.     }  
  10.   
  11.     unsigned int result = 0;  
  12.     int sign = -1;  
  13.   
  14.     if(*str == '-' || *str == '+')  
  15.     {  
  16.         if(*str == '-')  
  17.         {  
  18.             sign = -1;  
  19.         }  
  20.         str ++ ;  
  21.     }  
  22.   
  23.     while('0' <= *str && *str <= '9')  
  24.     {  
  25.         unsigned int digit = *str - '0';  
  26.         if( (result >INT_MAX /10) || sign >0 ? (result == INT_MAX /10 && digit > INT_MAX %10)  
  27.             :( result == (( unsigned int)INT_MAX + 1) /10 &&digit >(((unsigned int)INT_MAX +1 %10))))  
  28.         {  
  29.             return sign > 0 ? INT_MAX : INT_MIN;  
  30.         }  
  31.         result = result * 10 + digit;  
  32.         str ++ ;  
  33.     }  
  34.     return ((int)result) * sign;  
  35. }  



3、今天我才明白const char* str; 是修饰字符串不能改变的,而不是指针, 修饰常量指针的是char* const str;

    那我就说说吧:
    const int * const pint;  //一个const指针,指向一个const成员
    const int * pint;   //一个非const指针, 指向一个const成员
    int *pint;  //一个非const指针,指向一个非const成员
    int * const pint;  //一个const指针,指向一个非const成员
    int const * pint; //和第二个一样, 一个非const指针, 指向一个const成员, 这个不常用

0 0
原创粉丝点击