Skip to content

Commit e284a7e

Browse files
committed
new: add Fib func
1 parent 931f0c6 commit e284a7e

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

c/Fib/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
PROJECT(Fib)
2+
CMAKE_MINIMUM_REQUIRED(VERSION 2.9)
3+
INCLUDE_DIRECTORIES(
4+
${CMAKE_BINARY_DIR}/../include
5+
)
6+
AUX_SOURCE_DIRECTORY(
7+
${CMAKE_BINARY_DIR}/../SRC
8+
DIR_SRC
9+
)
10+
ADD_EXECUTABLE(
11+
Fib
12+
${DIR_SRC}
13+
)

c/Fib/include/Fib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
int Fib(int n);

c/Fib/src/Fib.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "../include/Fib.h"
2+
int Fib(int n)
3+
{
4+
if (n < 1)
5+
{
6+
return -1;
7+
}
8+
else if (n == 1 || n == 2)
9+
{
10+
return 1;
11+
}
12+
else
13+
{
14+
return Fib(n - 1) + Fib(n - 2);
15+
}
16+
}

c/Fib/src/Fib_test.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "../include/Fib.h"
2+
int main(int argc, char argv[])
3+
{
4+
unsigned int n, fib;
5+
scanf("%d", &n);
6+
rewind(stdin);
7+
fib = Fib(n);
8+
if (fib != -1)
9+
{
10+
printf("Fib(%d)=%d", n, fib);
11+
}
12+
else
13+
{
14+
printf("The input is not a valid value.");
15+
}
16+
return 0;
17+
}

0 commit comments

Comments
 (0)