实验五
task1_1
源代码:
1 #include <stdio.h> 2 #define N 5 3 #include <stdlib.h> 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 for(i = 0; i < n; ++i) 32 scanf("%d", &x[i]); 33 } 34 35 void output(int x[], int n) { 36 int i; 37 for(i = 0; i < n; ++i) 38 printf("%d ", x[i]); 39 printf("\n"); 40 } 41 42 void find_min_max(int x[], int n, int *pmin, int *pmax) { 43 int i; 44 *pmin = *pmax = x[0]; 45 for(i = 0; i < n; ++i) 46 if(x[i] < *pmin) 47 *pmin = x[i]; 48 else if(x[i] > *pmax) 49 *pmax = x[i]; 50 }
运行结果截图:

回答问题:
1.查找出数组中的最大值和最小值,并通过指针返回结果。
2.pmin指向min,pmax指向max。
task1_2
源代码:
1 #include <stdio.h> 2 #define N 5 3 #include<stdlib.h> 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 printf("录入%d个数据:\n", N); 13 input(a, N); 14 printf("数据是: \n"); 15 output(a, N); 16 printf("数据处理...\n"); 17 pmax = find_max(a, N); 18 printf("输出结果:\n"); 19 printf("max = %d\n", *pmax); 20 system("pause"); 21 return 0; 22 } 23 24 void input(int x[], int n) { 25 int i; 26 for(i = 0; i < n; ++i) 27 scanf("%d", &x[i]); 28 } 29 30 void output(int x[], int n) { 31 int i; 32 for(i = 0; i < n; ++i) 33 printf("%d ", x[i]); 34 printf("\n"); 35 } 36 37 int *find_max(int x[], int n) { 38 int max_index = 0; 39 int i; 40 for(i = 0; i < n; ++i) 41 if(x[i] > x[max_index]) 42 max_index = i; 43 return &x[max_index]; 44 }
运行结果截图:

task2_1
源代码:
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 #include<stdlib.h> 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 printf("sizeof(s1) vs. strlen(s1): \n"); 11 printf("sizeof(s1) = %d\n", sizeof(s1)); 12 printf("strlen(s1) = %d\n", strlen(s1)); 13 printf("\nbefore swap: \n"); 14 printf("s1: %s\n", s1); 15 printf("s2: %s\n", s2); 16 printf("\nswapping...\n"); 17 strcpy(tmp, s1); 18 strcpy(s1, s2); 19 strcpy(s2, tmp); 20 printf("\nafter swap: \n"); 21 printf("s1: %s\n", s1); 22 printf("s2: %s\n", s2); 23 system("pause"); 24 return 0; 25 }
运行结果截图:

回答问题:
1.s1大小 80 字节;sizeof(s1)是数组总字节数;strlen(s1)是有效字符数(不含\0)。
2.不能替换:数组名是地址常量,不能直接用=赋值字符串。
3.执行后s1与s2内容已交换。
task2_2
源代码:
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 #include<stdlib.h> 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 }
运行结果截图:

回答问题:
1.s1存放字符串常量"Learing makes me happy"首字母L的内存地址。
2.sizeof(s1)计算指针变量s1自身占用的内存字节数。
3.strlen(s1)统计s1指向地址后有效字符的个数。
4.可以替换,原写法是定义指针同时初始化赋值;拆的写法先定义空指针,后续语句赋值。
task3
源代码:
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 printf("输出1: 使用数组名、下标直接访问二维数组元素\n"); 10 for (i = 0; i < 2; ++i) { 11 for (j = 0; j < 4; ++j) 12 printf("%d ", x[i][j]); 13 printf("\n"); 14 } 15 printf("\n输出2: 使用指针变量ptr1(指向元素)访问\n"); 16 for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) { 17 printf("%d ", *ptr1); 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 system("pause"); 28 return 0;}
运行结果截图:

