实验6
四、实验结论:
1.
task1_1.c
1 #include <stdio.h> 2 #define N 4 3 4 int main() 5 { 6 int x[N] = {1, 9, 8, 4}; 7 int i; 8 int *p; 9 10 // 方式1:通过数组名和下标遍历输出数组元素 11 for(i=0; i<N; ++i) 12 printf("%d", x[i]); 13 printf("\n"); 14 15 // 方式2:通过指针变量遍历输出数组元素 (写法1) 16 for(p=x; p<x+N; ++p) 17 printf("%d", *p); 18 printf("\n"); 19 20 // 方式2:通过指针变量遍历输出数组元素(写法2) 21 p = x; 22 for(i=0; i<N; ++i) 23 printf("%d", *(p+i)); 24 printf("\n"); 25 26 // 方式2:通过指针变量遍历输出数组元素(写法3) 27 p = x; 28 for(i=0; i<N; ++i) 29 printf("%d", p[i]); 30 printf("\n"); 31 32 return 0; 33 } 34 35 36 //Q1: 2004
task1_2.c
1 #include <stdio.h> 2 #define N 4 3 4 int main() 5 { 6 char x[N] = {'1', '9', '8', '4'}; 7 int i; 8 char *p; 9 10 // 方式1:通过数组名和下标遍历输出数组元素 11 for(i=0; i<N; ++i) 12 printf("%c", x[i]); 13 printf("\n"); 14 15 // 方式2:通过指针变量遍历输出数组元素 (写法1) 16 for(p=x; p<x+N; ++p) 17 printf("%c", *p); 18 printf("\n"); 19 20 // 方式2:通过指针变量遍历输出数组元素(写法2) 21 p = x; 22 for(i=0; i<N; ++i) 23 printf("%c", *(p+i)); 24 printf("\n"); 25 26 // 方式2:通过指针变量遍历输出数组元素(写法3) 27 p = x; 28 for(i=0; i<N; ++i) 29 printf("%c", p[i]); 30 printf("\n"); 31 32 return 0; 33 } 34 35 36 //Q2: 2001 37 //Q3: 因为 int类型占用 4字节,char类型占用 1字节
Q3: int 类数据储存用 4个字节,char 类数据储存用1个字节。
2.
task2_1.c
1 #include <stdio.h> 2 3 int main() 4 { 5 int x[2][4] = { {1,9,8,4}, {2,0,2,2}} ; 6 int i, j; 7 int *p; // 指针变量,存放int类型数据的地址 8 int (*q)[4]; // 指针变量,指向包含4个int型元素的一维数组 9 10 // 使用数组名、下标访问二维数组元素 11 for(i=0; i<2; ++i) 12 { 13 for(j=0; j<4; ++j) 14 printf("%d", x[i][j]); 15 printf("\n"); 16 } 17 18 // 使用指针变量p间接访问二维数组元素 19 for(p = &x[0][0], i = 0; p < &x[0][0] + 8; ++p, ++i) 20 { 21 printf("%d", *p); 22 if( (i+1)%4 == 0) 23 printf("\n"); 24 } 25 26 // 使用指针变量q间接访问二维数组元素 27 for(q=x; q<x+2; ++q) 28 { 29 for(j=0; j<4; ++j) 30 printf("%d", *(*q+j)); 31 printf("\n"); 32 } 33 34 return 0; 35 } 36 37 38 //Q1: 2004 39 //Q2: 2016
task2_2.c
1 #include <stdio.h> 2 3 int main() 4 { 5 char x[2][4] = { {'1', '9', '8', '4'}, {'2', '0', '2', '2'} }; 6 int i, j; 7 char *p; // 指针变量,存放char类型数据的地址 8 char (*q)[4]; // 指针变量,指向包含4个char型元素的一维数组 9 10 // 使用数组名、下标访问二维数组元素 11 for(i=0; i<2; ++i) 12 { 13 for(j=0; j<4; ++j) 14 printf("%c", x[i][j]); 15 printf("\n"); 16 } 17 18 // 使用指针变量p间接访问二维数组元素 19 for(p = &x[0][0], i = 0; p < &x[0][0] + 8; ++p, ++i) 20 { 21 printf("%c", *p); 22 if( (i+1)%4 == 0) 23 printf("\n"); 24 } 25 26 // 使用指针变量q间接访问二维数组元素 27 for(q=x; q<x+2; ++q) 28 { 29 for(j=0; j<4; ++j) 30 printf("%c", *(*q+j)); 31 printf("\n"); 32 } 33 34 return 0; 35 } 36 37 38 //Q3: 2001 39 //Q4: 2004
Q3: int 类数据储存用 4个字节,char 类数据储存用1个字节。
3.
task3_1.c
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 5 int main() 6 { 7 char s1[] = "C, I love u."; 8 char s2[] = "C, I hate u."; 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 return 0; 29 } 30 31 32 //Q1: sizeof()计算的是包含'\0'的全部字符数 strlen()计算的是在'\0'前的全部字符数 33 //Q2: 不行,定义错误
task3_2.c
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 5 int main() 6 { 7 char *s1 = "C, I love u."; 8 char *s2 = "C, I hate u."; 9 char *tmp; 10 11 printf("sizeof(s1) vs. strlen(s1): \n"); 12 printf("%d\n",*s1); 13 printf("sizeof(s1) = %d\n", sizeof(s1)); 14 printf("strlen(s1) = %d\n", strlen(s1)); 15 16 printf("\nbefore swap: \n"); 17 printf("s1: %s\n", s1); 18 printf("s2: %s\n", s2); 19 20 printf("\nswapping...\n"); 21 tmp = s1; 22 s1 = s2; 23 s2 = tmp; 24 25 printf("\nafter swap: \n"); 26 printf("s1: %s\n", s1); 27 printf("s2: %s\n", s2); 28 29 return 0; 30 } 31 32 33 //Q1: 数组 s1的地址 sizeof(s1)计算的是 s1地址的字节数 strlen(s1)计算的是在'\0'之前的总字符数 34 //Q2: 可以 35 //Q3: 交换的是地址值 没有变化
4.
task4.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,j; 31 32 if(strlen(str)==18) 33 ; 34 else return 0; 35 36 for(i=0;*(str+i)!='\0';i++) 37 { 38 if((*(str+i) <= '9' && *(str+i) >= '0') || *(str+i) == 'X') 39 ; 40 else return 0; 41 } 42 return 1; 43 }
5.
task5.c
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 80 5 int is_palindrome(char *s); // 函数声明 6 7 int main() 8 { 9 char str[N]; 10 int flag; 11 12 printf("Enter a string:\n"); 13 gets(str); 14 15 flag = is_palindrome(str); // 函数调用 16 17 if (flag) 18 printf("YES\n"); 19 else 20 printf("NO\n"); 21 22 return 0; 23 } 24 25 // 函数定义 26 // 功能:判断s指向的字符串是否是回文串 27 // 如果是,返回1;否则,返回0 28 int is_palindrome(char *s) 29 { 30 int i, j; 31 32 for(i=0,j=strlen(s); i<j; i++,j--) 33 { 34 if(*(s+i)==*(s+j-1)) 35 ; 36 else return 0; 37 } 38 return 1; 39 }
6.
task6.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 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 21 return 0; 22 } 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, j; 34 35 for(i=0; *s!='\0'; s++) 36 { 37 if(( *s<='z' && *s>='a' ) || ( *s<='Z' && *s>='A' )) 38 switch(*s) 39 { 40 case 'z': *s='a'; break; 41 case 'Z': *s='A'; break; 42 default :*s+=1; 43 } 44 } 45 } 46 47 48 /*函数定义 49 功能:对s指向的字符串进行解码处理 50 解码规则: 51 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换 52 其它非字母字符,保持不变 53 */ 54 void decoder(char *s) 55 { 56 int i, j; 57 58 for(i=0; *s!='\0'; s++) 59 { 60 if(( *s<='z' && *s>='a' ) || ( *s<='Z' && *s>='A' )) 61 switch(*s) 62 { 63 case 'a': *s='z'; break; 64 case 'A': *s='Z'; break; 65 default :*s-=1; 66 } 67 } 68 }