From 1a690b5737e50e8e7e7d69c881e60a14312e6e1e Mon Sep 17 00:00:00 2001 From: lzy Date: Mon, 1 Apr 2024 15:38:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E7=AB=A0=E5=AE=8C=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 +- Chapter4/C4-流程控制.md | 8 +- Chapter5/C5-数组.md | 403 ++++++++++++++++++++++++++++++++++++++++ Chapter5/char.c | 28 +++ Chapter5/char_func.c | 54 ++++++ Chapter5/douarr.c | 27 +++ Chapter5/simple1.c | 38 ++++ Chapter5/simple_test.c | 164 ++++++++++++++++ 8 files changed, 720 insertions(+), 8 deletions(-) create mode 100644 Chapter5/C5-数组.md create mode 100644 Chapter5/char.c create mode 100644 Chapter5/char_func.c create mode 100644 Chapter5/douarr.c create mode 100644 Chapter5/simple1.c create mode 100644 Chapter5/simple_test.c diff --git a/.gitignore b/.gitignore index d290bfe..63732c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,17 +1,13 @@ -# 忽略所有文件 * -# 但不忽略目录,因为我们需要查找目录中的.c和.h文件 +# 不忽略目录 !/**/ -# 不忽略.gitignore和README.md文件 !.gitignore !README.md -# 不忽略LICENSE文件 !LICENSE -# 允许跟踪所有的.c和.h文件 !**/*.c !**/*.h !**/*.md \ No newline at end of file diff --git a/Chapter4/C4-流程控制.md b/Chapter4/C4-流程控制.md index aa0ccce..dd3ba3b 100644 --- a/Chapter4/C4-流程控制.md +++ b/Chapter4/C4-流程控制.md @@ -1,3 +1,5 @@ +# 目录 + - [流程控制](#流程控制) - [关键字](#关键字) - [详解选择](#详解选择) @@ -280,8 +282,8 @@ for(exp1;exp2;exp3) ### `goto` -- 慎重使用 if-goto -- goto 实现的是 无条件的跳转,且不能跨函数跳转 +- 慎重使用`if-goto` +- `goto`实现的是 无条件的跳转,且不能跨函数跳转 @@ -369,7 +371,7 @@ for(;;); > 包括钻石型 > > ```bash -> * +> * > * * > * * * > * * diff --git a/Chapter5/C5-数组.md b/Chapter5/C5-数组.md new file mode 100644 index 0000000..f20fe31 --- /dev/null +++ b/Chapter5/C5-数组.md @@ -0,0 +1,403 @@ +# 目录 +- [目录](#目录) +- [数组](#数组) + - [一维数组](#一维数组) + - [练习部分](#练习部分) + - [斐波那契数列前10项](#斐波那契数列前10项) + - [数据排序](#数据排序) + - [冒泡](#冒泡) + - [选择法](#选择法) + - [进制转换](#进制转换) + - [删除法求质数](#删除法求质数) + - [二维数组](#二维数组) + - [练习部分](#练习部分-1) + - [行列互换](#行列互换) + - [求最大值及其所在位置](#求最大值及其所在位置) + - [求各行与各列的和](#求各行与各列的和) + - [矩阵乘积](#矩阵乘积) + - [字符数组](#字符数组) + - [常用函数](#常用函数) + - [练习部分](#练习部分-2) + - [单词计数](#单词计数) + - [多维数组](#多维数组) + + +# 数组 + +构造类型之一,连续存放。 + + + +> [!warning] +> +> 时间关系,大量的练习题,没有做笔记,只记录了题目。自己思考加看课程足矣。 + + + +## 一维数组 + +1. 定义 + + 【存储类型】 数据类型 标识符 【下标】 + +2. 初始化 + + - 不初始化 + - 全部初始化 + - 部分初始化 + - `static` + +3. 元素引用 + + 数组名【下标】 + +4. 数组名 + + 数组名是表示地址的**常量**,也是数组的起始位置。 + +5. 数组越界 + + + +### 练习部分 + +#### 斐波那契数列前10项 + +```c +static void fibonacci(void) +{ + int fib[10] = {1, 1}; + + for (int i = 2; i < sizeof(fib) / sizeof(fib[0]); i++) + { + fib[i] = fib[i - 2] + fib[i - 1]; + } + for (int i = 0; i < sizeof(fib) / sizeof(fib[0]); i++) + { + printf("fib[%d] = %d\n", i, fib[i]); + } + + printf("\n"); + + int i = 0; + int j = sizeof(fib) / sizeof(fib[0]) - 1; + int tmp; + + while (i < j) + { + tmp = fib[i]; + fib[i] = fib[j]; + fib[j] = tmp; + + i++; + j--; + } + + for (int i = 0; i < sizeof(fib) / sizeof(fib[0]); i++) + { + printf("fib[%d] = %d\n", i, fib[i]); + } +} +``` + +#### 数据排序 + +##### 冒泡 + +```c +#define N 10 +static void sort1(void) +{ + int a[N] = {12, 8, 45, 30, 98, 67, 2, 7, 68, 11}; + int tmp; + + for (int i = 0; i < N; i++) + { + printf("%d ", a[i]); + } + printf("\n"); + + for (int i = 0; i < (N - 1); i++) + { + for (int j = 0; j < N - 1 - i; j++) + { + if (a[j] > a[j + 1]) + { + tmp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = tmp; + } + } + } + + for (int i = 0; i < N; i++) + { + printf("%d ", a[i]); + } + printf("\n"); +} +``` + +##### 选择法 + +```c +static void sort2(void) +{ + int a[N] = {23, 45, 90, 76, 13, 55, 76, 45, 3, 8}; + int i, j, k, tmp; + + for (int i = 0; i < N; i++) + { + printf("%d ", a[i]); + } + printf("\n"); + + for (i = 0; i < N - 1; i++) + { + k = i; + for (j = i + 1; j < N; j++) + { + if (a[j] < a[k]) + k = j; + } + if (i != k) + { + tmp = a[i]; + a[i] = a[k]; + a[k] = tmp; + } + } + + for (int i = 0; i < N; i++) + { + printf("%d ", a[i]); + } + printf("\n"); +} +``` + +#### 进制转换 + +```c +static void base_convert(void) +{ + int num, base; + int n[128]; + int i = 0; + + printf("Please enter the converted num:\n"); + scanf("%d", &num); + printf("Please enter the base:\n"); + scanf("%d", &base); + + do + { + n[i] = num % base; + num = num / base; + i++; + } while (num != 0); + + for (i--; i >= 0; i--) + { + if (n[i] >= 10) + printf("%c", n[i] - 10 + 'A'); + else + printf("%d", n[i]); + } + printf("\n"); +} +``` + +#### 删除法求质数 + +```c +static void primer(void) +{ + char primer[1001] = {0}; + int i, j; + + for (i = 2; i < 1001; i++) + { + for (j = i * 2; j < 1001; j += i) + primer[j] = -1; + } + + for (i = 2; i < 1001; i++) + { + if (primer[i] == 0) + printf("%d is a primer.\n", i); + } +} +``` + + + +## 二维数组 + +1. 定义,初始化 + + 【存储类型】 数据类型 标识符 【行下标】 【列下标】 + + > [!note] + > + > 初始化时,行号可省,列号不可省 + +2. 元素引用 + + 数组名 【行标】【列标】 + +3. 存储形式 + + 顺序存储,按行存储 + +4. 对于二维数组的深入理解 + + ```c + int a[M][N] = {1, 2, 3, 4, 5}; + int i, j; + + printf("a = %p\n", a); + printf("a+1 = %p\n", a + 1); + + for (i = 0; i < M; i++) + { + for (j = 0; j < N; j++) + { + printf("%p --> %d\n", &a[i][j], a[i][j]); + } + printf("\n"); + } + ``` + + 输出: + + ```c + a = 0x7ffcc2376f80 + a+1 = 0x7ffcc2376f8c + 0x7ffcc2376f80 --> 1 + 0x7ffcc2376f84 --> 2 + 0x7ffcc2376f88 --> 3 + + 0x7ffcc2376f8c --> 4 + 0x7ffcc2376f90 --> 5 + 0x7ffcc2376f94 --> 0 + ``` + + `a+1`跨越了行而不是单个元素。 + + + + + +### 练习部分 + +#### 行列互换 + +#### 求最大值及其所在位置 + +#### 求各行与各列的和 + +#### 矩阵乘积 + + + + + +## 字符数组 + +1. 定义,初始化,存储特点 + + 【存储类型】 数据类型 标识符 【下标】... + + - 单个字符初始化 + - 用字符串常量初始化 + +2. 输入输出 + +3. 常用函数 + + + +### 常用函数 + +1. `strlen`和`sizeof` + + ```c + size_t strlen(const char *s); + // strlen函数可以后去字符串的长度,不包括尾'\0' + // 以'\0'作为结束,所以对于"hello\0abc",abc就不计入 + + // sizeof是完整的识别。 + + char str[] = "hello\0abc"; + + printf("%d\n", strlen(str)); + // out: 5 + printf("%d\n", sizeof(str)); + // out: 6 + ``` + +2. `strcpy`与`strncpy` + + ```c + char *strcpy(char *dest, const char *src); + // 把src内容拷贝到dest,以'\0'结束,返回dest的地址 + // 当src大于dest,造成越界 + + char *strncpy(char *dest, const char *src, size_t n); + // 把src内容拷贝n个字节到dest + + strcpy(str, "abcde"); + puts(str); + // out: abcde + + strncpy(str, "abcde", STR_SIZE); + puts(str); + // out: abcde + ``` + +3. `strcat`与`strncat` + + ```c + char *strcat(char *dest, const char *src); + // 把src补到dest后面 + // 需要保证dest空间足够 + + char *strncat(char *dest, const char *src, size_t n); + // 把src拿最多n个字符到dest + + strcat(str1, " "); + strcat(str1, "world"); + // out:hello world + + strncat(str, " ", STR_SIZE); + strncat(str, "world", STR_SIZE); + // out:hello world + ``` + + + +4. `strcmp`与`strncmp` + + ```c + int strcmp(const char *s1, const char *s2); + // 返回两个字符串,ascii码的差值 + + int strncmp(const char (s1, const char *s2, size_t n); + // 指定比较前5个字符 + ``` + + + +### 练习部分 + +#### 单词计数 + + + +## 多维数组 + +```c +int a[2][3][4]; +``` + diff --git a/Chapter5/char.c b/Chapter5/char.c new file mode 100644 index 0000000..4af54fc --- /dev/null +++ b/Chapter5/char.c @@ -0,0 +1,28 @@ +#include +#include + +// #define N 10 +#define N 32 + +int main() +{ + + // char str[N] = {'a', 'b', 'c'}; + char str[N] = "a"; + + scanf("%s", str); + // str数组名本身就是地址,不需要&str + + printf("%s", str); + + // gets(str); + // puts(str); + + // for (int i = 0; i < N; i++) + // { + // printf("%c ", str[i]); + // } + printf("\n"); + + exit(0); +} \ No newline at end of file diff --git a/Chapter5/char_func.c b/Chapter5/char_func.c new file mode 100644 index 0000000..f9257e9 --- /dev/null +++ b/Chapter5/char_func.c @@ -0,0 +1,54 @@ +#include +#include + +#include + +/* strlen & sizeof */ +/* strcpy & strncpy */ +/* strcat & strncat */ +/* strcmp & strncmp */ + +#define STR_SIZE 32 + +int main() +{ + char str[32] = "hello\0abc"; + char str1[STR_SIZE] = "hello"; + char str2[STR_SIZE] = "world"; + + printf("%d\n", strcmp(str1, str2)); + // out:-15 + + + + // strcat(str1, " "); + // strcat(str1, "world"); + // // out:hello world + + // strncat(str, " ", STR_SIZE); + // strncat(str, "world", STR_SIZE); + // // out:hello world + + + + // strcpy(str, "abcde"); + // puts(str); + // // out: abcde + + // strncpy(str, "abcde", STR_SIZE); + // puts(str); + // // out: abcde + + + + + + // printf("%d\n", strlen(str)); + // // out: 5 + // printf("%d\n", sizeof(str)); + // // out: 6 + + + + exit(0); +} \ No newline at end of file diff --git a/Chapter5/douarr.c b/Chapter5/douarr.c new file mode 100644 index 0000000..76078aa --- /dev/null +++ b/Chapter5/douarr.c @@ -0,0 +1,27 @@ +#include +#include + +#define M 2 +#define N 3 + +int main() +{ + + // int a[M][N] = {{1, 2, 3}, {4, 5, 6}}; + int a[M][N] = {1, 2, 3, 4, 5}; + int i, j; + + printf("a = %p\n", a); + printf("a+1 = %p\n", a + 1); + + for (i = 0; i < M; i++) + { + for (j = 0; j < N; j++) + { + printf("%p --> %d\n", &a[i][j], a[i][j]); + } + printf("\n"); + } + + exit(0); +} \ No newline at end of file diff --git a/Chapter5/simple1.c b/Chapter5/simple1.c new file mode 100644 index 0000000..beaf65c --- /dev/null +++ b/Chapter5/simple1.c @@ -0,0 +1,38 @@ +#include +#include + +#define M 3 + +int main() +{ + // int arr[M] = {1}; + // static int arr[M]; + int arr[] = {1, 2, 3, 4, 5, 6}; + + printf("%d\n", sizeof(arr) / sizeof(arr[0])); + + printf("%d\n", sizeof(arr)); + // out: 12 + + printf("arr = %p\n", arr); + + // arr = {4, 5, 6}; + // arr常量,不可赋值 + + // for (int i = 0; i < M; i++) + // { + // scanf("%d", &arr[i]); + // } + + arr[3] = 10; + printf("arr[3] = %d\n", arr[3]); + // out: arr[3] = 10 + // 正常输出,起始越界了 + + for (int i = 0; i < M; i++) + { + printf("%p --> %d\n", &arr[i], arr[i]); + } + + exit(0); +} \ No newline at end of file diff --git a/Chapter5/simple_test.c b/Chapter5/simple_test.c new file mode 100644 index 0000000..dabd69c --- /dev/null +++ b/Chapter5/simple_test.c @@ -0,0 +1,164 @@ +#include +#include + +static void fibonacci(void) +{ + int fib[10] = {1, 1}; + + for (int i = 2; i < sizeof(fib) / sizeof(fib[0]); i++) + { + fib[i] = fib[i - 2] + fib[i - 1]; + } + for (int i = 0; i < sizeof(fib) / sizeof(fib[0]); i++) + { + printf("fib[%d] = %d\n", i, fib[i]); + } + + printf("\n"); + + int i = 0; + int j = sizeof(fib) / sizeof(fib[0]) - 1; + int tmp; + + while (i < j) + { + tmp = fib[i]; + fib[i] = fib[j]; + fib[j] = tmp; + + i++; + j--; + } + + for (int i = 0; i < sizeof(fib) / sizeof(fib[0]); i++) + { + printf("fib[%d] = %d\n", i, fib[i]); + } +} + +#define N 10 +static void sort1(void) +{ + int a[N] = {12, 8, 45, 30, 98, 67, 2, 7, 68, 11}; + int tmp; + + for (int i = 0; i < N; i++) + { + printf("%d ", a[i]); + } + printf("\n"); + + for (int i = 0; i < (N - 1); i++) + { + for (int j = 0; j < N - 1 - i; j++) + { + if (a[j] > a[j + 1]) + { + tmp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = tmp; + } + } + } + + for (int i = 0; i < N; i++) + { + printf("%d ", a[i]); + } + printf("\n"); +} + +static void sort2(void) +{ + int a[N] = {23, 45, 90, 76, 13, 55, 76, 45, 3, 8}; + int i, j, k, tmp; + + for (int i = 0; i < N; i++) + { + printf("%d ", a[i]); + } + printf("\n"); + + for (i = 0; i < N - 1; i++) + { + k = i; + for (j = i + 1; j < N; j++) + { + if (a[j] < a[k]) + k = j; + } + if (i != k) + { + tmp = a[i]; + a[i] = a[k]; + a[k] = tmp; + } + } + + for (int i = 0; i < N; i++) + { + printf("%d ", a[i]); + } + printf("\n"); +} + +static void base_convert(void) +{ + int num, base; + int n[128]; + int i = 0; + + printf("Please enter the converted num:\n"); + scanf("%d", &num); + printf("Please enter the base:\n"); + scanf("%d", &base); + + do + { + n[i] = num % base; + num = num / base; + i++; + } while (num != 0); + + for (i--; i >= 0; i--) + { + if (n[i] >= 10) + printf("%c", n[i] - 10 + 'A'); + else + printf("%d", n[i]); + } + printf("\n"); +} + +static void primer(void) +{ + char primer[1001] = {0}; + int i, j; + + for (i = 2; i < 1001; i++) + { + for (j = i * 2; j < 1001; j += i) + primer[j] = -1; + } + + for (i = 2; i < 1001; i++) + { + if (primer[i] == 0) + printf("%d is a primer.\n", i); + } +} + +int main() +{ + // fibonacci(); + + // sort1(); + + // sort2(); + + // base_convert(); + + primer(); + + exit(0); +} \ No newline at end of file