实验5
Task1_1
1 #include <stdio.h> 2 #define N 5 3 void input(int x[], int n); 4 void output(int x[], int n); 5 void find_min_max(int x[], int n, int *pmin, int *pmax); 6 int main() { 7 int a[N]; 8 int min, max; 9 printf("录入%d个数据:\n", N); 10 input(a, N); 11 printf("数据是: \n"); 12 output(a, N); 13 printf("数据处理...\n"); 14 find_min_max(a, N, &min, &max); 15 printf("输出结果:\n"); 16 printf("min = %d, max = %d\n", min, max); 17 return 0; 18 } 19 void input(int x[], int n) { 20 int i; 21 for(i = 0; i < n; ++i) 22 scanf("%d", &x[i]); 23 } 24 void output(int x[], int n) { 25 int i; 26 for(i = 0; i < n; ++i) 27 printf("%d ", x[i]); 28 printf("\n"); 29 } 30 void find_min_max(int x[], int n, int *pmin, int *pmax) { 31 int i; 32 *pmin = *pmax = x[0]; 33 for(i = 1; i < n; ++i) 34 if(x[i] < *pmin) 35 *pmin = x[i]; 36 else if(x[i] > *pmax) 37 *pmax = x[i]; 38 }
Rs1_1
(1)找到数组中的最大值和最小值
(2)x[0]
Task1_2
1 #include <stdio.h> 2 #define N 5 3 void input(int x[], int n); 4 void output(int x[], int n); 5 int *find_max(int x[], int n); 6 int main() { 7 int a[N]; 8 int *pmax; 9 printf("录入%d个数据:\n", N); 10 input(a, N); 11 printf("数据是: \n"); 12 output(a, N); 13 printf("数据处理...\n"); 14 pmax = find_max(a, N); 15 printf("输出结果:\n"); 16 printf("max = %d\n", *pmax); 17 return 0; 18 }void input(int x[], int n) { 19 int i; 20 for(i = 0; i < n; ++i) 21 scanf("%d", &x[i]); 22 } 23 void output(int x[], int n) { 24 int i; 25 for(i = 0; i < n; ++i) 26 printf("%d ", x[i]); 27 printf("\n"); 28 } 29 int *find_max(int x[], int n) { 30 int max_index = 0; 31 int i; 32 for(i = 1; i < n; ++i) 33 if(x[i] > x[max_index]) 34 max_index = i; 35 return &x[max_index]; 36 }
Rs1_2
(1)最大元素地址
(2)不可以,可以找到最大值但会破坏原来的数组
Task2_1
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 int main() { 5 char s1[] = "Learning makes me happy"; 6 char s2[] = "Learning makes me sleepy"; 7 char tmp[N]; 8 printf("sizeof(s1) vs. strlen(s1): \n"); 9 printf("sizeof(s1) = %d\n", sizeof(s1)); 10 printf("strlen(s1) = %d\n", strlen(s1)); 11 printf("\nbefore swap: \n"); 12 printf("s1: %s\n", s1); 13 printf("s2: %s\n", s2); 14 printf("\nswapping...\n"); 15 strcpy(tmp, s1); 16 strcpy(s1, s2); 17 strcpy(s2, tmp); 18 printf("\nafter swap: \n"); 19 printf("s1: %s\n", s1); 20 printf("s2: %s\n", s2); 21 return 0; 22 }
Rs2_1
(1)24字节,s1数组占用的字节数,有效字符占用的字节数
(2)不行,字符数组定义时要定义长度
(3)不交换,长度不一致
Task2_2
1 int main() { 2 char *s1 = "Learning makes me happy"; 3 char *s2 = "Learning makes me sleepy"; 4 char *tmp; 5 printf("sizeof(s1) vs. strlen(s1): \n"); 6 printf("sizeof(s1) = %d\n", sizeof(s1)); 7 printf("strlen(s1) = %d\n", strlen(s1)); 8 printf("\nbefore swap: \n"); 9 printf("s1: %s\n", s1); 10 printf("s2: %s\n", s2); 11 printf("\nswapping...\n"); 12 tmp = s1; 13 s1 = s2; 14 s2 = tmp; 15 printf("\nafter swap: \n"); 16 printf("s1: %s\n", s1); 17 printf("s2: %s\n", s2); 18 return 0; 19 }
Rs2_2
(1)第一个字符串的地址,地址占用的字节,指针所指位置字符串有效字符占用的字节数
(2)可以,1先定义一个字符串,地址放在s1;2先定义一个指针,使其指向一个字符串。
(3)字符串的指针交换,但字符串及其位置没有改变
Task3
1 #include <stdio.h> 2 #include <stdio.h> 3 int main() { 4 int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}}; 5 int i, j; 6 int *ptr1; // 指针变量,存放int类型数据的地址 7 int(*ptr2)[4]; // 指针变量,指向包含4个int元素的一维数组 8 printf("输出1: 使用数组名、下标直接访问二维数组元素\n"); 9 for (i = 0; i < 2; ++i) { 10 for (j = 0; j < 4; ++j) 11 printf("%d ", x[i][j]); 12 printf("\n"); 13 } 14 printf("\n输出2: 使用指向元素的指针变量p间接访问二维数组元素\n"); 15 for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) { 16 printf("%d ", *ptr1); 17 if ((i + 1) % 4 == 0) 18 printf("\n"); 19 } 20 printf("\n输出3: 使用指向一维数组的指针变量q间接访问二维数组元素\n"); 21 for (ptr2 = x; ptr2 < x + 2; ++ptr2) { 22 for (j = 0; j < 4; ++j) 23 printf("%d ", *(*ptr2 + j)); 24 printf("\n"); 25 } 26 return 0; 27 }
Rs3

