如何在ANSI C中退出递归

时间:2014-05-29 01:55:19

标签: c recursion factorial

我正在编写一个简单的程序来计算ANSI C中的阶乘。程序可以正常工作,因为过去13这个数字对于INT来说太大了。我想要做的是打印前12个,然后以某种方式 BREAK 并发送第13个数字的错误。但问题是,当我到达第13个数字并发送错误时,前12个不打印。是否可以返回错误并仍然获得前12?

我的代码:

#include <math.h>
#include<stdio.h>

int getIntFactorial(int x){

printf("Processing factorial of (%d) \n", x);
if (x <=1){
    printf("\n Reached base case, returning %d \n", x);
    printf("Now returning total value \n");
    return 1;
}else if (x < 13){
    printf("\n Doing recursion by calling factorial (%d - 1) \n", x);

    int counter = x * getIntFactorial(x - 1);

    printf("Receiving results of factorial (%d) = %d * %d = %d \n", x, x, (x-1), counter);
    return counter;
}else {
///number is greater than 13
    printf("Sorry, we cannot do the factorial for %d, only goes up to 12 \n", x);
    return;
}



}


main()
{
    getIntFactorial(13);


}

输出13:

Processing factorial of (13)
Sorry, we cannot do the factorial for 13, only goes up to 12

Process returned 62 (0x3E)   execution time : 0.009 s
Press any key to continue.

输出为12:

Processing factorial of (12)

 Doing recursion by calling factorial (12 - 1)
Processing factorial of (11)

 Doing recursion by calling factorial (11 - 1)
Processing factorial of (10)

 Doing recursion by calling factorial (10 - 1)
Processing factorial of (9)

 Doing recursion by calling factorial (9 - 1)
Processing factorial of (8)

 Doing recursion by calling factorial (8 - 1)
Processing factorial of (7)

 Doing recursion by calling factorial (7 - 1)
Processing factorial of (6)

 Doing recursion by calling factorial (6 - 1)
Processing factorial of (5)

 Doing recursion by calling factorial (5 - 1)
Processing factorial of (4)

 Doing recursion by calling factorial (4 - 1)
Processing factorial of (3)

 Doing recursion by calling factorial (3 - 1)
Processing factorial of (2)

 Doing recursion by calling factorial (2 - 1)
Processing factorial of (1)

 Reached base case, returning 1
Now returning total value
Receiving results of factorial (2) = 2 * 1 = 2
Receiving results of factorial (3) = 3 * 2 = 6
Receiving results of factorial (4) = 4 * 3 = 24
Receiving results of factorial (5) = 5 * 4 = 120
Receiving results of factorial (6) = 6 * 5 = 720
Receiving results of factorial (7) = 7 * 6 = 5040
Receiving results of factorial (8) = 8 * 7 = 40320
Receiving results of factorial (9) = 9 * 8 = 362880
Receiving results of factorial (10) = 10 * 9 = 3628800
Receiving results of factorial (11) = 11 * 10 = 39916800
Receiving results of factorial (12) = 12 * 11 = 479001600

Process returned 479001600 (0x1C8CFC00)   execution time : 0.015 s
Press any key to continue.

3 个答案:

答案 0 :(得分:2)

你并不完全清楚你在这里要做什么。很清楚的是,根据您的代码,您观察到的行为是正确的:当您尝试呼叫getIntFactorial(13)时,前十二个数字的阶乘不会打印出来。你的代码是这样做的。

伪代码中getIntFactorial的结构如下:

if the number is less than or equal to 1:
    return 1;
if the number is less than 13:
    return the number times the factorial of the number minus one
if the number is greater than or equal to 13:
    print an error and return nothing

顺便提一下,请注意,如果您的输入大于13,则在不返回任何内容的情况下调用return。您的函数声称返回int 某事。您返回的没有 非常不同 。你的编译器应该抱怨,它应该大声抱怨。如果它没有,你应该换一个新的。

答案 1 :(得分:1)

根据我对要求的理解,将return;块中的else更改为return getIntFactorial(x-1)会产生您想要的结果吗?

答案 2 :(得分:0)

您可以测试您的参数是否首先超过12,如果是,请打印您的错误消息,然后使用12作为参数调用递归函数。