实验五
实验任务1:
1.源代码:
1 #include <stdio.h> 2 #define N 5 3 void input(int x[], int n); 4 void output(int x[], int n); 5 void find_min_max(int x[], int n, int* pmin, int* pmax); 6 int main() { 7 int a[N]; 8 int min, max; 9 printf("录入%d个数据:\n", N); 10 input(a, N); 11 printf("数据是: \n"); 12 output(a, N); 13 printf("数据处理...\n"); 14 find_min_max(a, N, &min, &max); 15 printf("输出结果:\n"); 16 printf("min = %d, max = %d\n", min, max); 17 return 0; 18 } 19 void input(int x[], int n) { 20 int i; 21 for (i = 0; i < n; ++i) 22 scanf_s("%d", &x[i]); 23 } 24 void output(int x[], int n) { 25 int i; 26 27 for (i = 0; i < n; ++i) 28 printf("%d ", x[i]); 29 printf("\n"); 30 } 31 void find_min_max(int x[], int n, int* pmin, int* pmax) { 32 int i; 33 34 *pmin = *pmax = x[0]; 35 for (i = 0; i < n; ++i) 36 if (x[i] < *pmin) 37 *pmin = x[i]; 38 else if (x[i] > *pmax) 39 *pmax = x[i]; 40 }
2.运行效果截图:

3.问题回答:
(1)功能是找到这组数据中的最大数和最小数
(2)指向数组中第一个数地址
实验任务1.1:
1.源代码:
1 #include <stdio.h> 2 #define N 5 3 void input(int x[], int n); 4 void output(int x[], int n); 5 int* find_max(int x[], int n); 6 int main() { 7 int a[N]; 8 int* pmax; 9 printf("录入%d个数据:\n", N); 10 input(a, N); 11 printf("数据是: \n"); 12 output(a, N); 13 printf("数据处理...\n"); 14 pmax = find_max(a, N); 15 printf("输出结果:\n"); 16 printf("max = %d\n", *pmax); 17 return 0; 18 } 19 void input(int x[], int n) { 20 int i; 21 for (i = 0; i < n; ++i) 22 scanf_s("%d", &x[i]); 23 } 24 void output(int x[], int n) { 25 int i; 26 27 for (i = 0; i < n; ++i) 28 printf("%d ", x[i]); 29 printf("\n"); 30 } 31 int* find_max(int x[], int n) { 32 int max_index = 0; 33 int i; 34 for (i = 0; i < n; ++i) 35 if (x[i] > x[max_index]) 36 max_index = i; 37 38 return &x[max_index]; 39 }
2.运行效果截图:

3.问题回答:
(1)功能是找到录入数据中的最大值,返回找到最大数的地址
(2)可以实现
实验任务2.1
1.源代码:
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 int main() { 5 char s1[N] = "Learning makes me happy"; 6 char s2[N] = "Learning makes me sleepy"; 7 char tmp[N]; 8 printf("sizeof(s1) vs. strlen(s1): \n"); 9 printf("sizeof(s1) = %d\n", sizeof(s1)); 10 printf("strlen(s1) = %d\n", strlen(s1)); 11 printf("\nbefore swap: \n"); 12 printf("s1: %s\n", s1); 13 printf("s2: %s\n", s2); 14 printf("\nswapping...\n"); 15 strcpy(tmp, s1); 16 strcpy(s1, s2); 17 strcpy(s2, tmp); 18 printf("\nafter swap: \n"); 19 printf("s1: %s\n", s1); 20 printf("s2: %s\n", s2); 21 return 0; 22 }
2.运行效果截图:

3.问题回答:
(1)80字节;数组总字节数(含\0);有效字符长度(不含\0)
(2)不能;因为数组名是地址,不能被赋值
(3)交换
实验任务2.2
1.源代码
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 int main() { 5 const char* s1 = "Learning makes me happy"; 6 const char* s2 = "Learning makes me sleepy"; 7 const char* tmp; 8 printf("sizeof(s1) vs. strlen(s1): \n"); 9 printf("sizeof(s1) = %d\n", sizeof(s1)); 10 printf("strlen(s1) = %d\n", strlen(s1)); 11 printf("\nbefore swap: \n"); 12 printf("s1: %s\n", s1); 13 printf("s2: %s\n", s2); 14 printf("\nswapping...\n"); 15 tmp = s1; 16 s1 = s2; 17 s2 = tmp; 18 printf("\nafter swap: \n"); 19 printf("s1: %s\n", s1); 20 printf("s2: %s\n", s2); 21 return 0; 22 }
2.运行效果截图:

3.问题回答:
(1)存放字符串常量地址;s1指针大小;s1有效字符数
(2)可以;2.1是将字符串赋值给数组;2.2是将字符串赋值给指针地址
(3)交换了指针s1 s2 指向;没有交换
实验任务3:
1.源代码:
1 #include <stdio.h> 2 int main() { 3 int x[2][4] = { {1, 9, 8, 4}, {2, 0, 4, 9} }; 4 int i, j; 5 int* ptr1; 6 int(*ptr2)[4]; 7 printf("输出1: 使用数组名、下标直接访问二维数组元素\n"); 8 for (i = 0; i < 2; ++i) { 9 for (j = 0; j < 4; ++j) 10 printf("%d ", x[i][j]); 11 printf("\n"); 12 } 13 printf("\n输出2: 使用指针变量ptr1(指向元素)访问\n"); 14 for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) { 15 printf("%d ", *ptr1); 16 if ((i + 1) % 4 == 0) 17 printf("\n"); 18 } 19 20 printf("\n输出3: 使用指针变量ptr2(指向一维数组)访问\n"); 21 for (ptr2 = x; ptr2 < x + 2; ++ptr2) { 22 for (j = 0; j < 4; ++j) 23 printf("%d ", *(*ptr2 + j)); 24 printf("\n"); 25 } 26 return 0; 27 }
2.运行效果截图:

3. 问题回答:
(1)int (*ptr)[4]; 中,标识符ptr表示的语义是指向包含四个int元素的一维数组
(2)int *ptr[4];中,标识符ptr表示的语义是存放4个int的指针数组
实验任务4:
1.源代码:
1 #include <stdio.h> 2 #define N 80 3 void replace(char *str, char old_char, char new_char); // 函数声明 4 int main() { 5 char text[N] = "Programming is difficult or not, it is a question."; 6 printf("原始文本: \n"); 7 printf("%s\n", text); 8 replace(text, 'i', '*'); // 函数调用 注意字符形参写法,单引号不能少 9 printf("处理后文本: \n"); 10 printf("%s\n", text); 11 return 0; 12 } 13 // 函数定义 14 void replace(char *str, char old_char, char new_char) { 15 int i; 16 while(*str) { 17 if(*str == old_char) 18 *str = new_char; 19 str++; 20 } 21 }
2.运行效果截图:

3.问题回答:
(1)作用是将文本中的i换成*
(2)可以
实验任务5:
1.源代码:
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_s(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 { 19 char* p = str; 20 while (*p != '\0') 21 { 22 if (*p == x) 23 { 24 *p = '\0'; 25 break; 26 } 27 p++; 28 } 29 return str; 30 }
2.运行效果截图:

3.问题回答:
作用是吸收输入字符后遗留的换行符,避免其干扰下一次输入,删除后会导致程序自动读取换行符、跳过字符输入步骤
实验任务6:
1.源代码:
1 #include <stdio.h> 2 #include <string.h> 3 #define N 5 4 int check_id(const char* str); 5 int main() 6 { 7 const 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(const char* str) 21 { 22 int len = strlen(str); 23 if (len != 18) 24 return 0; 25 for (int i = 0; i < 17; i++) 26 { 27 if (str[i] < '0' || str[i] > '9') 28 return 0; 29 } 30 if ((str[17] >= '0' && str[17] <= '9') || str[17] == 'X') 31 return 1; 32 return 0; 33 }
2.运行效果截图:

实验任务7:
1.源代码:
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_s(words); 10 printf("输入n: "); 11 scanf_s("%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 void encoder(char* str, int n) { 21 while (*str != '\0') 22 { 23 if (*str >= 'a' && *str <= 'z') 24 { 25 *str = (*str - 'a' + n) % 26 + 'a'; 26 } 27 else if (*str >= 'A' && *str <= 'Z') 28 { 29 *str = (*str - 'A' + n) % 26 + 'A'; 30 } 31 str++; 32 } 33 } 34 35 void decoder(char* str, int n) { 36 while (*str != '\0') 37 { 38 if (*str >= 'a' && *str <= 'z') 39 { 40 *str = (*str - 'a' - n + 26) % 26 + 'a'; 41 } 42 else if (*str >= 'A' && *str <= 'Z') 43 { 44 *str = (*str - 'A' - n + 26) % 26 + 'A'; 45 } 46 str++; 47 } 48 }
2.运行效果截图:

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


浙公网安备 33010602011771号