您的位置:首页 > 编程语言

itoa与atoi源代码

2013-07-10 17:05 183 查看
int Myatoi( char* str )
{
if (str == NULL)
{
printf("Invalid Input!\n");
return -1;
}

while ( (*str)==' ')
{
str++;
}

int nSign = (*str=='-')?-1:1;

if( *str=='+'|| *str=='-')
{
*str++;
}

int nResult = 0;

while( *str>='0' && *str<='9')
{
nResult = nResult*10 + (*str - '0');
*str++;
}

return nResult*nSign;
}

char* Myitoa(int num)
{
char str[1024];
int sign=num, i=0, j=0;
char temp[11];
if (sign<0)
{
num = -num;
}

do
{
temp[i] = num%10 + '0';
num /= 10;
i++;
}while (num>0);

if (sign<0)
{
temp[i++] = '-';
}

temp[i]='\0';
i--;

while (i>=0)
{
str[j] = temp[i];
j++;
i--;
}

str[j] = '\0';
return str;
}
int main()
{
printf("%d\n",Myatoi("123"));
printf("%s\n",Myitoa(-234));
return 0;
}
itoa与atoi源代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  itoa ato