实验5

 

实验任务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 
27     return 0;
28 }
29 
30 void input(int x[], int n) {
31     int i;
32 
33     for(i = 0; i < n; ++i)
34         scanf("%d", &x[i]);
35 }
36 
37 void output(int x[], int n) {
38     int i;
39     
40     for(i = 0; i < n; ++i)
41         printf("%d ", x[i]);
42     printf("\n");
43 }
44 
45 void find_min_max(int x[], int n, int *pmin, int *pmax) {
46     int i;
47     
48     *pmin = *pmax = x[0];
49 
50     for(i = 0; i < n; ++i)
51         if(x[i] < *pmin)
52             *pmin = x[i];
53         else if(x[i] > *pmax)
54             *pmax = x[i];
55 }
1

运行截图

2

问题

1)找输入数据的最小值和最大值

2)指向数组x的第一个元素

 

实验任务2

源代码

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.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("sizeof(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("\nswappping...\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     return 0;
29 }
2.1

运行结果

2.1

问题

1)s1的大小为80个字节;sizeof(s1)计算的是s1所占空间的内存大小;strlen(s1)统计的是s1中含有的字符数量

2)不能,不能将字符串作为整体输入给数组

3)交换

源代码

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.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("sizeof(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("\nswappping...\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     return 0;
29 }
2.2

运行结果

2.2

问题

1)指针变量s1存放的是s1[0]的地址;sizeof(s1)计算的是指针变量s1所占空间的内存大小;strlen(s1)统计的是s1内字符数量

2)能;原代码是定义一个指向数组的指针变量并将字符串赋值给此变量;给出代码是先定义一个指针变量后进行赋值

3)交换的是地址;字符串常量未进行交换

 

实验任务3

源代码

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

运行结果

3

实验任务4

源代码

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

运行结果

4

问题

1)replace的作用是将字符串中的i全部替换为*

2)可以

 

实验任务5

源代码

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

运行结果

55

问题

去掉后,接下来不能重新输入一个字符,line18的作用为重置ch,以便接下来输入字符

 

实验任务6

源代码

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

运行结果

66

 

实验任务7

源代码

 1 #include <stdio.h>
 2 #define N 80
 3 
 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_s("%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     return 0;
26 
27 }
28 
29 void encoder(char* str, int n) {
30     int i;
31 
32     for (i = 0; str[i] != '\0'; ++i) {
33         if (str[i] >= 'a' && str[i] <= 'z') {
34             str[i] = 'a' + (str[i] - 'a' + n) % 26;
35         }
36 
37         else if (str[i] >= 'A' && str[i] <= 'Z') {
38             str[i] = 'A' + (str[i] - 'A' + n) % 26;
39         }
40     }
41 
42 }
43 
44 void decoder(char* str, int n) {
45     int i;
46 
47     for (i = 0; str[i] != '\0'; ++i) {
48         if (str[i] >= 'a' && str[i] <= 'z') {
49             str[i] = 'a' + (str[i] - 'a' - n) % 26;
50         }
51 
52         else if (str[i] >= 'A' && str[i] <= 'Z') {
53             str[i] = 'A' + (str[i] - 'A' - n) % 26;
54         }
55     }
56 }
7

运行结果

7

77

777

 

实验任务8

源代码1

 1 #include <stdio.h>
 2 
 3 int main(int argc, char *argv[]) {
 4     int i;
 5 
 6     for(i = 1; i < argc; ++i)
 7         printf("hello, %s\n", argv[i]);
 8 
 9     return 0;
10 }
11

运行结果

1

posted @ 2025-12-11 10:32  辣椒酱拌芥末  阅读(6)  评论(0)    收藏  举报