实验5 指针
1.task1
task1_1.c
1 #include<stdio.h> 2 #define N 4 3 int main() 4 { 5 int x[N]={1,9,8,4}; 6 int i; 7 int *p; 8 9 //方式1:通过数组名和下标遍历输出数组元素 10 for(i=0;i<N;++i) 11 printf("%d",x[i]); 12 printf("\n"); 13 14 //方式2:通过指针变量遍历输出数组元素(写法1) 15 for(p=x;p<x+N;++p) 16 printf("%d",*p); 17 printf("\n"); 18 19 //方式2:通过指针变量遍历输出数组元素(写法2) 20 p=x; 21 for(i=0;i<N;++i) 22 printf("%d",*(p+i)); 23 printf("\n"); 24 25 //方式2:通过指针变量遍历输出数组元素(写法3) 26 p=x; 27 for(i=0;i<N;++i) 28 printf("%d",p[i]); 29 printf("\n"); 30 31 return 0; 32 }
task1_2.c
1 #include <stdio.h> 2 3 int main() 4 { 5 int x[2][4]={{1,9,8,4},{2,0,4,9}}; 6 int i,j; 7 int *p; 8 int(*q)[4]; 9 10 //使用数组名、下标访问二维数组元素 11 for(i=0;i<2;++i) 12 { 13 for(j=0;j<4;++j) 14 printf("%d",x[i][j]); 15 printf("\n"); 16 } 17 18 //使用指针变量p间接访问二维数组 19 for(p=&x[0][0],i=0;p<&x[0][0]+8;++p,++i) 20 { 21 printf("%d",*p); 22 if((i+1)%4==0) 23 printf("\n"); 24 } 25 26 //使用指针变量q间接访问二维数组 27 for(q=x;q<x+2;++q) 28 { 29 for(j=0;j<4;++j) 30 printf("%d",*(*q+j)); 31 printf("\n"); 32 } 33 return 0; 34 }
2.task2
1 #include<stdio.h> 2 #include<string.h> 3 #define N 80 4 5 int main() 6 { 7 char s1[]="Learning makes me happy"; 8 char s2[]="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 28 return 0; 29 }
1.数组s1的大小是23,sizeof(s1)计算的是s1占用的字节数,strlen(s1)计算的是s1的实际长度(除\0以外的字符个数)
2.不能替换。数组只能在定义是对其赋值。
3.s1和s2内容交换了
task2_2.c
1 #include <stdio.h> 2 #include<string.h> 3 #define N 80 4 int main() 5 { 6 char *s1="Learing makes me happy"; 7 //char *s1; line 6代码可以换成这两行; 8 //s1="Learning makes me happy"; 9 char *s2="Learing makes me slpeey"; 10 char *tmp; 11 12 printf("sizeof(s1)vs.strlen(s1):\n"); 13 printf("sizeof(s1)=%d\n",sizeof(s1)); 14 printf("strlen(s1)=%d\n",strlen(s1)); 15 16 printf("\nbefore swap:\n"); 17 printf("s1:%s\n",s1); 18 printf("s2:%s\n",s2); 19 20 printf("\nswapping...\n"); 21 tmp=s1; 22 s1=s2; 23 s2=tmp; 24 25 printf("\nafter swap:\n"); 26 printf("s1:%s\n",s1); 27 printf("s2:%s\n",s2); 28 29 return 0; 30 }
1.指针变量s1中存放的是字符串,sizeof(s1)计算的是字符串所占用的字节数,strlen(s1)计算的是有效字符数(除\0以外的字符个数)
2.可以替换。2.1中先定义一个空的数组s1,在s1中存放字符串。2.2中定义一个字符型指针变量s1,s1指向数组
3.交换的是指针的指向,s1和s2在内存存储单元中没有交换。
3.task3
task3.c
1 #include<stdio.h> 2 void str_cpy(char *target,const char *source); 3 void str_cat(char *str1,char *str2); 4 5 int main() 6 { 7 char s1[80],s2[20]="1984"; 8 9 str_cpy(s1,s2); 10 puts(s1); 11 12 str_cat(s1,"Animal Farm"); 13 puts(s1); 14 15 return 0; 16 } 17 void str_cpy(char *target,const char *source) 18 { 19 while(*target++=*source++) 20 ; 21 } 22 23 void str_cat(char *str1,char *str2) 24 { 25 while(*str1) 26 str1++; 27 28 while(*str1++=*str2++) 29 ; 30 }
4.task4
task4.c
1 #include<stdio.h> 2 #define N 80 3 int func(char*); 4 5 int main() 6 { 7 char str[80]; 8 9 while(gets(str)!=NULL) 10 { 11 if(func(str)) 12 printf("yes\n"); 13 else 14 printf("no\n"); 15 } 16 return 0; 17 } 18 int func(char *str) 19 { 20 char *begin,*end; 21 begin=end=str; 22 while(*end) 23 end++; 24 end--; 25 while(begin<end) 26 { 27 if(*begin!=*end) 28 return 0; 29 else 30 { 31 begin++; 32 end--; 33 } 34 } 35 return 1; 36 }
5.task5
task5.c
1 #include<stdio.h> 2 #define N 80 3 4 void func(char *); 5 6 int main() 7 { 8 char s[N]; 9 while(scanf("%s",s)!=EOF) 10 { 11 func(s); 12 puts(s); 13 } 14 return 0; 15 } 16 17 void func(char *str) 18 { 19 int i; 20 char *p1,*p2,*p; 21 p1=str; 22 while(*p1=='*') 23 p1++; 24 p2=str; 25 while(*p2) 26 p2++; 27 p2--; 28 while(*p2=='*') 29 p2--; 30 p=str; 31 i=0; 32 while(p<p1) 33 { 34 str[i]=*p; 35 p++; 36 i++; 37 } 38 while(p<=p2) 39 { 40 if(*p!='*') 41 { 42 str[i]=*p; 43 i++; 44 } 45 p++; 46 } 47 while(*p!='\0') 48 { 49 str[i]=*p; 50 p++; 51 i++; 52 } 53 str[i]='\0'; 54 }
6.task6
task6_1.c
1 #include<stdio.h> 2 #include<string.h> 3 void sort(char *name[],int n); 4 5 int main() 6 { 7 char *course[4]={"C program","C++ Object Oriented Program","Operating System","Data Structure and Algorithms"}; 8 int i; 9 sort(course,4); 10 for(i=0;i<4;i++) 11 printf("%s\n",course[i]); 12 return 0; 13 } 14 void sort(char *name[],int n) 15 { 16 int i,j; 17 char *tmp; 18 for(i=0;i<n-1;++i) 19 { 20 for(j=0;j<n-1-i;++j) 21 { 22 if(strcmp(name[j],name[j+1])>0)//strcmp比较字符串,<0表示s1<s2;>0表示s1大于s2 23 { 24 tmp=name[j]; 25 name[j]=name[j+1]; 26 name[j+1]=tmp; 27 } 28 } 29 } 30 }
task6_2.c
1 #include<stdio.h> 2 #include<string.h> 3 void sort(char *name[],int n); 4 5 int main() 6 { 7 char *course[4]={"C program","C++ Object Oriented Program","Operating System","Data Structure and Algorithms"}; 8 int i; 9 sort(course,4); 10 for(i=0;i<4;i++) 11 printf("%s\n",course[i]); 12 13 return 0; 14 } 15 void sort(char *name[],int n) 16 { 17 int i,j,k; 18 char *tmp; 19 20 for(i=0;i<n-1;i++) 21 { 22 k=i; 23 for(j=i+1;j<n;j++) 24 { 25 if(strcmp(name[j],name[k])<0) 26 k=j; 27 } 28 if(k!=i) 29 { 30 tmp=name[i]; 31 name[i]=name[k]; 32 name[k]=tmp; 33 } 34 } 35 }
两种算法实现中,交换的是指针变量的值。
7.task7
task7.c
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 "330106199609203301", 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 { 30 if(strlen(str)!=18) 31 return 0; 32 else 33 { 34 while(((*str>='0'&&*str<='9')||(*str=='X'))&&(*str!='\0')) 35 str++; 36 if(*str=='\0') 37 return 1; 38 else 39 return 0; 40 } 41 }
8.task8
task8.c
1 #include <stdio.h> 2 #define N 80 3 void encoder(char *s); // 函数声明 4 void decoder(char *s); // 函数声明 5 6 int main() 7 { 8 char words[N]; 9 10 printf("输入英文文本: "); 11 gets(words); 12 13 printf("编码后的英文文本: "); 14 encoder(words); // 函数调用 15 printf("%s\n", words); 16 17 printf("对编码后的英文文本解码: "); 18 decoder(words); // 函数调用 19 printf("%s\n", words); 20 21 return 0; 22 } 23 24 /*函数定义 25 功能:对s指向的字符串进行编码处理 26 编码规则: 27 对于a~z或A~Z之间的字母字符,用其后的字符替换; 其中,z用a替换,Z用A替换 28 其它非字母字符,保持不变 29 */ 30 void encoder(char *s) 31 { 32 char *p; 33 p=s; 34 while(*p) 35 { 36 if((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z')) 37 (*p)++; 38 else if(*p=='z'||*p=='Z') 39 *p=*p-25; 40 p++; 41 } 42 } 43 44 /*函数定义 45 功能:对s指向的字符串进行解码处理 46 解码规则: 47 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换 48 其它非字母字符,保持不变 49 */ 50 void decoder(char *s) 51 { 52 char *p; 53 p=s; 54 while(*p) 55 { 56 if(*p>='b'&&*p<='z'||*p>='B'&&*p<='Z') 57 (*p)--; 58 else if (*p=='a'||*p=='A') 59 *p=*p+25; 60 p++; 61 } 62 }