十步法将递归程序快速转变成迭代或循环程序

前言:在有些情况下,我们更倾向于使用递归函数,如排序(合并排序)或树相关操作的算法(heapify/ heapify)。但是,递归函数在某些环境中,如在Visual C ++中,如果调用层次过深,可能会出现如堆栈溢出。许多专业的开发人员可能已经知道如何更改递归功能用迭代函数或使用栈(堆栈)和while循环解决(模拟递归的过程)堆栈溢出问题。

        如果您使用的是递归函数,因为你不控制调用堆栈和堆栈大小的限制,当递归调用的深度过深stackoverflow/heap-corruption可能随时发生。如果是换成迭代函数,这个过程还是比较复杂的。但是,为了做到这一点,这里介绍一个十步法将递归程序变换成迭代形式的程序。从而取代使用递归函数的栈,以避免堆栈溢出。下面依次介绍这十条规则。

           

 规则一:1.定义一个结构体,这里取名为:Snapshot.这个结构体主要是用来保存递归程序中每一步用到的数据。

                  2.该结构体Snapshot应该包含如下信息:

                         a.递归函数调用过程变化的参数,当该参数为引用的形式,不需要包含在Snapshot结构体中。如下面的函数:void SomeFunc(int n, int &retVal);

                          参数n须要包括在里面,参数retVal不须要保存。

                          b.过程保存变量"Stage",通常是一个int型变量来标志当前的阶段。具体看【规则六】

                          c.调用递归函数返回的局部变量(在二分递归和嵌套递归中会出现)。

  

[cpp]  view plain copy
  1. //规则一的例子:递归函数  
  2. int SomeFunc(int n, int &retIdx)  
  3. {  
  4.    ...  
  5.    if(n>0)  
  6.    {  
  7.       int test = SomeFunc(n-1, retIdx);  
  8.       test--;  
  9.       ...  
  10.       return test;  
  11.    }  
  12.    ...  
  13.    return 0;  
  14. }   
