简体   繁体   English

在C中递归计算Fibonacci数

[英]Calculating Fibonacci Numbers Recursively in C

I'm trying to learn C by writing a simple program to output Fibonacci numbers. 我正在尝试通过编写一个简单的程序来输出Fibonacci数来学习C. It isn't working. 它不起作用。

fibonacci.h fibonacci.h

unsigned int fibonacci_recursive(unsigned int n);

fibonacci.c fibonacci.c

#include <stdio.h>
#include "fibonacci.h"

main() {
    unsigned int i;
    for (i = 0; i < 10; i++) {
        printf("%d\t%n", fibonacci_recursive(i));
    }
    getchar();
}

fibonacci_recursive.c fibonacci_recursive.c

unsigned int fib_rec(unsigned int n);

main(unsigned int n) {
     return fib_rec(n);
}

unsigned int fib_rec(unsigned int n) {
    if (n == 0) {
        return 0;
     } 
     if (n == 1) {
           return 1;
     }
     return fib_rec(n - 1) + fib_rec(n - 2);
}

This is the error message VS 2010 gives me when I try to build the project: 这是我尝试构建项目时VS 2010给出的错误消息:

1>ClCompile:
1>  fibonacci_recursive.c
1>fibonacci_recursive.obj : error LNK2005: _main already defined in fibonacci.obj
1>fibonacci.obj : error LNK2019: unresolved external symbol _fibonacci_recursive referenced in function _main
1>c:\users\odp\documents\visual studio 2010\Projects\Fibonacci\Debug\Fibonacci.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>

What am I doing wrong here? 我在这做错了什么? Thanks for helping someone new to C. 感谢您帮助C新手。

Your approach seems strange, you should have: 你的方法看起来很奇怪,你应该:

  • a main file (example main.c ) with the main method and that includes fibonacci.h 主文件(例如main.c )和main方法,包括fibonacci.h
  • a fibonacci.h with the prototype unsigned int fibonacci_recursive(unsigned int n); 一个fibonacci.h ,原型unsigned int fibonacci_recursive(unsigned int n);
  • a fibonacci.c with the implementation of the method, and it should include fibonacci.h too 一个fibonacci.c与该方法的实现,它也应该包括fibonacci.h

Actually you define main function twice too.. 实际上你也定义了两次main函数..

main.c main.c中

#include <stdio.h>
#include "fibonacci.h"

main()
{
    unsigned int i;
    for (i = 0; i < 10; i++)
    {
        printf("%d\t%n", fibonacci_recursive(i));
    }
    getchar();
}

fibonacci.h fibonacci.h

unsigned int fibonacci_recursive(unsigned int n);

fibonacci.c fibonacci.c

#include "fibonacci.h"
unsigned int fibonacci_recursive(unsigned int n)
{
    if (n == 0) 
    {
        return 0;
     } 
     if (n == 1) {
           return 1;
     }
     return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2);
}

You have the main() function defined twice in your project. 您在项目中定义了两次main()函数。 This is the entry point of your program, and you only need one. 这是您的程序的入口点,您只需要一个。

You need \\n not %n for your printf. 你的printf需要\\ n而不是%n。 Also, you can simplify as: 此外,您可以简化为:

#include "fibonacci.h"

unsigned int fibonacci_recursive(unsigned int n) {
if (n < 2) 
    return n;
else
    return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2);
}

您尚未创建在fibonacci.h中声明的fibonacci_recursive函数。

您声明了两个main()函数,新行字符为'\\ n'

Well, I preface that recursive function is not an efficient method to calculate Fibonacci and it may be used for dev training/demonstrations purposes only, because every recursion is stored in stack, and it may also overflow for large fibonacci numbers. 好吧,我的前言是递归函数不是计算Fibonacci的有效方法,它可能仅用于开发训练/演示目的,因为每个递归都存储在堆栈中,并且它也可能溢出大的斐波纳契数。 It is rather worth the effort to write down a more efficient Fibonacci function that uses a loop, like following code: 写下一个使用循环的更有效的Fibonacci函数是非常值得的,如下面的代码:

#include <stdio.h>

#define MAX_ITERS 20


int fibonacci(int);

int main(int argc, char *argv[])
{
    unsigned int iters;

    if(argc>1) {
        iters=atoi(argv[1]);
    } else
        iters=MAX_ITERS;

    fibonacci(iters);

    return 0;
}

int fibonacci(int iterations)
{
   unsigned register int i;
   double first=0.0, second = 1.0, lastsum;
   printf("First %d iterations of Fibonacci series are :\n",iterations);

   for ( i = 0 ; i < iterations ; i++ )
   {
      if ( i <= 1 )
         lastsum = (double)i;
      else
      {
         lastsum = first + second;
         first = second;
         second = lastsum;
      }
      printf("%.0f\n",lastsum);
   }

}

Try to compare by your own, running ./fibonacci 50 with this method, for instance on a low cost processor (eg. on a Raspberry PI), and the one with the recursive functions and 50 first numbers as well, and see the difference! 尝试用你自己的方法进行比较,用这种方法运行./fibonacci 50 ,例如在低成本处理器上(例如在Raspberry PI上),以及具有递归函数和50个第一个数字的处理器,并查看差异! ,-) , - )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM