【发布时间】:2015-09-13 02:54:47
【问题描述】:
编写一个递归函数,显示由 xs、0s 和 1s 组成的字符串表示的所有二进制(以 2 为底)数字。 xs 代表可以是 0 或 1 的数字。 例如,字符串 xx 代表数字 00,01,10,11。
代码有效,但我很难想象中间步骤。有人可以帮我演练吗?
void get_first_x(char *line,char *line2,char *line3);
void display(char *line);
int main(int argc, const char * argv[]) {
char line[256];
printf("Binary number: ");
scanf("%s",line);
display(line);
return 0;
}
void display(char *line){
char line2[80];
char line3[80];
if(strchr(line,'x') == NULL)
{
printf("%s\n",line);
return;
}
get_first_x(line,line2,line3);
display(line2);
display(line3);
}
void get_first_x(char* line,char* line2,char *line3) {
char* check;
check = strchr(line,'x');
*check = '0';
strcpy(line2,line);
*check = '1';
strcpy(line3,line);
}//replacement of x with 0 and 1. One argument produces 2 strings
这是我的看法
1st call display(xx)
2nd call display(0x)
3rd call display(00) { print statement/ return}
display(1x)
display(01) { print statement/return}
display(10) { print statement/return}
recursion exits
Input: xx
output: 00,01,10,11
I'm not understanding something...here
【问题讨论】: