diff --git a/day8/homework/h2/README.md b/day8/homework/h2/README.md new file mode 100644 index 0000000..24275c1 --- /dev/null +++ b/day8/homework/h2/README.md @@ -0,0 +1,24 @@ +## 题目要求: 创建 a.h、a.c,、b.h、b.c 四个文件,在 a 文件中创建求阶乘函数并实现,在 b 文件中创建数组的排序函数并实现,要求将 a,b 编译成静态库文件。并编写 test.c 文件,引入 a 和 b 两个文件的功能并编译运行。 + +> 1. `libs` 中包含使用 `tempfiles` 文件夹中的源代码生成的静态库 +> 2. `includes` 中包含 `test*.c` 需要用到的头文件 + +### 生成静态库 + +```bash +# 汇编源文件: a.s 生成目标文件 a.o +gcc -c a.c -o a.o + +# 使用上一步生成的目标文件 a.o 生成静态库 liba.a +ar rc liba.a a.o +``` + +### 编译时指定头文件可以使用 + +```bash +# 编译时指定头文件和静态库的位置,生成可执行文件 testFactorial,-la 表示链接静态库 liba.a +gcc -static testFactorial.c -I ../includes -L ../libs -la -o testFactorial + +# 编译时指定头文件和静态库的位置,生成可执行文件 testArrSort,-lb 表示链接静态库 libb.a +gcc -static testArrSort.c -L ./libs/ -lb -I ./includes/ -o testArrSort +``` diff --git a/day8/homework/h2/includes/a.h b/day8/homework/h2/includes/a.h new file mode 100644 index 0000000..b34462a --- /dev/null +++ b/day8/homework/h2/includes/a.h @@ -0,0 +1,6 @@ +#ifndef __A_H__ +#define __A_H__ + +int factorial(int); // 声明阶乘函数 + +#endif \ No newline at end of file diff --git a/day8/homework/h2/includes/b.h b/day8/homework/h2/includes/b.h new file mode 100644 index 0000000..9009917 --- /dev/null +++ b/day8/homework/h2/includes/b.h @@ -0,0 +1,6 @@ +#ifndef __B_H__ +#define __B_H__ + +int *arrSort(int arrInput[], int arrlen); // 声明数组排序函数 + +#endif \ No newline at end of file diff --git a/day8/homework/h2/libs/liba.a b/day8/homework/h2/libs/liba.a new file mode 100644 index 0000000..c2755ec Binary files /dev/null and b/day8/homework/h2/libs/liba.a differ diff --git a/day8/homework/h2/libs/libb.a b/day8/homework/h2/libs/libb.a new file mode 100644 index 0000000..78e41d2 Binary files /dev/null and b/day8/homework/h2/libs/libb.a differ diff --git a/day8/homework/h2/tempfiles/a.c b/day8/homework/h2/tempfiles/a.c new file mode 100644 index 0000000..1aa104c --- /dev/null +++ b/day8/homework/h2/tempfiles/a.c @@ -0,0 +1,10 @@ +#include "a.h" +#include + +int factorial(int n) +{ + if (n == 0) + return 1; + else + return n * factorial(n - 1); +} \ No newline at end of file diff --git a/day8/homework/h2/tempfiles/a.h b/day8/homework/h2/tempfiles/a.h new file mode 100644 index 0000000..b34462a --- /dev/null +++ b/day8/homework/h2/tempfiles/a.h @@ -0,0 +1,6 @@ +#ifndef __A_H__ +#define __A_H__ + +int factorial(int); // 声明阶乘函数 + +#endif \ No newline at end of file diff --git a/day8/homework/h2/tempfiles/b.c b/day8/homework/h2/tempfiles/b.c new file mode 100644 index 0000000..ba4fe1d --- /dev/null +++ b/day8/homework/h2/tempfiles/b.c @@ -0,0 +1,20 @@ +#include "b.h" +#include + +int *arrSort(int arrInput[], int arrlen) +{ + int i, j, temp; + for (i = 0; i < arrlen - 1; i++) + { + for (j = 0; j < arrlen - 1 - i; j++) + { + if (arrInput[j] > arrInput[j + 1]) + { + temp = arrInput[j]; + arrInput[j] = arrInput[j + 1]; + arrInput[j + 1] = temp; + } + } + } + return arrInput; +} \ No newline at end of file diff --git a/day8/homework/h2/tempfiles/b.h b/day8/homework/h2/tempfiles/b.h new file mode 100644 index 0000000..9009917 --- /dev/null +++ b/day8/homework/h2/tempfiles/b.h @@ -0,0 +1,6 @@ +#ifndef __B_H__ +#define __B_H__ + +int *arrSort(int arrInput[], int arrlen); // 声明数组排序函数 + +#endif \ No newline at end of file diff --git a/day8/homework/h2/testArrSort.c b/day8/homework/h2/testArrSort.c new file mode 100644 index 0000000..384a063 --- /dev/null +++ b/day8/homework/h2/testArrSort.c @@ -0,0 +1,24 @@ +#include "b.h" +#include + +int main() +{ + int N; // 定义数组长度 + printf("Please input the length of the array: "); + scanf("%d", &N); + int arrInput[N]; // 未初始化的数组 + printf("Please input %d numbers: ", N); + int i = 0; + while (i < N) + { + scanf("%d", &arrInput[i++]); + } + + int *arrOutput = arrSort(arrInput, N); + for (int i = 0; i < N; i++) + { + printf("%d ", arrOutput[i]); + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/day8/homework/h2/testFactorial.c b/day8/homework/h2/testFactorial.c new file mode 100644 index 0000000..d39cf5e --- /dev/null +++ b/day8/homework/h2/testFactorial.c @@ -0,0 +1,11 @@ +#include "./includes/a.h" +#include + +int main() +{ + int n; + printf("Please input a number: "); + scanf("%d", &n); + printf("%d! = %d\n", n, factorial(n)); + return 0; +} \ No newline at end of file