Yh-c-learning

导航

 

实验任务1

源代码1-1

 

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

 

 运行截图:

屏幕截图 2026-06-03 002027

 

 

 

问题一:函数find_min_max的功能是:找出所给数组中的最大值和最小值的位置,并用两个指针分别指向最大最小值所在的位置。

问题二:指针变量使用时必须指向确定地址,执行到line45时,指针变量pmin、pmax分别指向什么?指针变量pmin和pmax都指向x[0]的位置
 
源代码1-2
 1 #include <stdio.h>
 2 #define N 5
 3 
 4 void input(int x[], int n);
 5 void output(int x[], int n);
 6 int *find_max(int x[], int n);
 7 
 8 int main(){
 9     int a[N];
10     int *pmax;
11     
12     printf("录入%d个数据:\n",N);
13     input(a,N);
14     
15     printf("数据是: \n");
16     output(a, N);
17     
18     printf("数据处理...\n");
19     pmax = find_max(a, N);
20     
21     printf("输出结果:\n");
22     printf("max = %d\n", *pmax);
23     
24     return 0;
25 }
26 
27 void input(int x[], int n) {
28     int i;
29     
30     for(i=0; i<n; ++i)
31     scanf("%d", &x[i]);
32 }
33 
34 void output(int x[], int n){
35     int i;
36     
37     for(i = 0; i < n; ++i)
38         printf("%d", x[i]);
39     printf("\n");
40 }
41 
42 int *find_max(int x[], int n){
43     int max_index = 0;
44     int i;
45     
46     for(i=0; i < n; ++i)
47         if(x[i] > x[max_index])
48             max_index = i;
49             
50     return &x[max_index];
51 }
View Code

 

运行截图

屏幕截图 2026-06-03 002153

 问题1:找到数组中的最大值,返回最大值的地址

问题2:可以实现,逻辑等价

 

实验任务2

源代码2-1

 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

运行截图

屏幕截图 2026-06-03 002728

 问题一:s1 大小 80 字节;sizeof 统计数组整体占用字节;strlen 统计有效字符个数 (不含 '\0')。

 问题二:不能;数组名是常量地址,不能被直接赋值。

 问题三:是,完成两个数组内部字符内容互换

源代码2-2

 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

运行截图

屏幕截图 2026-06-03 003015

问题一:s1 存放字符串首地址;sizeof (s1) 是指针所占字节;strlen 统计字符串有效字符。

问题二:可以写法;char [] 开辟数组空间存字符串;char * 仅保存常量串首地址,字符串在常量区。

问题三:交换指针变量存的地址;内存中字符串常量内容不变。

 

实验任务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

运行截图

屏幕截图 2026-06-03 003435

问题 答案

 

int(*ptr2)[4]:ptr 是指向含 4 个 int 的一维数组指针;

 

int *ptr[4]: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

运行截图

屏幕截图 2026-06-03 003722

问题一:replace:遍历字符串,把所有 old_char 替换成 new_char。

问题二:可以,*str等价*str!='\0',字符串末尾 '\0' 值为 0。

 

实验任务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     while(printf("输入字符串: "), gets(str) != NULL) {
10         printf("输入一个字符: ");
11         ch = getchar();
12         printf("截断处理...\n");
13         str_trunc(str, ch);
14         printf("截断处理后的字符串: %s\n\n", str);
15         getchar();  // line18
16     }
17     return 0;
18 }
19 
20 // 函数str_trunc定义
21 char *str_trunc(char *str, char x) {
22     char *p = str;
23     while (*p != '\0') {
24         if (*p == x) {
25             *p = '\0';   // 截断:在此位置放字符串结束符
26             break;
27         }
28         p++;
29     }
30     return str;  // 返回原字符串地址
31 }
View Code

运行截图

屏幕截图 2026-06-03 004302

 

  去掉 getchar ():回车残留被下一次 getchar 接收,提前读取换行导致程序异常截断;

  作用:吸收输入字符后的回车换行符,消除缓冲区遗留回车。
 
实验任务6
源代码
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include<stdlib.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 // 功能: 检查指针str指向的身份证号码串形式上是否合法
29 // 形式合法,返回1,否则,返回0
30 int check_id(char *str)
31 {
32     int i;
33     char last;
34    if(strlen(str)!=18)
35        return 0;
36    for(i=0;i<17;i++)
37    {
38        if(*(str+i)<'0'||*(str+i)>'9')
39            return 0;
40    }
41    last =*(str+17);
42    if((last>='0'&&last<='9')||last=='X')
43        return 1;
44    else
45        return 0;
46 }
View Code

运行截图

屏幕截图 2026-06-03 004604

 

实验任务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     while (1) {
11         printf("\n输入英文文本: ");
12         if (gets(words) == NULL) break;
13         printf("输入n: ");
14         scanf("%d", &n);
15         while (getchar() != '\n');
16         printf("编码后的英文文本: ");
17         encoder(words, n);
18         printf("%s\n", words);
19         printf("对编码后的英文文本解码: ");
20         decoder(words, n);
21         printf("%s\n", words);
22     }
23     return 0;
24 }
25 
26 void encoder(char *str, int n) {
27     while (*str) {
28         if (*str >= 'a' && *str <= 'z')
29             *str = (*str - 'a' + n) % 26 + 'a';
30         else if (*str >= 'A' && *str <= 'Z')
31             *str = (*str - 'A' + n) % 26 + 'A';
32         str++;
33     }
34 }
35 
36 void decoder(char *str, int n) {
37     while (*str) {
38         if (*str >= 'a' && *str <= 'z')
39             *str = (*str - 'a' - n + 26) % 26 + 'a';
40         else if (*str >= 'A' && *str <= 'Z')
41             *str = (*str - 'A' - n + 26) % 26 + 'A';
42         str++;
43     }
44 }
View Code

运行截图

屏幕截图 2026-06-03 005051

 

实验任务8
源代码
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 
 6 int cmp_str(const void *a, const void *b) {
 7     
 8     return strcmp(*(const char **)a, *(const char **)b);
 9 }
10 
11 int main(int argc, char *argv[]) {
12     int i; 
13 
14    
15     qsort(argv + 1, argc - 1, sizeof(char *), cmp_str);
16 
17     
18     for (i = 1; i < argc; i++) {
19         printf("hello, %s\n", argv[i]);
20     }
21 
22     return 0;
23 }
View Code

运行截图

屏幕截图 2026-06-03 011216

 

 
posted on 2026-06-03 01:12  葛益豪  阅读(13)  评论(0)    收藏  举报