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

Task1

task1_1

 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 = 1; i < n; ++i)
50         if(x[i] < *pmin)
51             *pmin = x[i];
52         else if(x[i] > *pmax)
53             *pmax = x[i];
54 }

A1:寻找输入数据中的最大值和最小值。

A2:指向x[0]的地址。

 

task1_2

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 #define N 5
 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 = 1; i < n; ++i)
49         if(x[i] > x[max_index])
50             max_index = i;
51     
52     return &x[max_index];
53 }

A1:返回一组数据中的最大值的地址。

A2:可以。

 

Task2

task2_1

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include<stdlib.h>
 4 #define N 80
 5 
 6 int main() {
 7     char s1[] = "Learning makes me happy";
 8     char s2[] = "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 }

A1: 数组s1的大小是24。

      sizeof(s1)计算的是s1占用的内存字节数。

      strlen(s1) 统计的是s1的字符串长度。

A2:不能。因为s1存放地址,不能直接将字符串赋值给它。

A3:已经交换。

 

task2_2

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include<stdlib.h>
 4 #define N 80
 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 }

A1:指针变量s1中存放的是字符串地址。

     sizeof(s1) 计算的是单词数。

     strlen(s1) 统计的是字符串长度。

A2:不能。task2_1中定义的是数组,而这里是指针数组。

A3:交换两个指针所指的地址。没有交换。

 

Task3

 1 #include <stdio.h>
 2 #include<stdlib.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 
 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     system("pause");
32     return 0;
33 }

A:int (*ptr)[4]; 中,标识符ptr表示的语义是指针。

    int *ptr[4]; 中,标识符ptr表示的语义是指针数组名 。

 

Task4

task4_1

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 #define N 80
 4 
 5 void replace(char *str, char old_char, char new_char); // 函数声明
 6 
 7 int main() {
 8     char text[N] = "c 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 }

A1: 函数 replace 的功能是替换字符。

A2: line24,圆括号里循环条件可以改写成 *str != '\0' 。 

 

task4_2

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 #define N 80
 4 
 5 void str_trunc(char *str, char x);
 6 
 7 int main() {
 8     char str[N];
 9     char ch;
10 
11     printf("输入字符串: ");
12     gets(str);
13 
14     printf("输入一个字符: ");
15     ch = getchar();
16 
17     printf("截断处理...\n");
18     str_trunc(str, ch);
19 
20     printf("截断处理后的字符串: %s\n", str);
21     
22     system("pause");
23     return 0;
24 }
25 
26 void str_trunc(char *str, char x) {
27     while(*str) {
28         if(*str == x)
29             *str='\0';     // blank1
30 
31              str++;   // blank2
32     }
33 
34     str--;// blank3
35 }

 

Task5

task5_1

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

 

task5_2

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

A:这两种算法实现中交换的是指针变量的值。

 

Task6

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 #include <string.h>
 4 #define N 5
 5 
 6 int check_id(char *str); // 函数声明
 7 
 8 int main() {
 9     char *pid[N] = {"31010120000721656X",
10                     "330106199609203301",
11                     "53010220051126571",
12                     "510104199211197977",
13                     "53010220051126133Y"};
14     int i;
15 
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 
22     system("pause");
23     return 0;
24 }
25 
26 // 函数定义
27 // 功能: 检查指针str指向的身份证号码串形式上是否合法。
28 // 形式合法,返回1,否则,返回0
29 int check_id(char *str) {
30     int len=strlen(str);
31     int i = 0;
32     if (len!=18)
33         return 0;
34     while (*str) {
35         for (; i < len - 1; i++) {
36             if (*(str + i) < '0' || (*(str + i) > '9'))
37 
38                 return 0;
39         }
40         for (i=len-1; i < len;i++) 
41         {
42         if (*(str + i) < '0' || (*(str + i) > '9' && *(str + i) != 'X'))
43             return 0;
44         }
45         return 1;
46     }
47 }

 

 

Task7

 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 
20     return 0;
21 }
22 
23 /*函数定义
24 功能:对s指向的字符串进行编码处理
25 编码规则:
26 对于a~z或A~Z之间的字母字符,用其后的字符替换; 其中,z用a替换,Z用A替换
27 其它非字母字符,保持不变
28 */
29 void encoder(char* str) {
30     while (*str) {
31         if (*str == 'z')
32             *str = 'a';
33         else if (*str == 'Z')
34             *str == 'A';
35         else if ((*str >= 'a' && *str <= 'y') || (*str >= 'A' && *str <= 'Y'))
36             (*str)++;
37         str++;
38     }
39         
40 }
41 
42 /*函数定义
43 功能:对s指向的字符串进行解码处理
44 解码规则:
45 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换
46 其它非字母字符,保持不变
47 */
48 void decoder(char* str) {
49     while (*str) {
50         if (*str == 'a')
51             *str = 'z';
52         else if (*str == 'A')
53             *str == 'z';
54         else if ((*str >= 'b' && *str <= 'z') || (*str >= 'B' && *str <= 'Z'))
55             (*str)--;
56         str++;
57     }
58 
59 }

 

posted @ 2024-05-20 22:17  InitialJ  阅读(36)  评论(0)    收藏  举报