实验5
实验1
源码1:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 5 4 5 void input(int x[],int n); 6 void output(int x[],int n); 7 void find_min_max(int x[],int n,int *pmin,int *pmax); 8 9 int main(){ 10 int a[N]; 11 int min,max; 12 13 printf("录入%d个数据:\n",N); 14 input(a,N); 15 16 printf("数据是:\n"); 17 output(a,N); 18 19 printf("数据处理...\n"); 20 find_min_max(a,N,&min,&max); 21 22 printf("输出结果:\n"); 23 printf("min=%d,max=%d\n",min,max); 24 25 system("pause"); 26 return 0; 27 } 28 29 void input(int x[],int n){ 30 int i; 31 32 for(i=0;i<n;i++) 33 scanf("%d",&x[i]); 34 } 35 36 void output(int x[],int n){ 37 int i; 38 39 for(i=0;i<n;i++) 40 printf("%d ",x[i]); 41 printf("\n"); 42 } 43 44 void find_min_max(int x[],int n,int *pmin,int *pmax){ 45 int i; 46 47 *pmin=*pmax=x[0]; 48 49 for(i=1;i<n;i++) 50 if(x[i]<*pmin) 51 *pmin=x[i]; 52 else if(x[i]>*pmax) 53 *pmax=x[i]; 54 }
截图1:
问题:
1.找到数组中的最大值和最小值
2.指向x[0]的地址
源码2:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 5 4 5 void input(int x[],int n); 6 void output(int x[],int n); 7 int *find_max(int x[],int n); 8 9 int main(){ 10 int a[N]; 11 int *pmax; 12 13 printf("录入%d个数据:\n",N); 14 input(a,N); 15 16 printf("数据是:\n"); 17 output(a,N); 18 19 printf("数据处理...\n"); 20 pmax=find_max(a,N); 21 22 printf("输出结果:\n"); 23 printf("max=%d\n",*pmax); 24 25 system("pause"); 26 return 0; 27 } 28 29 void input(int x[],int n){ 30 int i; 31 32 for(i=0;i<n;i++) 33 scanf("%d",&x[i]); 34 } 35 36 void output(int x[],int n){ 37 int i; 38 39 for(i=0;i<n;i++) 40 printf("%d ",x[i]); 41 printf("\n"); 42 } 43 44 int *find_max(int x[],int n){ 45 int max_index=0; 46 int i; 47 48 for(i=1;i<n;i++) 49 if(x[i]>x[max_index]) 50 max_index=i; 51 52 return &x[max_index]; 53 }
截图2:
问题:
1.返回数组中最大元素的地址
2.可以
实验2:
源码1:
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #define N 80 5 6 int main(){ 7 char s1[N]="Learning makes me happy"; 8 char s2[N]="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 system("pause"); 29 return 0; 30 }
截图1:
源码2:
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #define N 80 5 6 int main(){ 7 char *s1="Learning makes me happy"; 8 char *s2="Learning makes me sleepy"; 9 char *tmp; 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 tmp=s1; 21 s1=s2; 22 s2=tmp; 23 24 printf("\nafter swap:\n"); 25 printf("s1:%s\n",s1); 26 printf("s2:%s\n",s2); 27 28 system("pause"); 29 return 0; 30 }
截图2:
实验3:
源码:
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main(){ 5 int x[2][4]={{1,9,8,4},{2,0,4,9}}; 6 int i,j; 7 int *ptr1; 8 int (*ptr2)[4]; 9 10 printf("\n输出1:使用数组名、下标直接访问二维数组元素\n"); 11 for(i=0;i<2;i++){ 12 for(j=0;j<4;j++) 13 printf("%d ",x[i][j]); 14 printf("\n"); 15 } 16 17 printf("\n输出2:使用指针变量ptr1(指向元素)间接访问\n"); 18 for(ptr1=&x[0][0],i=0;ptr1<&x[0][0]+8;ptr1++,i++){ 19 printf("%d ",*ptr1); 20 21 if((i+1)%4==0) 22 printf("\n"); 23 } 24 25 printf("\n输出3:使用指针变量ptr2(指向一维数组)间接访问\n"); 26 for(ptr2=x;ptr2<x+2;ptr2++){ 27 for(j=0;j<4;j++) 28 printf("%d ",*(*ptr2+j)); 29 printf("\n"); 30 } 31 32 system("pause"); 33 return 0; 34 }
截图:
问题:
指向一个包含四个int类型元素的数组的指针变量 一个包含四个指向int类型数据的指针的指针数组
实验4:
源码:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 80 4 5 void replace(char *str,char old_char,char new_char); 6 7 int main(){ 8 char text[N]="Programming is difficult or not,it is a question."; 9 10 printf("原始文本:\n"); 11 printf("%s\n",text); 12 13 replace(text,'i','*'); 14 15 printf("处理后文本:\n"); 16 printf("%s\n",text); 17 18 system("pause"); 19 return 0; 20 } 21 22 void replace(char *str,char old_char,char new_char){ 23 int i; 24 25 while(*str){ 26 if(*str==old_char) 27 *str=new_char; 28 str++; 29 } 30 }
截图:
问题:
将字符串中的某种字符换成某种指定的新字符 可以
实验5:
源码:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 80 4 5 char *str_trunc(char *str,char x); 6 7 int main(){ 8 char str[N]; 9 char ch; 10 11 while(printf("输入字符串:"),gets(str)!=NULL){ 12 printf("输入一个字符:"); 13 ch=getchar(); 14 15 printf("截断处理...\n"); 16 str_trunc(str,ch); 17 18 printf("截断处理后的字符串:%s\n\n",str); 19 getchar(); 20 } 21 22 system("pause"); 23 return 0; 24 } 25 26 char *str_trunc(char *str,char x){ 27 while(*str){ 28 if(*str!=x) 29 str++; 30 else{ 31 *str='\0'; 32 break; 33 } 34 } 35 }
截图:
问题;
第一次操作后,无法输入指定字符 接受上一次操作的换行符
实验6:
源码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #define N 5 5 6 int check_id(char *str); 7 8 int main(){ 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 system("pause"); 23 return 0; 24 } 25 26 int check_id(char *str){ 27 int count=0; 28 while(*str){ 29 count++; 30 if(*str=='0'||*str=='1'||*str=='2'||*str=='3'||*str=='4'||*str=='5'||*str=='6'||*str=='7'||*str=='8'||*str=='9'){ 31 str++; 32 if(count==17) 33 break; 34 else 35 continue; 36 } 37 else 38 return 0; 39 } 40 if(count!=17) 41 return 0; 42 else 43 if(*str=='0'||*str=='1'||*str=='2'||*str=='3'||*str=='4'||*str=='5'||*str=='6'||*str=='7'||*str=='8'||*str=='9'||*str=='X'&&*(str+1)=='\0') 44 return 1; 45 else 46 return 0; 47 48 }
截图:
实验7:
源码:
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 7 int main(){ 8 char words[N]; 9 int n; 10 11 printf("输入英文文本:"); 12 gets(words); 13 14 printf("输入n:"); 15 scanf("%d",&n); 16 17 printf("编码后的英文文本:"); 18 encoder(words,n); 19 printf("%s\n",words); 20 21 printf("对编码后的英文文本解码:"); 22 decoder(words,n); 23 printf("%s",words); 24 25 system("pause"); 26 return 0; 27 } 28 29 void encoder(char *str,int n){ 30 while(n>=26) 31 n-=26; 32 while(*str){ 33 if((*str>='a'&&*str<=('z'-n))||(*str>='A'&&*str<=('Z'-n))) 34 *str+=n; 35 else if((*str<='z'&&*str>('z'-n))||(*str<='Z'&&*str>('Z'-n))) 36 *str=*str+n-26; 37 str++; 38 } 39 } 40 41 void decoder(char *str,int n){ 42 while(n>=26) 43 n-=26; 44 while(*str){ 45 if((*str>=('a'+n)&&*str<='z')||(*str>=('A'+n)&&*str<='Z')) 46 *str-=n; 47 else if((*str<('a'+n)&&*str>='a')||(*str<('A'+n)&&*str>='A')) 48 *str=*str+26-n; 49 str++; 50 } 51 }
截图:
实验8:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int main(int argc,char *argv[]){ 6 int i,j; 7 char *t; 8 9 for(j=1;j<argc;j++) 10 for(i=1;i<argc-j;i++) 11 if(strcmp(argv[i],argv[i+1])>0){ 12 t=argv[i]; 13 argv[i]=argv[i+1]; 14 argv[i+1]=t; 15 } 16 17 for(i=1;i<argc;i++) 18 printf("hello,%s\n",argv[i]); 19 20 system("pause"); 21 return 0; 22 }
截图: