From ee8b2039919ae7da5a16dddd093f944b7a4c85f8 Mon Sep 17 00:00:00 2001 From: flykhan Date: Wed, 12 Jul 2023 23:35:07 +0800 Subject: [PATCH] =?UTF-8?q?day8=20homework:=20=E9=9D=99=E6=80=81=E7=9A=84?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=92=8C=E4=BD=BF=E7=94=A8=E9=A2=98=E7=9B=AE?= =?UTF-8?q?=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day8/homework/h2/README.md | 24 ++++++++++++++++++++++++ day8/homework/h2/includes/a.h | 6 ++++++ day8/homework/h2/includes/b.h | 6 ++++++ day8/homework/h2/libs/liba.a | Bin 0 -> 1514 bytes day8/homework/h2/libs/libb.a | Bin 0 -> 1608 bytes day8/homework/h2/tempfiles/a.c | 10 ++++++++++ day8/homework/h2/tempfiles/a.h | 6 ++++++ day8/homework/h2/tempfiles/b.c | 20 ++++++++++++++++++++ day8/homework/h2/tempfiles/b.h | 6 ++++++ day8/homework/h2/testArrSort.c | 24 ++++++++++++++++++++++++ day8/homework/h2/testFactorial.c | 11 +++++++++++ 11 files changed, 113 insertions(+) create mode 100644 day8/homework/h2/README.md create mode 100644 day8/homework/h2/includes/a.h create mode 100644 day8/homework/h2/includes/b.h create mode 100644 day8/homework/h2/libs/liba.a create mode 100644 day8/homework/h2/libs/libb.a create mode 100644 day8/homework/h2/tempfiles/a.c create mode 100644 day8/homework/h2/tempfiles/a.h create mode 100644 day8/homework/h2/tempfiles/b.c create mode 100644 day8/homework/h2/tempfiles/b.h create mode 100644 day8/homework/h2/testArrSort.c create mode 100644 day8/homework/h2/testFactorial.c 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 0000000000000000000000000000000000000000..c2755ec22e5e244cb2891d3da366b1097704298f GIT binary patch literal 1514 zcmbtUL2DCH5S~r6v9a1zJP7r$y@;ZH?$)@}A|)ln77+y@q9^;Z-4JP$z~%+2P(1{M z;Kje-&70TWJ*fY{V=qFXy?FDmP-k}E#Fuq*(OKS`neWYf^LF0bUAQYlzqMMtX^)Fz zxQT7fw(Tx+z-AEuEF4do(pTN^h1>xtx?_Zs*t~K%rg3k1ns2lCruN`o&LYA@ELe@{ z*=PMU#}+^(HY+d#(@@_S{@55Cmxg=MU@wAxVV}5;=HAt!!J#$$a!9eWpK8&!&#+#t zuGm-WjebYWG&}2I{=W!Crk3klqGqX%7T%lFabaP z7f#3pV?XgbQj`=v{J8?Kpfl;1=)I$pL~H{$ju1ffUbdBNpjKg`TWp2Fjua}`RUo#c zk{}wr9tgkNZU-I2I$afr^@sJvo|67Hh&ViGJ#U7x9q4lZt!UqLM(GY#5F;7&A#sfk zCBQr?I+aS|j^?jKigGDYz@HP}6~?dNI)$cBd|T(Gm8KoRH_#%^f6aF>mpC09rf_}F z1kvXDtXB-o4Ei>Z*#C@jz)7QZHjL#vK t?j1t#OXoi$$B^{?v-VEm2Ri4w)@M;u?R5QV#Iowgzc*Z)#bHXi{%=~ucd!5e literal 0 HcmV?d00001 diff --git a/day8/homework/h2/libs/libb.a b/day8/homework/h2/libs/libb.a new file mode 100644 index 0000000000000000000000000000000000000000..78e41d2a453fd09c052c8145c51892ba57c7cb77 GIT binary patch literal 1608 zcmbtUL2DCH5T4!CT4QUY^dQv3_M~F_+%4&1t&~W3b`cTL<|uYIZV6}-*lZvQ)&w{0Tm;75pu|(5NCGZlb3aK(Sdz$=9~Fu=Dm5z!Ywx_SJt!FjZrZb zGuGABFf3aJY-IsJ!*A0Kf=Bfr1dr86(v8by=krk#D{se&v6X$z@7>WgN*9X;>rrxP z%YC3r4WJOI1vm>+P;xrooX)FWr+IwD`{ej>fXfjdMEXZuyL{k$aCYb35aM)s&(Xeg zb1~XJ(45_A#q+rftA{(=sT}C=lN86{_*5NT@u>|U=cJ>z`9SOP<91KG#`|l0FuV6) zI2^WlU!ZHeKf7lkL|s4r1RKTTP2)<*Yu3W1vBL7qT(X;yW$jotGxN+^z8X7>Wz(`v z+gznXqs+w{XfGdv*2-z~)2FB2 + +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