首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于栈的内容,不能了解的一个地方

2014-01-05 
关于栈的内容,不能理解的一个地方。本帖最后由 u013363978 于 2014-01-03 18:41:41 编辑#include stdio.h

关于栈的内容,不能理解的一个地方。
本帖最后由 u013363978 于 2014-01-03 18:41:41 编辑

#include <stdio.h>
void singTheSong(int numberOfBottles)
{
    if(numberOfBottles == 0)
    {
        printf("There are simply no more bottles of beer on the wall\n.");
    }else{
        printf("%d bottles . %d bootles of beer.\n",numberOfBottles,numberOfBottles);
        int oneFewer = numberOfBottles - 1;
        printf("Take, p, %d bottles of beer on the wall.\n",oneFewer);
        singTheSong(oneFewer);
        printf("Put a bottle, %d empty bottles in the bin.\n",numberOfBottles);
    }
}

int main(int argc, const char * argv[])
{

    singTheSong(9);
    return 0;
}

结果是这样的

3 bottles of beer on the wall. 3 bootles of beer.
Take one down, pass it  around, 2 bottles of beer on the wall.
2 bottles of beer on the wall. 2 bootles of beer.
Take one down, pass it  around, 1 bottles of beer on the wall.
1 bottles of beer on the wall. 1 bootles of beer.
Take one down, pass it  around, 0 bottles of beer on the wall.
There are simply no more bottles of beer on the wall
.Put a bottle in the recycling, 1 empty bottles in the bin.
Put a bottle in the recycling, 2 empty bottles in the bin.
Put a bottle in the recycling, 3 empty bottles in the bin.

对于后面这个最后的三句输出的变量哪里来的自增  不理解  
小弟自学的请各位大哥谅解 
[解决办法]
这是递归,singTheSong一个个返回时,走的printf("Put a bottle,

学习编程,最好先学会单步调试
[解决办法]
你这个递归调用了调用是3->2->1,然后打印顺序就是1->2->3了。楼主可以单步跟踪一下就明白了。
就是你的这段代码:
int oneFewer = numberOfBottles - 1;
printf("Take, p, %d bottles of beer on the wall.\n",oneFewer);
singTheSong(oneFewer);
printf("Put a bottle, %d empty bottles in the bin.\n",numberOfBottles);

热点排行