实现atoi函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wwwww_bw/article/details/53823432
  1. int my_atoi(char* pstr)  
  2. {  
  3.     int Ret_Integer = 0;  
  4.     int Integer_sign = 1;  
  5.       
  6.     /* 
  7.     * 判断指针是否为空 
  8.     */  
  9.     if(pstr == NULL)  
  10.     {  
  11.         printf("Pointer is NULL\n");  
  12.         return 0;  
  13.     }  
  14.       
  15.     /* 
  16.     * 跳过前面的空格字符 
  17.     */  
  18.     while(isspace(*pstr) == 0)  
  19.     {  
  20.         pstr++;  
  21.     }  
  22.       
  23.     /* 
  24.     * 判断正负号 
  25.     * 如果是正号,指针指向下一个字符 
  26.     * 如果是符号,把符号标记为Integer_sign置-1,然后再把指针指向下一个字符 
  27.     */  
  28.     if(*pstr == '-')  
  29.     {  
  30.         Integer_sign = -1;  
  31.     }  
  32.     if(*pstr == '-' || *pstr == '+')  
  33.     {  
  34.         pstr++;  
  35.     }  
  36.       
  37.     /* 
  38.     * 把数字字符串逐个转换成整数,并把最后转换好的整数赋给Ret_Integer 
  39.     */  
  40.     while(*pstr >= '0' && *pstr <= '9')  
  41.     {  
  42.         Ret_Integer = Ret_Integer * 10 + *pstr - '0';  
  43.         pstr++;  
  44.     }  
  45.     Ret_Integer = Integer_sign * Ret_Integer;  
  46.       
  47.     return Ret_Integer;  
  48. }  

猜你喜欢

转载自blog.csdn.net/wwwww_bw/article/details/53823432