实验5

试验任务1

代码1-1:

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 #define N 5
 4 
 5 void input(int x[], int n);
 6 void output(int x[], int n);
 7 void find_min_max(int x[], int n, int *pmin, int *pmax);
 8 
 9 int main() {
10     int a[N];
11     int min, max;
12 
13     printf("录入%d个数据:\n", N);
14     input(a, N);
15 
16     printf("数据是: \n");
17     output(a, N);
18 
19     printf("数据处理...\n");
20     find_min_max(a, N, &min, &max);
21 
22     printf("输出结果:\n");
23     printf("min = %d, max = %d\n", min, max);
24 
25     system("pause");
26     return 0;
27 }
28 
29 void input(int x[], int n) {
30     int i;
31 
32     for(i = 0; i < n; ++i)
33         scanf("%d", &x[i]);
34 }
35 
36 void output(int x[], int n) {
37     int i;
38     
39     for(i = 0; i < n; ++i)
40         printf("%d ", x[i]);
41     printf("\n");
42 }
43 
44 void find_min_max(int x[], int n, int *pmin, int *pmax) {
45     int i;
46     
47     *pmin = *pmax = x[0];
48 
49     for(i = 0; i < n; ++i)
50         if(x[i] < *pmin)
51             *pmin = x[i];
52         else if(x[i] > *pmax)
53             *pmax = x[i];
54 }

 

截图1-1:

屏幕截图 2026-06-02 191842

 

问题回答1-1:

1.找出数组中的最小值和最大值;

2.pmin指向main函数中定义的局部变量min的内存地址,pmax指向main函数中定义的局部变量max的内存地址。

 

代码1-2:

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

 

截图1-2:

屏幕截图 2026-06-02 192959

 

问题回答1-2:

1.找出数组中的最大值;

2.可以实现相同功能。

 

试验任务2

代码2-1:

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

 

截图2-1:

屏幕截图 2026-06-02 194221

 

问题回答2-1:

1.数组s1占用80字节,sizeof(s1)计算整个字符数组分配的总内存字节数,strlen(s1)统计字符串中有效字符个数;

2.不能替换,s1是数组名不能赋值;

3.交换了。

 

代码2-2:

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

 

截图2-2:

屏幕截图 2026-06-02 195143

 

问题回答2-2:

1.s1存放字符串常量"Learning makes me happy"在常量区的首字符地址,sizeof(s1)计算指针变量自身占用内存大小,strlen(s1)统计有效字符数量;

2.可以,一个是数组复制内容,一个是指针保存地址;

3.交换s1,s2两个指针变量里保存的地址值,两个字符串内容没有交换。

 

试验任务3

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main() {
 5     int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
 6     int i, j;
 7     int *ptr1;     
 8     int(*ptr2)[4]; 
 9 
10     printf("输出1: 使用数组名、下标直接访问二维数组元素\n");
11     for (i = 0; i < 2; ++i) {
12         for (j = 0; j < 4; ++j)
13             printf("%d ", x[i][j]);
14         printf("\n");
15     }
16 
17     printf("\n输出2: 使用指针变量ptr1(指向元素)访问\n");
18     for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) {
19         printf("%d ", *ptr1);
20 
21         if ((i + 1) % 4 == 0)
22             printf("\n");
23     }
24                          
25     printf("\n输出3: 使用指针变量ptr2(指向一维数组)访问\n");
26     for (ptr2 = x; ptr2 < x + 2; ++ptr2) {
27         for (j = 0; j < 4; ++j)
28             printf("%d ", *(*ptr2 + j));
29         printf("\n");
30     }
31 
32     system("pause");
33     return 0;
34 }

 

截图:

屏幕截图 2026-06-02 200501

 

问题回答:

1.int (*ptr)[4]中ptr是一个指针变量;

2.int *ptr[4]中ptr是有4个元素的数组。

 

试验任务4

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define N 80
 4 
 5 void replace(char *str, char old_char, char new_char); 
 6 
 7 int main() {
 8     char text[N] = "Programming is difficult or not, it is a question.";
 9 
10     printf("原始文本: \n");
11     printf("%s\n", text);
12 
13     replace(text, 'i', '*');
14 
15     printf("处理后文本: \n");
16     printf("%s\n", text);
17 
18     system("pause");
19     return 0;
20 }
21 
22 
23 void replace(char *str, char old_char, char new_char) {
24     int i;
25 
26     while(*str) {
27         if(*str == old_char)
28             *str = new_char;
29         str++;
30     }
31 }

 

截图:

屏幕截图 2026-06-02 201107

 

