task1.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 { 11 int a[N]; 12 int min, max; 13 14 printf("录入%d个数据:\n", N); 15 input(a, N); 16 17 printf("数据是: \n"); 18 output(a, N); 19 20 printf("数据处理...\n"); 21 find_min_max(a, N, &min, &max); 22 23 printf("输出结果:\n"); 24 printf("min = %d, max = %d\n", min, max); 25 26 system("pause"); 27 return 0; 28 } 29 void input(int x[], int n) { 30 int i; 31 32 for(i = 0; i < n; ++i) 33 scanf("%d", &x[i]); 34 } 35 36 void output(int x[], int n) { 37 int i; 38 39 for(i = 0; i < n; ++i) 40 printf("%d ", x[i]); 41 printf("\n"); 42 } 43 44 void find_min_max(int x[], int n, int *pmin, int *pmax) { 45 int i; 46 47 *pmin = *pmax = x[0]; 48 49 for(i = 1; i < n; ++i) 50 if(x[i] < *pmin) 51 *pmin = x[i]; 52 else if(x[i] > *pmax) 53 *pmax = x[i]; 54 }
2.运行结果

找出输入数据中的最大和最小值,并存到形参中
指向x[0]的地址
task1.2
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 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 return 0; 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 int *find_max(int x[], int n) { 44 int max_index = 0; 45 int i; 46 47 for(i = 1; i < n; ++i) 48 if(x[i] > x[max_index]) 49 max_index = i; 50 51 return &x[max_index]; 52 }
2.运行结果

取出输入数据的最大值
可行
task2.1
1.源代码
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[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 return 0; 30 }
2.运行结果

24
不能,数组定义必须有常量
交换
task2.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 = "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 system("pause"); 28 return 0; 29 }
2.运行结果

存放的是第一个字符串
不可以,s1是野指针
交换的是地址,在储存单元中没有交换
task3
1.源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include <string.h> 4 int main() { 5 int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}}; 6 int i, j; 7 int *ptr1; // 指针变量,存放int类型数据的地址 8 int(*ptr2)[4]; // 指针变量,指向包含4个int元素的一维数组 9 10 printf("输出1: 使用数组名、下标直接访问二维数组元素\n"); 11 for (i = 0; i < 2; ++i) { 12 for (j = 0; j < 4; ++j) 13 printf("%d ", x[i][j]); 14 printf("\n"); 15 } 16 17 printf("\n输出2: 使用指向元素的指针变量p间接访问二维数组元素\n"); 18 for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) { 19 printf("%d ", *ptr1); 20 21 if ((i + 1) % 4 == 0) 22 printf("\n"); 23 } 24 25 printf("\n输出3: 使用指向一维数组的指针变量q间接访问二维数组元素\n"); 26 for (ptr2 = x; ptr2 < x + 2; ++ptr2) { 27 for (j = 0; j < 4; ++j) 28 printf("%d ", *(*ptr2 + j)); 29 printf("\n"); 30 } 31 32 system("pause"); 33 return 0; 34 }
2.运行结果

task.4
1.源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 80 4 5 void replace(char *str, char old_char, char new_char); // 函数声明 6 7 int main() { 8 char text[N] = "c programming is difficult or not, it is a question."; 9 10 printf("原始文本: \n"); 11 printf("%s\n", text); 12 13 replace(text, 'i', '*'); // 函数调用 注意字符形参写法,单引号不能少 14 15 printf("处理后文本: \n"); 16 printf("%s\n", text); 17 18 system("pause"); 19 return 0; 20 } 21 22 void replace(char *str, char old_char, char new_char) { 23 int i; 24 25 while(*str) { 26 if(*str == old_char) 27 *str = new_char; 28 str++; 29 } 30 }
2.运行结果

替换指定字母
可以
task4.2
1.源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 80 4 5 void str_trunc(char *str, char x); 6 7 int main() { 8 char str[N]; 9 char ch; 10 11 printf("输入字符串: "); 12 gets(str); 13 14 printf("输入一个字符: "); 15 ch = getchar(); 16 17 printf("截断处理...\n"); 18 str_trunc(str, ch); 19 20 printf("截断处理后的字符串: %s\n", str); 21 22 23 system("pause"); 24 return 0; 25 } 26 27 void str_trunc(char *str, char x) 28 { 29 while(*str) 30 { 31 if(*str == x) 32 *str='\0';// blank1 33 34 str++;// blank2 35 } 36 }
2.运行结果

task5.1
1.源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include <string.h> 4 void sort(char *name[], int n); 5 6 int main() { 7 char *course[4] = {"C Program", 8 "C++ Object Oriented Program", 9 "Operating System", 10 "Data Structure and Algorithms"}; 11 int i; 12 13 sort(course, 4); 14 15 for (i = 0; i < 4; i++) 16 printf("%s\n", course[i]); 17 18 19 system("pause"); 20 return 0; 21 } 22 23 void sort(char *name[], int n) { 24 int i, j; 25 char *tmp; 26 27 for (i = 0; i < n - 1; ++i) 28 for (j = 0; j < n - 1 - i; ++j) 29 if (strcmp(name[j], name[j + 1]) > 0) { 30 tmp = name[j]; 31 name[j] = name[j + 1]; 32 name[j + 1] = tmp; 33 } 34 }
2.运行结果

task5.2
1.源代码
1 #include <stdio.h> 2 #include <string.h> 3 void sort(char *name[], int n); 4 5 int main() { 6 char *course[4] = {"C Program", 7 "C++ Object Oriented Program", 8 "Operating System", 9 "Data Structure and Algorithms"}; 10 int i; 11 12 sort(course, 4); 13 for (i = 0; i < 4; i++) 14 printf("%s\n", course[i]); 15 16 return 0; 17 } 18 19 void sort(char *name[], int n) { 20 int i, j, k; 21 char *tmp; 22 23 for (i = 0; i < n - 1; i++) { 24 k = i; 25 for (j = i + 1; j < n; j++) 26 if (strcmp(name[j], name[k]) < 0) 27 k = j; 28 29 if (k != i) { 30 tmp = name[i]; 31 name[i] = name[k]; 32 name[k] = tmp; 33 } 34 } 35 }
2.运行结果

交换的是指针变量的值
task6
1.源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include <string.h> 4 #define N 5 5 6 int check_id(char *str); // 函数声明 7 8 int main() { 9 char *pid[N] = {"31010120000721656X", 10 "330106199609203301", 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 23 system("pause"); 24 return 0; 25 } 26 27 // 函数定义 28 // 功能: 检查指针str指向的身份证号码串形式上是否合法。 29 // 形式合法,返回1,否则,返回0 30 int check_id(char *str) 31 { 32 int i=strlen(str); 33 int flag=1; 34 if(i!=18) 35 return 0; 36 else 37 { 38 while(*str!='\0') 39 { 40 if((*str<'0'||*str>'9')&&*str!='X') 41 {flag=0;break;} 42 str++; 43 } 44 return flag; 45 } 46 }
2.运行结果

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