From 4e670ac3a663796377e56bf2b6a65b1cb833229d Mon Sep 17 00:00:00 2001 From: flykhan Date: Mon, 17 Jul 2023 23:48:18 +0800 Subject: [PATCH] =?UTF-8?q?day10=20homework:=20=E5=8A=A8=E6=80=81=E5=86=85?= =?UTF-8?q?=E5=AD=98=E7=94=B3=E8=AF=B7=EF=BC=8C=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E6=BC=8F=EF=BC=8C=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day11/homework/h1.c | 35 +++++++++++++++++++++++++ day11/homework/h2.c | 35 +++++++++++++++++++++++++ day11/homework/h3.c | 58 +++++++++++++++++++++++++++++++++++++++++ day11/homework/h5.c | 35 +++++++++++++++++++++++++ day11/homework/h6.c | 33 ++++++++++++++++++++++++ day11/homework/h7.c | 17 ++++++++++++ day11/homework/h9.c | 63 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 276 insertions(+) create mode 100644 day11/homework/h1.c create mode 100644 day11/homework/h2.c create mode 100644 day11/homework/h3.c create mode 100644 day11/homework/h5.c create mode 100644 day11/homework/h6.c create mode 100644 day11/homework/h7.c create mode 100644 day11/homework/h9.c diff --git a/day11/homework/h1.c b/day11/homework/h1.c new file mode 100644 index 0000000..6e5cfd6 --- /dev/null +++ b/day11/homework/h1.c @@ -0,0 +1,35 @@ +// 编写一个程序,要求用户输入一个整数n,然后动态创建一个大小为n的整数数组,并通过循环将数组的元素赋值为1到n的连续整数。最后打印数组的内容。 +#include +#include +#include + +int *create_arr(int n) +{ + int *p = (int *)malloc(n * sizeof(int *)); + if (NULL == p) + { + perror("malloc"); + exit(0); + } + memset(p, 0, n * sizeof(int *)); // 设定初始值为 0 + + int i = 0; + for (; i < n; i++) + *(p + i) = i + 1; + + return p; +} + +int main() +{ + int n; + printf("请输入一个整数: "); + scanf("%d", &n); + int *nums = create_arr(n); + while (*nums) + printf("%d ", *(nums++)); + printf("\n"); + free(nums); + + return 0; +} \ No newline at end of file diff --git a/day11/homework/h2.c b/day11/homework/h2.c new file mode 100644 index 0000000..89555b2 --- /dev/null +++ b/day11/homework/h2.c @@ -0,0 +1,35 @@ +// 编写一个程序,要求用户输入一个字符串,然后动态创建一个字符数组,使其能够容纳用户输入的字符串。然后将用户输入的字符串复制到动态创建的数组中,并打印该字符串。 +#include +#include + +char *str_cpy(char *dest, const char *src) +{ + int cnt; + while (src[cnt++]) + ; + + char *res = (char *)malloc(cnt * sizeof(char *)); + if (NULL == res) + { + perror("malloc"); + exit(0); + } + + for (int i = 0; i < cnt; i++) + res[i] = src[i]; + + return res; +} + +int main() +{ + char s1[100]; + printf("请输入一个字符串: "); + // scanf("%s", s1); + fgets(s1, sizeof(s1), stdin); // scanf() 不能接收换行,故改用 fgets() + char *s2 = str_cpy(s2, s1); + printf("s2 被复制后的结果为: %s", s2); + free(s2); + + return 0; +} \ No newline at end of file diff --git a/day11/homework/h3.c b/day11/homework/h3.c new file mode 100644 index 0000000..b8edcba --- /dev/null +++ b/day11/homework/h3.c @@ -0,0 +1,58 @@ +// 编写一个程序,要求用户输入两个整数m和n,然后动态创建一个m行n列的二维整数数组。通过循环将数组的元素赋值为m行n列的连续整数。最后打印数组的内容。 +#include +#include + +int **create_nums_arr(int m, int n) +{ + // 创建二维数组 + int **nums_arr = (int **)malloc(m * sizeof(int *)); // 先给二维数组分配内存,每一行的内存在下面分配 + if (NULL == nums_arr) + { + perror("malloc"); + exit(-1); + } + + for (int i = 0; i < m; i++) + { + nums_arr[i] = (int *)malloc(n * sizeof(int *)); // 给每一行分配内存 + if (NULL == nums_arr[i]) + { + perror("malloc"); + exit(-1); + } + } + + int i = 0; + for (int j = 0; j < m; j++) + { + for (int k = 0; k < n; k++) + nums_arr[j][k] = i++; // 从 0 开始赋值 + } + + return nums_arr; // 返回二维数组 +} + +int main() +{ + int m, n; + printf("请输入两个整数 m 和 n: "); + scanf("%d%d", &m, &n); + int **nums = create_nums_arr(m, n); // 创建二维数组 + + printf("二维数组的内容为: \n"); + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + printf("%d ", nums[i][j]); + printf("\n"); + } + + // 释放内存 + for (int i = 0; i < m; i++) + { + free(nums[i]); // 释放每一行的内存 + } + free(nums); // 再释放二维数组的内存 + + return 0; +} \ No newline at end of file diff --git a/day11/homework/h5.c b/day11/homework/h5.c new file mode 100644 index 0000000..a2d75e4 --- /dev/null +++ b/day11/homework/h5.c @@ -0,0 +1,35 @@ +// 编写一个程序,要求用户输入一个整数n,然后动态创建一个整数数组,并通过循环从用户输入中读取n个整数并存储到数组中。然后,使用realloc函数将数组的大小减半,并打印减半后的数组内容。 +#include +#include + +int main() +{ + int n; + printf("请输入一个整数: "); + scanf("%d", &n); + int *nums = (int *)malloc(n * sizeof(int *)); + if (NULL == nums) + { + perror("malloc"); + exit(-1); + } + + printf("请输入 %d 个整数: ", n); + for (int i = 0, temp = 0; i < n; i++) + { + scanf("%d", &temp); + nums[i] = temp; + } + + int halfN = n / 2; // n 的一半 + nums = (int *)realloc(nums, halfN * sizeof(int *)); + + printf("减半后的数组内容打印如下: \n"); + while (halfN--) + printf("%d ", *nums++); + printf("\n"); + + free(nums); + + return 0; +} \ No newline at end of file diff --git a/day11/homework/h6.c b/day11/homework/h6.c new file mode 100644 index 0000000..bef494b --- /dev/null +++ b/day11/homework/h6.c @@ -0,0 +1,33 @@ +// 编写一个程序,要求用户输入一个字符串,然后使用strrev函数将字符串反转,并打印反转后的字符串。 +#include +#include + +char *strrev(char *src) +{ + int cnt = 0; + while (src[cnt++]) + ; + + char *res = (char *)malloc(cnt * sizeof(char *)); + + int tmp = cnt; + for (int i = 0; i < cnt; i++) + { + res[i] = src[tmp - 2]; // tmp - 2 是为了去掉换行符, tmp - 1 是为了去掉字符串末尾的 \0 + tmp--; + } + + return res; +} + +int main() +{ + char s1[100]; + printf("请输入一个字符串: "); + fgets(s1, sizeof(s1), stdin); // scanf() 不能接收换行,故改用 fgets() + char *s2 = strrev(s1); + printf("反转后的字符串结果为: %s", s2); + free(s2); + + return 0; +} \ No newline at end of file diff --git a/day11/homework/h7.c b/day11/homework/h7.c new file mode 100644 index 0000000..de23f9b --- /dev/null +++ b/day11/homework/h7.c @@ -0,0 +1,17 @@ +// 编写一个程序,要求用户输入一个字符串,然后使用strncpy函数将字符串的一部分复制到另一个字符数组中,但限制复制的长度为原字符串的一半,并打印复制后的字符串。 +#include +#include +#include + +int main() +{ + char s1[100]; + printf("请输入一个字符串: "); + fgets(s1, sizeof(s1), stdin); // scanf() 不能接收换行,故改用 fgets() + char *s2 = (char *)malloc(strlen(s1) / 2 * sizeof(char *)); + strncpy(s2, s1, strlen(s1) / 2); + + printf("复制后的字符串结果为: %s", s2); + + return 0; +} \ No newline at end of file diff --git a/day11/homework/h9.c b/day11/homework/h9.c new file mode 100644 index 0000000..d155072 --- /dev/null +++ b/day11/homework/h9.c @@ -0,0 +1,63 @@ +/* + 以下为我们的手机收到的短信的格式,请利用指针数组与strtok函数对其解析 + char msg_src[] = {"+CMGR:REC UNREAD,+8613466630259,98/10/01,18:22:11+00,ABCdefGHI"}; + + 解析结果: + 日期:98 / 10 / 01 + 时间:18 : 22 : 11 + 发件人:13466630259 + 内容:ABCdefGHI + + 参考以下的函数名字以及参数,完成相应的要求 + int msg_deal(char *msg_src, char *msg_done[], char *str) + 参数1:待切割字符串的首地址 + 参数2:指针数组:存放切割完字符串的首地址 + 参数3:切割字符 + 返回值:切割的字符串总数量 +*/ +#include +#include +#include + +int msg_deal(char *msg_src, char *msg_done[], char *str) +{ + int cnt = 0; + char *tmp = strtok(msg_src, str); + while (tmp != NULL) + { + + if (cnt == 1) // 如果是发件人,去掉前面的 +86 + { + if (strncmp(tmp, "+86", 3) == 0) // 如果前三位是 +86 + tmp += 3; // +86 的长度为 3, tmp 指针向后移动 3 位 + } + + if (cnt == 3) // 处理时间字符串 + { + int len = strlen(tmp); // 获取字符串长度 + if (len >= 3 && strcmp(tmp + len - 3, "+00") == 0) // 如果末尾是 +00 且字符串长度大于等于 3 + tmp[len - 3] = '\0'; // 去掉末尾的 +00,即将 +00 替换为 \0 + } + + msg_done[cnt++] = tmp; // 将切割后的字符串存入指针数组 + tmp = strtok(NULL, str); // 继续切割 + } + + return cnt; // 返回切割的字符串总数量 +} + +int main() +{ + char msg_src[] = {"+CMGR:REC UNREAD,+8613466630259,98/10/01,18:22:11+00,ABCdefGHI"}; + char *msg_done[5]; // 5 个字符串 + char str[] = ","; // 以逗号为切割字符 + + int cnt = msg_deal(msg_src, msg_done, str); + + printf("日期:%s\n", msg_done[2]); + printf("时间:%s\n", msg_done[3]); + printf("发件人:%s\n", msg_done[1]); + printf("内容:%s\n", msg_done[4]); + + return 0; +}