[cpp]  view plain copy
  1. // 转换成迭代形式  
  2. int SomeFuncLoop(int n, int &retIdx)  
  3. {  
  4.      // (规则一)  
  5.     struct SnapShotStruct {  
  6.        int n;        // - 输入参数  
  7.        int test;     // - 用来保存递归函数返回值的局部变量  
  8.                      //       
  9.                      // - retIdx 参数是引用,这里忽略  
  10.        int stage;    // - 递归函数调用后,需要到哪个阶段(规则六  
  11. // }}   



 

规则二:1.在迭代函数的顶部定义一个局部变量,用来保存递归函数的返回值。

                         a.在迭代函数中,该变量是用来保存递归函数中每一次递归调用的返回值

                         b.如果该递归函数返回值类型为Void, 则可以不定义该局部变量。

                         c.如果有默认的返回值,则用默认的返回值对该局部变量进行初始化。

[cpp]  view plain copy
  1. // 规则二的例子:  
  2. int SomeFunc(int n, int &retIdx)  
  3. {  
  4.    ...  
  5.    if(n>0)  
  6.    {  
  7.       int test = SomeFunc(n-1, retIdx);  
  8.       test--;  
  9.       ...  
  10.       return test;  
  11.    }  
  12.    ...  
  13.    return 0;  
  14. }  



[cpp]  view plain copy
  1. // Conversion to Iterative Function  
  2. int SomeFuncLoop(int n, int &retIdx)  
  3. {  
  4.      // (First rule)  
  5.     struct SnapShotStruct {  
  6.        int n;        // - parameter input  
  7.        int test;     // - local variable that will be used   
  8.                      //     after returning from the function call  
  9.                      // - retIdx can be ignored since it is a reference.  
  10.        int stage;    // - Since there is process needed to be done   
  11.                      //     after recursive call. (Sixth rule)  
  12.     };  
  13.   
  14.     // (Second rule)  
  15.     int retVal = 0;  // initialize with default returning value  
  16.   
  17.     ...  
  18.     // (Second rule)  
  19.     return retVal;  
  20. }   

 

规则三:创建一个包含Snapshot结构的栈容器。

[cpp]  view plain copy
  1. // Recursive Function "Third rule" example  
  2.   
  3. // Conversion to Iterative Function  
  4. int SomeFuncLoop(int n, int &retIdx)  
  5. {  
  6.      // (First rule)  
  7.     struct SnapShotStruct {  
  8.        int n;        // - parameter input  
  9.        int test;     // - local variable that will be used   
  10.                      //     after returning from the function call  
  11.                      // - retIdx can be ignored since it is a reference.  
  12.        int stage;    // - Since there is process needed to be done   
  13.                      //     after recursive call. (Sixth rule)  
  14.     };  
  15.   
  16.     // (Second rule)  
  17.     int retVal = 0;  // initialize with default returning value  
  18.   
  19.     // (Third rule)  
  20.     stack<SnapShotStruct> snapshotStack;  
  21.     ...  
  22.     // (Second rule)  
  23.     return retVal;  
  24. }    


规则四:1.创建一个Snapshot实例,并对里面的参数进行赋初值,然后把它加入到栈容器中。

[cpp]  view plain copy
  1. // Recursive Function "Fourth rule" example  
  2.   
  3. // Conversion to Iterative Function  
  4. int SomeFuncLoop(int n, int &retIdx)  
  5. {  
  6.      // (First rule)  
  7.     struct SnapShotStruct {  
  8.        int n;        // - parameter input  
  9.        int test;     // - local variable that will be used   
  10.                      //     after returning from the function call  
  11.                      // - retIdx can be ignored since it is a reference.  
  12.        int stage;    // - Since there is process needed to be done   
  13.                      //     after recursive call. (Sixth rule)  
  14.     };  
  15.   
  16.     // (Second rule)  
  17.     int retVal = 0;  // initialize with default returning value  
  18.   
  19.     // (Third rule)  
  20.     stack<SnapShotStruct> snapshotStack;  
  21.   
  22.     // (Fourth rule)  
  23.     SnapShotStruct currentSnapshot;  
  24.     currentSnapshot.n= n;          // set the value as parameter value  
  25.     currentSnapshot.test=0;        // set the value as default value  
  26.     currentSnapshot.stage=0;       // set the value as initial stage  
  27.   
  28.     snapshotStack.push(currentSnapshot);  
  29.   
  30.     ...  
  31.     // (Second rule)  
  32.     return retVal;  
  33. }    


 

 规则五:在函数里面建立一个while循环,当栈容器不为空的时候循环,在循环里面弹出一个Snapshot


[cpp]  view plain copy
  1. // Recursive Function "Fifth rule" example  
  2.   
  3. // Conversion to Iterative Function  
  4. int SomeFuncLoop(int n, int &retIdx)  
  5. {  
  6.      // (First rule)  
  7.     struct SnapShotStruct {  
  8.        int n;        // - parameter input  
  9.        int test;     // - local variable that will be used   
  10.                      //     after returning from the function call  
  11.                      // - retIdx can be ignored since it is a reference.  
  12.        int stage;    // - Since there is process needed to be done   
  13.                      //     after recursive call. (Sixth rule)  
  14.     };  
  15.     // (Second rule)  
  16.     int retVal = 0;  // initialize with default returning value  
  17.     // (Third rule)  
  18.     stack<SnapShotStruct> snapshotStack;  
  19.     // (Fourth rule)  
  20.     SnapShotStruct currentSnapshot;  
  21.     currentSnapshot.n= n;          // set the value as parameter value  
  22.     currentSnapshot.test=0;        // set the value as default value  
  23.     currentSnapshot.stage=0;       // set the value as initial stage  
  24.     snapshotStack.push(currentSnapshot);  
  25.     // (Fifth rule)  
  26.     while(!snapshotStack.empty())  
  27.     {  
  28.        currentSnapshot=snapshotStack.top();  
  29.        snapshotStack.pop();  
  30.        ...  
  31.     }  
  32.     // (Second rule)  
  33.     return retVal;  
  34. }    


规则六:     1.当递归函数里面仅有一处自身调用,则将该过程分为两个阶段;第一阶段是在自身调用之前要处理的事情,第二阶段是在自身调用完之后需要处理的事情。

                  2.当递归函数有两处自身调用,则需要将该过程分为三个阶段;Stage 1-->递归调用-->(从第一次递归调用返回)Stage 2(第一阶段递归调用)---》(第二次递归调用返回)Stage 3

                  3.当递归函数有三处自身调用,则需要将该过程分为四个阶段。以此类推。

[cpp]  view plain copy
  1. // Recursive Function "Sixth rule" example  
  2. int SomeFunc(int n, int &retIdx)  
  3. {  
  4.    ...  
  5.    if(n>0)  
  6.    {  
  7.       int test = SomeFunc(n-1, retIdx);  
  8.       test--;  
  9.       ...  
  10.       return test;  
  11.    }  
  12.    ...  
  13.    return 0;  
  14. }  
[cpp]  view plain copy
  1. / Conversion to Iterative Function  
  2. int SomeFuncLoop(int n, int &retIdx)  
  3. {  
  4.      // (First rule)  
  5.     struct SnapShotStruct {  
  6.        int n;        // - parameter input  
  7.        int test;     // - local variable that will be used   
  8.                      //     after returning from the function call  
  9.                      // - retIdx can be ignored since it is a reference.  
  10.        int stage;    // - Since there is process needed to be done   
  11.                      //     after recursive call. (Sixth rule)  
  12.     };  
  13.     // (Second rule)  
  14.     int retVal = 0;  // initialize with default returning value  
  15.     // (Third rule)  
  16.     stack<SnapShotStruct> snapshotStack;  
  17.     // (Fourth rule)  
  18.     SnapShotStruct currentSnapshot;  
  19.     currentSnapshot.n= n;          // set the value as parameter value  
  20.     currentSnapshot.test=0;        // set the value as default value  
  21.     currentSnapshot.stage=0;       // set the value as initial stage  
  22.     snapshotStack.push(currentSnapshot);  
  23.     // (Fifth rule)  
  24.     while(!snapshotStack.empty())  
  25.     {  
  26.        currentSnapshot=snapshotStack.top();  
  27.        snapshotStack.pop();  
  28.        // (Sixth rule)  
  29.        switch( currentSnapshot.stage)  
  30.        {  
  31.        case 0:  
  32.           ...      // before ( SomeFunc(n-1, retIdx); )  
  33.           break;   
  34.        case 1:   
  35.           ...      // after ( SomeFunc(n-1, retIdx); )  
  36.           break;  
  37.        }  
  38.     }  
  39.     // (Second rule)  
  40.     return retVal;  
  41. }   

 

 规则七:根据Snapshot结构体stage的值来分别处理各个过程。

[cpp]  view plain copy
  1. // Recursive Function "Seventh rule" example  
  2. int SomeFunc(int n, int &retIdx)  
  3. {  
  4.    ...  
  5.    if(n>0)  
  6.    {  
  7.       int test = SomeFunc(n-1, retIdx);  
  8.       test--;  
  9.       ...  
  10.       return test;  
  11.    }  
  12.    ...  
  13.    return 0;  
  14. }  


 

[cpp]  view plain copy
  1. // Conversion to Iterative Function  
  2. int SomeFuncLoop(int n, int &retIdx)  
  3. {  
  4.      // (First rule)  
  5.     struct SnapShotStruct {  
  6.        int n;        // - parameter input  
  7.        int test;     // - local variable that will be used   
  8.                      //     after returning from the function call  
  9.                      // - retIdx can be ignored since it is a reference.  
  10.        int stage;    // - Since there is process needed to be done   
  11.                      //     after recursive call. (Sixth rule)  
  12.     };  
  13.   
  14.     // (Second rule)  
  15.     int retVal = 0;  // initialize with default returning value  
  16.   
  17.     // (Third rule)  
  18.     stack<SnapShotStruct> snapshotStack;  
  19.   
  20.     // (Fourth rule)  
  21.     SnapShotStruct currentSnapshot;  
  22.     currentSnapshot.n= n;          // set the value as parameter value  
  23.     currentSnapshot.test=0;        // set the value as default value  
  24.     currentSnapshot.stage=0;       // set the value as initial stage  
  25.   
  26.     snapshotStack.push(currentSnapshot);  
  27.   
  28.     // (Fifth rule)  
  29.     while(!snapshotStack.empty())  
  30.     {  
  31.        currentSnapshot=snapshotStack.top();  
  32.        snapshotStack.pop();  
  33.   
  34.        // (Sixth rule)  
  35.        switch( currentSnapshot.stage)  
  36.        {  
  37.        case 0:  
  38.           // (Seventh rule)  
  39.           if( currentSnapshot.n>0 )  
  40.           {  
  41.              ...  
  42.           }  
  43.           ...  
  44.           break;   
  45.        case 1:   
  46.           // (Seventh rule)  
  47.           currentSnapshot.test = retVal;  
  48.           currentSnapshot.test--;  
  49.           ...  
  50.           break;  
  51.        }  
  52.     }  
  53.     // (Second rule)  
  54.     return retVal;  
  55. }   


 

规则八:1.如果递归函数中有返回值,则在迭代函数中要用一个局部变量来保存循环迭代的结果。如retVal变量            

               2.当循环退出时,该局部变量的值就是递归函数最后的结果

[cpp]  view plain copy
  1. // Recursive Function "Eighth rule" example  
  2. int SomeFunc(int n, int &retIdx)  
  3. {  
  4.    ...  
  5.    if(n>0)  
  6.    {  
  7.       int test = SomeFunc(n-1, retIdx);  
  8.       test--;  
  9.       ...  
  10.       return test;  
  11.    }  
  12.    ...  
  13.    return 0;  
  14. }  


[cpp]  view plain copy
  1. // Conversion to Iterative Function  
  2. int SomeFuncLoop(int n, int &retIdx)  
  3. {  
  4.      // (First rule)  
  5.     struct SnapShotStruct {  
  6.        int n;        // - parameter input  
  7.        int test;     // - local variable that will be used   
  8.                      //     after returning from the function call  
  9.                      // - retIdx can be ignored since it is a reference.  
  10.        int stage;    // - Since there is process needed to be done   
  11.                      //     after recursive call. (Sixth rule)  
  12.     };  
  13.     // (Second rule)  
  14.     int retVal = 0;  // initialize with default returning value  
  15.     // (Third rule)  
  16.     stack<SnapShotStruct> snapshotStack;  
  17.     // (Fourth rule)  
  18.     SnapShotStruct currentSnapshot;  
  19.     currentSnapshot.n= n;          // set the value as parameter value  
  20.     currentSnapshot.test=0;        // set the value as default value  
  21.     currentSnapshot.stage=0;       // set the value as initial stage  
  22.     snapshotStack.push(currentSnapshot);  
  23.     // (Fifth rule)  
  24.     while(!snapshotStack.empty())  
  25.     {  
  26.        currentSnapshot=snapshotStack.top();  
  27.        snapshotStack.pop();  
  28.        // (Sixth rule)  
  29.        switch( currentSnapshot.stage)  
  30.        {  
  31.        case 0:  
  32.           // (Seventh rule)  
  33.           if( currentSnapshot.n>0 )  
  34.           {  
  35.              ...  
  36.           }  
  37.           ...  
  38.           // (Eighth rule)  
  39.           retVal = 0 ;  
  40.           ...  
  41.           break;   
  42.        case 1:   
  43.           // (Seventh rule)  
  44.           currentSnapshot.test = retVal;  
  45.           currentSnapshot.test--;  
  46.           ...  
  47.           // (Eighth rule)  
  48.           retVal = test;  
  49.           ...  
  50.           break;  
  51.        }  
  52.     }  
  53.     // (Second rule)  
  54.     return retVal;  
  55. }   


规则九:1.在递归函数中,如果有return关键字,在循环中要把它变成continue关键字。                             

             a.如果在递归函数中返回一个值,则用一个局部变量保存该值(如果规则八的retVal),然后再Continue。                  

            b.规则九一般是可选的,主要是为了避免逻辑错误。  

 

[cpp]  view plain copy
  1. // Recursive Function "Ninth rule" example  
  2. int SomeFunc(int n, int &retIdx)  
  3. {  
  4.    ...  
  5.    if(n>0)  
  6.    {  
  7.       int test = SomeFunc(n-1, retIdx);  
  8.       test--;  
  9.       ...  
  10.       return test;  
  11.    }  
  12.    ...  
  13.    return 0;  
  14. }  

 

[cpp]  view plain copy
  1. // Conversion to Iterative Function  
  2. int SomeFuncLoop(int n, int &retIdx)  
  3. {  
  4.      // (First rule)  
  5.     struct SnapShotStruct {  
  6.        int n;        // - parameter input  
  7.        int test;     // - local variable that will be used   
  8.                      //     after returning from the function call  
  9.                      // - retIdx can be ignored since it is a reference.  
  10.        int stage;    // - Since there is process needed to be done   
  11.                      //     after recursive call. (Sixth rule)  
  12.     };  
  13.     // (Second rule)  
  14.     int retVal = 0;  // initialize with default returning value  
  15.     // (Third rule)  
  16.     stack<SnapShotStruct> snapshotStack;  
  17.     // (Fourth rule)  
  18.     SnapShotStruct currentSnapshot;  
  19.     currentSnapshot.n= n;          // set the value as parameter value  
  20.     currentSnapshot.test=0;        // set the value as default value  
  21.     currentSnapshot.stage=0;       // set the value as initial stage  
  22.     snapshotStack.push(currentSnapshot);  
  23.     // (Fifth rule)  
  24.     while(!snapshotStack.empty())  
  25.     {  
  26.        currentSnapshot=snapshotStack.top();  
  27.        snapshotStack.pop();  
  28.        // (Sixth rule)  
  29.        switch( currentSnapshot.stage)  
  30.        {  
  31.        case 0:  
  32.           // (Seventh rule)  
  33.           if( currentSnapshot.n>0 )  
  34.           {  
  35.              ...  
  36.           }  
  37.           ...  
  38.           // (Eighth rule)  
  39.           retVal = 0 ;  
  40.             
  41.           // (Ninth rule)  
  42.           continue;  
  43.           break;   
  44.        case 1:   
  45.           // (Seventh rule)  
  46.           currentSnapshot.test = retVal;  
  47.           currentSnapshot.test--;  
  48.           ...  
  49.           // (Eighth rule)  
  50.           retVal = test;  
  51.   
  52.           // (Ninth rule)  
  53.           continue;  
  54.           break;  
  55.        }  
  56.     }  
  57.     // (Second rule)  
  58.     return retVal;  
  59. }   


 

规则十:为了变换递归函数中的递归调用,在迭代函数中新建一个"Snapshot" 对象,并根据递归调用时候的参数来初始 化"Snapshot" 对象的参数。然后加入到容器栈中,加上 Continue. 如果在递归调用之后还有处理语句,更改当

前"Snapshot" 对象的    stage变量的值到相关阶段,在加入新"Snapshot" 对象到容器栈之前,先把当前更新的"Snapshot"对象压入容器栈。

[cpp]  view plain copy
  1. // Recursive Function "Tenth rule" example  
  2. int SomeFunc(int n, int &retIdx)  
  3. {  
  4.    ...  
  5.    if(n>0)  
  6.    {  
  7.       int test = SomeFunc(n-1, retIdx);  
  8.       test--;  
  9.       ...  
  10.       return test;  
  11.    }  
  12.    ...  
  13.    return 0;  
  14. }  


 

[cpp]  view plain copy
  1. // Conversion to Iterative Function  
  2. int SomeFuncLoop(int n, int &retIdx)  
  3. {  
  4.      // (First rule)  
  5.     struct SnapShotStruct {  
  6.        int n;        // - parameter input  
  7.        int test;     // - local variable that will be used   
  8.                      //     after returning from the function call  
  9.                      // - retIdx can be ignored since it is a reference.  
  10.        int stage;    // - Since there is process needed to be done   
  11.                      //     after recursive call. (Sixth rule)  
  12.     };  
  13.     // (Second rule)  
  14.     int retVal = 0;  // initialize with default returning value  
  15.     // (Third rule)  
  16.     stack<SnapShotStruct> snapshotStack;  
  17.     // (Fourth rule)  
  18.     SnapShotStruct currentSnapshot;  
  19.     currentSnapshot.n= n;          // set the value as parameter value  
  20.     currentSnapshot.test=0;        // set the value as default value  
  21.     currentSnapshot.stage=0;       // set the value as initial stage  
  22.     snapshotStack.push(currentSnapshot);  
  23.     // (Fifth rule)  
  24.     while(!snapshotStack.empty())  
  25.     {  
  26.        currentSnapshot=snapshotStack.top();  
  27.        snapshotStack.pop();  
  28.        // (Sixth rule)  
  29.        switch( currentSnapshot.stage)  
  30.        {  
  31.        case 0:  
  32.           // (Seventh rule)  
  33.           if( currentSnapshot.n>0 )  
  34.           {  
  35.              // (Tenth rule)  
  36.              currentSnapshot.stage = 1;            // - current snapshot need to process after  
  37.                                                    //     returning from the recursive call  
  38.              snapshotStack.push(currentSnapshot);  // - this MUST pushed into stack before   
  39.                                                    //     new snapshot!  
  40.              // Create a new snapshot for calling itself  
  41.              SnapShotStruct newSnapshot;  
  42.              newSnapshot.n= currentSnapshot.n-1;   // - give parameter as parameter given  
  43.                                                    //     when calling itself  
  44.                                                    //     ( SomeFunc(n-1, retIdx) )  
  45.              newSnapshot.test=0;                   // - set the value as initial value  
  46.              newSnapshot.stage=0;                  // - since it will start from the   
  47.                                                    //     beginning of the function,   
  48.                                                    //     give the initial stage  
  49.              snapshotStack.push(newSnapshot);  
  50.              continue;  
  51.           }  
  52.           ...  
  53.           // (Eighth rule)  
  54.           retVal = 0 ;  
  55.             
  56.           // (Ninth rule)  
  57.           continue;  
  58.           break;   
  59.        case 1:   
  60.           // (Seventh rule)  
  61.           currentSnapshot.test = retVal;  
  62.           currentSnapshot.test--;  
  63.           ...  
  64.           // (Eighth rule)  
  65.           retVal = test;  
  66.           // (Ninth rule)  
  67.           continue;  
  68.           break;  
  69.        }  
  70.     }  
  71.     // (Second rule)  
  72.     return retVal;  
  73. }   


 


一个详细的例子: 


 

[cpp]  view plain copy
  1. int Fact(long n)  
  2. {  
  3.     if(0>n)  
  4.         return -1;  
  5.     if(0 == n)  
  6.         return 1;  
  7.     else  
  8.     {  
  9.         return ( n* Fact(n-1));  
  10.     }  
  11. }   
  12.   
  13. int FactLoop(long n)  
  14. {  
  15.     // (First rule)  
  16.     struct SnapShotStruct // this can be declared as local structure   
  17.                           //   since it will be only used within this function.  
  18.     {  
  19.         long inputN;      // parameter that changes  
  20.                           // no local variable  
  21.         int stage;        // the stage variable to track where the snapshot has taken  
  22.     } ;  
  23.   
  24.     // (Second rule)  
  25.     int returnVal;        // the return value at the point    
  26.   
  27.     // (Third rule)  
  28.     stack<SnapShotStruct> snapshotStack;  
  29.   
  30.     // (Fourth rule)  
  31.     SnapShotStruct currentSnapshot;  
  32.     currentSnapshot.inputN=n;  
  33.     currentSnapshot.stage=0; // as initial stage  
  34.   
  35.     snapshotStack.push(currentSnapshot);    
  36.   
  37.     // (Fifth rule)  
  38.     while(!snapshotStack.empty())    
  39.     {       
  40.         currentSnapshot=snapshotStack.top();           
  41.         snapshotStack.pop();         
  42.   
  43.         // (Sixth rule)  
  44.         switch(currentSnapshot.stage)  
  45.         {  
  46.             // (Seventh rule)  
  47.         case 0:  
  48.             if(0>currentSnapshot.inputN)  
  49.             {  
  50.                 // (Eighth rule && Ninth rule)  
  51.                 returnVal = -1;  
  52.                 continue;  
  53.             }  
  54.             if(0 == currentSnapshot.inputN)  
  55.             {  
  56.                 // (Eighth rule && Ninth rule)  
  57.                 returnVal = 1;       
  58.                 continue;  
  59.             }  
  60.             else  
  61.             {  
  62.                 // (Tenth rule)  
  63.   
  64.                 // return ( n* Fact(n-1)); this is actually two steps   
  65.                 //  (first calling itself, and second with the value returns multiply with current n value.)  
  66.                 //  this is where we need make a snapshot.  
  67.                 currentSnapshot.stage=1; // current snapshot is done processing and   
  68.                                           //   only waiting for result of calling itself,  
  69.                                           //   so becomes stage "1"  
  70.                 snapshotStack.push(currentSnapshot);  
  71.   
  72.                 // Create a new snapshot for calling itself  
  73.                 SnapShotStruct newSnapshot;  
  74.                 newSnapshot.inputN= currentSnapshot.inputN -1 ; // give parameter as parameter given   
  75.                                                                  //   when calling itself.  
  76.                 newSnapshot.stage = 0 ;                          // since it will start from the begining of   
  77.                                                                  //   the function, give the initial stage  
  78.                 snapshotStack.push(newSnapshot);  
  79.                 continue;  
  80.   
  81.             }  
  82.             break;  
  83.             // (Seventh rule)  
  84.         case 1:  
  85.   
  86.             // (Eighth rule)  
  87.   
  88.             returnVal = currentSnapshot.inputN * returnVal;  
  89.   
  90.             // (Ninth rule)  
  91.             continue;  
  92.             break;  
  93.         }  
  94.     }  
  95.       
  96.     // (Second rule)  
  97.     return returnVal;  
  98. }     
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值