实验5

实验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 
 8 int main() {
 9     int a[N];
10     int min, max;
11 
12     printf("录入%d个数据:\n", N);
13     input(a, N);
14 
15     printf("数据是: \n");
16     output(a, N);
17 
18     printf("数据处理...\n");
19     find_min_max(a, N, &min, &max);
20 
21     printf("输出结果:\n");
22     printf("min = %d, max = %d\n", min, max);
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 void find_min_max(int x[], int n, int *pmin, int *pmax) {
43     int i;
44     
45     *pmin = *pmax = x[0];
46 
47     for(i = 0; i < n; ++i)
48         if(x[i] < *pmin)
49             *pmin = x[i];
50         else if(x[i] > *pmax)
51             *pmax = x[i];
52 }
View Code
 1 id input(int x[], int n);
 2 void output(int x[], int n);
 3 int *find_max(int x[], int n);
 4 
 5 int main() {
 6     int a[N];
 7     int *pmax;
 8 
 9     printf("录入%d个数据:\n", N);
10     input(a, N);
11 
12     printf("数据是: \n");
13     output(a, N);
14 #include <stdio.h>
15 #define N 5
16 
17 vo
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

 

实验结果捕获

c1问题回答:1.找出你输入一组数据的最大值和最小值2.pmin是min的地址 pmax是max的地址

c2问题回答:1.找到数组中的最大值对应的元素2.可以 因为ptr指向的是主函数中数组a的元素

实验2

源代码

 1 id input(int x[], int n);
 2 void output(int x[], int n);
 3 int *find_max(int x[], int n);
 4 
 5 int main() {
 6     int a[N];
 7     int *pmax;
 8 
 9     printf("录入%d个数据:\n", N);
10     input(a, N);
11 
12     printf("数据是: \n");
13     output(a, N);
14 #include <stdio.h>
15 #define N 5
16 
17 vo
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
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 80
 4 
 5 int main() {
 6     char *s1 = "Learning makes me happy";
 7     char *s2 = "Learning makes me sleepy";
 8     char *tmp;
 9 
10     printf("sizeof(s1) vs. strlen(s1): \n");
11     printf("sizeof(s1) = %d\n", sizeof(s1));
12     printf("strlen(s1) = %d\n", strlen(s1));
13 
14     printf("\nbefore swap: \n");
15     printf("s1: %s\n", s1);
16     printf("s2: %s\n", s2);
17 
18     printf("\nswapping...\n");
19     tmp = s1;
20     s1 = s2;
21     s2 = tmp;
22 
23     printf("\nafter swap: \n");
24     printf("s1: %s\n", s1);
25     printf("s2: %s\n", s2);
26 
27     return 0;
28 }
View Code

 

实验结果IMG_20251207_123840

 

c1问题回答:1.s1的大小是80字节2.计算整个字符数组s1占用的总字节数3.统计字符串中从起始到末尾的有效字符个数

c2问题回答:1.地址  指针变量自身的字节大小  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;     // 指针变量,存放int类型数据的地址
 7     int(*ptr2)[4]; // 指针变量,指向包含4个int元素的一维数组
 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 }
View Code

 

实验结果IMG_20251207_125023

 

问题回答

实验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 }
View Code

 

实验结果IMG_20251207_125205

 

问题回答:1将字符串中old的字符替换成new2.可以

实验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          
11          printf("截断处理...\n");
12          str_trunc(str, ch); // 函数调用
13          
14          printf("截断处理后的字符串:%s\n\n", str);
15          getchar();
16      }
17      return 0;
18  }
19  // 函数str_trunc定义
20  // 功能:对字符串作截断处理,把指定字符自第一次出现及其后的字符全部删除,并返回字符串地址
21  char *str_trunc(char *str, char x) {
22      char *p = str; // 用指针p遍历字符串
23      // 找到第一个x的位置,或遍历到字符串末尾
24      while (*p != '\0' && *p != x) {
25          p++;
26      }
27      // 将x的位置(或末尾)设为字符串结束符'
View Code

 

实验结果IMG_20251207_125922

 

问题回答:1.除第一次正常运行 后续 会运行混乱2保证下一次正常执行

实验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  int check_id(char *str) {
22      // 1. 检查长度是否为18位
23      if (strlen(str) != 18) {
24          return 0;
25      }
26  }
View Code

 

实验结果IMG_20251207_130449

 

问题回答

实验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  void encoder(char *str, int n) {
22      char *p = str;
23      while (*p != '\0') {
24          if (*p >= 'a' && *p <= 'z') {
25              *p = (*p - 'a' + n) % 26 + 'a';
26          } else if (*p >= 'A' && *p <= 'Z') {
27              *p = (*p - 'A' + n) % 26 + 'A';
28          }
29          p++;
30      }
31  }
32  // 解码函数实现
33  void decoder(char *str, int n) {
34      char *p = str;
35      while (*p != '\0') {
36          if (*p >= 'a' && *p <= 'z') {
37              *p = (*p - 'a' - n + 26) % 26 + 'a';
38          } else if (*p >= 'A' && *p <= 'Z') {
39              *p = (*p - 'A' - n + 26) % 26 + 'A';
40          }
41          p++;
42      }
43  }
View Code

 

实验结果IMG_20251207_131031

 

问题回答

实验8

源代码

 1 #include <stdio.h>
 2  #include <string.h>
 3  int main(int argc, char *argv[]) {
 4      int i, j;
 5      char *temp;
 6      // 冒泡排序:对argv[1]到argv[argc-1]按字典序升序排列
 7      for (i = 1; i < argc - 1; i++) {
 8          for (j = 1; j < argc - i; j++) {
 9              // strcmp返回>0表示argv[j]字典序比argv[j+1]大,交换
10              if (strcmp(argv[j], argv[j+1]) > 0) {
11                  temp = argv[j];
12                  argv[j] = argv[j+1];
13                  argv[j+1] = temp;
14              }
15          }
16      }
17      // 遍历排序后的参数,打印问候
18      for (i = 1; i < argc; i++) {
19          printf("hello, %s\n", argv[i]);
20      }
21      return 0;
22  }
View Code

 

实验结果IMG_20251207_131344

 

posted @ 2025-12-07 13:19  错过不算爱  阅读(2)  评论(0)    收藏  举报