Task4_1
1 #include <stdio.h> 2 #define N 80 3 void replace(char *str, char old_char, char new_char); // 函数声明 4 int main() { 5 char text[N] = "c programming is difficult or not, it is a question."; 6 printf("原始文本: \n"); 7 printf("%s\n", text); 8 replace(text, 'i', '*'); // 函数调用 注意字符形参写法,单引号不能少 9 printf("处理后文本: \n"); 10 printf("%s\n", text); 11 return 0; 12 } 13 // 函数定义 14 void replace(char *str, char old_char, char new_char) { 15 int i; 16 while(*str) { 17 if(*str == old_char) 18 *str = new_char; 19 str++; 20 } 21 }
Rs4_1

(1)把字符串中的I替换为*
(2)可以
Task4_2
1 #include <stdio.h> 2 #define N 80 3 void str_trunc(char *str, char x); 4 int main() { 5 char str[N]; 6 char ch; 7 printf("输入字符串: "); 8 gets(str); 9 printf("输入一个字符: "); 10 ch = getchar(); 11 printf("截断处理...\n"); 12 str_trunc(str, ch); 13 printf("截断处理后的字符串: %s\n", str); 14 } 15 void str_trunc(char *str, char x) { 16 while(*str) { 17 if(*str == x){ 18 19 *str='\0'; // blank1 20 break; // blank2 21 } 22 str++; 23 if (*str==0) break;// blank3 24 }}
Rs4_2

Task5
Rs5指针变量的值
Task6
#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; } int check_id(char *str) { int i, j; char a; i = strlen( str ); if( i != 18){ return 0; } for(j = 0; j < i; j++){ a = str[j]; if( a < 48){ return 0; } if( a > 57 && a != 88){ return 0; } } return 1; }
Rs6

Task7
#include<stdio.h> #include<stdlib.h> #define N 80 void encoder(char *str); // 函数声明 void decoder(char *str); // 函数声明 int main() { char words[N]; printf("输入英文文本: "); gets(words); printf("编码后的英文文本: "); encoder(words); // 函数调用 printf("%s\n", words); printf("对编码后的英文文本解码: "); decoder(words); // 函数调用 printf("%s\n", words); system("pause"); return 0; } /*函数定义 功能:对s指向的字符串进行编码处理 编码规则: 对于a~z或A~Z之间的字母字符,用其后的字符替换; 其中,z用a替换,Z用A替换 其它非字母字符,保持不变 */ void encoder(char *str) { while(*str) { if(*str=='z'||*str=='Z') { *str=*str-26; } if((*str<'z'&&*str>='a')||(*str<'Z'&&*str>='A')) *str=*str+1; str++; } } /*函数定义 功能:对s指向的字符串进行解码处理 解码规则: 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换 其它非字母字符,保持不变 */ void decoder(char *str) { while(*str) { if(*str=='a'||*str=='A') { *str=*str+26; } if((*str>'a'&&*str<='z')||(*str>'A'&&*str<='Z')) *str=*str-1; str++; } }
Rs7

浙公网安备 33010602011771号