• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
nuist0415
博客园    首页    新随笔    联系   管理    订阅  订阅
实验五

task1_1 code

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

task1_1 result

1.找到最大和最小值并把它付给max,min。

2.指向数组中第一个元素的地址。

task1_2 code

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

task1_2 result

1. 找到最大值,并返回其地址。

2.可以。

task2_1 code

 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[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

task2_1 result

1.s1大小为24,sizeof计算的是其所占用内存,strlen统计字符数(除去空字符)。

2.不可以,s1是地址常量,不可以赋值。

 task2_2 code

 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 
 9     printf("sizeof(s1) vs. strlen(s1): \n");
10     printf("sizeof(s1) = %d\n", sizeof(s1));
11     printf("strlen(s1) = %d\n", strlen(s1));
12     printf("\nbefore swap: \n");
13     printf("s1: %s\n", s1);
14     printf("s2: %s\n", s2);
15     printf("\nswapping...\n");
16     tmp = s1;
17     s1 = s2;
18     s2 = tmp;
19     printf("\nafter swap: \n");
20     printf("s1: %s\n", s1);
21     printf("s2: %s\n", s2);
22     return 0;
23 }
View Code

task2_2 result

1.s1存放第一个字符串的起始地址。sizeof计算的是地址所占的内存大小。strlen统计的是字符数。

2.可以。把指针s1指向了这个字符串。

3.交换的是指针的指向。两个字符串在内存储存单元中没有交换。

task3 code

 1 #include <stdio.h>
 2 #include <stdio.h>
 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     printf("输出1: 使用数组名、下标直接访问二维数组元素\n");
 9     for (i = 0; i < 2; ++i) {
10         for (j = 0; j < 4; ++j)
11             printf("%d ", x[i][j]);
12         printf("\n");
13     }
14     printf("\n输出2: 使用指向元素的指针变量p间接访问二维数组元素\n");
15     for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) {
16         printf("%d ", *ptr1);
17         if ((i + 1) % 4 == 0)
18             printf("\n");
19     }
20     printf("\n输出3: 使用指向一维数组的指针变量q间接访问二维数组元素\n");
21     for (ptr2 = x; ptr2 < x + 2; ++ptr2) {
22         for (j = 0; j < 4; ++j)
23             printf("%d ", *(*ptr2 + j));
24         printf("\n");
25     }
26     return 0;
27 }
View Code

task3 result

1.指针变量,指向包含4个int元素的一维数组。

2.数组名,一个可以存放四个指针变量的数组。

task4_1 code

 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] = "c 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

task4_1 result

1.把旧的字符串中所有的i替换成*。

2.可以。

task4_2 code

 1 #include <stdio.h>
 2 #define N 80
 3 void str_trunc(char* str, char x);
 4 int main() {
 5     char str[N];
 6     char ch;
 7     printf("输入字符串: ");
 8     gets(str);
 9     printf("输入一个字符: ");
10     ch = getchar();
11     printf("截断处理...\n");
12     str_trunc(str, ch);
13     printf("截断处理后的字符串: %s\n", str);
14 }
15 void str_trunc(char* str, char x) {
16     while (*str) {
17         if (*str == x)
18             *str = NULL;
19         str++;
20     }
21 }
View Code

task4_2 result

task5_1 code

 1 #include <stdio.h>
 2 #include <string.h>
 3 void sort(char* name[], int n);
 4 int main() {
 5     char* course[4] = { "C Program",
 6     "C++ Object Oriented Program",
 7     "Operating System",
 8     "Data Structure and Algorithms" };
 9     int i;
10     sort(course, 4);
11     for (i = 0; i < 4; i++)
12         printf("%s\n", course[i]);
13     return 0;
14 }
15 void sort(char* name[], int n) {
16     int i, j;
17     char* tmp;
18     for (i = 0; i < n - 1; ++i)
19         for (j = 0; j < n - 1 - i; ++j)
20             if (strcmp(name[j], name[j + 1]) > 0) {
21                 tmp = name[j];
22                 name[j] = name[j + 1];
23                 name[j + 1] = tmp;
24             }
25 }
View Code

task5_1 result

task5_2 code

 1 #include <stdio.h>
 2 #include <string.h>
 3 void sort(char* name[], int n);
 4 int main() {
 5     char* course[4] = { "C Program",
 6     "C++ Object Oriented Program",
 7     "Operating System",
 8     "Data Structure and Algorithms" };
 9     int i;
10     sort(course, 4);
11     for (i = 0; i < 4; i++)
12         printf("%s\n", course[i]);
13     return 0;
14 }
15 void sort(char* name[], int n) {
16     int i, j, k;
17     char* tmp;
18     for (i = 0; i < n - 1; i++) {
19         k = i;
20         for (j = i + 1; j < n; j++)
21             if (strcmp(name[j], name[k]) < 0)
22                 k = j;
23         if (k != i) {
24             tmp = name[i];
25             name[i] = name[k];
26             name[k] = tmp;
27         }
28     }
29 }
View Code

task5_2 result

交换的是指针变量的值。

task6 code

 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     "330106199609203301",
 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 int check_id(char* str) {
21     int flag = 1, cnt = 0;
22 
23     while (*str) {
24         if (*str != 'X') {
25             if (*str >= '0' && *str <= '9');
26             else
27             flag = 0;
28         }
29         str++;
30         cnt++;
31     }
32     if (cnt != 18)
33         flag = 0;
34 
35     return flag;
36 }
View Code

task6 result

task7 code

 1 #include <stdio.h>
 2 #define N 80
 3 void encoder(char* str);
 4 void decoder(char* str);
 5 
 6 int main() {
 7     char words[N];
 8 
 9     printf("输入英文文本: ");
10     gets(words);
11 
12     printf("编码后的英文文本: ");
13     encoder(words); // 函数调用
14     printf("%s\n", words);
15 
16     printf("对编码后的英文文本解码: ");
17     decoder(words); // 函数调用
18     printf("%s\n", words);
19     return 0;
20 }
21 void encoder(char* str) {
22     while (*str) {
23         if (*str >= 'a' && *str <= 'y')
24             *str = *str + 1;
25         else if (*str >= 'A' && *str <= 'Y')
26             *str = *str + 1;
27         else if (*str == 'z')
28             *str = 'a';
29         else if (*str == 'Z')
30             *str = 'A';
31         str++;
32     }
33 }
34 void decoder(char* str) {
35     while (*str) {
36         if (*str > 'a' && *str <= 'z')
37             *str = *str - 1;
38         else if (*str > 'A' && *str <= 'Z')
39             *str = *str - 1;
40         else if (*str == 'a')
41             *str = 'z';
42         else if (*str == 'A')
43             *str = 'Z';
44         str++;
45     }
46 
47 }
View Code

task7 result

 

posted on 2023-11-27 19:04  Haruto  阅读(16)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3