下面程序的功能是用递归法将一个整数存入到一个字符数组中。存放时按逆顺序存放。如483存放成384
程序本身并不难,把它拿出来也就是帮助理解递归的实现过程。
#include
"
stdio.h
"
void convert ( char * a, int n)
{
int i;
if ((i = n / 10 ) != 0 )
convert(a + 1 ,i); // 或者更改为convert((a++,a--),i);
* a = n % 10 + ' 0 ' ;
printf( " %c\n " , * a);
}
void main()
{
int number;
char str[ 10 ] = " " ;
scanf( " %d " , & number);
convert(str,number);
puts(str);
}
void convert ( char * a, int n)
{
int i;
if ((i = n / 10 ) != 0 )
convert(a + 1 ,i); // 或者更改为convert((a++,a--),i);
* a = n % 10 + ' 0 ' ;
printf( " %c\n " , * a);
}
void main()
{
int number;
char str[ 10 ] = " " ;
scanf( " %d " , & number);
convert(str,number);
puts(str);
}
运行结果: