实验5
任务1
task1_1:
- 源代码
View Code1 #include <stdio.h> 2 #define N 5 3 4 void input(int x[], int n); 5 void output(int x[], int n); 6 void find_min_max(int x[], int n, int *pmin, int *pmax); 7 8 int main() { 9 int a[N]; 10 int min, max; 11 12 printf("录入%d个数据:\n", N); 13 input(a, N); 14 15 printf("数据是: \n"); 16 output(a, N); 17 18 printf("数据处理...\n"); 19 find_min_max(a, N, &min, &max); 20 21 printf("输出结果:\n"); 22 printf("min = %d, max = %d\n", min, max); 23 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 void find_min_max(int x[], int n, int *pmin, int *pmax) { 43 int i; 44 45 *pmin = *pmax = x[0]; 46 47 for(i = 0; i < n; ++i) 48 if(x[i] < *pmin) 49 *pmin = x[i]; 50 else if(x[i] > *pmax) 51 *pmax = x[i]; 52 }
- 运行测试截图

- 回答问题:
1.找到数组x里的最大值和最小值,通过指针参数返回结果
2.调用函数时输入的变量的地址
task1_2:
- 源代码
View Code1 #include <stdio.h> 2 #define N 5 3 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 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
task2_1:
- 源代码
View Code1 #define _CRT_SECURE_NO_WARNINGS 2 3 4 5 #include <stdio.h> 6 #include <string.h> 7 #define N 80 8 9 int main() { 10 char s1[N] = "Learning makes me happy"; 11 char s2[N] = "Learning makes me sleepy"; 12 char tmp[N]; 13 14 printf("sizeof(s1) vs. strlen(s1): \n"); 15 printf("sizeof(s1) = %d\n", sizeof(s1)); 16 printf("strlen(s1) = %d\n", strlen(s1)); 17 18 printf("\nbefore swap: \n"); 19 printf("s1: %s\n", s1); 20 printf("s2: %s\n", s2); 21 22 printf("\nswapping...\n"); 23 strcpy(tmp, s1); 24 strcpy(s1, s2); 25 strcpy(s2, tmp); 26 27 printf("\nafter swap: \n"); 28 printf("s1: %s\n", s1); 29 printf("s2: %s\n", s2); 30 31 return 0; 32 }
- 运行测试截图

- 回答问题
1.80;占总内存字节数(含\0);字符长度(不含\0)
2.不能,数组名是地址,不能赋值
3.是
task2_2:
- 源代码
View Code1 #define _CRT_SECURE_NO_WARNINGS 2 3 4 5 #include <stdio.h> 6 #include <string.h> 7 #define N 80 8 9 int main() { 10 char *s1 = "Learning makes me happy"; 11 char *s2 = "Learning makes me sleepy"; 12 char *tmp; 13 14 printf("sizeof(s1) vs. strlen(s1): \n"); 15 printf("sizeof(s1) = %d\n", sizeof(s1)); 16 printf("strlen(s1) = %d\n", strlen(s1)); 17 18 printf("\nbefore swap: \n"); 19 printf("s1: %s\n", s1); 20 printf("s2: %s\n", s2); 21 22 printf("\nswapping...\n"); 23 tmp = s1; 24 s1 = s2; 25 s2 = tmp; 26 27 printf("\nafter swap: \n"); 28 printf("s1: %s\n", s1); 29 printf("s2: %s\n", s2); 30 31 return 0; 32 }
- 运行测试截图

- 回答问题
1.字符串的地址;指针大小;有效字符数(不含\0)
2.可以;2-1是将内容存在数组s1中,2-2是指针s1指向字符串
3.指针指向;没有交换
任务3
- 源代码
View Code1 #define _CRT_SECURE_NO_WARNINGS 2 3 4 #include <stdio.h> 5 6 int main() { 7 int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}}; 8 int i, j; 9 int *ptr1; // 指针变量,存放int类型数据的地址 10 int(*ptr2)[4]; // 指针变量,指向包含4个int元素的一维数组 11 12 printf("输出1: 使用数组名、下标直接访问二维数组元素\n"); 13 for (i = 0; i < 2; ++i) { 14 for (j = 0; j < 4; ++j) 15 printf("%d ", x[i][j]); 16 printf("\n"); 17 } 18 19 printf("\n输出2: 使用指针变量ptr1(指向元素)访问\n"); 20 for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) { 21 printf("%d ", *ptr1); 22 23 if ((i + 1) % 4 == 0) 24 printf("\n"); 25 } 26 27 printf("\n输出3: 使用指针变量ptr2(指向一维数组)访问\n"); 28 for (ptr2 = x; ptr2 < x + 2; ++ptr2) { 29 for (j = 0; j < 4; ++j) 30 printf("%d ", *(*ptr2 + j)); 31 printf("\n"); 32 } 33 34 return 0; 35 }
- 运行测试截图

