From 6d709758347ef4393b41fcc8e9bb78b96a62c27b Mon Sep 17 00:00:00 2001 From: flykhan Date: Sat, 15 Jul 2023 17:55:35 +0800 Subject: [PATCH] day10 homework --- day10/homework/h11.c | 18 ++++++++++++++++++ day10/homework/h11_2.c | 16 ++++++++++++++++ day10/homework/h12.c | 26 ++++++++++++++++++++++++++ day10/homework/h14.c | 28 ++++++++++++++++++++++++++++ day10/homework/h15.c | 37 +++++++++++++++++++++++++++++++++++++ day10/homework/h4.c | 25 +++++++++++++++++++++++++ day10/homework/h5.c | 19 +++++++++++++++++++ day10/homework/h8.c | 28 ++++++++++++++++++++++++++++ day10/homework/h9.c | 14 ++++++++++++++ 9 files changed, 211 insertions(+) create mode 100644 day10/homework/h11.c create mode 100644 day10/homework/h11_2.c create mode 100644 day10/homework/h12.c create mode 100644 day10/homework/h14.c create mode 100644 day10/homework/h15.c create mode 100644 day10/homework/h4.c create mode 100644 day10/homework/h5.c create mode 100644 day10/homework/h8.c create mode 100644 day10/homework/h9.c diff --git a/day10/homework/h11.c b/day10/homework/h11.c new file mode 100644 index 0000000..4bc2552 --- /dev/null +++ b/day10/homework/h11.c @@ -0,0 +1,18 @@ +#include + +void fun(char *c, int d) +{ + printf("%d\n", *c); // 97 + c = c + 1; + printf("%d\n", *c); // 65 + d = d + 1; + printf("fun ---> %c, %c\n", *c, d); +} + +int main() +{ + char a = 'A', b = 'a'; + fun(&b, a); + printf("main --> %c, %c\n", a, b); + return 0; +} \ No newline at end of file diff --git a/day10/homework/h11_2.c b/day10/homework/h11_2.c new file mode 100644 index 0000000..c5bdd06 --- /dev/null +++ b/day10/homework/h11_2.c @@ -0,0 +1,16 @@ +#include + +void fun(char *c, int *d) +{ + *c = *c + 1; + *d = *d + 1; + printf("fun ---> %c, %c\n", *c, *d); +} + +int main() +{ + char a = 'A', b = 'a'; + fun(&b, &a); + printf("main --> %c, %c\n", a, b); + return 0; +} diff --git a/day10/homework/h12.c b/day10/homework/h12.c new file mode 100644 index 0000000..5efe13e --- /dev/null +++ b/day10/homework/h12.c @@ -0,0 +1,26 @@ +// 设计函数int toInt(char *str), 实现将字符串转成数值的功能。 【提示】如 toInt("123") 返回 123 +#include + +int toInt(char *str) +{ + int n = 0; + while (*str) + { + if (*str < '0' || *str > '9') // 先判断是否是数字 + { + return -1; + } + n = n * 10 + *str - '0'; // 将字符转换为数字 + str++; + } + return n; +} + +int main() +{ + char *s = "123"; + int n = toInt(s); + printf("%d\n", n); + + return 0; +} \ No newline at end of file diff --git a/day10/homework/h14.c b/day10/homework/h14.c new file mode 100644 index 0000000..16cc852 --- /dev/null +++ b/day10/homework/h14.c @@ -0,0 +1,28 @@ +// 设计函数,给定两个字符串指针,实现将第二字符串的内容拼接到第一个字符串中,并返回第一个字符串指针。 +#include + +char *concat(char *dest, char *src) +{ + while (*dest) // 找到 dest 的末尾 + { + dest++; + } + while (*src) // 将 src 的内容复制到 dest 的末尾 + { + *dest = *src; + dest++; + src++; + } + *dest = '\0'; // 最后补上字符串结束符 + return dest; // 返回 dest 的首地址 +} + +int main() +{ + char m[128] = ""; + concat(m, "disen"); + concat(m, ", 666"); + printf("%s\n", m); // 输出结果:disen, 666 + + return 0; +} \ No newline at end of file diff --git a/day10/homework/h15.c b/day10/homework/h15.c new file mode 100644 index 0000000..5cc9605 --- /dev/null +++ b/day10/homework/h15.c @@ -0,0 +1,37 @@ +// 设计函数,接收一个数值类型的5行3列的二维数组和查找的数值,返回数值在二维数组的序号。 +// 【提示】第一个元素的序号为0, 第二行第一个元素的序号为3 +#include + +int find(int (*arr)[3], int num) +{ + int *p = arr[0]; // arr[0] == &arr[0][0] + // int *p = &arr[0][0]; // arr[0] == &arr[0][0] + int n = 0; + for (int i = 0; i < 5; i++) + { + for (int j = 0; j < 3; j++) + { + // if (arr[i][j] == num) + if (*(p + i * 3 + j) == num) // arr[i][j] == *(p+i*3+j) + { + return n; + } + n++; // 序号加1 + } + } + return -1; // 没找到 +} + +int main() +{ + int a[5][3] = { + {1, 2, 3}, + {4, 5, 6}, + {7, 9, 9}, + {10, 11, 12}, + {13, 14, 15}}; + + printf("%d\n", find(a, 9)); // 输出结果:7 + + return 0; +} \ No newline at end of file diff --git a/day10/homework/h4.c b/day10/homework/h4.c new file mode 100644 index 0000000..824f1d3 --- /dev/null +++ b/day10/homework/h4.c @@ -0,0 +1,25 @@ +// 以下程序的输出结果是哪一项 ? () +// #include +// main() +// { +// int a[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, \* p[4] = {NULL}, i = 0; +// for (i = 0; i < 4; i++) +// p[i] = &a[i\* 3]; +// printf("%d\n", *p[3]); +// } +// A.输出项不合法 B.4 C.7 D.10 +#include + +int main() +{ + int a[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, + *p[4] = {NULL}, + i = 0; + + for (i = 0; i < 4; i++) + p[i] = &a[i * 3]; + + printf("%d\n", *p[3]); // 10 + + return 0; +} \ No newline at end of file diff --git a/day10/homework/h5.c b/day10/homework/h5.c new file mode 100644 index 0000000..67582ec --- /dev/null +++ b/day10/homework/h5.c @@ -0,0 +1,19 @@ +// 有以下说明和语句, 则p2 - p1的值为多少 ? int a[10] = {0}, *p1 = NULL, *p2 = NULL; +// p1 = a; +// p2 = &a[5]; +// A.5 B.6 C.10 D.没有指针与指针的减法 +#include +#include + +int main() +{ + int a[10] = {0}, + *p1 = NULL, + *p2 = NULL; + p1 = a; + + p2 = &a[5]; // p2 指向 a[5] 的地址 + printf("%ld\n", p2 - p1); // 同一个数组的指针相减,得到的是两个指针之间的元素个数 + + return 0; +} \ No newline at end of file diff --git a/day10/homework/h8.c b/day10/homework/h8.c new file mode 100644 index 0000000..ecef9cf --- /dev/null +++ b/day10/homework/h8.c @@ -0,0 +1,28 @@ +// 以下程序的输出结果是( ) +// ​ A)17 B)18 C)19 D)20 +// main() +// { +// int a[] = {2, 4, 6, 8, 10}, y = 1, x = 0, *p = NULL; +// p = &a[1]; +// for (; x < 3; x++) +// y += *(p + x); +// printf(“% d\n”, y); +// } + +#include + +int main() +{ + int a[] = {2, 4, 6, 8, 10}, + y = 1, + x = 0, + *p = NULL; + + p = &a[1]; + for (; x < 3; x++) + y += *(p + x); + + printf("%d\n", y); + + return 0; +} \ No newline at end of file diff --git a/day10/homework/h9.c b/day10/homework/h9.c new file mode 100644 index 0000000..8fdffd3 --- /dev/null +++ b/day10/homework/h9.c @@ -0,0 +1,14 @@ +// 下面程序段的运行结果是_________.char *s = "abcde"; +// s += 2; +// printf("%d", s); +// a) cde b)字符'c' c)字符'c'的地址 d)无确定的输出结果 +#include + +int main() +{ + char *s = "abcde"; + s += 2; + printf("%d\n", s); // %d 会将 s 当作 int 输出,但是 s 是 char * 类型,所以会出现不确定的输出结果 + + return 0; +} \ No newline at end of file