在c运算Fibonacci数递归 [英] Calculating Fibonacci Numbers Recursively in C
本文介绍了在c运算Fibonacci数递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过编写一个简单的程序来输出斐波那契数学C。这是行不通的。
I'm trying to learn C by writing a simple program to output Fibonacci numbers. It isn't working.
fibonacci.h
unsigned int fibonacci_recursive(unsigned int n);
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
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);
}
这是VS 2010的出错信息给我,当我尝试生成项目:
This is the error message VS 2010 gives me when I try to build the project:
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>
我在做什么错在这里?感谢您的帮助别人新的C
What am I doing wrong here? Thanks for helping someone new to C.
推荐答案
您的方法似乎很奇怪,你应该有:
Your approach seems strange, you should have:
- 主文件(例如
的main.c
)的主要方法,这包括fibonacci.h
- 在
fibonacci.h
与原型unsigned int类型fibonacci_recursive(无符号整数N);
- 在
fibonacci.c
与方法的实现,它应该包括fibonacci.h
太
- a main file (example
main.c
) with the main method and that includesfibonacci.h
- a
fibonacci.h
with the prototypeunsigned int fibonacci_recursive(unsigned int n);
- a
fibonacci.c
with the implementation of the method, and it should includefibonacci.h
too
其实你定义主
功能太两倍。
Actually you define main
function twice too..
的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
unsigned int fibonacci_recursive(unsigned int n);
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);
}
这篇关于在c运算Fibonacci数递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文