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与atoi源代码
最新推荐文章于 2020-05-04 17:08:37 发布