content
How to make internal declarations
Declare and define external functions
Compiling and using static library files
Two necessary conditions for recursion
Exercise 1: Take an integer value (unsigned) and print its bits in order.
Exercise 3: Use recursion to find the factorial of n. (regardless of overflow)
Exercise 4: Find the nth Fibonacci number. (regardless of overflow)
Exercise 5: Implementing Fibonacci Numbers Non-Recursively
Function declaration:
1. Tell the compiler what a function is called, what the parameters are, and what the return type is. But does it exist specifically, the functionThe statement does not decide.2. The declaration of a function generally appears before the use of the function. To satisfy the declaration before use .3. Function declarations are generally placed in header files.
How to make internal declarations
When we put the function definition before the main function, since the program will be executed in order from top to bottom, when the fun function is defined, the fun function is also declared
After we put the defined fun function in the main function, a warning will be reported.
To avoid this from happening, we only need to declare the function before the main function
There is no warning at this time. There are two ways to write this. The type of the parameter in the parentheses is clearly stated or a variable of this type is created.
How to use external functions
When we want to use a function in another .c file, we need to declare it or add extern to use it directly
Use the functions in test1.c in test.c and use exrrn
Declare and define external functions
We want to use the fun function in test1.c in test.c. First, we need to create a header file to declare the fun function.
Secondly, define the fun function in test1.c
After the definition, if you want to reference it in test.c, you only need to input the header file that defines the fun function into test.c
At this point, you can use the add function in test.c normally.
Compiling and using static library files
When someone wants to use our function, and we are unwilling to give him the code, we can generate a static library file at this time and let him use it
Generate static library files
Edit the function to be given to others into a static library file, first click the project name, and then select Properties
In General select the configuration type, then select the static library file and apply
Press ctrl+F5, the static library file will be generated at this time
We find the .lib file in the file directory
Copy it and the .h file to the file that needs to be sent
At this point, in the project we need to use, add the .h file we just copied to, and reference the header file
Then enter #pragma comment (lib, "add.lib"), then it can run successfully
function recursion
The programming trick by which a program calls itself is called recursion .Recursion as an algorithm is widely used in programming languages. A procedure or function is directly or indirectly in its definition or specificationcall its ownA method that usually transforms a large and complex problem into a smaller problem similar to the original problem to solve,recursive strategyOnly a small amount of programs can be used to describe the repeated calculations required for the problem-solving process, which greatly reduces the code amount of the program.The main way of thinking about recursion is: make big things small
Two necessary conditions for recursion
There is a constraint, when this constraint is met, the recursion does not continue.It gets closer and closer to this limit after each recursive call.
Exercise 1: Take an integer value (unsigned) and print its bits in order.
#include<stdio.h>
int fun(int a)
{
if (a > 9)
{
fun(a / 10);
}
printf("%d ", a%10);
}
int main()
{
int a = 1234;
fun(a);
return 0;
}
Exercise 1 thought analysis:
Exercise 2: Write a function that does not allow the creation of temporary variables to find the length of a string.
#include<stdio.h>
int fun(char * b)
{
if (*b != '\0')
return 1 + fun(b + 1);
else
return 0;
}
int main()
{
char a[] ="abcdef";
printf("%d",fun(a));
return 0;
}
Exercise 3: Use recursion to find the factorial of n . (regardless of overflow)
#include<stdio.h>
int fun(int b)
{
if (b <= 1)
return 1;
else
return b*fun(b-1);
}
int main()
{
int a =5;
printf("%d",fun(a));
return 0;
}
Exercise 4: Find the nth Fibonacci number. (regardless of overflow)
#include<stdio.h>
int fun(int b)
{
if (b <=2)
return 1;
else
return fun(b-2)+fun(b-1);
}
int main()
{
int a =10;
printf("%d",fun(a));
return 0;
}
Exercise 5: Implementing Fibonacci Numbers Non-Recursively
#include<stdio.h>
int fun(int x)
{
int a = 1;
int b = 1;
int c = 1;
while (x >= 3) //前俩个数都为 1 1
{
c = a + b;
a = b;
b = c;
x--;
}
return c;
}
int main()
{
int a =10;
printf("%d",fun(a));
return 0;
}
Exercise 6 : Arrange the characters in the parameter string in reverse order instead of printing them in reverse order.
Requirement: String manipulation functions in the C library cannot be used.
for example:
#include<stdio.h>
void fun(char *b)
{
if (*b != '\0')
fun(b + 1);
if (*b == '\0')
{
return;
}
printf("%c", *b);
}
int main()
{
char a[] ="abcdefg";
fun(a);
return 0;
}
Exercise 7: Tower of Hanoi
Rearrange the discs on another post in order of size, starting from the bottom. And it is stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved at a time between the three posts
If there is a plate on A, move A directly to C
If there are two plates on A, move the first plate on A to B, the bottom plate to C, and then move the plate on B to C
If there are three plates on A, move the first plate to C, the second plate to B, then move the plate on C to B, then move the plate at the bottom of A to C, and then move the first plate on B to B. One moves to A, then moves B to C, moves A to C
#include<stdio.h>
void move(char A,char B)
{
printf("%c-%c ", A, B); //移动盘子的函数
}
void Han(int a,char z,char x,char c) //a:盘子的个数 z:其实的柱子,x:经过x进行转移的柱子 c:目的柱子
{
if (a == 1)
{
move(z, c);
}
else
{
Han(a - 1, z, c, x); //如果盘子个数大于1,则先通过第三个柱子把除底部最后一个盘子的其余盘子移动到第二个柱子
move(z,c); // 接着把第一个柱子上最后一个盘子,移动到C,
Han(a - 1, x, z, c);// 把第二根柱子上的盘子,通过第一个柱子移动到第三跟柱子上
}
}
int main()
{
Han(1, 'A', 'B', 'C');
return 0;
}
We found a problem ; when finding Fibonacci numbers recursively, when the number is too large, the program will crash
At this time, the program will not print. This is because when the Fibonacci sequence is calculated, each number will call the function twice and open up two spaces. When the space is all used up and we are still opening up space, at this time program will crash
How to solve the above problem:1. Rewrite recursion into non-recursive.2. Use static objects instead of nonstatic local objects. In recursive function design, static objects can be used insteadnonstatic local objects (ie stack objects), which do notIt can only reduce the overhead of generating and releasing nonstatic objects on each recursive call and return, and static objects can also preserveStores the intermediate state of the recursive call, and can beAccessed by each call layer.
hint:
1. Many problems are explained in recursive form simply because it is clearer than the non-recursive form.2. But iterative implementations of these problems are often more efficient than recursive implementations, although the code is slightly less readable.3. When a problem is quite complex and difficult to implement iteratively, the simplicity of recursive implementation can compensate for the runtime development it brings.pin.