int atoi(const char *str)
{
int numberic = 0;
int sign = 1;
if(str == (void *)0)
{
return numberic;
}
while(*str == ' ')++str;
if(*str == '-' || *str == '+')
{
if(*str == '-')
sign = -1;
++str;
assert(*str);
}
while(*str)
{
unsigned char ch = *str - '0';
if(ch >= 0 && ch <= 9)
{
numberic = numberic*10 + ch;
++str;
}
else
{
break;
}
}
return numberic * sign ;
}
//itoa实现
void Swap(char &a,char &b)
{
a ^= b;
b ^= a;
a ^= b;
}
void itoa(int value,char *str,int radix)
{
int i = 0;
int ret = value;
int j = 0;
assert(str != NULL)
switch(radix)
{
case 2:
case 8:
case 10:
{
while( ret )
{
str[i] = (ret % radix) + '0';
ret = ret / radix ;
++i;
}
break;
}
case 16:
{
while( ret )
{
if(ret % radix >= 10)
{
str[i] = 'A' + (ret % radix) % 10;
}
else
str[i] = (ret % radix) + '0';
ret = ret / radix ;
++i;
}
}
default:
break;
}
for(;j < i / 2 ;j++)
Swap(str[j],str[i-j-1]);
}