itoa()源码

来源:互联网 发布:安卓mac修改器apk 编辑:程序博客网 时间:2024/06/06 01:37

据说是itoa的Solaris源码,

[cpp] view plaincopy
  1. char *  
  2. itoa(n, base)  
  3. long n;   /* abs k16 */  
  4. int base;  
  5. {  
  6.     register char *p;  
  7.     register int minus;  
  8.     static char buf[36];  
  9.     p = &buf[36];  
  10.     *--p = '/0';  
  11.     if (n < 0) {  
  12.         minus = 1;  
  13.         n = -n;  
  14.     }  
  15.     else  
  16.         minus = 0;  
  17.     if (n == 0)  
  18.         *--p = '0';  
  19.     else  
  20.         while (n > 0) {  
  21.             *--p = "0123456789abcdef"[n % base];  
  22.             n /= base;  
  23.         }  
  24.         if (minus)  
  25.             *--p = '-';  
  26.         return p;  
  27. }  

static char buf[36]挺tricky的。

 

 

Microsoft的实现

[cpp] view plaincopy
  1. char* _itoa(int value, char* string, int radix)  
  2. {  
  3.     char tmp[33];  
  4.     char* tp = tmp;  
  5.     int i;  
  6.     unsigned v;  
  7.     int sign;  
  8.     char* sp;  
  9.     if (radix > 36 || radix <= 1)  
  10.     {  
  11.         __set_errno(EDOM);  
  12.         return 0;  
  13.     }  
  14.     sign = (radix == 10 && value < 0);  
  15.     if (sign)  
  16.         v = -value;  
  17.     else  
  18.         v = (unsigned)value;  
  19.     while (v || tp == tmp)  
  20.     {  
  21.         i = v % radix;  
  22.         v = v / radix;  
  23.         if (i < 10)  
  24.             *tp++ = i+''0'';  
  25.         else  
  26.             *tp++ = i + ''a'' - 10;  
  27.     }  
  28.     if (string == 0)  
  29.         string = (char*)malloc((tp-tmp)+sign+1);  
  30.     sp = string;  
  31.     if (sign)  
  32.         *sp++ = ''-'';  
  33.     while (tp > tmp)  
  34.         *sp++ = *--tp;  
  35.     *sp = 0;  
  36.     return string;  
  37. }  

使用了临时数组,做了出错处理,且传入参数string为NULL时,会动态分配空间。

不解的一点是,为毛sign = (radix == 10 && value < 0); ?正负跟radix等不等于10有毛关系,求高手指点。

 今天面试写的代码里没有用临时数组,将倒序的字符写入传入参数string里,最后将string做了一遍倒序,真的不如用个临时数组或静态字符数组方便。


另外,下面有个更狠的,不管你多少进制的,都到Z了;

atoi简单版本
int myatoi(const char *src)
{
  int total = 0;
  while (*src)
  {
    total = total*10 + (int)(*src - '0');
    src++;
  }
  return total;
}

复杂版本:
  1. isspace(int x)  
  2. {  
  3.     if(x==' '||x=='\t'||x=='\n'||x=='\f'||x=='\b'||x=='\r')  
  4.         return 1;  
  5.     else   
  6.         return 0;  
  7. }  
  8.   
  9. isdigit(int x)  
  10. {  
  11.     if(x<='9'&&x>='0')           
  12.         return 1;   
  13.     else   
  14.         return 0;  
  15.   
  16. }  
  17.   
  18. int atoi(const char *nptr)  
  19. {  
  20.         int c;              /* current char */  
  21.         int total;         /* current total */  
  22.         int sign;           /* if '-', then negative, otherwise positive */  
  23.   
  24.   
  25.         /* skip whitespace */  
  26.         while ( isspace((int)(unsigned char)*nptr) )  
  27.             ++nptr;  
  28.   
  29.   
  30.         c = (int)(unsigned char)*nptr++;  
  31.         sign = c;           /* save sign indication */  
  32.         if (c == '-' || c == '+')  
  33.             c = (int)(unsigned char)*nptr++;    /* skip sign */  
  34.   
  35.   
  36.         total = 0;  
  37.         while (isdigit(c)) {  
  38.             total = 10 * total + (c - '0');     /* accumulate digit */  
  39.             c = (int)(unsigned char)*nptr++;    /* get next char */  
  40.         }  
  41.   
  42.   
  43.         if (sign == '-')  
  44.             return -total;  
  45.         else  
  46.             return total;   /* return result, negated if necessary */  
  47. }  
  48.    


itoa函数
char *itoa(int num, char *str, int radix)   
{
  char string[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  char* ptr = str;
  int i;
  int j;
  while (num)
  {
      *ptr++ = string[num % radix];
      num /= radix;

     if (num < radix)
     {
       *ptr++ = string[num];
       *ptr = '\0';
       break;
     }
  }
  j = ptr - str - 1;
  for (i = 0; i < (ptr - str) / 2; i++)
  {
     int temp = str[i];
     str[i] = str[j];
     str[j--] = temp;
  }
  return str;
}


原文链接:点击打开链接

0 0
原创粉丝点击