修正代码#include <stdio.h> long fun(int g) { /**********found**********/ switch(g); { case 0: return 0; /**********found**********/ case 1 ;case 2 : return 1 ; } return( fun(g-1)+fun(g-2) ); } main() { long fib; int n; printf("Input n: "); scanf("%d",&n); pri
时间: 2023-07-01 16:28:25 浏览: 66
修正后的代码如下:
```c
#include <stdio.h>
long fun(int g) {
switch(g) {
case 0:
return 0;
case 1:
case 2:
return 1;
default:
return (fun(g-1) + fun(g-2));
}
}
int main() {
long fib;
int n;
printf("Input n: ");
scanf("%d", &n);
fib = fun(n);
printf("fib(%d) = %ld\n", n, fib);
return 0;
}
```
修改说明:
1. 删除了 switch 语句后的分号。
2. 添加了 default 分支。
3. 修改了 main 函数的返回类型。
4. 在 main 函数中添加了对计算结果的输出。
这样修改后的代码能够正确计算斐波那契数列的第 n 项,并且不会出现编译错误。
相关问题
#include <stdio.h> char * longestCommonPrefix(char ** strs, int strsSize){ bool flag = true; } 错误在哪里,怎么结局
根据你提供的代码片段,错误可能出现在两个地方:
1. 缺少头文件:在使用 `bool` 类型之前,需要包含 `<stdbool.h>` 头文件。
2. 函数未结束:在 `longestCommonPrefix` 函数中,缺少返回值。你需要根据实际需求确定函数返回值的类型,并使用 `return` 语句返回相应的值。
以下是修正后的代码示例:
```c
#include <stdio.h>
#include <stdbool.h>
char * longestCommonPrefix(char ** strs, int strsSize){
bool flag = true;
// 在这里添加你的代码
return NULL; // 根据实际需求返回相应的值
}
int main() {
// 在这里添加测试代码
return 0;
}
```
修正后的代码中,我们包含了缺少的头文件 `<stdbool.h>`,同时在 `longestCommonPrefix` 函数中添加了返回值。请注意,这只是一个示例,你需要根据具体需求来实现 `longestCommonPrefix` 函数的功能,并在 `main` 函数中进行相应的测试。
#include <stdio.h> #include <stdlib.h> #include <string.h> int (*compare)(float a, float b)
这段代码是C语言中的头文件引用部分,包括了<stdio.h>、<stdlib.h>和<string.h>三个头文件。这些头文件提供了一些常用的函数和类型定义,可以在程序中使用。
具体来说:
- <stdio.h>提供了输入输出相关的函数,比如printf和scanf。
- <stdlib.h>提供了一些通用的函数,比如内存分配函数malloc和随机数生成函数rand。
- <string.h>提供了一些字符串处理函数,比如字符串复制函数strcpy和字符串比较函数strcmp。
至于代码中的int (*compare)(float a, float b),它是一个函数指针声明。这个函数指针指向一个返回类型为int,参数类型为float的函数。可以根据需要将这个函数指针指向不同的函数,以实现不同的比较功能。