实验五
实验任务一
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 system("pause"); 25 return 0; 26 } 27 28 void input(int x[], int n) { 29 int i; 30 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 38 for(i = 0; i < n; ++i) 39 printf("%d ", x[i]); 40 printf("\n"); 41 } 42 43 void find_min_max(int x[], int n, int *pmin, int *pmax) { 44 int i; 45 46 *pmin = *pmax = x[0]; 47 48 for(i = 0; i < n; ++i) 49 if(x[i] < *pmin) 50 *pmin = x[i]; 51 else if(x[i] > *pmax) 52 *pmax = x[i]; 53 }

1.得出数组x中的最小值和最大值,并通过指针pmin和pmax返回。
2.pmin指向main,pmax指向max。
1-2
1 #include <stdio.h> 2 #define N 5 3 #include <stdlib.h> 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", 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("输出结果:\n"); 22 printf("max = %d\n", *pmax); 23 system("pause"); 24 return 0; 25 } 26 27 void input(int x[], int n) { 28 int i; 29 30 for(i = 0; i < n; ++i) 31 scanf("%d", &x[i]); 32 } 33 34 void output(int x[], int n) { 35 int i; 36 37 for(i = 0; i < n; ++i) 38 printf("%d ", x[i]); 39 printf("\n"); 40 } 41 42 int *find_max(int x[], int n) { 43 int max_index = 0; 44 int i; 45 46 for(i = 0; i < n; ++i) 47 if(x[i] > x[max_index]) 48 max_index = i; 49 50 return &x[max_index]; 51 }

1.找出数组x中的最大值;返回该元素的地址
2.可以
实验任务二
2-1
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <string.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 return 0; 29 }

1.80字节;计算数组总内存字节数;有效字符数。
2.不能,s1代表地址常量,不能被赋值。
3.是。
2-2
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <string.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 return 0; 29 }

1.“Learning makes me happy"的首地址;sizeof(s1)计算的是指针变量本身的字节大小;strlen(s1)统计的是字符串的有效长度。
2.可以;原是在定义指针变量的同时赋初值,后者是先定义未初始化的指针再赋值。
3.交换的是字符指针变量s1、s2的值;没交换。
实验任务三
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() { 4 int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}}; 5 int i, j; 6 int *ptr1; // 指针变量,存放int类型数据的地址 7 int(*ptr2)[4]; // 指针变量,指向包含4个int元素的一维数组 8 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 16 printf("\n输出2: 使用指针变量ptr1(指向元素)访问\n"); 17 for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) { 18 printf("%d ", *ptr1); 19 20 if ((i + 1) % 4 == 0) 21 printf("\n"); 22 } 23 24 printf("\n输出3: 使用指针变量ptr2(指向一维数组)访问\n"); 25 for (ptr2 = x; ptr2 < x + 2; ++ptr2) { 26 for (j = 0; j < 4; ++j) 27 printf("%d ", *(*ptr2 + j)); 28 printf("\n"); 29 } 30 return 0; 31 }

1.是一个指针变量,指向包含 4 个int元素的一维数组。
2.指包含 4 个元素的数组,每个元素都是指向int的指针。
实验任务四
1 #include <stdio.h> 2 #define N 80 3 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 9 printf("原始文本: \n"); 10 printf("%s\n", text); 11 12 replace(text, 'i', '*'); // 函数调用 注意字符形参写法,单引号不能少 13 14 printf("处理后文本: \n"); 15 printf("%s\n", text); 16 17 return 0; 18 } 19 20 // 函数定义 21 void replace(char *str, char old_char, char new_char) { 22 int i; 23 24 while(*str) { 25 if(*str == old_char) 26 *str = new_char; 27 str++; 28 } 29 }

1.将字符串str其中所有等于old_char的字符串替换为new_char,然后修改原字符串内容。
2.可以,因为*str != '\0'与*str等价。
实验任务五
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 char *p = str; 19 while(*p != '\0' && *p != x) { 20 p++; 21 } 22 if(*p == x) { 23 *p = '\0'; 24 } 25 return str; 26 }

不同:第一次输入正常,第二次后输入时输出空结果。
line18的作用:消耗掉上一次输入缓冲区中的换行符'\n',确保下一次正常运行。
实验任务六
1 #include <stdio.h> 2 #include <string.h> 3 #define N 5 4 int check_id(char *str); // 函数声明 5 int main() 6 { 7 char *pid[N] = {"31010120000721656X", 8 "3301061996X0203301", 9 "53010220051126571", 10 "510104199211197977", 11 "53010220051126133Y"}; 12 int i; 13 for (i = 0; i < N; ++i) 14 if (check_id(pid[i])) // 函数调用 15 printf("%s\tTrue\n", pid[i]); 16 else 17 printf("%s\tFalse\n", pid[i]); 18 return 0; 19 } 20 int check_id(char *str) { 21 int a= strlen(str); 22 int i; 23 if (a!= 18) { 24 return 0; 25 } 26 for (i = 0; i <a; ++i) { 27 if (!(str[i] >= '0' && str[i] <= '9')) { 28 if (i != 17 || str[i] != 'X') { 29 return 0; 30 } 31 } 32 } 33 return 1; 34 }

实验任务七
1 #include <stdio.h> 2 #define N 80 3 void encoder(char *str, int n); // 函数声明 4 void decoder(char *str, int n); // 函数声明 5 int main() { 6 char words[N]; 7 int n; 8 printf("输入英文文本: "); 9 gets(words); 10 printf("输入n: "); 11 scanf("%d", &n); 12 printf("编码后的英文文本: "); 13 encoder(words, n); // 函数调用 14 printf("%s\n", words); 15 printf("对编码后的英文文本解码: "); 16 decoder(words, n); // 函数调用 17 printf("%s\n", words); 18 return 0; 19 } 20 /*函数定义 21 功能:对str指向的字符串进行编码处理 22 编码规则: 23 对于a~z或A~Z之间的字母字符,用其后第n个字符替换; 其它非字母字符,保持不变 24 */ 25 void encoder(char *str, int n) { 26 while(*str != '0'){ 27 if(*str >= 'a' && *str <= 'z'){ 28 *str = (*str - 'a' + n) % 26 + 'a'; 29 } 30 else if (*str >= 'A' && *str <= 'Z') { 31 *str = (*str - 'A' + n) % 26 + 'A'; 32 } 33 str++; 34 } 35 // 补足函数实现 36 // ××× 37 } 38 /*函数定义 39 功能:对str指向的字符串进行解码处理 40 解码规则: 41 对于a~z或A~Z之间的字母字符,用其前面第n个字符替换; 其它非字母字符,保持不变 42 */ 43 void decoder(char *str, int n) { 44 while (*str != '\0') { 45 if (*str >= 'a' && *str <= 'z') { 46 *str = (*str - 'a' - n + 26) % 26 + 'a'; 47 } 48 else if (*str >= 'A' && *str <= 'Z') { 49 *str = (*str - 'A' - n + 26) % 26 + 'A'; 50 } 51 str++; 52 } 53 }



实验任务八
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 int main(int argc, char* argv[]) { 4 int i, j; 5 char* temp; 6 for (i = 1; i < argc - 1; ++i) { 7 for (j = 1; j < argc - i; ++j) { 8 if (strcmp(argv[j], argv[j + 1]) > 0) { 9 temp = argv[j]; 10 argv[j] = argv[j + 1]; 11 argv[j + 1] = temp; 12 } 13 } 14 } 15 for (i = 1; i < argc; ++i) 16 printf("hello, %s\n", argv[i]); 17 return 0; 18 }


浙公网安备 33010602011771号