实验五

实验任务1
 1 #include <stdio.h>
 2 #define N 5
 3 void input(int x[], int n);
 4 void output(int x[], int n);
 5 void find_min_max(int x[], int n, int *pmin, int *pmax);
 6 int main() {
 7 int a[N];
 8 int min, max;
 9 printf("录入%d个数据:\n", N);
10 input(a, N);
11 printf("数据是: \n");
12 output(a, N);
13 printf("数据处理...\n");
14 find_min_max(a, N, &min, &max);
15 printf("输出结果:\n");
16 printf("min = %d, max = %d\n", min, max);
17 return 0;
18 }
19 void input(int x[], int n) {
20 int i;
21 for(i = 0; i < n; ++i)
22 scanf("%d", &x[i]);
23 }
24 void output(int x[], int n) {
25 int i;
26 for(i = 0; i < n; ++i)
27 printf("%d ", x[i]);
28 printf("\n");
29 }
30 void find_min_max(int x[], int n, int *pmin, int *pmax) {
31 int i;
32 *pmin = *pmax = x[0];
33 for(i = 0; i < n; ++i)
34 if(x[i] < *pmin)
35 *pmin = x[i];
36 else if(x[i] > *pmax)
37 *pmax = x[i];
38 }
View Code

实验结论

实验任务5task1.c

问题1:find_min_max的功能是找到数组中的最小值和最大值

问题2:指x[0]

task1_2.c

 1 #include <stdio.h>
 2 #define N 5
 3 void input(int x[], int n);
 4 void output(int x[], int n);
 5 int *find_max(int x[], int n);
 6 int main() {
 7 int a[N];
 8 int *pmax;
 9 printf("录入%d个数据:\n", N);
10 input(a, N);
11 printf("数据是: \n");
12 output(a, N);
13 printf("数据处理...\n");
14 pmax = find_max(a, N);
15 printf("输出结果:\n");
16 printf("max = %d\n", *pmax);
17 return 0;
18 }
19 void input(int x[], int n) {
20 int i;
21 for(i = 0; i < n; ++i)
22 scanf("%d", &x[i]);
23 }
24 void output(int x[], int n) {
25 int i;
26 for(i = 0; i < n; ++i)
27 printf("%d ", x[i]);
28 printf("\n");
29 }
30 int *find_max(int x[], int n) {
31 int max_index = 0;
32 int i;
33 for(i = 0; i < n; ++i)
34 if(x[i] > x[max_index])
35 max_index = i;
36 return &x[max_index];
37 }
View Code

实验结论

实验任务5task1_2.c

问题1:函数find_max的功能是找到一组数据中的最大值

其返回值是最大元素的地址

问题2:不可以,第6行中x[i]>*ptr存在语法错误(一个是整型数值另一个是指针,类型不一致)

实验任务2

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 80
 4 int main() {
 5 char s1[N] = "Learning makes me happy";
 6 char s2[N] = "Learning makes me sleepy";
 7 char tmp[N];
 8 printf("sizeof(s1) vs. strlen(s1): \n");
 9 printf("sizeof(s1) = %d\n", sizeof(s1));
10 printf("strlen(s1) = %d\n", strlen(s1));
11 printf("\nbefore swap: \n");
12 printf("s1: %s\n", s1);
13 printf("s2: %s\n", s2);
14 printf("\nswapping...\n");
15 strcpy(tmp, s1);
16 strcpy(s1, s2);
17 strcpy(s2, tmp);
18 printf("\nafter swap: \n");
19 printf("s1: %s\n", s1);
20 printf("s2: %s\n", s2);
21 return 0;
22 }
View Code

实验结论

实验任务5task2.c

 问题1:s1的大小是80

计算的是s1所占字节数

统计的是有效字符个数

问题2:不能替换,因为s1是地址常量,不能被赋值

问题3:会交换