问题回答:

1.将字符串中所有old_char全部替换成new_char;

2.可以。

 

试验任务5

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define N 80
 4 
 5 char *str_trunc(char *str, char x);
 6 
 7 int main() {
 8     char str[N];
 9     char ch;
10 
11     while(printf("输入字符串: "), gets(str) != NULL) {
12         printf("输入一个字符: ");
13         ch = getchar();
14 
15         printf("截断处理...\n");
16         str_trunc(str, ch);        
17 
18         printf("截断处理后的字符串: %s\n\n", str);
19         getchar();
20     }
21 
22     system("pause");
23     return 0;
24 }
25 
26 char *str_trunc(char *str, char x){
27     char *p = str;
28     while(*p && *p != x)
29         p++;
30     *p = '\0';
31     return str;
32 }

 

截图:

屏幕截图 2026-06-02 202059

 

问题回答:

删除后无法多组输入,作用是吸收缓冲区里残留的回车换行符。

 

试验任务6

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #define N 5
 5 
 6 int check_id(char *str); 
 7 
 8 int main()
 9 {
10     char *pid[N] = {"31010120000721656X",
11                     "3301061996X0203301",
12                     "53010220051126571",
13                     "510104199211197977",
14                     "53010220051126133Y"};
15     int i;
16 
17     for (i = 0; i < N; ++i)
18         if (check_id(pid[i]))
19             printf("%s\tTrue\n", pid[i]);
20         else
21             printf("%s\tFalse\n", pid[i]);
22 
23     system("pause");
24     return 0;
25 }
26 
27 
28 int check_id(char *str) {
29     int i;
30     if(strlen(str) != 18)
31        return 0;
32     for(i = 0;i < 18;i++)
33     {
34         if(str[i] >= '0' && str[i] <= '9')
35             continue;
36         else if(str[i] == 'X')
37         {
38             if(i !=17)
39                 return 0;
40         }
41         else return 0;
42     }
43     return 1;
44 }

 

截图:

屏幕截图 2026-06-02 203217

 

试验任务7

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define N 80
 4 void encoder(char *str, int n);
 5 void decoder(char *str, int n); 
 6 
 7 int main() {
 8     char words[N];
 9     int n;
10 
11     printf("输入英文文本: ");
12     gets(words);
13 
14     printf("输入n: ");
15     scanf("%d", &n);
16 
17     printf("编码后的英文文本: ");
18     encoder(words, n);     
19     printf("%s\n", words);
20 
21     printf("对编码后的英文文本解码: ");
22     decoder(words, n); 
23     printf("%s\n", words);
24 
25     system("pause");
26     return 0;
27 }
28 
29 
30 void encoder(char *str, int n) {
31     while(*str != '\0')
32     {
33         if(*str >= 'a' && *str <= 'z')
34         {
35             *str = (*str - 'a' + n) % 26 + 'a';
36         }
37         else if(*str >= 'A' && *str <= 'Z')
38         {
39             *str = (*str -'A' + n) % 26 + 'A';
40         }
41         str++;
42     }
43 }
44 
45 
46 void decoder(char *str, int n) {
47     while(*str != '\0')
48     {
49         if(*str >= 'a' && *str <= 'z')
50         {
51             *str = (*str - 'a' - n +26) % 26 + 'a';
52         }
53         else if(*str >= 'A' && *str <= 'Z')
54         {
55             *str = (*str -'A' - n +26) % 26 + 'A';
56         }
57         str++;
58     } 
59 }

 

截图:

屏幕截图 2026-06-02 204320

屏幕截图 2026-06-02 204404

 

试验任务8

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 void sort (char *ps[], int n);
 5 
 6 int main(int argc, char *argv[]) {
 7     int i;
 8     sort(argv + 1,argc - 1);
 9 
10     for(i = 1; i < argc; ++i)
11         printf("hello, %s\n", argv[i]);
12 
13     system("pause");
14     return 0;
15 }
16 
17 void sort (char *ps[], int n){
18     int i, j;
19     char *temp;
20     for(i = 0; i < n - 1; ++i)
21     {
22         for(j = 0; j < n - 1 - i; ++j)
23         {
24             if (strcmp(ps[j], ps[j + 1])>0)
25             {
26                 temp = ps[j];
27                 ps[j] = ps[j + 1];
28                 ps[j + 1] = temp;
29             }
30         }
31     }
32 }

 

截图:

屏幕截图 2026-06-02 224224

 

posted @ 2026-06-02 22:45  Hhhm00  阅读(4)  评论(0)    收藏  举报