改写atoi

int myAtoi (const char * strSrc)
{
    if(strSrc == NULL)
    {
        throw "Invalid argument(s)";
    }
    int strValue = 0;    
    while( *strSrc != '\0' )
    {
        if(*strSrc <= '9' && *strSrc >= '0' )
        {
            strValue = strValue*10 + *strSrc -'0';
            strSrc++;
        }
        else 
        {
            throw "Str string contain not numeric character" ;
        }
    } 
    return strValue;
}

或者


long myAtoi(const char* tmpArray,int nLen)
{
    if ( tmpArray  == NULL )
    {
        throw "invalid parameter(s) of function myAtoi.";    
    }
    
    bool flag = false;
    long temp =0  ;

    if(tmpArray[0] == '-' )
    {
        flag = true;
    }    

    for(int i = 0 ;i< nLen ;i ++)
    {
      if(tmpArray[i] == '-' && i == 0)
      {
          continue;
      }
      else if(tmpArray[i]  > '0' && tmpArray[i] < '9')
      {
           temp = temp*10 + (long)(tmpArray[i] -'0');
           continue;    
      }
      throw "input parameter content with no-numeric character.";
    }
    return flag ? -1 * temp : temp;
}


考虑负数

int myAtoi (const char * strSrc)
{
    if(strSrc == NULL)
    {
        throw "Invalid argument(s)";
    }
    int strValue = 0;
    bool neg = true;  
    
    if('-' == *strSrc )
    {
        neg = false;
        strSrc ++;
    }
       
    while( *strSrc != '\0' )
    {
        if(*strSrc <= '9' && *strSrc >= '0' )
        {
            strValue = strValue*10 + *strSrc -'0';
            strSrc++;
        }
        else 
        {
            throw "Str string contain not numeric character" ;
        }
    } 
    return neg ? strValue:-1*strValue;
}


考虑到长整型溢出的问题

其中 LONG_MAX 是在 #include <limitS.h> 里.

long myAtoi(const char* tmpArray,int nLen)
{
    if ( tmpArray  == NULL )
    {
        throw "invalid parameter(s) of function myAtoi.";    
    }
    
    bool flag = false;
    long temp =0  ;

    if(tmpArray[0] == '-' )
    {
        flag = true;
    }    

    for(int i = 0 ;i< nLen ;i ++)
    {
      if(tmpArray[i] == '-' && i == 0)
      {
          continue;
      }
      else if(tmpArray[i]  > '0' && tmpArray[i] < '9')
      {
             if((LONG_MAX - (long)(tmpArray[i] -'0'))/10 > temp)
             {
               temp = temp*10 + (long)(tmpArray[i] -'0');
                 continue;    
             }
             else 
             {
            throw "Overflow .";     
           }
      }
      throw "input parameter content with no-numeric character.";
    }
    return flag ? -1 * temp : temp;
}


下面改写itoa()


#define SWAP(a,b) {int swab; swab = a ; a = b; b = swab;}

加入了异常处理和对负号数的支持


char* myItoa (int intSrc,char * strDest)
{
    if(strDest == NULL)
    {
        throw "Invalid argument(s)";
    }

    bool neg = true;

    int intSrcCopy = intSrc;
    char * strDestCopy = strDest;
    char * strDestPosition ;

    if( intSrc < 0) 
    {
        neg = false;
        intSrcCopy *= -1;
    }     

    while(1)
    {
        if(intSrcCopy< 10)
        {
            *strDestCopy = intSrcCopy + '0';

            if(neg == false )
            {
                strDestCopy++ ;
                *strDestCopy  = '-';          
            }

            strDestPosition = strDestCopy ; // make the strDestPosition point to the last of
                                            // this char [] ,be used for negative sequence.
            strDestCopy++;
            *strDestCopy ='\0';
            break;
        }    
        else if( intSrcCopy / 10 >= 0 )
        {
            *strDestCopy = intSrcCopy%10 + '0';
            intSrcCopy /= 10;
            strDestCopy ++ ;
        }

    }

    //negative sequence
    strDestCopy =  strDest;
    while(strDestPosition > strDestCopy)
    {
        SWAP (*strDestPosition,*strDestCopy);
        strDestPosition -- ;
        strDestCopy ++ ;

    }

    return  strDest;
}

改写了一下myItoa 下面这个可能容易懂点. 

char * myItoa(int temp,char * destStr )
{

    if ( destStr  == NULL )
    {
        throw "invalid argument(s) of function myItoa.";    
    }
    
    bool flag = false;
    char* copyDestStr = destStr;
    char* copyDestStr1 = destStr;
    
    if(temp < 0 )
    {
        flag = true;
        temp *= -1;
    }
        
    while(temp>0)
    {
        if(temp > 10 )
        {
            *copyDestStr = temp % 10 +'0';
            temp /= 10;
            copyDestStr ++;
        }
        else 
        {
            *copyDestStr = (temp + '0');
            copyDestStr++ ;
            
            if(flag == true)
            {
                *copyDestStr = '-';
                copyDestStr++;
            }
            *copyDestStr = '\0';
            break;
        }    
    }
    
    //switch the position of the char array 
    copyDestStr--; 
    while(copyDestStr > copyDestStr1 )
    {
        SWAP(*copyDestStr,*copyDestStr1);
        copyDestStr--;
        copyDestStr1++;

    }    
    return destStr;    
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值