实验5

实验任务1

task 1_1

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

屏幕截图 2025-12-11 191318

Q1:输出数组中最大最小值

Q2:存放数组中第一个值的地址

 

task 1_2

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

屏幕截图 2025-12-11 192607

Q1:输出数组中的最大值;返回数组中的最大值的地址

Q2:可以

 

实验任务2

task 2_1

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 
 6 #define N 80
 7 int main() {
 8     char s1[N] = "Learning makes me happy";
 9     char s2[N] = "Learning makes me sleepy";
10     char tmp[N];
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     printf("\nbefore swap: \n");
15     printf("s1: %s\n", s1);
16     printf("s2: %s\n", s2);
17     printf("\nswapping...\n");
18     strcpy(tmp, s1);
19     strcpy(s1, s2);
20     strcpy(s2, tmp);
21     printf("\nafter swap: \n");
22     printf("s1: %s\n", s1);
23     printf("s2: %s\n", s2);
24     return 0;
25 }
View Code

屏幕截图 2025-12-11 194944

Q1:s1大小为80;sizeof(s1)计算的是数组占用字节数;strlen(s1)统计的是数组中的字符数

Q2:不能,s1是指针常量,不能直接赋值

Q3:互换了

 

task 2_2

 

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

 

屏幕截图 2025-12-11 200240

Q1:s1存放字符串的地址;sizeof(s1)计算s1占用字节数;strlen(s1)统计字符串长度

Q2:可以。这里s1是指针变量,可以赋值。前面是常量

Q3:交换的是地址;没有交换

 

实验任务3

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 
 6 int main() {
 7     int x[2][4] = { {1, 9, 8, 4}, {2, 0, 4, 9} };
 8     int i, j;
 9     int* ptr1;     // 指针变量,存放int类型数据的地址
10     int(*ptr2)[4]; // 指针变量,指向包含4个int元素的一维数组
11     printf("输出1: 使用数组名、下标直接访问二维数组元素\n");
12     for (i = 0; i < 2; ++i) {
13         for (j = 0; j < 4; ++j)
14             printf("%d ", x[i][j]);
15         printf("\n");
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         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     return 0;
31 }
View Code

屏幕截图 2025-12-11 201229

Q1:指包含4个int元素的一维数组

Q2:数组名

 

实验任务4

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

屏幕截图 2025-12-11 204750

Q1:将字符串中的一个字符换成新的字符

Q2:可以

 

实验任务5

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

屏幕截图 2025-12-11 211836

 Q1:无法再次输入字符串,只能输入字符;防止回车被视为字符

 

实验任务6

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

屏幕截图 2025-12-12 132734

 

实验任务7

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

屏幕截图 2025-12-12 141225

屏幕截图 2025-12-12 141347

屏幕截图 2025-12-12 141414

 

实验任务8

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 
 6 int main(int argc, char* argv[]) {
 7     int i, j;
 8     char* a;
 9     for (i = 1; i < argc - 1; i++) {
10         for (j = 1; j < argc - i; j++) {
11             if (strcmp(argv[j], argv[j + 1]) > 0) {
12                 a = argv[j];
13                 argv[j] = argv[j + 1];
14                 argv[j + 1] = a;
15             }
16         }
17     }
18     for (i = 1; i < argc; ++i)
19         printf("hello, %s\n", argv[i]);
20     return 0;
21 }
View Code

屏幕截图 2025-12-12 143751

 

posted @ 2025-12-12 14:39  刘奕晨  阅读(2)  评论(0)    收藏  举报