实验5 c语言指针应用编程
1 #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", 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_s("%d", &x[i]); 32 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 void find_min_max(int x[], int n, int* pmin, int* pmax) { 44 int i; 45 46 *pmin = *pmax = x[0]; 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:功能是找到一组数据中的最大最小值
问题2:都指向数组中的第一个元素的地址
task1_2.c
1 #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_s("%d", &x[i]); 32 printf("\n"); 33 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 int *find_max(int x[], int n) { 45 int max_index = 0; 46 int i; 47 48 for (i = 0;i < n;++i) 49 if (x[i] > x[max_index]) 50 max_index = i; 51 52 return &x[max_index]; 53 }
问题1:功能是找到一组数据中的最大值,返回的是最大值的地址
问题2:可以
task2_1.c
1 #include<stdio.h> 2 #include<string.h> 3 #define N 80 4 #define _CRT_SECURE_NO_WARNINGS 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_s(tmp, s1); 21 strcpy_s(s1, s2); 22 strcpy_s(s2, tmp); 23 24 printf("\nafter swap:\n"); 25 printf("s1:%s\n", s1); 26 printf("s2:%s\n", s2); 27 28 return 0; 29 }
问题1:s1的大小是80,sizeof计算的是定义数组时的长度,strlen统计的是数组的实际长度包括空格
问题2:s1是数组名,代表的是数组第一个元素的地址,不能将字符串赋值给它
问题3:没有
task2_2.c
1 #include<stdio.h> 2 #include<string.h> 3 #define N 80 4 #define _CRT_SECURE_NO_WARNINGS 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 29 return 0; 30 }
问题1:s1中存放的是字符串,sizeof计算的是指针变量本身的长度,strlen计算的是字符串长度包括空格
问题2:可以,2.1中数组名是常量,不可以赋值,而2.2指针是变量,表示将字符串的起始地址赋值给指针变量
问题3:交换的是两组字符串的地址,两者在内存中没有交换
task3:
1 #include<stdio.h> 2 3 int main() { 4 int x[2][4] = { {1,9,8,4},{2,0,4,9} }; 5 int i, j; 6 int* ptr1; 7 int(*ptr2)[4]; 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 printf("\n输出2:使用指针变量ptr1(指向元素)间接访问\n"); 16 for (ptr1 = &x[0][0], i = 0;ptr1 < &x[0][0] + 8;++ptr1, ++i) { 17 printf("%d", *ptr1); 18 19 if ((i + 1) % 4 == 0) 20 printf("\n"); 21 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 32 return 0; 33 }
问题1:int (*ptr)[4]代表的是一个数组名,int*ptr[4]代表的是指针变量
task4:
1 #include<stdio.h> 2 #define N 80 3 4 void replace(char* str, char old_char, char new_char); 5 6 int main() { 7 char text[N] = "Programming is difficult or not,it is a question."; 8 9 printf("原始文本:\n"); 10 printf("%s\n", text); 11 12 replace(text, 'i', '*'); 13 14 printf("处理后文本:\n"); 15 printf("%s\n", text); 16 17 return 0; 18 } 19 20 void replace(char* str, char old_char, char new_char) { 21 int i; 22 23 while (*str) { 24 if (*str == old_char) 25 *str = new_char; 26 str++; 27 } 28 }
问题1:replace的功能是将字符串中old_char的部分改为new_char
问题2:可以
task5:
1 #include<stdio.h> 2 #define N 80 3 4 char* str_trunc(char* str, char x); 5 6 int main() { 7 char str[N]; 8 char ch; 9 10 while (printf("输入一个字符串:"), gets(str) != NULL) { 11 printf("输入一个字符:"); 12 ch = getchar(); 13 14 printf("截断处理...\n"); 15 str_trunc(str, ch); 16 17 printf("截断处理后的字符串:%s\n\n", str); 18 getchar(); 19 } 20 21 return 0; 22 23 } 24 25 char* str_trunc(char* str, char x) { 26 while (*str) { 27 if (*str == x) 28 *(str + 1) = '\0'; 29 str++; 30 } 31 return str; 32 }
问题:第二个循环时回车被当作有效字符送给了字符串,作用是避免回车被当作有效字符
task6:
1 #include<stdio.h> 2 #include<string.h> 3 #define N 5 4 5 int check_id(char* str); 6 7 int main() { 8 char* pid[N] = { "31010120000721656x", 9 "3301061996x0203310", 10 "53010220051126571", 11 "510104199211197977", 12 "53010220051126133Y" }; 13 int i; 14 for (i = 0;i < N;++i) 15 if (check_id(pid[i])) 16 printf("%s\tTrue\n", pid[i]); 17 else 18 printf("%s\tFalse\n", pid[i]); 19 20 21 return 0; 22 23 } 24 25 int check_id(char* str) { 26 int i,j; 27 i = strlen(str); 28 if (i != 18) 29 return 0; 30 for (j = 0;j < 17;j++) { 31 if (str[j] < '0' || str[j]>'9') 32 return 0; 33 } 34 if ((str[17] > '0' && str[17] < '9') || str[17] == 'x') 35 return 1; 36 else 37 return 0; 38 }
task7:
1 #include<stdio.h> 2 #define N 80 3 void encoder(char* str, int n); 4 void decoder(char* str, int n); 5 6 int main() { 7 char words[N]; 8 int n; 9 10 printf("输入英文文本:"); 11 gets(words); 12 printf("输入n:"); 13 scanf_s("%d", &n); 14 15 printf("编码后的英文文本:"); 16 encoder(words, n); 17 printf("%s\n", words); 18 19 printf("对编码后的英文文本解码:"); 20 decoder(words, n); 21 printf("%s\n", words); 22 23 return 0; 24 } 25 26 27 void encoder(char* str, int n) { 28 int i; 29 for (i = 0;str[i] != '\0';++i) { 30 if (str[i] >= 'A' && str[i] <= 'Z') { 31 if (str[i] + n >= 'A' && str[i] + n <= 'Z') 32 str[i] = str[i] + n; 33 else 34 str[i] = str[i] + n - 26; 35 } 36 37 else if (str[i] >= 'a' && str[i] <= 'z') { 38 if (str[i] + n >= 'a' && str[i] + n <= 'z') 39 str[i] = str[i] + n; 40 else 41 str[i] = str[i] + n - 26; 42 } 43 else 44 str[i] = str[i]; 45 } 46 47 } 48 49 void decoder(char* str, int n) { 50 int i; 51 for (i = 0;str[i] != '\0';++i) { 52 if (str[i] >= 'A' && str[i] <= 'Z') { 53 if (str[i] - n >= 'A' && str[i] - n <= 'Z') 54 str[i] = str[i] - n; 55 else 56 str[i] = str[i] + 26 - n; 57 } 58 59 else if (str[i] >= 'a' && str[i] <= 'z') { 60 if (str[i] - n >= 'a' && str[i] - n <= 'z') 61 str[i] = str[i] - n; 62 else 63 str[i] = str[i] + 26 - n; 64 } 65 else 66 str[i] = str[i]; 67 }
task8
1 #include<string.h> 2 #include<stdio.h> 3 4 int main(int argc, char *argv[]) { 5 int i,j,k; 6 char* tmp; 7 8 for (i = 0;i < argc - 1;++i) { 9 k = i; 10 for (j = i + 1;j < argc;j++) 11 if (strcmp(argv[j], argv[k]) < 0) 12 k = j; 13 if (k != i) { 14 tmp = argv[i]; 15 argv[i] = argv[k]; 16 argv[k] = tmp; 17 } 18 19 } 20 21 for(i = 1; i < argc; ++i) 22 printf("hello, %s\n", argv[i]); 23 24 return 0; 25 }