atoi和itoa函数的实现方法

//atoi的实现

#include
using namespace std;
int atio1(char *s)
{
int sign=1,num=0;
    if(*s=='-')
        sign=-1;
    s++;
    while((*s)!='\0')
    {
        num=num*10+(*s-'0');
        s++;
    }   
    return num*sign;   
}

//itoa的实现

char *itoa(int num, char *str, int radix)
{
    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;
}


你可能感兴趣的:(软件编程(C/C++))