Experiment5

Task1_1:

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

T1

问题1:

函数的功能是找出数组中的最大值和最小值

问题2:

*pmin和*pmax均指向x[]的第一个元素地址

 Task1_2:

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

E5T1

问题一:

函数的功能是找出最大值,并返回该元素的指针

问题二:

可以用以下代码替代,这个代码是先用指针获得该元素的地址,再用ptr获取值进行比较

Task2_1:

 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 }

T2_1

问题1:

数组s1的大小是80,sizeof(s1)计算的是数组s1的大小,strlen(s1)统计的是数组s1中字符串的长度(到"/0"为止)

问题2:

不能替换成图中写法,s1是一个数组,指向首个元素的地址,不能被随意改变

问题3:

执行完后,内容被交换

Task2_2:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 80
 4 
 5 int main(){
 6     const char *s1 = "Learning makes me happy";
 7     const char *s2 = "Learning makes me sleepy";
 8     const char *tmp;
 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     tmp=s1;
20     s1=s2;
21     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 }

 

T2_2

在原来文件中老师给的代码在devc上显示警告,查询之后显示"不建议将字符串常量直接复制给普通的char*指针",写法并不安全,所以我在char之前加了const,const char*声明只读字符串指针,如果用字符数组,不能直接交换数组名(是常量,不能直接赋值),要用上面代码中strcpy交换字符串内容

 问题1:

指针s1存放的是字符串的内存地址,sizeof(s1)计算的是指针变量的大小,strlen(s1)统计的是指针指向的字符串的长度

问题2:

能替换,区别:指针s1是变量,但数组s1是一个常量,不能赋值

问题3:

交换的指针变量的值,即指针指向的内存地址,字符串常量的内存没有交换

Task3:

 

 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     
 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         
18         if((i+1)%4==0)
19         printf("\n");
20     }
21     printf("\n输出3:使用指针变量ptr2(指向一维数组)间接访问\n");
22     for(ptr2 = x;ptr2<x+2;++ptr2){
23         for(j=0;j<4;++j)
24         printf("%d",*(*ptr2 +j));
25         printf("\n");
26     }
27     return 0;
28 }

 

TT3

问题1:

int (*ptr)[4];ptr是指针,表示数组指针,指向包含4个int类型的数组
int *ptr[4];ptr是一个数组,包含4个int类型的指针
 
Task4:
 1 #include <stdio.h>
 2 #define N 80
 3 void replace(char *str,char old_char,char new_char);
 4 
 5 int main(){
 6     char text[N] = "Programming is different or not,it is a question.";
 7     printf("原始文本:\n");
 8     printf("%s\n",text);
 9     
10     replace(text,'i','*');
11     
12     printf("处理后文本:\n");
13     printf("%s\n",text);
14     
15     return 0;
16 }
17 void replace(char *str,char old_char,char new_char){
18     int i;
19     
20     while(*str){
21         if(*str == old_char)
22         *str = new_char;
23         str++;
24     }
25 }

T4

问题1:

1.函数的功能:将字符串中旧的字符串替换成新的字符串

2.可以写成 *str != '\0'

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     
11     while(1){
12         printf("请输入字符串:\n");
13         gets(str);
14         printf("请输入一个字符:\n");
15         ch = getchar();
16         
17         printf("截断处理...\n");
18         str_trunc(str,ch);
19         
20         printf("截断处理后的字符串:%s\n\n",str);
21         getchar();
22     }
23     return 0;
24 }
25 char *str_trunc(char *str,char x){
26     char *p=str;
27     while(*p!='\0'&&*p!=x){
28         p++;
29     }
30     if(*p==x)
31     *p='\0';
32     
33     return str;
34 }

T5

问题:

去掉getchar(),运行效果

T5_1

去掉getchar不能清除残留的换行符,导致下一次循环的时候,输入字符串直接读取换行符

Task6:

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

T6

 Task7:

 1 #include <stdio.h>
 2 #define N 80
 3 void encode(char *str,int n);
 4 void decode(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     encode(words,n);
18     printf("%s\n",words);
19     
20     printf("对编码后的英文文本解码:");
21     decode(words,n);
22     printf("%s\n",words);
23     
24     return 0;
25 }
26 
27 void encode(char *str,int n){
28     int i=0;
29     while(str[i]!='\0'){
30         char ch = str[i];
31         
32         if(ch>=65&&ch<=90){
33             str[i]=65+(ch-65+n)%26;
34         }
35         else if(ch>=97&&ch<=122){
36             str[i]=97+(ch-97+n)%26;
37         }
38         i++;
39     }
40     
41 }
42 void decode(char *str,int n){
43     int i=0;
44     while(str[i]!='\0'){
45         char ch=str[i];
46         if(ch>=65&&ch<=90){
47             str[i]=65+(ch-65-n+26)%26;
48         }
49         else if(ch>=97&&ch<=122){
50             str[i]=97+(ch-97-n+26)%26;
51         }
52         i++;
53     }
54 }

T7

Task8:

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

T8

 

posted @ 2025-12-10 22:47  郑子鑫  阅读(2)  评论(0)    收藏  举报