实验5
1.1
#include <stdio.h> #define N 4 int main() { int x[N] = {1, 9, 8, 4}; int i; int *p; // 方式1:通过数组名和下标遍历输出数组元素 for (i = 0; i < N; ++i) printf("%d", x[i]); printf("\n"); // 方式2:通过指针变量遍历输出数组元素 (写法1) for (p = x; p < x + N; ++p) printf("%d", *p); printf("\n"); // 方式2:通过指针变量遍历输出数组元素(写法2) p = x; for (i = 0; i < N; ++i) printf("%d", *(p + i)); printf("\n"); // 方式2:通过指针变量遍历输出数组元素(写法3) p = x; for (i = 0; i < N; ++i) printf("%d", p[i]); printf("\n"); getchar(); return 0; }
1.2
#include <stdio.h> int main() { int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}}; int i, j; int *p; // 指针变量,存放int类型数据的地址 int(*q)[4]; // 指针变量,指向包含4个int型元素的一维数组 // 使用数组名、下标访问二维数组元素 for (i = 0; i < 2; ++i) { for (j = 0; j < 4; ++j) printf("%d", x[i][j]); printf("\n"); } // 使用指针变量p间接访问二维数组元素 for (p = &x[0][0], i = 0; p < &x[0][0] + 8; ++p, ++i) { printf("%d", *p); if ((i + 1) % 4 == 0) printf("\n"); } // 使用指针变量q间接访问二维数组元素 for (q = x; q < x + 2; ++q) { for (j = 0; j < 4; ++j) printf("%d", *(*q + j)); printf("\n"); } getchar(); return 0; }
2.1
#include <stdio.h> #include <string.h> #define N 80 int main() { char s1[]= "Learning makes me happy"; char s2[] = "Learning makes me sleepy"; char tmp[N]; printf("sizeof(s1) vs. strlen(s1): \n"); printf("sizeof(s1) = %d\n", sizeof(s1)); printf("strlen(s1) = %d\n", strlen(s1)); printf("\nbefore swap: \n"); printf("s1: %s\n", s1); printf("s2: %s\n", s2); printf("\nswapping...\n"); strcpy(tmp, s1); strcpy(s1, s2); strcpy(s2, tmp); printf("\nafter swap: \n"); printf("s1: %s\n", s1); printf("s2: %s\n", s2); return 0; } //Q1:数组s1的大小是24 //sizeof(s1)计算的是数组s1的大小【包括结束标记\0】 //strlen(s1)统计的是数组s1的字符串大小 //Q2:不能,因为s1指的是数组的起始地址,无法赋值进行数组初始化 //Q3:line20-22执行后,字符数组s1和s2中的内容交换了
2.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 8 char *s2 = "Learning makes me sleepy"; 9 char *tmp; 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 tmp = s1; 18 s1 = s2; 19 s2 = tmp; 20 printf("\nafter swap: \n"); 21 printf("s1: %s\n", s1); 22 printf("s2: %s\n", s2); 23 getchar(); 24 return 0; 25 } 26 /* 27 Q1 指针变量s1中存放的是字符串常量 28 sizeof(s1)计算的是s1起始地址的大小 29 strlen(s1)统计的是字符串中字符的数量 30 Q2 能,2.1中意思是对数组s1的起始地址赋值为字符串,本处意思是对地址进行操作 31 Q3 line20-line22,交换的是什么? 32 字符串常量"Learning makes me happy"和字符串常 33 量"Learning makes me sleepy"在内存存储单元中没有交换。是地址交换了
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 system("pause"); 12 return 0; 13 } 14 void str_cpy(char *target, const char *source) 15 { 16 while (*target++ = *source++) 17 ; 18 } 19 void str_cat(char *str1, char *str2) 20 { 21 while (*str1) 22 str1++; 23 while (*str1++ = *str2++) 24 ; 25 } 26 //对字符串进行复制和连接
4
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++;//找出结束标记\0 22 end--;//得到最后一个字符 23 while (begin < end) 24 { 25 if (*begin != *end)//开头和结尾的字符不相同 26 return 0; 27 else 28 { 29 begin++; 30 end--; 31 }//向中间取一位再比较 32 } 33 return 1; 34 }
5
1 include <stdio.h> 2 #define N 80 3 4 void func(char *); 5 6 int main() 7 { 8 char s[N]; 9 10 while (scanf("%s", s) != EOF) 11 { 12 func(s); 13 puts(s); 14 } 15 return 0; 16 } 17 void func(char *str) 18 { 19 int i; 20 char *p1, *p2, *p; 21 p1 = str;//位于起始位置 22 while (*p1 == '*') 23 p1++;//p1找到第一个字符 24 25 p2 = str; 26 while (*p2) 27 p2++; 28 p2--;//p2来到最后位置 29 while (*p2 == '*') 30 p2--; 31 //p2找到最后一个字符 32 p = str; 33 i = 0; 34 while (p < p1) 35 { 36 str[i] = *p; 37 p++; 38 i++; 39 } 40 while (p <= p2) 41 { 42 if (*p != '*') 43 { 44 str[i] = *p; 45 i++; 46 } 47 p++; 48 } 49 while (*p != '\0') 50 { 51 str[i] = *p; 52 p++; 53 i++; 54 } 55 str[i] = '\0'; 56 }
6
1 #include <stdio.h> 2 #include <string.h> 3 4 void sort(char *name[], int n); 5 6 int main() 7 { 8 char *course[4] = {"C Program", 9 "C++ Object Oriented Program", 10 "Operating System", 11 "Data Structure and Algorithms"}; 12 13 int i; 14 sort(course, 4); 15 16 for (i = 0; i < 4; i++) 17 printf("%s\n", course[i]); 18 system("pause"); 19 return 0; 20 } 21 void sort(char *name[], int n) 22 { 23 int i, j; 24 char *tmp; 25 26 for (i = 0; i < n - 1; ++i) 27 for (j = 0; j < n - 1 - i; ++j) 28 if (strcmp(name[j], name[j + 1]) > 0) 29 { 30 tmp = name[j]; 31 name[j] = name[j + 1]; 32 name[j + 1] = tmp; 33 } 34 }
6.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 12 sort(course, 4); 13 14 for (i = 0; i < 4; i++) 15 printf("%s\n", course[i]); 16 system("pause"); 17 return 0; 18 } 19 void sort(char *name[], int n) 20 { 21 int i, j, k; 22 char *tmp; 23 24 for (i = 0; i < n - 1; i++) 25 { 26 k = i; 27 for (j = i + 1; j < n; j++) 28 if (strcmp(name[j], name[k]) < 0) 29 k = j; 30 if (k != i) 31 { 32 tmp = name[i]; 33 name[i] = name[k]; 34 name[k] = tmp; 35 } 36 } 37 }
7
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 "330106199609203301", 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 getchar(); 19 return 0; 20 } 21 // 函数定义 22 // 功能: 检查指针str指向的身份证号码串形式上是否合法。 23 // 形式合法,返回1,否则,返回0 24 int check_id(char *str) 25 { 26 27 char *begin; 28 begin=str; 29 30 while((*begin >= '0' && *begin <= '9')||*begin=='X') 31 begin++; 32 33 if(*begin == '\0' && strlen(str) == 18) 34 return 1; 35 else 36 return 0; 37 }
8
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 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 getchar(); 21 return 0; 22 } 23 /*函数定义 24 功能:对s指向的字符串进行编码处理 25 编码规则: 26 对于a~z或A~Z之间的字母字符,用其后的字符替换; 其中,z用a替换,Z用A替换 27 其它非字母字符,保持不变 28 */ 29 void encoder(char *s) 30 { 31 char *begin ,*end; 32 begin=s; 33 end=s; 34 35 while(*end) 36 end++; 37 end--; 38 while(begin<=end) 39 { 40 if ('A'<= *begin<'Z' || 'a'<=*begin<'z') 41 {*begin=*begin+1; 42 begin++;} 43 else 44 if( *begin== 'Z' || *begin=='z') 45 {*begin=*begin-25; 46 begin++;} 47 else 48 begin++; 49 } 50 } 51 /*函数定义 52 功能:对s指向的字符串进行解码处理 53 解码规则: 54 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换 55 其它非字母字符,保持不变 56 */ 57 void decoder(char *s) 58 { 59 char *begin ,*end; 60 begin=s; 61 end=s; 62 63 while(*end) 64 end++; 65 end--; 66 while(begin<=end) 67 { 68 if ('A'< *begin<='Z' || 'a'<*begin<='z') 69 {*begin=*begin-1; 70 begin++;} 71 else 72 if( *begin== 'A' || *begin=='a') 73 {*begin=*begin-25; 74 begin++;} 75 else 76 begin++; 77 } 78 }