实验五
实验任务一
task1-1
源码
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 for (i = 0; i < N; ++i) 9 printf("%d", x[i]); 10 printf("\n"); 11 12 for (p = x; p < x + N; ++p) 13 printf("%d", *p); 14 printf("\n"); 15 16 p = x; 17 for (i = 0; i < N; ++i) 18 printf("%d", *(p + i)); 19 printf("\n"); 20 21 p = x; 22 for (i = 0; i < N; ++i) 23 printf("%d", p[i]); 24 printf("\n"); 25 return 0; 26 }
实验结论
task1-2
源码
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; 7 int(*q)[4]; 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 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 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 }
实验结论
实验任务二
源码
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[N]; 9 printf("sizeof(s1) vs. strlen(s1): \n"); 10 printf("sizeof(s1) = %d\n", (int)sizeof(s1)); 11 printf("strlen(s1) = %d\n", (int)strlen(s1)); 12 printf("\nbefore swap: \n"); 13 printf("s1: %s\n", s1); 14 printf("s2: %s\n", s2); 15 printf("\nswapping...\n"); 16 strcpy_s(tmp,N, s1); 17 strcpy_s(s1,N, s2); 18 strcpy_s(s2,N, tmp); 19 printf("\nafter swap: \n"); 20 printf("s1: %s\n", s1); 21 printf("s2: %s\n", s2); 22 return 0; 23 }
实验结论
实验反思
1、s1大小为24,但是长度为23
sizeof(s1)表示数组所占的空间大小(因为“\0”也占了空间)
strlen(s1)表示数组的长度(不加上“\0”)
2、不能。因为结构不完整,即没有在char那一行对数组进行定义,计算机不知道s1的长度故会报错。
3、是
task2-2
源码
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、s1存放的是"Learning makes me happy"这组字符串
sizeof(s1)指的是s1这个地址,地址是8位
strlen(s1)统计的是数组元素的个数(不包括结束符)
2、能。数组需要一开始就定义长度。而指针只需要分配一个地址
3、有交换
实验任务三
源码
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 }
实验结论
实验任务四
源码
1 int main() 2 { 3 char str[80]; 4 while (gets(str) != NULL) 5 { 6 if (func(str)) 7 printf("yes\n"); 8 else 9 printf("no\n"); 10 } 11 return 0; 12 } 13 int func(char* str) 14 { 15 char* begin, * end; 16 begin = end = str; 17 while (*end) 18 end++; 19 end--; 20 while (begin < end) 21 { 22 if (*begin != *end) 23 return 0; 24 else 25 { 26 begin++; 27 end--; 28 } 29 } 30 return 1; 31 }
实验结论
实验任务五
源码
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", s,N) != 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
源码
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
源码
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 }
实验结论
实验任务七
源码
#include <stdio.h> #include <string.h> #define N 5 int check_id(char* str); // 函数声明 int main() { char* pid[N] = { "31010120000721656X", "330106199609203301", "53010220051126571", "510104199211197977", "53010220051126133Y" }; int i; for (i = 0; i < N; ++i) if (check_id(pid[i])) // 函数调用 printf("%s\tTrue\n", pid[i]); else printf("%s\tFalse\n", pid[i]); return 0; } // 函数定义 // 功能: 检查指针str指向的身份证号码串形式上是否合法。 // 形式合法,返回1,否则,返回0 int check_id(char* str) { // 补足函数实现 // ... int flag = 0, i, j, k; char x[11] = "1234567890X"; for (i = 1; *str != '\0'; i++) { for (k = 0; k < 11; k++) if (*str == x[k]) { flag = 1; break; } else flag = 0; *str++; } if (i == 19) return flag; else return 0; }
实验结论
实验任务八
源码
#include <stdio.h> #define N 80 void encoder(char* s); // 函数声明 void decoder(char* s); // 函数声明 int main() { char words[N]; printf("输入英文文本: "); gets(words); printf("编码后的英文文本: "); encoder(words); // 函数调用 printf("%s\n", words); printf("对编码后的英文文本解码: "); decoder(words); // 函数调用 printf("%s\n", words); return 0; } /*函数定义 功能:对s指向的字符串进行编码处理 编码规则: 对于a~z或A~Z之间的字母字符,用其后的字符替换; 其中,z用a替换,Z用A替换 其它非字母字符,保持不变 */ void encoder(char* s) { // 补足函数实现 // ××× for (; *s != '\0'; s++) if (*s >= 'a' && *s <= 'z' || *s >= 'A' && *s <= 'Z') (*s)++, * s = (*s == 'z' + 1 || *s == 'Z' + 1) ? *s - 26 : *s; } /*函数定义 功能:对s指向的字符串进行解码处理 解码规则: 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换 其它非字母字符,保持不变 */ void decoder(char* s) { // 补足函数实现 // ××× for (; *s != '\0'; s++) if (*s >= 'a' && *s <= 'z' || *s >= 'A' && *s <= 'Z') (*s)--, * s = (*s == 'a' - 1 || *s == 'A' - 1) ? *s + 26 : *s; }
实验结论