- 回答问题
int (*ptr)[4]:指向4个int的一维数组
int *ptr[4]:指针数组,存放4个int*
任务4
- 源代码
View Code1 #define _CRT_SECURE_NO_WARNINGS 2 3 4 5 #include <stdio.h> 6 #define N 80 7 8 void replace(char *str, char old_char, char new_char); // 函数声明 9 10 int main() { 11 char text[N] = "Programming is difficult or not, it is a question."; 12 13 printf("原始文本: \n"); 14 printf("%s\n", text); 15 16 replace(text, 'i', '*'); // 函数调用 注意字符形参写法,单引号不能少 17 18 printf("处理后文本: \n"); 19 printf("%s\n", text); 20 21 return 0; 22 } 23 24 // 函数定义 25 void replace(char *str, char old_char, char new_char) { 26 int i; 27 28 while(*str) { 29 if(*str == old_char) 30 *str = new_char; 31 str++; 32 } 33 }
- 运行测试截图

- 回答问题
1.将字符串中的i替换成*
2.可以
任务5
- 源代码
View Code1 #define _CRT_SECURE_NO_WARNINGS 2 3 4 #include <stdio.h> 5 #define N 80 6 7 char *str_trunc(char *str, char x); 8 9 int main() { 10 char str[N]; 11 char ch; 12 13 while(printf("输入字符串: "), gets(str) != NULL) { 14 printf("输入一个字符: "); 15 ch = getchar(); 16 17 printf("截断处理...\n"); 18 str_trunc(str, ch); // 函数调用 19 20 printf("截断处理后的字符串: %s\n\n", str); 21 getchar(); 22 } 23 24 return 0; 25 } 26 27 // 函数str_trunc定义 28 // 功能: 对字符串作截断处理,把指定字符自第一次出现及其后的字符全部删除, 并返回字符串地址 29 // 待补足... 30 // xxx 31 char* str_trunc(char* str, char x) { 32 char* p = str; 33 while (*p !='\0' && *p != x) 34 p++; 35 *p = '\0'; 36 return str; 37 }
- 运行测试截图

- 回答问题
吸收换行符,避免影响下一次输入
任务6
- 源代码
View Code1 #define _CRT_SECURE_NO_WARNINGS 2 3 4 #include <stdio.h> 5 #include <string.h> 6 #define N 5 7 8 int check_id(char *str); // 函数声明 9 10 int main() 11 { 12 char *pid[N] = {"31010120000721656X", 13 "3301061996X0203301", 14 "53010220051126571", 15 "510104199211197977", 16 "53010220051126133Y"}; 17 int i; 18 19 for (i = 0; i < N; ++i) 20 if (check_id(pid[i])) // 函数调用 21 printf("%s\tTrue\n", pid[i]); 22 else 23 printf("%s\tFalse\n", pid[i]); 24 25 return 0; 26 } 27 28 // 函数定义 29 // 功能: 检查指针str指向的身份证号码串形式上是否合法 30 // 形式合法,返回1,否则,返回0 31 int check_id(char *str) { 32 int len = strlen(str); 33 if (len != 18) 34 return 0; 35 for (int i = 0; i < 17; i++) { 36 if (!(str[i] >= '0' && str[i] <= '9')) { 37 if (i != 17 || str[i] != 'X') 38 return 0; 39 } 40 41 } 42 43 return 1; 44 }
- 运行测试截图

任务7
- 源代码
View Code1 #define _CRT_SECURE_NO_WARNINGS 2 3 4 #include <stdio.h> 5 #define N 80 6 void encoder(char *str, int n); // 函数声明 7 void decoder(char *str, int n); // 函数声明 8 9 int main() { 10 char words[N]; 11 int n; 12 13 printf("输入英文文本: "); 14 gets(words); 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 功能:对str指向的字符串进行编码处理 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 = (*str - 'a' + n) % 26 + 'a'; 39 } 40 else if (*str >= 'A' && *str <= 'Z') { 41 *str = (*str - 'A' + n) % 26 + 'A'; 42 } 43 str++; 44 } 45 } 46 47 /*函数定义 48 功能:对str指向的字符串进行解码处理 49 解码规则: 50 对于a~z或A~Z之间的字母字符,用其前面第n个字符替换; 其它非字母字符,保持不变*/ 51 52 void decoder(char *str, int n) { 53 while (*str) { 54 if (*str >= 'a' && *str <= 'z') { 55 *str = (*str - 'a' - n + 26) % 26 + 'a'; 56 } 57 else if (*str >= 'A' && *str <= 'Z') { 58 *str = (*str - 'A' - n + 26) % 26 + 'A'; 59 } 60 str++; 61 } 62 }
- 运行测试截图

任务8
- 源代码
View Code1 #define _CRT_SECURE_NO_WARNINGS 2 3 4 #include <stdio.h> 5 #include <string.h> 6 7 int main(int argc, char *argv[]) { 8 int i; 9 for ( i = 1; i < argc - 1; i++) { 10 for (int j = i + 1; j < argc; j++) { 11 if (strcmp(argv[i], argv[j]) > 0) { 12 char* tmp = argv[i]; 13 argv[i] = argv[j]; 14 argv[j] = tmp; 15 } 16 } 17 } 18 for(i = 1; i < argc; ++i) 19 printf("hello, %s\n", argv[i]); 20 21 return 0; 22 }
- 运行测试截图


浙公网安备 33010602011771号