实验5

 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     {
28         printf("%d", x[i]);
29     }
30     printf("\n");
31 }
32 void find_min_max(int x[], int n, int* pmin, int* pmax) {
33     int i;
34     *pmin = *pmax = x[0];
35     for (i = 0; i < n; ++i)
36     {
37         if (x[i] < *pmin)
38             *pmin = x[i];
39         else if (x[i] > *pmax)
40             *pmax = x[i];
41     }
42 }

问题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 <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;s1是20;strlen统计s1长度

问题2;不能,数组不能直接赋值;

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

问题1;“learning makes me happy ”的地址,sizeof是地址大小;strlen计算的该字符串长度;

问题2;可以

问题3;只是交换了地址;

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

问题1;ptr是一个指针,它指向一个包含 4 个int类型元素的数组。

问题2;ptr是一个数组,它是一个包含 4 个元素的数组,每个元素都是int类型的指针。

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

问题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 {
19     int i;
20     for (i=0;str[i]!='\0';i++)
21     {
22         if (str[i]==x)
23         {
24             str[i]='\0';
25         }
26     }
27     return str;
28 }

问题1;结果错乱;

问题2;读取用户在输入字符串和字符后按下的回车键。

 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     for (i = 0; i < N; ++i)
16         if (check_id(pid[i]))
17             printf("%s\tTrue\n", pid[i]);
18         else
19             printf("%s\tFalse\n", pid[i]);
20     return 0;
21 }
22 
23 int check_id(char* str) {
24     char c[20];
25     strcpy(c, str);
26     int i;
27     if (strlen(c) != 18) {
28         return 0;
29     }
30     for (i = 0; i < 17; i++)
31     {
32         switch (c[i]) {
33         case '0':
34         case '1':
35         case '2':
36         case '3':
37         case '4':
38         case '5':
39         case '6':
40         case '7':
41         case '8':
42         case '9':break;
43         default:
44             return 0;
45         }
46     }
47         switch (c[17]) {
48         case '0':
49         case '1':
50         case '2':
51         case '3':
52         case '4':
53         case '5':
54         case '6':
55         case '7':
56         case '8':
57         case '9':
58         case 'X':
59             return 1;
60         default:
61             return 0;
62         }
63     }

 

 1 #include <stdio.h>
 2 #include <string.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     return 0;
20     }
21 void encoder(char* str, int n) {
22     int i;
23     char x[N];
24     strcpy(x, str);
25     for (i = 0; x[i]!='\0'; i++)
26     {
27         if ((x[i] >= 'a' && x[i] <= 'z') || (x[i] >= 'A' && x[i] <= 'Z'))
28         {
29             if (x[i] > 120 ||( x[i] > 88 && x[i] < 90))
30             {
31                 x[i] = x[i] - 26 + n;
32             }
33             else {
34                 x[i] = x[i] + n;
35             }
36         }
37     }
38     strcpy(str, x);
39 }
40 void decoder(char* str, int n) {
41     int i;
42     char x[N];
43     strcpy(x, str);
44     for (i = 0; x[i] != '\0'; i++)
45     {
46         if ((x[i] >= 'a' && x[i] <= 'z') || (x[i] >= 'A' && x[i] <= 'Z'))
47         {
48             if (x[i] < 67 || (x[i] > 96 && x[i] < 99))
49             {
50                 x[i] = x[i] + 26 - n;
51             }
52             else {
53                 x[i] = x[i] - n;
54             }
55         }
56     }
57     strcpy(str, x);
58 }

 

1 #include <stdio.h>
2 int main(int argc, char *argv[]) {
3 int i;
4 for(i = 1; i < argc; ++i)
5 printf("hello, %s\n", argv[i]);
6 return 0;
7 }

 

posted @ 2024-12-05 21:06  zhj910  阅读(2)  评论(0编辑  收藏  举报