为了加深对函数递归调用过程中的理解,本Demo程序特意在VS2008 C#控制台程序实现了阶乘的计算功能,用于观察函数递归调用过程中的调用堆栈的情况。

源码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RecursiveTset
{
    class Program
    {
        //阶乘的定义:n!=n*(n-1)!,特别的,1!=1;0!=1
        //阶乘的实现:采用递归调用方式。主要测试和观察递归过程中的函数堆栈的调用情况!
        public static int JiechengFun(int num)
        {
            int result=1;
            if (num == 0)
                result = 1;
            if (num >= 1)
                result = num * JiechengFun(num - 1);
            return result;
        }

        static void Main(string[] args)
        {
            int res = Program.JiechengFun(4);
            System.Console.WriteLine(res);
        }
    }
}

函数递归调用过程中的调用堆栈的情况截图如下:

函数递归调用过程中的调用堆栈的情况

源码下载:https://pan.baidu.com/s/18SHyws1vX2a-fvbT-nQUtw

相关文章:

  • 2022-12-23
  • 2021-06-23
  • 2022-01-07
  • 2021-12-15
猜你喜欢
  • 2021-12-02
  • 2021-05-20
  • 2021-06-12
  • 2021-11-28
  • 2021-04-16
  • 2021-09-14
  • 2021-12-14
相关资源
相似解决方案