回答问题:
问题答案
1.int (*ptr)[4]:ptr是数组指针,指向含 4 个 int 的一维数组。
2.int *ptr[4]:ptr是指针数组,存放 4 个 int * 类型指针。
task4
源代码:
1 #include <stdio.h> 2 #define N 80 3 #include<stdlib.h> 4 void replace(char *str, char old_char, char new_char); 5 6 int main() { 7 char text[N] = "Programming is difficult or not, it is a question."; 8 printf("原始文本: \n"); 9 printf("%s\n", text); 10 replace(text, 'i', '*'); 11 printf("处理后文本: \n"); 12 printf("%s\n", text); 13 system("pause"); 14 return 0; 15 } 16 17 void replace(char *str, char old_char, char new_char) { 18 while(*str) { 19 if(*str == old_char) 20 *str = new_char; 21 str++; 22 } 23 }
运行结果截图:

回答问题:
1.replace功能:将字符串中指定字符替换为新字符。
2.可以写成*str != '\0',与*str等价。
task5
源代码:
1 int main() { 2 char str[N]; 3 char ch; 4 while(printf("输入字符串: "), gets(str) != NULL) { 5 printf("输入一个字符: "); 6 ch = getchar(); 7 printf("截断处理...\n"); 8 str_trunc(str, ch); 9 printf("截断处理后的字符串: %s\n\n", str); 10 getchar(); 11 } 12 return 0; 13 } 14 15 char *str_trunc(char *str, char x) { 16 char *p = str; 17 while(*p != '\0' && *p != x) 18 p++; 19 *p = '\0'; 20 system("pause"); 21 return str; 22 }
运行结果截图:

回答问题:
去掉getchar()会吞掉换行符,导致字符输入异常;
getchar()作用是吸收缓冲区多余换行,保证正常输入。
task6
源代码:
1 #include <stdio.h> 2 #define N 80 3 #include<stdlib.h> 4 5 char *str_trunc(char *str, char x); 6 7 #include <stdio.h> 8 #include <string.h> 9 #define N 5 10 int check_id(char *str); 11 12 int main() { 13 char *pid[N] = { 14 "31010120000721656X", 15 "3301061996X0203301", 16 "53010220051126571", 17 "510104199211197977", 18 "53010220051126133Y" 19 }; 20 int i; 21 for (i = 0; i < N; ++i) 22 if (check_id(pid[i])) 23 printf("%s\tTrue\n", pid[i]); 24 else 25 printf("%s\tFalse\n", pid[i]); 26 system("pause"); 27 return 0; 28 } 29 30 int check_id(char *str) { 31 int len = strlen(str); 32 if(len != 18) return 0; 33 for(int i=0; i<17; i++) { 34 if(!(str[i]>='0'&&str[i]<='9')) 35 return 0; 36 } 37 char last = str[17]; 38 if(!((last>='0'&&last<='9')||last=='X')) 39 return 0; 40 return 1; 41 }
运行结果截图:

task7
源代码:
1 #include <stdio.h> 2 #include <stdio.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 printf("输入英文文本: "); 11 gets(words); 12 printf("输入n: "); 13 scanf("%d", &n); 14 printf("编码后的英文文本: "); 15 encoder(words, n); 16 printf("%s\n", words); 17 printf("对编码后的英文文本解码: "); 18 decoder(words, n); 19 printf("%s\n", words); 20 system("pause"); 21 return 0; 22 } 23 24 void encoder(char *str, int n) { 25 while(*str) { 26 if(*str>='a'&&*str<='z') { 27 *str = (*str-'a'+n)%26+'a'; 28 } else if(*str>='A'&&*str<='Z') { 29 *str = (*str-'A'+n)%26+'A'; 30 } 31 str++; 32 } 33 } 34 35 void decoder(char *str, int n) { 36 while(*str) { 37 if(*str>='a'&&*str<='z') { 38 *str = (*str-'a'-n+26)%26+'a'; 39 } else if(*str>='A'&&*str<='Z') { 40 *str = (*str-'A'-n+26)%26+'A'; 41 } 42 str++; 43 } 44 }
运行结果截图:

task8
源代码:
1 #include <stdio.h> 2 #include <string.h> 3 4 void sort(char *arr[], int n) { 5 int i,j; 6 char *tmp; 7 for(i=1; i<n; i++) 8 for(j=1; j<n-i; j++) 9 if(strcmp(arr[j],arr[j+1])>0) { 10 tmp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=tmp; 11 } 12 } 13 14 int main(int argc, char *argv[]) { 15 sort(argv,argc); 16 for(int i=1; i<argc; i++) 17 printf("hello, %s\n", argv[i]); 18 return 0; 19 }
浙公网安备 33010602011771号