实验5
task1_1.c
1 #include <stdio.h> 2 #define N 4 3 int main() 4 { 5 int x[N] = { 1, 9, 8, 4 }; 6 int i; 7 int* p; 8 // 方式1:通过数组名和下标遍历输出数组元素 9 for (i = 0; i < N; ++i) 10 printf("%d", x[i]); 11 printf("\n"); 12 // 方式2:通过指针变量遍历输出数组元素 (写法1) 13 for (p = x; p < x + N; ++p) 14 printf("%d", *p); 15 printf("\n"); 16 // 方式2:通过指针变量遍历输出数组元素(写法2) 17 p = x; 18 for (i = 0; i < N; ++i) 19 printf("%d", *(p + i)); 20 printf("\n"); 21 // 方式2:通过指针变量遍历输出数组元素(写法3) 22 p = x; 23 for (i = 0; i < N; ++i) 24 printf("%d", p[i]); 25 printf("\n"); 26 return 0; 27 }
task1_2.c
1 #include <stdio.h> 2 int main() 3 { 4 int x[2][4] = { {1, 9, 8, 4}, {2, 0, 4, 9} }; 5 int i, j; 6 int* p; // 指针变量,存放int类型数据的地址 7 int(*q)[4]; // 指针变量,指向包含4个int型元素的一维数组 8 // 使用数组名、下标访问二维数组元素 9 for (i = 0; i < 2; ++i) 10 { 11 for (j = 0; j < 4; ++j) 12 printf("%d", x[i][j]); 13 printf("\n"); 14 } 15 // 使用指针变量p间接访问二维数组元素 16 for (p = &x[0][0], i = 0; p < &x[0][0] + 8; ++p, ++i) 17 { 18 printf("%d", *p); 19 if ((i + 1) % 4 == 0) 20 printf("\n"); 21 } 22 // 使用指针变量q间接访问二维数组元素 23 for (q = x; q < x + 2; ++q) 24 { 25 for (j = 0; j < 4; ++j) 26 printf("%d", *(*q + j)); 27 printf("\n"); 28 } 29 return 0; 30 }
task2_1.c
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 5 int main() 6 { 7 char s1[] = "Learning makes me happy"; 8 char s2[] = "Learning makes me sleepy"; 9 char tmp[N]; 10 printf("sizeof(s1) vs. strlen(s1): \n"); 11 printf("sizeof(s1) = %d\n", sizeof(s1)); 12 printf("strlen(s1) = %d\n", strlen(s1)); 13 printf("\nbefore swap: \n"); 14 printf("s1: %s\n", s1); 15 printf("s2: %s\n", s2); 16 printf("\nswapping...\n"); 17 strcpy(tmp, s1); 18 strcpy(s1, s2); 19 strcpy(s2, tmp); 20 printf("\nafter swap: \n"); 21 printf("s1: %s\n", s1); 22 printf("s2: %s\n", s2); 23 return 0; 24 }
1.大小:24:;字节数;有效长度
2.不能;原因:s1是地址常量,不能赋值
task2_2.c
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 int main() 5 { 6 char *s1 = "Learning makes me happy"; 7 char *s2 = "Learning makes me sleepy"; 8 char *tmp; 9 printf("sizeof(s1) vs. strlen(s1): \n"); 10 printf("sizeof(s1) = %d\n", sizeof(s1)); 11 printf("strlen(s1) = %d\n", strlen(s1)); 12 printf("\nbefore swap: \n"); 13 printf("s1: %s\n", s1); 14 printf("s2: %s\n", s2); 15 printf("\nswapping...\n"); 16 tmp = s1; 17 s1 = s2; 18 s2 = tmp; 19 printf("\nafter swap: \n"); 20 printf("s1: %s\n", s1); 21 printf("s2: %s\n", s2); 22 return 0; 23 }
1.字符串的地址;地址的字节数(8字节即64位);字符串的有效长度
2.可以;task2_1.c中line7指定义字符串数组并初始化,该代码指定义指针变量,并使用字符串地址初始化,进而间接访问
task3.c
1 #include <stdio.h> 2 void str_cpy(char* target, const char* source); 3 void str_cat(char* str1, char* str2); 4 int main() 5 { 6 char s1[80], s2[20] = "1984"; 7 str_cpy(s1, s2); 8 puts(s1); 9 str_cat(s1, " Animal Farm"); 10 puts(s1); 11 return 0; 12 } 13 void str_cpy(char* target, const char* source) 14 { 15 while (*target++ = *source++) 16 ; 17 } 18 void str_cat(char* str1, char* str2) 19 { 20 while (*str1) 21 str1++; 22 while (*str1++ = *str2++) 23 ; 24 }
task4.c
1 #include <stdio.h> 2 #define N 80 3 int func(char*); 4 int main() 5 { 6 char str[80]; 7 while (gets(str) != NULL) 8 { 9 if (func(str)) 10 printf("yes\n"); 11 else 12 printf("no\n"); 13 } 14 return 0; 15 } 16 int func(char* str) 17 { 18 char* begin, * end; 19 begin = end = str; 20 while (*end) 21 end++; 22 end--; 23 while (begin < end) 24 { 25 if (*begin != *end) 26 return 0; 27 else 28 { 29 begin++; 30 end--; 31 } 32 }return 1; 33 }
task5.c
1 #include <stdio.h> 2 #define N 80 3 void func(char*); 4 int main() 5 { 6 char s[N]; 7 while (scanf("%s", s) != EOF) 8 { 9 func(s); 10 puts(s); 11 } 12 return 0; 13 } 14 void func(char* str) 15 { 16 int i; 17 char* p1, * p2, * p; 18 p1 = str; 19 while (*p1 == '*') 20 p1++; 21 p2 = str; 22 while (*p2) 23 p2++; 24 p2--; 25 while (*p2 == '*') 26 p2--; 27 p = str; 28 i = 0; 29 while (p < p1) 30 { 31 str[i] = *p; 32 p++; 33 i++; 34 } 35 while (p <= p2) 36 { 37 if (*p != '*') 38 { 39 str[i] = *p; 40 i++; 41 } 42 p++; 43 } 44 while (*p != '\0') 45 { 46 str[i] = *p; 47 p++; 48 i++; 49 } 50 str[i] = '\0'; 51 }
task6_1.c
1 #include <stdio.h> 2 #include <string.h> 3 void sort(char* name[], int n); 4 int main() 5 { 6 char* course[4] = { "C Program", 7 "C++ Object Oriented Program", 8 "Operating System", 9 "Data Structure and Algorithms" }; 10 int i; 11 sort(course, 4); 12 for (i = 0; i < 4; i++) 13 printf("%s\n", course[i]); 14 return 0; 15 } 16 void sort(char* name[], int n) 17 { 18 int i, j; 19 char* tmp; 20 for (i = 0; i < n - 1; ++i) 21 for (j = 0; j < n - 1 - i; ++j) 22 if (strcmp(name[j], name[j + 1]) > 0) 23 { 24 tmp = name[j]; 25 name[j] = name[j + 1]; 26 name[j + 1] = tmp; 27 } 28 }
task6_2.c
1 #include <stdio.h> 2 #include <string.h> 3 void sort(char *name[], int n); 4 int main() 5 { 6 char *course[4] = {"C Program", 7 "C++ Object Oriented Program", 8 "Operating System", 9 "Data Structure and Algorithms"}; 10 int i; 11 sort(course, 4); 12 for (i = 0; i < 4; i++) 13 printf("%s\n", course[i]); 14 return 0; 15 } 16 void sort(char *name[], int n) 17 { 18 int i, j, k; 19 char *tmp; 20 for (i = 0; i < n - 1; i++) 21 { 22 k = i; 23 for (j = i + 1; j < n; j++) 24 if (strcmp(name[j], name[k]) < 0) 25 k = j; 26 if (k != i) 27 { 28 tmp = name[i]; 29 name[i] = name[k]; 30 name[k] = tmp; 31 } 32 } 33 }
这两种算法都交换的是指针变量的值
task7.c
1 #include <stdio.h> 2 #include <string.h> 3 #define N 5 4 5 int check_id(char* str); // 函数声明 6 7 int main() 8 { 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 return 0; 23 } 24 25 // 函数定义 26 // 功能: 检查指针str指向的身份证号码串形式上是否合法。 27 // 形式合法,返回1,否则,返回0 28 int check_id(char* str) 29 { 30 int i; 31 for (i = 0; str[i] != '\0'; i++); 32 if (i != 18) return 0; 33 if (str[i - 1] < '0' || str[i - 1] > '9' && str[i - 1] != 'X') return 0; 34 for (i = 0; i < 17; i++) 35 { 36 if (str[i] > '9' || str[i] < '0') 37 return 0; 38 39 }int j, year = 0, month = 0, day = 0; 40 41 for (j = 6; j <= 9; j++) 42 year = 10 * year +(str[j]-'0'); 43 for (j = 10; j <= 11; j++) 44 month = 10 * month + (str[j] - '0'); 45 for (j = 12; j <= 13; j++) 46 day = 10 * day + (str[j] - '0'); 47 48 if (year>2023) { return 0; } 49 switch (month) 50 { 51 case 1: 52 case 3: 53 case 5: 54 case 7: 55 case 8: 56 case 10: 57 case 12:if (day > 31) return 0; break; 58 case 4: 59 case 6: 60 case 9: 61 case 11:if (day > 30) return 0; break; 62 }if (month == 2) 63 64 { 65 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) 66 67 { 68 if (day > 29) return 0; 69 } 70 else 71 { 72 if (day > 28) return 0; 73 } 74 75 } 76 77 return 1; 78 79 } 80
task8.c
1 #include <stdio.h> 2 #define N 80 3 void encoder(char* s); // 函数声明 4 void decoder(char* s); // 函数声明 5 6 int main() 7 { 8 char words[N]; 9 10 printf("输入英文文本: "); 11 while (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 printf("\n"); 21 } 22 return 0; 23 } 24 25 /*函数定义 26 功能:对s指向的字符串进行编码处理 27 编码规则: 28 对于a~z或A~Z之间的字母字符,用其后的字符替换; 其中,z用a替换,Z用A替换 29 其它非字母字符,保持不变 30 */ 31 void encoder(char* s) 32 { 33 int i; 34 35 for (i = 0; s[i] != '\0'; i++) 36 { 37 if (s[i] < 'A' || s[i]>'Z' && s[i] < 'a' || s[i]>'z') 38 s[i] = s[i]; 39 else if (s[i] != 'Z' && s[i] != 'z') 40 s[i] = s[i] + 1; 41 else if (s[i] == 'z') 42 s[i] = 'a'; 43 else 44 s[i] = 'A'; 45 } 46 47 } 48 49 /*函数定义 50 功能:对s指向的字符串进行解码处理 51 解码规则: 52 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换 53 其它非字母字符,保持不变 54 */ 55 void decoder(char* s) 56 { 57 int i; 58 for (i = 0; s[i] != '\0'; i++) 59 { 60 if (s[i] < 'A' || s[i]>'Z' && s[i] < 'a' || s[i]>'z') 61 s[i] = s[i]; 62 else if (s[i] != 'A' && s[i] != 'a') 63 s[i] = s[i] - 1; 64 else if (s[i] == 'A') 65 s[i] = 'Z'; 66 else 67 s[i] = 'z'; 68 } 69 }