我已经编写了代码来查找Fibonacci编号以及进行调用以查找C语言中的递归版本的次数。我无法删除编译错误。请帮忙。
代码如下:
#include <stdio.h>
int main(int fib) {
int n,m, count=0; //'count' counts #times function is called
printf("enter n");
scanf("%d",&n);
return fib_rec(n, &count);
}
int fib_rec(int n, int *count)
{
int b=0,c=1;
*count = *count +1;
if(n<=1)
{
return n;
}
else
{
printf (count);
return fib_rec(n-1, *count)+ fib_rec(n-2, *count);
}
}
下面在reptl.it站点上运行时给出了编译错误,如图所示。
main.c: In function 'main':
main.c:7:10: warning: implicit declaration of function 'fib_rec' [-
Wimplicit-function-declaration]
return fib_rec(n, &count);
^~~~~~~
main.c: In function 'fib_rec':
main.c:20:13: warning: passing argument 1 of 'printf' from incompatible
pointer type [-Wincompatible-pointer-types]
printf (count);
^~~~~
In file included from main.c:1:
/usr/include/stdio.h:364:43: note: expected 'const char * restrict' but
argument is of type 'int *'
extern int printf (const char *__restrict __format, ...);
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
main.c:21:25: warning: passing argument 2 of 'fib_rec' makes pointer from
integer without a cast [-Wint-conversion]
return fib_rec(n-1, *count)+ fib_rec(n-2, *count);
^~~~~~
main.c:10:25: note: expected 'int *' but argument is of type 'int'
int fib_rec(int n, int *count)
~~~~~^~~~~
main.c:21:47: warning: passing argument 2 of 'fib_rec' makes pointer from
integer without a cast [-Wint-conversion]
return fib_rec(n-1, *count)+ fib_rec(n-2, *count);
^~~~~~
main.c:10:25: note: expected 'int *' but argument is of type 'int'
int fib_rec(int n, int *count)
~~~~~^~~~~
enter n 10
exit status -1
>
答案 0 :(得分:3)
我将您的代码粘贴到https://tio.run/#c-gcc上,结果如下:
main
原型.code.tio.c:3:7: warning: ‘main’ takes only zero or two arguments [-Wmain]
int main(int fib) {
^~~~
main
函数原型是int main(void)
或int main(int argc, char *argv[])
由于您希望用户输入数字,因此可以选择第一种形式。
count
类型.code.tio.c: In function ‘main’:
.code.tio.c:7:5: error: ‘count’ undeclared (first use in this function)
count = 0; // counts the number of times the function is called
^~~~~
您必须为count
变量指定类型,例如
int count = 0;
fib_rec
声明.code.tio.c:8:12: warning: implicit declaration of function ‘fib_rec’ [-Wimplicit-function-declaration]
return fib_rec(n, &count);
^~~~~~~
在使用该函数之前,您尚未声明该函数。
您可以这样声明:int fib_rec(int n, int *count)
例如在main
定义之前。
printf
的用法.code.tio.c: In function ‘fib_rec’:
.code.tio.c:21:15: warning: passing argument 1 of ‘printf’ from incompatible pointer type [-Wincompatible-pointer-types]
printf (count);
^~~~~
printf
函数要求某些格式。如果要显示整数值,请使用%d
:
printf("count value is: %d\n", count);
.code.tio.c:22:27: warning: passing argument 2 of ‘fib_rec’ makes pointer from integer without a cast [-Wint-conversion]
return fib_rec(n-1, *count)+ fib_rec(n-2, *count);
^~~~~~
这里count
已经是整数的指针,*
是不必要的:
return fib_rec(n-1, count)+ fib_rec(n-2, count);
您的代码返回计算值,但不显示它。 为此,用
替换return fib_rec(n, &count);
printf("fib_rec(%d) = %d\n", n, fib_rec(n, &count));
return 0;
因此,更正后的代码可能是:
#include <stdio.h>
int fib_rec(int n, int *count);
int main(void) {
int n;
printf("enter n\n");
scanf("%d",&n);
int count = 0; // counts the number of times the function is called
printf("fib_rec(%d) = %d\n", n, fib_rec(n, &count));
return 0;
}
int fib_rec(int n, int *count)
{
int b=0,c=1;
*count = *count +1;
if(n<=1)
{
return n;
}
else
{
printf ("count: %d\n", *count);
return fib_rec(n-1, count)+ fib_rec(n-2, count);
}
}
答案 1 :(得分:1)
在main
main
的原型为int main(void)
或int main(int argc, int argc *[])
。 fib
不是main
的有效输入。
main
通常将返回0
。因此,fib_rec(n, &count);
行应放在退货单之前。
变量count
。变量m
未使用。
在main
函数之前,应该存在fib_rec
的声明。 int fib_rec(int n, int *count);
在fib_rec
printf
的功能不正确。 -应该是%d
和*count
递归调用不正确,由于count
中的fib_rec
是一个指针,因此可以直接将其直接传递给函数而无需获取值。像这样-return (fib_rec(n-1, count)+ fib_rec(n-2, count));
未使用的变量b
和c
这解决了编译问题。代码在下面。
int fib_rec(int n, int *count);
int main(void) {
int n;
int count;
printf("enter n");
scanf("%d",&n);
count = 0; // counts the number of times the function is called
fib_rec(n, &count);
return 0;
}
int fib_rec(int n, int *count)
{
*count = *count +1;
if ((n<=1) )
{
return 1;
}
else
{
printf ("%d ", n);
return (fib_rec(n-1, count)+ fib_rec(n-2, count));
}
}