实验五
1.1c
1.find_min_max的作用是查找数组a[N]中的最大值和最小值

2.pmin指向数组a中的最小值min,pmax指向数组a中的最大值max
1.2c
1.查找数组a[N]中的最大值,返回数组a[N]最大的地址
2.可以
2.1c
1.80,sizeof(s1)计算s1数组的大小,strlen(s1)统计s1数组的字符个数

2.可以
3.发生交换
2.2c
1.存放字符串 Learning makes me happy的地址,计算s1本身的字节数,统计s1指向的地址从第一个字符开始到\0的字符个数
2.能
2.1的是字符数组,不能改变指向的地址,2.2的是指针变量

3.交换
3.
1.int (*ptr)中,ptr是包含四个int元素的数组x[2]
int *ptr[4]中,ptr是包含四个指向int元素的指针
4.
1.功能是把数组的字符串中的i加密为*
5
6
7
8

2.可以
5.
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 // 函数str_trunc定义 18 // 功能: 对字符串作截断处理,把指定字符自第一次出现及其后的字符全部删除, 并返回字符串地址 19 // 待补足... 20 // xxx 21 char* str_trunc(char* str, char x) { 22 int i=0; 23 while (str[i] != '\0') { 24 if (str[i] == x) { 25 str[i] = '\0'; 26 break; 27 } 28 i++; 29 } 30 return str; 31 }

getchar()的作用是吸收输入缓冲区中残留的回车符(换行符),确保下一次循环读取输入时不会出错
6.
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 21 int check_id(char* str) { 22 int i = 0; 23 if (strlen(str) != 18) 24 return 0; 25 26 for (i; i <17; i++) 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 0; 32 33 return 1; 34 }

7.
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 int i = 0; 27 for (i; str[i] != '\0'; i++) { 28 if (str[i] >= 'a' && str[i] <= 'z') { 29 str[i] = 'a' + (str[i] - 'a' + n) % 26; 30 } 31 else if (str[i] >= 'A' && str[i] <= 'Z') { 32 str[i] = 'A' + (str[i] - 'A' + n) % 26; 33 } 34 } 35 36 37 } 38 /*函数定义 39 功能:对str指向的字符串进行解码处理 40 解码规则: 41 对于a~z或A~Z之间的字母字符,用其前面第n个字符替换; 其它非字母字符,保持不变 42 */ 43 void decoder(char* str, int n) { 44 int i = 0; 45 for (i; str[i] != '\0'; i++) { 46 if (str[i] >= 'a' && str[i] <= 'z') { 47 str[i] = 'a' + (str[i] - 'a' - n+26) % 26; 48 } 49 else if (str[i] >= 'A' && str[i] <= 'Z') { 50 str[i] = 'A' + (str[i] - 'A' -n+26) % 26; 51 } 52 } 53 54 }

8.
1 #include <stdio.h> 2 #include <string.h> 3 4 void name(char* arr[], int n) { 5 int i, j; 6 for (i = 0; i < n - 1; i++) { 7 for (j = 0; j < n - 1 - i; j++) { 8 if (strcmp(arr[j], arr[j + 1]) > 0) { 9 // 交换指针 10 char* temp = arr[j]; 11 arr[j] = arr[j + 1]; 12 arr[j + 1] = temp; 13 } 14 } 15 } 16 } 17 18 int main(int argc, char* argv[]) { 19 if (argc < 2) { 20 printf("Usage: %s name1 name2 ...\n", argv[0]); 21 return 0; 22 } 23 24 char* names[argc - 1]; 25 for (int i = 1; i < argc; i++) { 26 names[i - 1] = argv[i]; 27 } 28 name(names, argc - 1); 29 for (int i = 0; i < argc - 1; i++) { 30 printf("hello, %s\n", names[i]); 31 } 32 33 return 0; 34 }

浙公网安备 33010602011771号