90分求:itoa, atoi,trim 三个函数最完美的实现代码解决办法

90分求:itoa, atoi,trim 三个函数最完美的实现代码

  rt

------解决方案--------------------
ITOA

C/C++ code

static void __cdecl xtoa (
        unsigned long val,
        char *buf,
        unsigned radix,
        int is_neg
        )
{
        char *p;                /* pointer to traverse string */
        char *firstdig;         /* pointer to first digit */
        char temp;              /* temp char */
        unsigned digval;        /* value of digit */

        p = buf;

        if (is_neg) {
            /* negative, so output '-' and negate */
            *p++ = '-';
            val = (unsigned long)(-(long)val);
        }

        firstdig = p;           /* save pointer to first digit */

        do {
            digval = (unsigned) (val % radix);
            val /= radix;       /* get next digit */

            /* convert to ascii and store */
            if (digval > 9)
                *p++ = (char) (digval - 10 + 'a');  /* a letter */
            else
                *p++ = (char) (digval + '0');       /* a digit */
        } while (val > 0);

        /* We now have the digit of the number in the buffer, but in reverse
           order.  Thus we reverse them now. */

        *p-- = '\0';            /* terminate string; p points to last digit */

        do {
            temp = *p;
            *p = *firstdig;
            *firstdig = temp;   /* swap *p and *firstdig */
            --p;
            ++firstdig;         /* advance to next two digits */
        } while (firstdig < p); /* repeat until halfway */
}

/* Actual functions just call conversion helper with neg flag set correctly,
   and return pointer to buffer. */

char * __cdecl _itoa (
        int val,
        char *buf,
        int radix
        )
{
        if (radix == 10 && val < 0)
            xtoa((unsigned long)val, buf, radix, 1);
        else
            xtoa((unsigned long)(unsigned int)val, buf, radix, 0);
        return buf;
}

------解决方案--------------------
ATOI

C/C++ code

#include <CTYPE.H>
long __cdecl atol(
        const char *nptr
        )
{
        int c;              /* current char */
        long total;         /* current total */
        int sign;           /* if '-', then negative, otherwise positive */

        /* skip whitespace */
        while ( isspace((int)(unsigned char)*nptr) )
            ++nptr;

        c = (int)(unsigned char)*nptr++;
        sign = c;           /* save sign indication */
        if (c == '-' || c == '+')
            c = (int)(unsigned char)*nptr++;    /* skip sign */

        total = 0;

        while (isdigit(c)) {
            total = 10 * total + (c - '0');     /* accumulate digit */
            c = (int)(unsigned char)*nptr++;    /* get next char */
        }

        if (sign == '-')
            return -total;
        else
            return total;   /* return result, negated if necessary */
}


/***
*int atoi(char *nptr) - Convert string to long
*
*Purpose:
*       Converts ASCII string pointed to by nptr to binary.
*       Overflow is not detected.  Because of this, we can just use
*       atol().
*
*Entry:
*       nptr = ptr to string to convert
*
*Exit:
*       return int value of the string
*
*Exceptions:
*       None - overflow is not detected.
*
*******************************************************************************/

int __cdecl atoi(
        const char *nptr
        )
{
        return (int)atol(nptr);
}

------解决方案--------------------
TRIM
C/C++ code

#include <windows.h>
#include <tchar.h>
#define LINE_LEN 256

    static void TrimLeft(
        const TCHAR*  pszParam,
        TCHAR*        pszResult
        )
    {
        TCHAR szBuffer[LINE_LEN];
        _tcscpy(szBuffer, "\0");
        if ( (TRUE != ::IsBadStringPtr(pszParam, LINE_LEN)) &&
            (_tcslen(pszParam) > 0) )
        {
            DWORD dwIndex = 0;
            while ( (dwIndex < _tcslen(pszParam)) && (pszParam[dwIndex] == ' ') )
                dwIndex++;
            if (dwIndex < _tcslen(pszParam))
                _tcscpy(szBuffer, &pszParam[dwIndex]);
        } // if
        _tcscpy(pszResult, szBuffer);
    }
        
    //
    // Trims trailing spaces and control characters from a string
    //
    static void TrimRight(
        const TCHAR*  pszParam,
        TCHAR*        pszResult
        )
    {
        TCHAR szBuffer[LINE_LEN];
        _tcscpy(szBuffer, "\0");
        if ( (TRUE != ::IsBadStringPtr(pszParam, LINE_LEN)) &&
            (_tcslen(pszParam) > 0) )
        {
            size_t nIndex = _tcslen(pszParam) - 1;
            while ( (nIndex >= 0) && (pszParam[nIndex] == ' ') )
                nIndex--;
            if (nIndex >= 0)
            {
                memcpy(
                    (PBYTE)szBuffer, 
                    (PBYTE)pszParam, 
                    (nIndex + 1)*sizeof(TCHAR)
                    ); 
                szBuffer[nIndex+1] = 0;
            } // if
        } // if
        _tcscpy(pszResult, szBuffer);
    }

    //
    // Trims leading and trailing spaces and control characters from a string
    //
    static void Trim(
        const TCHAR*  pszParam,
        TCHAR*        pszResult
        )
    {
        TrimLeft(pszParam, pszResult);
        TrimRight(pszParam, pszResult);
    }