实验5

task1-1
 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 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 
27     return 0;
28 }
29 
30 void input(int x[], int n) {
31     int i;
32 
33     for(i = 0; i < n; ++i)
34         scanf("%d", &x[i]);
35 }
36 
37 void output(int x[], int n) {
38     int i;
39     
40     for(i = 0; i < n; ++i)
41         printf("%d ", x[i]);
42     printf("\n");
43 }
44 
45 void find_min_max(int x[], int n, int *pmin, int *pmax) {
46     int i;
47     
48     *pmin = *pmax = x[0];
49 
50     for(i = 0; i < n; ++i)
51         if(x[i] < *pmin)
52             *pmin = x[i];
53         else if(x[i] > *pmax)
54             *pmax = x[i];
55 }

 1.找出最大值和最小值

2.执行到Line45时,指针变量pmin,pmax都指数组的第一个数

 

task 1-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 
27     return 0;
28 }
29 
30 void input(int x[], int n) {
31     int i;
32 
33     for(i = 0; i < n; ++i)
34         scanf("%d", &x[i]);
35 }
36 
37 void output(int x[], int n) {
38     int i;
39     
40     for(i = 0; i < n; ++i)
41         printf("%d ", x[i]);
42     printf("\n");
43 }
44 
45 int *find_max(int x[], int n) {
46     int max_index = 0;
47     int i;
48 
49     for(i = 0; i < n; ++i)
50         if(x[i] > x[max_index])
51             max_index = i;
52     
53     return &x[max_index];
54 }

 1.返回数组 x 中最大元素的地址

2.可以

 

task2-1

#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#define N 80

int main() {
    char s1[N] = "Learning makes me happy";
    char s2[N] = "Learning makes me sleepy";
    char tmp[N];

    printf("sizeof(s1) vs. strlen(s1): \n");
    printf("sizeof(s1) = %d\n", sizeof(s1));
    printf("strlen(s1) = %d\n", strlen(s1));

    printf("\nbefore swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);

    printf("\nswapping...\n");
    strcpy(tmp, s1);
    strcpy(s1, s2);
    strcpy(s2, tmp);

    printf("\nafter swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);

    system("pause");
    return 0;
}

 1.数组s1的大小是80,sizeof(s1)计算的是数组所占的字节数,strlen(s1)统计的是字符串的实际字符数

2.不能。char s1[N]; 声明后,s1是数组名(常量指针),不能作为左值被重新赋值

3.是

 

task2-2

#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#define N 80

int main() {
    char *s1 = "Learning makes me happy";
    char *s2 = "Learning makes me sleepy";
    char *tmp;

    printf("sizeof(s1) vs. strlen(s1): \n");
    printf("sizeof(s1) = %d\n", sizeof(s1));
    printf("strlen(s1) = %d\n", strlen(s1));

    printf("\nbefore swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);

    printf("\nswapping...\n");
    tmp = s1;
    s1 = s2;
    s2 = tmp;

    printf("\nafter swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);

    system("pause");

    return 0;
}

 1.s1存放的是字符串中首字符的内存地址,sizeof(s1)计算的是s1占的字节数,strlen(s1)计算的是字符串的长度

2.可以,task2_1中的s1[]是字符串常量不可修改,task2_2中的s1可以赋值修改

3.交换的是指针值,字符串常量在内存中未交换,只是改变了指针的指向关系

 

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 
33     return 0;
34 }

 

task4

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

 1.replace的功能是将数组中的某个旧元素用新元素代替

2.可以

 

task5

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

 去掉getchar()之后,第二个输出错误

getchar()是为了换行

 

task6

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 
 5 int check_id(char *str) {
 6     int len = strlen(str);
 7    
 8     if (len!= 18) {
 9         return 0;
10     }
11     int i;
12     for (i = 0; i < 17; i++) {
13       
14         if (str[i] < '0' || str[i] > '9') {
15             return 0;
16         }
17     }
18    
19     if (str[17] < '0' || str[17] > '9') {
20         if (str[17]!= 'X') {
21             return 0;
22         }
23     }
24     return 1;
25 }
26 
27 int main() {
28     char *pid[N] = {"31010120000721656X",
29                     "3301061996X0203301",
30                     "53010220051126571",
31                     "510104199211197977",
32                     "53010220051126133Y"};
33     int i;
34     for (i = 0; i < N; i++) {
35         if (check_id(pid[i]))
36             printf("%s\tTrue\n", pid[i]);
37         else
38             printf("%s\tFalse\n", pid[i]);
39     }
40     return 0;
41 }

 

 

task7

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

 

 

task8

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void swap(char **a, char **b) {
 5     char *temp = *a;
 6     *a = *b;
 7     *b = temp;
 8 }
 9 
10 
11 void bubbleSort(char *arr[], int n) {
12     int i, j;
13     for (i = 0; i < n - 1; i++) {
14         for (j = 0; j < n - i - 1; j++) {
15             if (strcmp(arr[j[j arr[j + 1]) > 0) {
16                 swap(&arr[j[j &arr[j + 1]);
17             }
18         }
19     }
20 }
21 
22 int main(int argc, char *argv[]) {
23     int i;
24    
25     if (argc > 1) {
26         bubbleSort(&argv[1], argc - 1);
27     }
28     for (i = 1; i < argc; i++)
29         printf("hello, %s\n", argv[i]);
30 
31     return 0;
32 }

 

posted @ 2025-05-18 23:59  我爱吃冰淇淋  阅读(3)  评论(0)    收藏  举报