zoukankan      html  css  js  c++  java
  • itoa函数的实现(不同进制)

    2013-07-08 17:12:30

    itoa函数相对于atoi函数,比较简单,还是要注意考虑的全面。

    小结:

    一下几点需要考虑:

    1. 对负数,要加上负号;
    2. 考虑不同进制,根据要求进行处理;对不同的进制转换方法不同(如对于16进制还要考虑10~15的情况:*str++ = digit[cnt--] - 10 + 'A');
    3. 最后要加上字符串结束符*str = '';
    4. 要正确输出绝对值最大的负数,就必须将输入转换为无符号数因为对下面的代码:

    long int num = -2147483648;

    cout<<-num<<endl;

    执行结果为:-2147483648

    因为-2147483648取负号,为2147483648,但对于32位的long int型,其所能表示的最大正数为2147483647,因此2147483648将转换为-2147483648,在执行下面的while循环时就会得到意想不到的怪异结果。

    对16进制,要加上前缀:

    if (false == PreFlag)
       {
        *str++ = '0';
        *str++ = 'x';
        PreFlag = true;
       }

    最8进制,同样:

    if (false == PreFlag)
       {
        *str++ = '0';
        PreFlag = true;
       }

    下面代码中的数组digit还可以进行优化,如类型改为char型,可以节省空间。

    代码:

      1 #include <iostream>
      2 #include <cmath>
      3 using namespace std;
      4 
      5 #define SIZE 100
      6 
      7 bool BaseError = false;
      8 
      9 //考虑不同进制的转换
     10 char * _itoa_1 (long int num,char str[],int base)
     11 {
     12     if ( !(base == 10 || base == 16 || base == 8) )
     13     {
     14         BaseError = true;
     15         return NULL;
     16     }
     17 
     18     int digit[20];   //保存各个数字
     19     int cnt = 0;
     20     int sign = 1;
     21     bool PreFlag = false;
     22     char *str_copy = str;
     23     unsigned long int uNum = 0;
     24 
     25     if (num < 0)   //对负数的处理
     26     {
     27         sign = -1;
     28         uNum = (unsigned long int) - num;  //转换为unsigned long int,这样才能处理绝对值最大的负数
     29     }
     30     else
     31     {
     32         uNum = num;
     33     }
     34 
     35     while (uNum)
     36     {
     37         digit[cnt++] = uNum % base;
     38         uNum = uNum / base;
     39     }
     40 
     41     --cnt;
     42 
     43     if (cnt >= 0 && -1 == sign)  //对负数,要加负号
     44     {    
     45         *str++ = '-';        
     46     }
     47 
     48     while (cnt >= 0)
     49     {
     50         if (10 == base)        //对不同的进制,转换方式不同
     51         {
     52             *str++ = digit[cnt--] + '0';
     53         }
     54         else if (16 == base)
     55         {
     56             if (false == PreFlag)
     57             {
     58                 *str++ = '0';
     59                 *str++ = 'x';
     60                 PreFlag = true;
     61             }
     62             else
     63             {
     64                 if (digit[cnt] >= 0 && digit[cnt] <= 9)
     65                 {
     66                     *str++ = digit[cnt--] + '0';
     67                 }
     68                 else
     69                 {
     70                     *str++ = digit[cnt--] - 10 + 'A';
     71                 }
     72             }
     73         }
     74         else if (8 == base)
     75         {
     76             if (false == PreFlag)
     77             {
     78                 *str++ = '0';
     79                 PreFlag = true;
     80             }
     81             else
     82             {
     83                 *str++ = digit[cnt--] + '0';
     84             }
     85         }
     86     }
     87 
     88     *str = '';
     89     return str_copy;  //返回指针地址
     90 }
     91 
     92 //测试程序
     93 int main()
     94 {
     95     char str[SIZE];
     96     int num = 0;
     97     int base = 0;
     98     cout<<"test _itoa_1..."<<endl;
     99     cout<<"please enter the integer number and the base:"<<endl;
    100     while(cin>>num>>base)
    101     {
    102         cout<<"the integer number is :"<<num<<endl;
    103         cout<<"the int number is : "<<_itoa_1(num,str,base)<<endl;
    104         cout<<"please enter the string :"<<endl;
    105         cout<<"please enter the integer number and the base:"<<endl;
    106     }
    107     
    108     return 0;
    109 }

    运行结果:

    test _itoa_1...
    please enter the integer number and the base:
    2147364748
    10
    the integer number is :2147364748
    the int number is : 2147364748
    please enter the string :
    please enter the integer number and the base:
    -2147364749
    10
    the integer number is :-2147364749
    the int number is : -2147364749
    please enter the string :
    please enter the integer number and the base:
    8934
    16
    the integer number is :8934
    the int number is : 0x22E6
    please enter the string :
    please enter the integer number and the base:
    78
    8
    the integer number is :78
    the int number is : 0116
    please enter the string :
    please enter the integer number and the base:
    -78
    16
    the integer number is :-78
    the int number is : -0x4E
    please enter the string :
    please enter the integer number and the base:
    -78
    8
    the integer number is :-78
    the int number is : -0116
    please enter the string :
    please enter the integer number and the base:
    ^Z
    请按任意键继续. . .
  • 相关阅读:
    【OpenCV学习笔记5】读取图像中任意点的像素值
    【收藏】国企央企
    Visual Studio 进化史
    【图像算法】不变矩
    工控博客精华链接
    投了...
    【图像算法】常见的数字图像处理程序大全
    Google C++编码规范
    Google员工自述:在哈佛教书和在Google工作的差别
    国立华侨大学校长写给2010届毕业生的话:人生的二和三
  • 原文地址:https://www.cnblogs.com/youngforever/p/3178461.html
Copyright © 2011-2022 走看看