实验五

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

image

问题1:在整数数组中找出最大值和最小值

问题2:pmin指向函数中min的地址;pmax指向函数中max的地址

 

task1_2

 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 }

image

 问题1:找到最大值元素的地址

问题2:可以

```

task2

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include<stdlib.h>
 4 #define N 80
 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     system("pause");
28     return 0;
29 }

image

 问题1:s1数组大小是23;sizeof计算数组在内存中占用的总字节数;strlen统计字符串中实际字节数

问题2:S1是一个地址不能被赋值,并且如果是字符串赋值需要用到函数strcpy

问题3:交换了

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;
3

 问题1:存放字符串在内存中的首地址;sizeof计算s1占用的内存字节数;strlen统计\0之前的字节数

问题2:不能。指针储存的是字符串的首地址,这是一个字符指针指向字符串;数组是储存字符串,给数组初始化。

问题3:交换的是数组内容,在内存中没有交换

```

task3

 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;    
 8     int(*ptr2)[4]; 
 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 }

image

 ```

task4

 

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

 

image

问题1:replace的作用是将字符串中的old_char替换成new_charbu

问题2:可以

 ```

task5:

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

char *str_trunc(char *str, char x);

int main() {
    char str[N];
    char ch;

    while(printf("输入字符串: "), gets(str) != NULL) {
        printf("输入一个字符: ");
        ch = getchar();

        printf("截断处理...\n");
        str_trunc(str, ch);         

        printf("截断处理后的字符串: %s\n\n", str);
        getchar();
    }

    system("pause");
    return 0;
}

char *str_trunc(char *str, char x){
    char *b=str;
    while(*b!='\0'&&*b!=x){
        b++;    
    }
        if(*b ==x){
            *b='\0';
        };
        return str;
}

 

image

 ```

task6

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 
 5 int check_id(char *str);
 6 
 7 int main()
 8 {
 9     char *pid[N] = {"31010120000721656X",
10                     "3301061996X0203301",
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     return 0;
23 }
24 
25 
26 int check_id(char *str) {
27     int i;
28     if(strlen(str)!=18)
29         return 0;
30         
31     for(i=0;i<17;i++){
32         if(str[i]<'0'||str[i]>'9')
33         return 0;
34     }
35     if (str[17]>='0'&&str[17]<='9'||str[17]=='X')
36         return 1;
37     else
38         return 0;
39 }

 

image

 ```

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     int i;
29    for(i=0;str[i]!='\0';i++){
30        if(str[i]>='a'&&str[i]<='z')
31      str[i]='a'+(str[i]+n-'a')%26;
32      if(str[i]>='A'&&str[i]<='Z')
33      str[i]='A'+(str[i]+n-'A')%26;
34  }
35 }
36 
37 void decoder(char *str, int n) {
38     int i;
39    for(i=0;str[i]!='\0';i++){
40        if(str[i]>='a'&&str[i]<='z')
41      str[i]='a'+(str[i]+(26-n)-'a')%26;
42      if(str[i]>='A'&&str[i]<='Z')
43      str[i]='A'+(str[i]+(26-n)-'A')%26;                                          
44 }
45 }

image

image

image

 ```

task8

 1 #include <stdio.h>
 2 #include <string.h>
 3 int main(int argc, char *argv[]) {
 4     int i,j;
 5     for (i=1;i<argc;i++){
 6         for(j=1;j<argc-i;j++){
 7         
 8         if(strcmp(argv[j],argv[j+1])>0){
 9             char *temp=argv[j];
10             argv[j]=argv[j+1];
11             argv[j+1]=temp;
12         }
13             }
14     }
15     for(i = 1; i < argc; ++i)
16         printf("hello, %s\n", argv[i]);
17 
18     return 0;
19 }

image

 

posted @ 2025-12-09 20:25  李佳颖  阅读(1)  评论(0)    收藏  举报