task2_2.c

 1  #include <stdio.h>
 2  #include <string.h>
 3  #define N 80
 4  int main() {
 5  char *s1 = "Learning makes me happy";
 6  char *s2 = "Learning makes me sleepy";
 7  char *tmp;
 8  printf("sizeof(s1) vs. strlen(s1): \n");
 9  printf("sizeof(s1) = %d\n", sizeof(s1));
10  printf("strlen(s1) = %d\n", strlen(s1));
11  printf("\nbefore swap: \n");
12  printf("s1: %s\n", s1);
13  printf("s2: %s\n", s2);
14  printf("\nswapping...\n");
15  tmp = s1;
16  s1 = s2;
17  s2 = tmp;
18  printf("\nafter swap: \n");
19  printf("s1: %s\n", s1);
20  printf("s2: %s\n", s2);
21  return 0;
22  }
View Code

实验结论

实验5task2_2.c

问题1:s1存放的是字符串常量"Learning makes me happy"在内存中的首地址

计算的是指针变量本身所占的字节数

统计的是吧指向的字符串中有效字符的个数

问题2:能替换

task2_1.c中数组内容可修改

task2_2.c中不能通过修改s1修改字符串内容

问题3:交换的是指针变量s1和s2存放的地址值

没有交换

实验任务3

 1 #include <stdio.h>
 2 int main() {
 3 int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
 4 int i, j;
 5 int *ptr1; // 指针变量,存放int类型数据的地址
 6 int(*ptr2)[4]; // 指针变量,指向包含4个int元素的一维数组
 7 printf("输出1: 使用数组名、下标直接访问二维数组元素\n");
 8 for (i = 0; i < 2; ++i) {
 9 for (j = 0; j < 4; ++j)
10 printf("%d ", x[i][j]);
11 printf("\n");
12 }
13 printf("\n输出2: 使用指针变量ptr1(指向元素)间接访问\n");
14 for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) {
15 printf("%d ", *ptr1);
16 if ((i + 1) % 4 == 0)
17 printf("\n");
18 }
19 printf("\n输出3: 使用指针变量ptr2(指向一维数组)间接访问\n");
20 for (ptr2 = x; ptr2 < x + 2; ++ptr2) {
21 for (j = 0; j < 4; ++j)
22 printf("%d ", *(*ptr2 + j));
23 printf("\n");
24 }
25 return 0;
26 }
View Code

实验结论

实验5task3.c

问题1:ptr是指向包含4个int类型元素的一维数组指针

ptr是包含4个int类型指针的数组

实验任务4

 1 #include <stdio.h>
 2 #define N 80
 3 void replace(char *str, char old_char, char new_char); // 函数声明
 4 int main() {
 5 char text[N] = "Programming is difficult or not, it is a question.";
 6 printf("原始文本: \n");
 7 printf("%s\n", text);
 8 replace(text, 'i', '*'); // 函数调用 注意字符形参写法,单引号不能少
 9 printf("处理后文本: \n");
10 printf("%s\n", text);
11 return 0;
12 }
13 // 函数定义
14 void replace(char *str, char old_char, char new_char) {
15 int i;
16 while(*str) {
17 if(*str == old_char)
18 *str = new_char;
19 str++;
20 }
21 }
View Code

实验结论

实验5task4.c

问题1:功能是将old_char字符替换成new_char字符

问题2:可以

实验任务5

 1 #include <stdio.h>
 2 #define N 80
 3 char *str_trunc(char *str, char x);
 4 int main() {
 5 char str[N];
 6 char ch;
 7 while(printf("输入字符串: "), gets(str) != NULL) {
 8 printf("输入一个字符: ");
 9 ch = getchar();
10 printf("截断处理...\n");
11 str_trunc(str, ch); // 函数调用
12 printf("截断处理后的字符串: %s\n\n", str);
13 getchar();
14 }
15 return 0;
16 }
17 // 函数str_trunc定义
18 // 功能: 对字符串作截断处理,把指定字符自第一次出现及其后的字符全部删除, 并返回字符串地址
19 // 待补足...
20 // xxx
21 char *str_trunc(char*str,char x){
22     char*p=str;
23     while (*p!='\0'&&*p!=x){
24         p++;
25     }
26     if(*p==x){
27         *p='\0';
28     }
29     return str;
30 }
View Code

