首先看atoi函数:
int atoiOwn(const char *a)
{
int val=;
bool b_plus=true;//判断符号
switch(*a) //过滤符号
{
case '+':
a++;
break;
case '-':
a++;
b_plus=false;
break;
default:
break;
} while(*a>='0'&&*a<='9') //可以用isdigit判断。
{
val=val*+(*a-'');
a++;
}
if(!b_plus)
val=-val;
return val;
}
int main()
{ char a[];
while(scanf("%s",a)!=EOF)
{
int ret=atoiOwn(a);
printf("%d\n",ret);
}
}
char * itoa ( int value, char * str, int base );
Converts an integer value to a null-terminated string using the specified base and stores the result in the array given by str parameter.
If base is 10 and value is negative, the resulting string is preceded with a minus sign (-). With any other base, valueis always considered unsigned.
str should be an array long enough to contain any possible value: (sizeof(int)*8+1) for radix=2, i.e. 17 bytes in 16-bits platforms and 33 in 32-bits platforms.
A standard-compliant alternative for some cases may be sprintf:
- sprintf(str,"%d",value) converts to decimal base.
- sprintf(str,"%x",value) converts to hexadecimal base.
- sprintf(str,"%o",value) converts to octal base.
以下的代码只是模拟了部分功能:
#include<stdio.h>
void itoa(int value, char *str)
{
if (value < ) //如果是负数,则str[0]='-',并把value取反(变成正整数) {
str[] = '-';
value = -value;
}
int i,j;
for(i=; value > ; i++,value/=) //从value[1]开始存放value的数字字符,不过是逆序,等下再反序过来 str[i] = value%+''; //将数字加上0的ASCII值(即'0')就得到该数字的ASCII值 for(j=i-,i=; j-i>=; j--,i++) //将数字字符反序存放 {
str[i] = str[i]^str[j];
str[j] = str[i]^str[j];
str[i] = str[i]^str[j];
}
if(str[] != '-') //如果不是负数,则需要把数字字符下标左移一位,即减1 {
for(i=; str[i+]!='\0'; i++)
str[i] = str[i+];
str[i] = '\0';
}
} void main()
{
int value = -;
char str[10] = {'\0'}; //记得把str全填充为'\0' 这个错误,其实这样赋值只是把第
1个元素赋值为\0,后面的都默认用\0填充,如果是char str[10]={'1'};
只有第一个为‘1’,后面都是\0.但千万不要以为写成char str[10];不赋值也可以。这样写里面的内容是乱的。
itoa(value, str);
printf("The result is:%s\n", str);
}
另一种写法:
void intToStr(int num,char str[])
{
int i=,j=,isNeg=;
if(num<)
{
num*=-;
isNeg=;
}
do{
str[i++]=(num%)+'';
num/=;
}while(num); if(isNeg)
str[i++]='-'; //reverse the characher;
for(int m=,n=i-;m<n;m++,n--)
swap(str[m],str[n]); str[i]='\0';
}
为什么写成:
do{
str[i++]=(num%10)+'0';
num/=10;
}while(num);
而不是
while(num)
{
}
因为当输入为0时,while(num)一次都不会执行,导致最后输出的是空串。因为至少要执行一次,所以用do while.
更好的办法:
http://blog.csdn.net/solstice/article/details/5139302
http://*.com/questions/3440726/what-is-the-proper-way-of-implementing-a-good-itoa-function
参考;http://www.cppblog.com/lizhongxu2008/archive/2009/02/11/73470.html
许多实现:http://www.jb.man.ac.uk/~slowe/cpp/itoa.html#dev
/** * Ansi C "itoa" based on Kernighan & Ritchie's "Ansi C": */ void strreverse(char* begin, char* end) { char aux; while(end>begin) aux=*end, *end--=*begin, *begin++=aux; } void itoa(int value, char* str, int base) { static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz"; char* wstr=str; int sign; // Validate base if (base< || base>){ *wstr='\0'; return; } // Take care of sign if ((sign=value) < ) value = -value; // Conversion. Number is reversed. do *wstr++ = num[value%base]; while(value/=base); if(sign<) *wstr++='-'; *wstr='\0'; // Reverse string strreverse(str,wstr-); } /** * Ansi C "itoa" based on Kernighan & Ritchie's "Ansi C" * with slight modification to optimize for specific architecture: */ void strreverse(char* begin, char* end) { char aux; while(end>begin) aux=*end, *end--=*begin, *begin++=aux; } void itoa(int value, char* str, int base) { static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz"; char* wstr=str; int sign; div_t res; // Validate base if (base< || base>){ *wstr='\0'; return; } // Take care of sign if ((sign=value) < ) value = -value; // Conversion. Number is reversed. do { res = div(value,base); *wstr++ = num[res.rem]; }while(value=res.quot); if(sign<) *wstr++='-'; *wstr='\0'; // Reverse string strreverse(str,wstr-); }