改寫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;    
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章