解释所需的 C 递归 [英] Explaining needed C recursion
问题描述
我需要有关此特定代码的帮助.我并没有真正完全理解它.有人可以花时间向我解释一下吗?它所做的是取一个单词并打印出它的反面.不过我不明白递归部分,对于 n=1,for 循环只会运行一次,并且递归将运行直到它读取整个单词并满足 ' ' 标记,但是它如何打印出相反的字?
I need some help with this particular code. I do not really understand it to its fullest. Could someone take the time and explain it to me? What it does is take a word and prints out its reverse. I don't understand the recursion part though, for n=1 the for loop will only run once, and the recursion will run until it reads the whole word and it meets the ' ' mark, but then how does it print out the reversed word?
void reverse()
{
char c;
scanf("%c", &c);
if (c!=' ')
{
reverse();
}
printf("%c", c);
}
int main()
{
int n, i;
printf("
The number of the words=");
scanf("%d", &n);
for(i=1; i<=n; ++i)
{
reverse();
printf("
");
}
printf("
End of the program.
");
return 0;
}
推荐答案
NOTE 您的示例在底部,首先是您的标题问题:解释所需的 C 递归
递归是一种允许操作调用自身的编程技术.
一个简单(但毫无意义)的例子是:
Recursion is a programming technique allowing operations to call themselves.
A simple (but meaningless) example would be:
void call(void);
int main(void)
{
call();
}
void call(void)
{
call();
}
注意:这会一直持续到堆栈确定堆积了太多调用,并导致程序崩溃.
Note: This would simply go until the stack determines too many calls have stacked up, and crash the program.
一个更有意义的例子(为了说明)是写一个回文字符系列(A - Z):
A more meaningful example (for illustration) would be to write a palindrome character series (A - Z):
void Palindrome(char a)
{
char b[2];
sprintf(b, "%c", a);
printf(b);
if((a >= 65)&&(a < 90))
{
Palindrome(a+1);
}
sprintf(b, "%c", a);
printf(b);
}
最后一个例子实际上做了第一个例子没有做的两件重要的事情:
1) 有一个受控的退出标准if((a >= 65)&&(a <= 90))
2) 将先前调用的结果与后续调用结合使用.
This last example actually does two important things the first example did not do:
1) has a controlled exit criteria if((a >= 65)&&(a <= 90))
2) uses the results of prior calls with subsequent calls.
在你的例子中:程序工作的原因是每次操作调用自身时,它都在节中嵌套更深(每次调用一个嵌套):(对于所有递归程序都是如此)
In your example: The reason the program works is that each time the operation calls itself, it is nesting deeper (one nest for each call) into the section: (this is true for all recursive programs)
{
reverse();
}
在概念上类似于:
{
//do something
{
//do something
{
//do something
//... and so on for as many recursions are necessary to meet exit criteria
}
}
}
...但是它是如何打印出反向单词的呢?"
只有在递归达到编程限制后,执行才会向下流过结束的 }
并到达下一部分,在那里它继续展开自身,每次访问时,以相反的顺序堆栈, c
的值,直到每个嵌套级别都被展开:
"...but then how does it print out the reversed word?"
Only after the recursion reaches the programmed limit does execution flow down past the closing }
and hit the next section, where it continues to unwind itself, each time accessing, in reverse order of stack, the values of c
until each nested level has been unwound:
} //after exit criteria is met, execution flow goes from here....
printf("%c", c); //prints values of c in reverse order.
} //to here until number of recursions is exhausted.
那时,如果您使用调试器逐步执行,您将看到从上层 }
到下层 }
执行 printf()
每次.
at that time, if you step through using a debugger, you will see flow going from the upper }
to the lower }
executing printf()
each time.
这篇关于解释所需的 C 递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!