实验5_C语言指针应用编程

实验任务1

task1_1.c源代码

 

 1 #include <stdio.h>
 2 #define N 5
 3 #include<stdlib.h>
 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     return 0;
27 }
28 
29 void input(int x[], int n) {
30     int i;
31 
32     for(i = 0; i < n; ++i)
33         scanf("%d", &x[i]);
34 }
35 
36 void output(int x[], int n) {
37     int i;
38     
39     for(i = 0; i < n; ++i)
40         printf("%d ", x[i]);
41     printf("\n");
42 }
43 
44 void find_min_max(int x[], int n, int *pmin, int *pmax) {
45     int i;
46     
47     *pmin = *pmax = x[0];
48 
49     for(i = 0; i < n; ++i)
50         if(x[i] < *pmin)
51             *pmin = x[i];
52         else if(x[i] > *pmax)
53             *pmax = x[i];
54 }
task1_1.c源代码

 

运行结果截图

 

 

 

问题回答

1.函数find_min_max实现的功能是找出所给数组中的最大值和最小值的位置,并用两个指针分别指向最大最小值所在的位置

2.指针变量pmin和pmax都指向x[0]的位置

 

task1_2.c源代码

 

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

 

 

 

 

运行结果截图

 

 

 

回答问题

1. 函数find_max的功能是返回数组中最大项所在的位置

2. 可以

 

实验任务2

task2_1.c源码

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 80
 4 #include<stdlib.h>
 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("strlen(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("\nswapping...\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     system("pause");
29     return 0;
30 }
task2_1.c源码

 

 

 

运行结果截图

 

 

 

回答问题

1. 数组s1的大小是80个字节;sizeof(s1)计算的是整个数组所占的内存空间大小;strlen(s1)统计的是数组中存储的字符串,逐个字符计数直到遇到结束标志为止

2.不能,s1是数组的起始地址,而等号右端是一串字符串,左右两端数据类型不一样

3.数组s1和s2中的内容交换了

 

task2_2.c源码

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 80
 4 #include<stdlib.h>
 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("strlen(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("\nswapping...\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     system("pause");
29     return 0;
30 }
task2_2.c源码

 

 

 

运行结果截图

 

 

 

回答问题

1.指针变量s1中存放的是字符串learning makes me happy的首位地址;

sizeof(s1)计算的是指针变量s1自身所占内存的字节数,并非字符串长度;

strlen(s1)统计的是字符串中所有的字符数量,直达遇到结束标志为止;

2.不能替换,下面的写法是定义一个字符指针s1,然后令一串字符串赋值给这个指针,这个写法是错误的,因为指针s1是不能更改的,这样程序会运行错误;而task2_1中是定义一个字符数组s2,然后将字符串放入其中

3.交换的是指针的指向,字符串常量在内存中没有交换

 

实验任务3

task3.c源代码

 

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main() {
 5     int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
 6     int i, j;
 7     int *ptr1;     // 指针变量,存放int类型数据的地址
 8     int(*ptr2)[4]; // 指针变量,指向包含4个int元素的一维数组
 9 
10     printf("输出1: 使用数组名、下标直接访问二维数组元素\n");
11     for (i = 0; i < 2; ++i) {
12         for (j = 0; j < 4; ++j)
13             printf("%d ", x[i][j]);
14         printf("\n");
15     }
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 
21         if ((i + 1) % 4 == 0)
22             printf("\n");
23     }
24                          
25     printf("\n输出3: 使用指针变量ptr2(指向一维数组)间接访问\n");
26     for (ptr2 = x; ptr2 < x + 2; ++ptr2) {
27         for (j = 0; j < 4; ++j)
28             printf("%d ", *(*ptr2 + j));
29         printf("\n");
30     }
31 
32     system("pause");
33     return 0;
34 }
task3.c源码

 

 

 

运行结果截图

 

 

 

回答问题

1.ptr表示一个数组指针,指向包含4个int类型元素的一维数组

2.ptr是一个指针数组,每个元素都是一个int类型的指针

 

实验任务4

task4.c源代码

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

 

运行结果截图

 

回答问题

1.replace的功能是遍历指针所指向的数组,并将指定的字符换成其他符号

2.可以

实验任务5

task5.c源代码

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

 

运行结果截图

 

回答问题

去掉line18的getchar()后多组输入时第二次输入字符串后会直接进行截断处理;这行的作用是清除缓冲区的回车

实验任务6

task6.c源代码

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 #include<stdlib.h>
 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     system("pause");
23     return 0;
24 }
25 
26 // 函数定义
27 // 功能: 检查指针str指向的身份证号码串形式上是否合法
28 // 形式合法,返回1,否则,返回0
29 int check_id(char *str) {
30     int lenth,i;
31     lenth=strlen(str);
32     if(lenth==18){
33     }
34     else 
35         return 0;
36     for(i=0;i<17;i++){
37         if(str[i]>='0'&&str[i]<='9'){
38         }
39         else
40             return 0;
41     }
42     if((str[17]>='0'&&str[17]<='9')||str[17]=='X'){
43     }
44     else
45         return 0;
46     return 1;
47 }
task6.c源代码

 

 

运行结果截图

 

 

 

试验任务7

task7.c源代码

 

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 #define N 80
 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("%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     system("pause");
25     return 0;
26 }
27 
28 /*函数定义
29 功能:对s指向的字符串进行编码处理
30 编码规则:
31 对于a~z或A~Z之间的字母字符,用其后第n个字符替换; 其它非字母字符,保持不变
32 */
33 void encoder(char *str, int n) {
34     while(*str!='\0'){
35         if(*str>='a'&&*str<='z')
36             *str='a'+(*str-'a'+n)%26;
37 
38         else if(*str>='A'&&*str<='Z')
39             *str='A'+(*str-'A'+n)%26;
40         str++;
41     }
42 }
43 
44 /*函数定义
45 功能:对s指向的字符串进行解码处理
46 解码规则:
47 对于a~z或A~Z之间的字母字符,用其前面第n个字符替换; 其它非字母字符,保持不变
48 */
49 void decoder(char *str, int n) {
50     // 补足函数实现
51         while(*str!='\0'){
52         if(*str>='a'&&*str<='z')
53             *str='a'+(*str-'a'-n+26)%26;
54 
55         else if(*str>='A'&&*str<='Z')
56             *str='A'+(*str-'A'-n+26)%26;
57         str++;
58         }
59 }
task7.c源代码

 

运行结果截图

 

 

 

 

实验任务8

task8.c源代码

 

 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 }
task8.c源代码

 

运行结果截图

 

 

posted @ 2025-05-16 18:01  High_Auditorium  阅读(24)  评论(0)    收藏  举报