我得到了輸出 00246 但我不明白代碼,有人可以詳細說明一下嗎?
int print(int nb)
{
if (nb < 0)
{
return (0);
}
printf("%d", nb print(nb - 1));
nb --;
return (nb);
}
int main(void)
{
print(4);
return (0);
}
這是代碼。
uj5u.com熱心網友回復:
我建議您在一張紙上寫下nb發生的不同值。
請注意,此行中的 print 函式:
在呼叫回傳值printf("%d", nb print(nb - 1))之前不會被呼叫。print(nb-1)最終,這意味著列印的第一個值將來自最后一級遞回。
對代碼的更改可以向您展示這是如何作業的:
int print(int nb, int level)
{
printf("\nEnter level %d nb= %d", level, nb);
if (nb < 0)
{
printf("\nExit level %d nb =%d return 0", level, nb);
return (0);
}
printf("\nOriginal Output level %d nb=%d",level, nb print(nb - 1, level 1));
nb --;
printf("\nExit level %d nb=%d", level, nb);
return (nb);
}
int main(void)
{
int level=0;
print(4, level);
return (0);
}
這給出了以下輸出:
Enter level 0 nb= 4
Enter level 1 nb= 3
Enter level 2 nb= 2
Enter level 3 nb= 1
Enter level 4 nb= 0
Enter level 5 nb= -1
Exit level 5 nb =-1 return 0
Original Output level 4 nb=0
Exit level 4 nb=-1
Original Output level 3 nb=0
Exit level 3 nb=0
Original Output level 2 nb=2
Exit level 2 nb=1
Original Output level 1 nb=4
Exit level 1 nb=2
Original Output level 0 nb=6
Exit level 0 nb=3
In the changed code I've added a variable level which tags a number to each level of recursion.
If you follow the output along with the code you can see that at the top level nb starts with a value of 4.
In the line
printf("\nOriginal Output level %d nb=%d",level, nb print(nb - 1, level 1));
you call your print function, this takes you to your next level of recursion where on Entry nb is 3.
At this point the printf statement has not been called as the code requires a return value from the print function.
The code runs until we again call print whereby the code enters the next level of recursion.
At each time print is called all local variable values at that point and a pointer to where the function was called is placed on the stack so that the flow of code can return to where it was called once it has been completed.
This cycle continues until at level 5 where nb is less than 0 and so the code returns the value of 0, it does not reach the printf statement at that level and hence the value of 0 is returned to where the print function was called on level 4. This is done by using and then removing the data that was placed on the stack.
The printf call can now be run as we have a value returned from the print function call. At this point the local value of nb is 0, as indicated by the statement Enter level 4 nb= 0 in the output. This local value is added to the return value from the call to print i.e. 0, 0 0=0 and so you see:
Original Output level 4 nb=0
After the printf code completes for that level nb is decremented returning the value of -1.
Exit level 4 nb=-1
Again the stack is rolled back one level, now at level 3 the return value of -1 is added to the level 3 value of nb (Enter level 3 nb= 1), 1-1=0 so the output of the printf is:
Original Output level 3 nb=0
回圈繼續,直到所有堆疊級別都已回滾。輸出顯示所有階段,直到第 5 級,它們被放置到堆疊中,并且從那里回滾所有階段,直到我們回到第 0 級。
uj5u.com熱心網友回復:
遞回呼叫不會停止,直到我們到達 nb==-1 的呼叫。這將回傳 0。
之前的通話現在可以恢復了。在那次通話中,nb==0。我們將該 0 添加到呼叫回傳的 0 中,其中 nb==-1 得到 0,并列印它。然后我們回傳nb-1(因為我們說nb--),也就是-1。
之前的通話現在可以恢復了。在那次通話中,nb==1。我們將該 1 添加到我們從 nb==0 呼叫中得到的結果中,即 -1。所以我們列印 1 -1 或 0。然后我們從當前的 nb 中減去 1,即 1,得到 0,然后回傳。
之前的通話現在可以恢復了。在那次通話中,nb==2。我們將這個 2 添加到我們從 nb==1 呼叫得到的結果中,即 0,并列印結果 2。然后我們從當前的 nb 中減去 1,即 2,得到 1,并回傳它。
之前的通話現在可以恢復了。在那次通話中,nb==3。我們將這個 3 添加到我們從 nb==2 呼叫得到的結果中,即 1,并列印結果 4。然后我們從當前的 nb 中減去 1,即 3,得到 2,并回傳它。
之前的通話現在可以恢復了。在那次通話中,nb==4。我們將這個 4 添加到我們從 nb==3 呼叫得到的結果中,即 2,并列印結果 6。然后我們從當前的 nb 中減去 1,即 4,得到 3,并將其回傳給 main,然后我們完成了。
所以這就是痕跡。您可以按照自己的建議使用一堆 printf 或在除錯器中執行此操作,就像我一樣。
但問題仍然存在:這是為了什么?它只是一個要解決的神秘程式嗎?如果不是,它可能會被簡化以使其更易于理解——但為此我們必須知道它的用途。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/455060.html
