实验五

任务一

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

1,找到输入的数据中的最大值和最小值;

2,指向x[0];

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

1 找到输入的数据的最大值,返回它的地址

2,可以

任务二

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

1,80个字节,strlen统计的是数组所包含的字符的个数

2,不能,数组的大小已经固定。

3,交换了

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

1,数组s1的起始地址,s1所占用的字节大小,s1数组所包含的字符个数

2,可以,1中是对数组进行初始化,2是定义一个指针,这个指针指向字符串所在的地址。

3,交换的是数组地址,没有。

任务三

 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 printf("输出1: 使用数组名、下标直接访问二维数组元素\n");
 9 for (i = 0; i < 2; ++i) {
10 for (j = 0; j < 4; ++j)
11 printf("%d ", x[i][j]);
12 printf("\n");
13 }
14 printf("\n输出2: 使用指针变量ptr1(指向元素)间接访问\n");
15 for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) {
16 printf("%d ", *ptr1);
17 if ((i + 1) % 4 == 0)
18 printf("\n");
19 }
20 printf("\n输出3: 使用指针变量ptr2(指向一维数组)间接访问\n");
21 for (ptr2 = x; ptr2 < x + 2; ++ptr2) {
22 for (j = 0; j < 4; ++j)
23 printf("%d ", *(*ptr2 + j));
24 printf("\n");
25 }
26 system("pause");
27 return 0;
28 }

int (*ptr)[4]中ptr 是一个指向包含4个整数的数组的指针。

int *ptr[4]中ptr 是一个包含4个指向整数的指针的数组。

任务四

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

1,将字符串中的i替换成*

2,可以

任务五

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

任务六

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

任务七

 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 int main() {
 7 char words[N];
 8 int n;
 9 printf("输入英文文本: ");
10 gets(words);
11 printf("输入n: ");
12 scanf("%d", &n);
13 printf("编码后的英文文本: ");
14 encoder(words, n);
15 printf("%s\n", words);
16 printf("对编码后的英文文本解码: ");
17 decoder(words, n);
18 printf("%s\n", words);
19 system("pause");
20 return 0;
21 }
22 void encoder(char *str, int n) {
23     int q,t;
24     while(*str){
25         if(*str<='Z'&&*str>='A'){
26             t = *str - 'A';
27             q = (t + n)%26;
28             *str = q + 'A';
29         }
30         if(*str<='z'&&*str>='a'){
31             t = *str - 'a';
32             q = (t + n)%26;
33             *str = q + 'a';
34         }
35         str++;
36     }
37 }
38 
39 void decoder(char *str, int n) {
40     int q,t;
41     while(*str){
42         if(*str<='Z'&&*str>='A'){
43             t = *str - 'A';
44             q = (t - n)%26;
45             if(q<0){
46                 q += 26; 
47             }
48             *str = q + 'A';
49         }
50         if(*str<='z'&&*str>='a'){
51             t = *str - 'a';
52             q = (t - n)%26;
53             if(q<0){
54                 q+=26;
55             }
56             *str = q + 'a';
57         }
58         str++;
59     }
60 }

 

任务八

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

 

posted @ 2024-12-02 14:08  Little_Zcy  阅读(8)  评论(0编辑  收藏  举报