实验结论

实验5task5.c

问题:去掉后,如果按回车,会跳过下一次的字符串输入,输入错位

在这里的作用是防止读取换行符

实验任务6

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 int check_id(char *str); // 函数声明
 5 int main()
 6 {
 7 char *pid[N] = {"31010120000721656X",
 8 "3301061996X0203301",
 9 "53010220051126571",
10 "510104199211197977",
11 "53010220051126133Y"};
12 int i;
13 for (i = 0; i < N; ++i)
14 if (check_id(pid[i])) // 函数调用
15 printf("%s\tTrue\n", pid[i]);
16 else
17 printf("%s\tFalse\n", pid[i]);
18 return 0;
19 }
20 // 函数定义
21 // 功能: 检查指针str指向的身份证号码串形式上是否合法
22 // 形式合法,返回1,否则,返回0
23 int check_id(char *str) {
24 // 补足函数实现
25 // ...
26     if (strlen(str) != 18) {
27         return 0;
28     }
29     char *p = str;
30     for (int i = 0; i < 17; i++) {
31         if (*p < '0' || *p > '9') {
32             return 0;
33         }
34         p++;
35     }
36     if ((*p != 'X') && (*p < '0' || *p > '9')) {
37         return 0;
38     }
39     return 1;
40 }
View Code

实验结论

实验5task6.c

实验任务7

 1 #include <stdio.h>
 2 #define N 80
 3 void encoder(char *str, int n); // 函数声明
 4 void decoder(char *str, int n); // 函数声明
 5 int main() {
 6 char words[N];
 7 int n;
 8 printf("输入英文文本: ");
 9 gets(words);
10 printf("输入n: ");
11 scanf("%d", &n);
12 printf("编码后的英文文本: ");
13 encoder(words, n); // 函数调用
14 printf("%s\n", words);
15 printf("对编码后的英文文本解码: ");
16 decoder(words, n); // 函数调用
17 printf("%s\n", words);
18 return 0;
19 }
20 /*函数定义
21 功能:对str指向的字符串进行编码处理
22 编码规则:
23 对于a~z或A~Z之间的字母字符,用其后第n个字符替换; 其它非字母字符,保持不变
24 */
25 void encoder(char *str, int n) {
26 // 补足函数实现
27 // ×××
28     char *p = str;
29     n = n % 26; 
30     while (*p != '\0') {
31         if (*p >= 'a' && *p <= 'z') {
32             *p = (*p - 'a' + n) % 26 + 'a';
33         }
34         else if (*p >= 'A' && *p <= 'Z') {
35             *p = (*p - 'A' + n) % 26 + 'A';
36         }
37         p++;
38     }
39 }
40 /*函数定义
41 功能:对str指向的字符串进行解码处理
42 解码规则:
43 对于a~z或A~Z之间的字母字符,用其前面第n个字符替换; 其它非字母字符,保持不变
44 */
45 void decoder(char *str, int n) {
46 // 补足函数实现
47 // ×××
48 char *p = str;
49     n = n % 26;
50     while (*p != '\0') {
51         if (*p >= 'a' && *p <= 'z') {
52             *p = (*p - 'a' - n + 26) % 26 + 'a';
53         }
54         else if (*p >= 'A' && *p <= 'Z') {
55             *p = (*p - 'A' - n + 26) % 26 + 'A';
56         }
57         p++;
58     }
59 }
View Code

实验结论

实验5task7.c

实验任务8

 老师,这个我实在写不出来QAQ

posted @ 2025-12-12 14:19  Aaaaa叉少  阅读(0)  评论(0)    收藏  举报