实验4 C语言数组应用编程
task1_1.c
1 #include <stdio.h> 2 #define N 4 3 4 void test1() { 5 int a[N] = {1, 9, 8, 4}; 6 int i; 7 8 // 输出数组a占用的内存字节数 9 printf("sizeof(a) = %d\n", sizeof(a)); 10 11 // 输出int类型数组a中每个元素的地址、值 12 for (i = 0; i < N; ++i) 13 printf("%p: %d\n", &a[i], a[i]); 14 15 // 输出数组名a对应的值 16 printf("a = %p\n", a); 17 } 18 19 void test2() { 20 char b[N] = {'1', '9', '8', '4'}; 21 int i; 22 23 // 输出数组b占用的内存字节数 24 printf("sizeof(b) = %d\n", sizeof(b)); 25 26 // 输出char类型数组b中每个元素的地址、值 27 for (i = 0; i < N; ++i) 28 printf("%p: %c\n", &b[i], b[i]); 29 30 // 输出数组名b对应的值 31 printf("b = %p\n", b); 32 } 33 34 int main() { 35 printf("测试1: int类型一维数组\n"); 36 test1(); 37 38 printf("\n测试2: char类型一维数组\n"); 39 test2(); 40 41 return 0; 42 }
① int型数组a,在内存中是连续存放的,每个元素占用4个内存字节单元, 数组名a对应的
值,和&a[0]是一样的。
② char型数组b,在内存中是连续存放的,每个元素占用1个内存字节单元? 数组名b对应的
值,和&b[0]是一样的。
task1_2.c
1 #include <stdio.h> 2 #define N 2 3 #define M 4 4 5 void test1() { 6 int a[N][M] = {{1, 9, 8, 4}, {2, 0, 4, 9}}; 7 int i, j; 8 9 // 输出int类型二维数组a占用的内存字节数 10 printf("sizeof(a) = %d\n", sizeof(a)); 11 12 // 输出int类型二维数组a中每个元素的地址、值 13 for (i = 0; i < N; ++i) 14 for (j = 0; j < M; ++j) 15 printf("%p: %d\n", &a[i][j], a[i][j]); 16 printf("\n"); 17 18 // 输出int类型二维数组名a, 以及,a[0], a[1]的值 19 printf("a = %p\n", a); 20 printf("a[0] = %p\n", a[0]); 21 printf("a[1] = %p\n", a[1]); 22 printf("\n"); 23 } 24 25 void test2() { 26 char b[N][M] = {{'1', '9', '8', '4'}, {'2', '0', '4', '9'}}; 27 int i, j; 28 29 // 输出char类型二维数组b占用的内存字节数 30 printf("sizeof(b) = %d\n", sizeof(b)); 31 32 // 输出char类型二维数组b中每个元素的地址、值 33 for (i = 0; i < N; ++i) 34 for (j = 0; j < M; ++j) 35 printf("%p: %c\n", &b[i][j], b[i][j]); 36 printf("\n"); 37 38 // 输出char类型二维数组名b, 以及,b[0], b[1]的值 39 printf("b = %p\n", b); 40 printf("b[0] = %p\n", b[0]); 41 printf("b[1] = %p\n", b[1]); 42 } 43 44 int main() { 45 printf("测试1: int型两维数组"); 46 test1(); 47 48 printf("\n测试2: char型两维数组"); 49 test2(); 50 51 return 0; 52 }
① int型二维数组a,在内存中是"按行连续存放"的,每个元素占用4个内存字节单元, 数组名
a的值、a[0]的值、&a[0][0]的值,在数字字面值上,是一样的。
② char型二维数组b,在内存中是"按行连续存放"的,每个元素占用1个内存字节单元,数组
名b的值、b[0]的值、&b[0][0]的值,在数字字面值上,是一样的。
③ 对于二维数组, a[0], a[1]的值,它们之间相差16个字节,b[0]和b[1]的值,它们之间相差4个字节。
相差的字节数为数组列数*每个元素所占内存数。
task2.c
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 80 5 6 void swap_str(char s1[], char s2[]); 7 void test1(); 8 void test2(); 9 10 int main() { 11 printf("测试1: 用两个一维char数组,实现两个字符串交换\n"); 12 test1(); 13 14 printf("\n测试2: 用二维char数组,实现两个字符串交换\n"); 15 test2(); 16 17 return 0; 18 } 19 20 void test1() { 21 char views1[N] = "hey, C, I hate u."; 22 char views2[N] = "hey, C, I love u."; 23 24 printf("交换前: \n"); 25 puts(views1); 26 puts(views2); 27 28 swap_str(views1, views2); 29 30 printf("交换后: \n"); 31 puts(views1); 32 puts(views2); 33 } 34 35 void test2() { 36 char views[2][N] = {"hey, C, I hate u.", 37 "hey, C, I love u."}; 38 39 printf("交换前: \n"); 40 puts(views[0]); 41 puts(views[1]); 42 43 swap_str(views[0], views[1]); 44 45 printf("交换后: \n"); 46 puts(views[0]); 47 puts(views[1]); 48 } 49 50 void swap_str(char s1[N], char s2[N]) { 51 char tmp[N]; 52 53 strcpy(tmp, s1); 54 strcpy(s1, s2); 55 strcpy(s2, tmp); 56 }
test2中加[]指向数组行所在地址,test1中不加[]指向该数组所在地址。
test3_1.c
1 #include <stdio.h> 2 #define N 80 3 4 int count(char x[]); 5 6 int main() { 7 char words[N+1]; 8 int n; 9 10 while(gets(words) != NULL) { 11 n = count(words); 12 printf("单词数: %d\n\n", n); 13 } 14 15 return 0; 16 } 17 18 int count(char x[]) { 19 int i; 20 int word_flag = 0; // 用作单词标志,一个新单词开始,值为1;单词结束,值为0 21 int cnt = 0; // 统计单词个数 22 23 for(i = 0; x[i] != '\0'; i++) { 24 if(x[i] == ' ') 25 word_flag = 0; 26 else if(word_flag == 0) { 27 word_flag = 1; 28 cnt++; 29 } 30 } 31 32 return cnt; 33 }
task3_2.c
1 #include <stdio.h> 2 #define N 1000 3 4 int main() { 5 char line[N]; 6 int word_len; // 记录当前单词长度 7 int max_len; // 记录最长单词长度 8 int end; // 记录最长单词结束位置 9 int i; 10 11 while(gets(line) != NULL) { 12 word_len = 0; 13 max_len = 0; 14 end = 0; 15 16 i = 0; 17 while(1) { 18 // 跳过连续空格 19 while(line[i] == ' ') { 20 word_len = 0; // 单词长度置0,为新单词统计做准备 21 i++; 22 } 23 24 // 在一个单词中,统计当前单词长度 25 while(line[i] != '\0' && line[i] != ' ') { 26 word_len++; 27 i++; 28 } 29 30 // 更新更长单词长度,并,记录最长单词结束位置 31 if(max_len < word_len) { 32 max_len = word_len; 33 end = i; // end保存的是单词结束的下一个坐标位置 34 } 35 36 // 遍历到文本结束时,终止循环 37 if(line[i] == '\0') 38 break; 39 } 40 41 // 输出最长单词 42 printf("最长单词: "); 43 for(i = end - max_len; i < end; ++i) 44 printf("%c", line[i]); 45 printf("\n\n"); 46 } 47 48 return 0; 49 }
task4.c
1 #include <stdio.h> 2 #define N 100 3 void dec_to_n(int x, int n); // 函数声明 4 5 int main() { 6 int x; 7 8 printf("输入一个十进制整数: "); 9 while(scanf("%d", &x) != EOF) { 10 dec_to_n(x, 2); // 函数调用: 把x转换成二进制输出 11 dec_to_n(x, 8); // 函数调用: 把x转换成八进制输出 12 dec_to_n(x, 16); // 函数调用: 把x转换成十六进制输出 13 14 printf("\n输入一个十进制整数: "); 15 } 16 17 return 0; 18 } 19 20 // 函数定义 21 // 功能: 把十进制数x转换成n进制,打印输出 22 // 补足函数实现 23 // ××× 24 void dec_to_n(int x, int n){ 25 char a[]="0123456789ABCDEF"; 26 char b[N]; 27 int i=0,j; 28 if(x==0) 29 { 30 printf("0\n"); 31 return; 32 } 33 while(x>0) 34 { 35 j = x % n; 36 b[i++] = a[j]; 37 x /= n; 38 } 39 for(i=i-1;i>=0;i--) 40 { 41 printf("%c",b[i]); 42 } 43 printf("\n"); 44 }
task5.c
1 #include <stdio.h> 2 #define N 5 3 // 函数声明 4 void input(int x[], int n); 5 void output(int x[], int n); 6 double average(int x[], int n); 7 void bubble_sort(int x[], int n); 8 9 int main() { 10 int scores[N]; 11 double ave; 12 13 printf("录入%d个分数:\n", N); 14 input(scores, N); 15 16 printf("\n输出课程分数: \n"); 17 output(scores, N); 18 19 printf("\n课程分数处理: 计算均分、排序...\n"); 20 ave = average(scores, N); 21 bubble_sort(scores, N); 22 23 printf("\n输出课程均分: %.2f\n", ave); 24 printf("\n输出课程分数(高->低):\n"); 25 output(scores, N); 26 27 return 0; 28 } 29 30 // 函数定义 31 // 输入n个整数保存到整型数组x中 32 void input(int x[], int n) { 33 int i; 34 35 for(i = 0; i < n; ++i) 36 scanf("%d", &x[i]); 37 } 38 39 // 输出整型数组x中n个元素 40 void output(int x[], int n) { 41 int i; 42 43 for(i = 0; i < n; ++i) 44 printf("%d ", x[i]); 45 printf("\n"); 46 } 47 48 // 计算整型数组x中n个元素均值,并返回 49 // 补足函数average()实现 50 // ××× 51 double average(int x[], int n){ 52 int i; 53 double ave,sum=0; 54 for(i=0;i<n;i++) 55 { 56 sum+=x[i]; 57 } 58 ave=sum/n; 59 return ave; 60 } 61 62 // 对整型数组x中的n个元素降序排序 63 // 补足函数bubble_sort()实现 64 // ××× 65 void bubble_sort(int x[],int n){ 66 int i,j,flag=0,temp=0; 67 for(i=0;i<n-1;i++) 68 { 69 for(j=0;j<n-i-1;j++) 70 { 71 if(x[i]>x[i+1]) 72 { 73 temp=x[i]; 74 x[i]=x[i+1]; 75 x[i+1]=temp; 76 flag=1; 77 } 78 } 79 if(!flag) 80 break; 81 } 82 }
task6.c
1 #include <stdio.h> 2 #include <string.h> 3 #define N 5 4 #define M 20 5 6 // 函数声明 7 void output(char str[][M], int n); 8 void bubble_sort(char str[][M], int n); 9 10 int main() { 11 char name[][M] = {"Bob", "Bill", "Joseph", "Taylor", "George"}; 12 int i; 13 14 printf("输出初始名单:\n"); 15 output(name, N); 16 17 printf("\n排序中...\n"); 18 bubble_sort(name, N); // 函数调用 19 20 printf("\n按字典序输出名单:\n"); 21 output(name, N); 22 23 return 0; 24 } 25 26 // 函数定义 27 // 功能:按行输出二维数组中的字符串 28 void output(char str[][M], int n) { 29 int i; 30 31 for(i = 0; i < n; ++i) 32 printf("%s\n", str[i]); 33 } 34 35 // 函数定义 36 // 功能:使用冒泡排序算法对二维数组str中的n个字符串按字典序排序 37 // 补足函数bubble_sort()实现 38 // ××× 39 void bubble_sort(char str[][M], int n){ 40 char temp[M]; 41 int i,j; 42 for(i=0;i<n-1;i++) 43 { 44 for(j=0;j<n-i-1;j++) 45 { 46 if(strcmp(str[j],str[j+1])>0) 47 { 48 strcpy(temp,str[j]); 49 strcpy(str[j],str[j+1]); 50 strcpy(str[j+1],temp); 51 } 52 } 53 } 54 }
task7.c
1 #include<stdio.h> 2 #include<string.h> 3 # define N 101 4 int is_repeated(char num[N]); 5 6 int main() 7 { 8 char num[N]; 9 while(scanf("%s",num) != EOF) 10 { 11 if(is_repeated(num)) 12 printf("YES\n"); 13 else 14 printf("NO\n"); 15 } 16 return 0; 17 } 18 19 int is_repeated(char x[N]){ 20 int cnt[10]={0}; 21 int i,j; 22 for(i=0; x[i] != '\0';i++) 23 { 24 j=x[i]-'0'; 25 cnt[j]++; 26 if(cnt[j]>1) 27 return 1; 28 } 29 return 0; 30 }
task8.c
1 #include <stdio.h> 2 #define N 100 3 #define M 4 4 5 // 函数声明 6 void output(int x[][N], int n); 7 void rotate_to_right(int x[][N], int n); 8 9 10 int main() { 11 int t[][N] = {{21, 12, 13, 24}, 12 {25, 16, 47, 38}, 13 {29, 11, 32, 54}, 14 {42, 21, 33, 10}}; 15 16 printf("原始矩阵:\n"); 17 output(t, M); // 函数调用 18 19 rotate_to_right(t, M); // 函数调用 20 21 printf("变换后矩阵:\n"); 22 output(t, M); // 函数调用 23 24 return 0; 25 } 26 27 // 函数定义 28 // 功能: 输出一个n*n的矩阵x 29 void output(int x[][N], int n) { 30 int i, j; 31 32 for (i = 0; i < n; ++i) { 33 for (j = 0; j < n; ++j) 34 printf("%4d", x[i][j]); 35 36 printf("\n"); 37 } 38 } 39 40 // 待补足3:函数rotate_to_right()定义 41 // 功能: 把一个n*n的矩阵x,每一列向右移, 最右边被移出去的一列绕回左边 42 // xxx 43 void rotate_to_right(int x[][N], int n){ 44 int i,j,temp[N]; 45 for(i=0;i<n;i++) 46 { 47 temp[i]=x[i][n-1]; 48 } 49 for(i=n-1;i>0;i--) 50 { 51 for(j=0;j<n;j++) 52 { 53 x[j][i]=x[j][i-1]; 54 } 55 } 56 for(i=0;i<n;i++) 57 { 58 x[i][0]=temp[i]; 59 } 60 61 }