当前位置: 代码迷 >> C语言 >> 递归问题求助
  详细解决方案

递归问题求助

热度:180   发布时间:2006-04-03 10:19:00.0
递归问题求助

请大家看下下面的程序,当输入是ABC时,输出应该是什么?
我对其中的递归过程很是不理解,请大家帮帮忙啊!高手来帮我解释一下
其中的递归过程吧,谢谢!

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>

char * NumberToChars( char number)
{
char *string;
string = ( char * )malloc( 10 * sizeof( char ) );
switch(number)
{
case '0': string = "000"; break;
case '1': string = "111"; break;
case '2': string = "ABC"; break;
case '3': string = "DEF"; break;
case '4': string = "GHI"; break;
case '5': string = "JKL"; break;
case '6': string = "MNO"; break;
case '7': string = "PRS"; break;
case '8': string = "TUV"; break;
case '9': string = "WXY"; break;
case '*': string = "***"; break;
case '#': string = "###"; break;
default : string = "???"; break;
}
return string;
}

char * ShixianChars( char *number)
{
char *p;
int i;
p = ( char *)malloc( 4 * strlen( number ) * sizeof( char ) );
for( i = 0; i < strlen( number ); i++ )
{
strcpy( p + i * 3, NumberToChars( number[ i ] ) );
}
return p;
}


static void ListMnemonics( char *p, int p1, int start, char *ps, int len )
{
int i;
if( strlen( ps ) == len )
{
printf( "%-10s", ps );
}
else
{
for( i = p1; i < p1 + 3; i++ )
{
if( p[i] == ' ')
{
continue;
}
ps[start] = p[i];
p[i] = ' ';
ps[start+1] = '\0';
ListMnemonics( p, p1+3, start+1, ps, len );
p[i] = ps[start];
}
}
}

main()
{
int len;
char number[10];
char *ps;
printf( "Input the numbers:" );
scanf( "%s", number );
len = strlen( number );
ps = ( char* )malloc( 100 * sizeof( char ) );
ListMnemonics( ShixianChars( number ), 0, 0, ps, len );
getch();
}

搜索更多相关的解决方案: 递归  

----------------解决方案--------------------------------------------------------
以下是引用黑衣圣堂在2006-4-3 10:19:00的发言:

请大家看下下面的程序,当输入是ABC时,输出应该是什么?
我对其中的递归过程很是不理解,请大家帮帮忙啊!高手来帮我解释一下
其中的递归过程吧,谢谢!

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>

char * NumberToChars( char number)
{
char *string;
string = ( char * )malloc( 10 * sizeof( char ) );
switch(number)
{
case '0': string = "000"; break;
case '1': string = "111"; break;
case '2': string = "ABC"; break;
case '3': string = "DEF"; break;
case '4': string = "GHI"; break;
case '5': string = "JKL"; break;
case '6': string = "MNO"; break;
case '7': string = "PRS"; break;
case '8': string = "TUV"; break;
case '9': string = "WXY"; break;
case '*': string = "***"; break;
case '#': string = "###"; break;
default : string = "???"; break;
}//对指针有错误认识才会写这样的代码,好好看看指针吧!char * 与c++中的String完全不是一会事,只有String string;才能string = "ABC";而char * string;string = ( char * )malloc( 10 * sizeof( char ) );则必须
strcpy(string,"ABC");
return string;
}

char * ShixianChars( char *number)
{
char *p;
int i;
p = ( char *)malloc( 4 * strlen( number ) * sizeof( char ) );
for( i = 0; i < strlen( number ); i++ )
{
strcpy( p + i * 3, NumberToChars( number[ i ] ) );
}
return p;
}


static void ListMnemonics( char *p, int p1, int start, char *ps, int len )
{
int i;
if( strlen( ps ) == len )
{
printf( "%-10s", ps );
}
else
{
for( i = p1; i < p1 + 3; i++ )
{
if( p[i] == ' ')
{
continue;
}
ps[start] = p[i];
p[i] = ' ';
ps[start+1] = '\0';
ListMnemonics( p, p1+3, start+1, ps, len );
p[i] = ps[start];
}
}
}

main()
{
int len;
char number[10];
char *ps;
printf( "Input the numbers:" );
scanf( "%s", number );
len = strlen( number );
ps = ( char* )malloc( 100 * sizeof( char ) );
ListMnemonics( ShixianChars( number ), 0, 0, ps, len );
getch();
}


----------------解决方案--------------------------------------------------------
但我运行后为什么能正确运行?
----------------解决方案--------------------------------------------------------
看得头晕,虽然能正确运行,但这句变成多余的了:
string = ( char * )malloc( 10 * sizeof( char ) );

----------------解决方案--------------------------------------------------------

  相关解决方案