实验5
实验5
实验任务1:
代码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 27 return 0; 28 } 29 30 void input(int x[], int n) { 31 int i; 32 33 for(i = 0; i < n; ++i) 34 scanf("%d", &x[i]); 35 } 36 37 void output(int x[], int n) { 38 int i; 39 40 for(i = 0; i < n; ++i) 41 printf("%d ", x[i]); 42 printf("\n"); 43 } 44 45 void find_min_max(int x[], int n, int *pmin, int *pmax) { 46 int i; 47 48 *pmin = *pmax = x[0]; 49 50 for(i = 0; i < n; ++i) 51 if(x[i] < *pmin) 52 *pmin = x[i]; 53 else if(x[i] > *pmax) 54 *pmax = x[i]; 55 }
截图
问题1:通过循环查找数组中的最大和最小值。
问题2:45行两个指针分别指向提供储存最小和最大值的变量地址。
代码1.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 27 return 0; 28 } 29 30 void input(int x[], int n) { 31 int i; 32 33 for(i = 0; i < n; ++i) 34 scanf("%d", &x[i]); 35 } 36 37 void output(int x[], int n) { 38 int i; 39 40 for(i = 0; i < n; ++i) 41 printf("%d ", x[i]); 42 printf("\n"); 43 } 44 45 int *find_max(int x[], int n) { 46 int max_index = 0; 47 int i; 48 49 for(i = 0; i < n; ++i) 50 if(x[i] > x[max_index]) 51 max_index = i; 52 53 return &x[max_index]; 54 }
截图
问题1:返回数组中最大元素的内存地址。
问题2:可以,前者通过return &x[max_index]定位最大元素地址,后者直接利用指针ptr指向x[0],都能达到返回最大值地址的效果。
实验任务2:
代码2.1

1 #include <stdio.h> 2 #include<stdlib.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 system("pause"); 29 30 return 0; 31 }
截图
问题1:s1大小占80字节,sizeof(s1)计算数组s1占用的内存字节,strlen(s1)统计的是s1中有效的字符个数(排除换行和空格等)。
问题2:不能,数组名s1是常量指针,不能进行赋值操作。
问题3:交换了。
代码2.2

1 #include <stdio.h> 2 #include<stdlib.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 system("pause"); 29 30 return 0; 31 }
截图
问题1:s1存放的是字符串对应的存储地址,sizeof(s1)计算的是指针变量s1 占用的字节数,strlen(s1)统计的是s1对应字符串的实际个数(不包括空格和换行等)。
问题2:可以,前者是通过定义s1的字符空间,再将字符复制到s1里;后者指针变量的指向改变,从而改变对应显示的内容。
问题3:交换的是指针变量s1 s2的对应储存地址,内存位置并没有改变。
实验任务3:
代码

1 #include <stdio.h> 2 #include <string.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 31 system("pause"); 32 33 return 0; 34 }
截图
实验任务4:
代码

1 #include <stdio.h> 2 #include<stdlib.h> 3 4 #define N 80 5 6 void replace(char *str, char old_char, char new_char); // 函数声明 7 8 int main() { 9 char text[N] = "Programming is difficult or not, it is a question."; 10 11 printf("原始文本: \n"); 12 printf("%s\n", text); 13 14 replace(text, 'i', '*'); // 函数调用 注意字符形参写法,单引号不能少 15 16 printf("处理后文本: \n"); 17 printf("%s\n", text); 18 19 system("pause"); 20 21 return 0; 22 } 23 // 函数定义 24 void replace(char *str, char old_char, char new_char) { 25 int i; 26 27 while(*str) { 28 if(*str == old_char) 29 *str = new_char; 30 str++; 31 } 32 }
截图
问题1:replace检查字符串str,通过自增指针后移,将所有等于old_char的字符替换为new_char。
问题2:可以。
实验任务5:
代码

1 #include <stdio.h> 2 #include<stdlib.h> 3 4 #define N 80 5 6 char *str_trunc(char *str, char x); 7 8 int main() { 9 char str[N]; 10 char ch; 11 12 while(printf("输入字符串: "), gets(str) != NULL) { 13 printf("输入一个字符: "); 14 ch = getchar(); 15 16 printf("截断处理...\n"); 17 str_trunc(str, ch); // 函数调用 18 19 printf("截断处理后的字符串: %s\n\n", str); 20 getchar(); 21 } 22 23 system("pause"); 24 25 return 0; 26 } 27 28 // 函数str_trunc定义 29 char *str_trunc(char *str,char x){ 30 while(*str){ 31 if(*str==x){ 32 *str='\0'; 33 return str; 34 } 35 str++; 36 } 37 return str; 38 } 39 40 // 功能: 对字符串作截断处理,把指定字符自第一次出现及其后的字符全部删除, 并返回字符串地址 41 // 待补足... 42 // xxx
截图
问题:多组输入后,除了第一次均无法出现结果,line 18的作用是获得输入的字符串,保证清除之前留下的残余字符,确保能够多次输入。
实验任务6:
代码

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 "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 return 0; 23 } 24 25 // 函数定义 26 // 功能: 检查指针str指向的身份证号码串形式上是否合法 27 // 形式合法,返回1,否则,返回0 28 int check_id(char* str) { 29 if (strlen(str) != 18) { 30 return 1; 31 } 32 int i; 33 for (i = 0; i < 17; i++) { 34 if (str[i] < '0' || str[i]>'9') { 35 return 1; 36 } 37 } 38 if (str[i] != 'X' && (str[i] < '0' || str[i]>'9')) { 39 return 1; 40 } 41 return 0; 42 // 补足函数实现 43 // ... 44 }
截图
实验任务7:
代码

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

1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) { 4 int i; 5 6 for(i = 1; i < argc; ++i) 7 printf("hello, %s\n", argv[i]); 8 9 return 0; 10 }
截图8.1
代码8.2

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