实验报告5

task1

代码:

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

回答:

1.实现查找最大值和最小值,用指针指向其地址

2.都指向x[0]所在地址

1.find_max实现查找最大值,返回值是x[max_index]指向的最大值

2.可以实现

task2

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 80
 4 
 5 int main() {
 6     char s1[N] = "Learning makes me happy";
 7     char s2[N] = "Learning makes me sleepy";
 8     char tmp[N];
 9 
10     printf("sizeof(s1) vs. strlen(s1): \n");
11     printf("sizeof(s1) = %d\n", sizeof(s1));
12     printf("strlen(s1) = %d\n", strlen(s1));
13 
14     printf("\nbefore swap: \n");
15     printf("s1: %s\n", s1);
16     printf("s2: %s\n", s2);
17 
18     printf("\nswapping...\n");
19     strcpy(tmp, s1);
20     strcpy(s1, s2);
21     strcpy(s2, tmp);
22 
23     printf("\nafter swap: \n");
24     printf("s1: %s\n", s1);
25     printf("s2: %s\n", s2);
26 
27     return 0;
28 }

1.80     整个数组所占的内存字节数   字符串中有效字符的个数

2.不能     s1 是字符串组名,在定义后不能被赋值

3.是

task4

代码:

 1 #include <stdio.h>
 2 #define N 80
 3 #include <stdlib.h>
 4 void replace(char *str, char old_char, char new_char);
 5 
 6 int main() {
 7     char text[N] = "Programming is difficult or not, it is a question.";
 8 
 9     printf("原始文本: \n");
10     printf("%s\n", text);
11 
12     replace(text, 'i', '*');
13     printf("处理后文本: \n");
14     printf("%s\n", text);
15     system("pause");
16     return 0;
17 }
18 
19 
20 void replace(char *str, char old_char, char new_char) {
21     int i;
22 
23     while(*str) {
24         if(*str == old_char)
25             *str = new_char;
26         str++;
27     }
28 }

1.将等于old_char的字符串替换为new_char

2.可以

task5

代码:

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

回答:吸收输入缓存区中残留的换行符。

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 // 功能: 检查指针str指向的身份证号码串形式上是否合法
27 // 形式合法,返回1,否则,返回0
28 int check_id(char *str) {
29     int len = strlen(str),i;
30     if(len!=18)
31         return 0;
32     for(i=0;i<17;i++)
33     {
34         if(str[i]<'0' || str[i]>'9')
35         return 0;
36     }
37     if(!((str[17]>='0' && str[17]<='9') || str[17]=='X'))
38         return 0;
39     return 1;
40 }

截图:

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 /*函数定义
28 功能:对str指向的字符串进行编码处理
29 编码规则:
30 对于a~z或A~Z之间的字母字符,用其后第n个字符替换; 其它非字母字符,保持不变
31 */
32 void encoder(char *str, int n) {
33     int i = 0;
34     n = n % 26; 
35     while (str[i]!='\0'){
36          if (str[i]>='a' &&  str[i]<='z') 
37             str[i]=(str[i]-'a'+n)%26+'a';
38         else if (str[i]>='A' && str[i]<='Z') 
39             str[i]=(str[i]-'A'+n)%26+'A';
40         i++;
41     }
42     }
43 void decoder(char *str, int n) {
44     int i=0;
45     n=n%26;
46     while (str[i]!='\0') {
47         if (str[i]>='a' && str[i]<='z') {
48             str[i]=(str[i]-'a'-n+26)%26+'a';
49         } else if (str[i]>='A' && str[i]<='Z') {
50             str[i]=(str[i]-'A'-n+26)%26+'A';
51         }
52         i++;
53     }
54 }

截图:

e79a806ee042ef9cff70623058bda1ba

 task8

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int cmp(const void *a, const void *b) {
 6     return strcmp(*(char**)a, *(char**)b);
 7 }
 8 
 9 int main(int argc, char *argv[]) {
10     int i;
11     qsort(&argv[1],argc-1,sizeof(char*),cmp);
12     for(i = 1; i < argc; ++i)
13         printf("hello, %s\n", argv[i]);
14 
15     return 0;
16 }

截图:

525ec986e8afa05feb8dd4826ba8f56d

 

posted @ 2026-05-29 23:54  franxx2022  阅读(15)  评论(0)    收藏  举报