实验4 C语言数组应用编程
实验任务1
代码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 printf("sizeof(a) = %d\n", sizeof(a)); 9 10 for(i = 0; i < N; ++i) 11 printf("%p: %d\n",&a[i], a[i]); 12 13 printf("a = %p\n", a); 14 } 15 16 void test2(){ 17 char b[N] = {'1', '9', '8', '4'}; 18 int i; 19 20 printf("sizeof(b) = %d\n", sizeof(b)); 21 22 for(i = 0; i < N; ++i) 23 printf("%p: %c\n",&b[i], b[i]); 24 25 printf("b = %p\n", b); 26 } 27 28 int main(){ 29 printf("测试1: int类型一维数据\n"); 30 test1(); 31 32 printf("\n测试2: char类型一维数据\n"); 33 test2(); 34 35 return 0; 36 }
运行结果
问题1
int型数组a,在内存中是连续存放的,每个元素占用4个内存字节单元,数组名a对应的值,和&a[0]是一样的
问题2
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 printf("sizeof(a) = %d\n", sizeof(a)); 10 11 for(i = 0; i < N; ++i) 12 for(j = 0; j < M; ++i) 13 printf("%p: %d\n",&a[i][j], a[i][j]); 14 printf("\n"); 15 16 printf("a = %p\n", a); 17 printf("a[0] = %p\n", a[0]); 18 printf("a[1] = %p\n", a[1]); 19 printf("\n"); 20 } 21 22 void test2(){ 23 char b[N][M] = {{1, 9, 8, 4},{2, 0, 4, 9}}; 24 int i, j; 25 26 printf("sizeof(b) = %d\n", sizeof(b)); 27 28 for(i = 0; i < N; ++i) 29 for(j = 0; j < M; ++i) 30 printf("%p: %c\n",&b[i][j], b[i][j]); 31 printf("\n"); 32 33 printf("b = %p\n", b); 34 printf("b[0] = %p\n", b[0]); 35 printf("b[1] = %p\n", b[1]); 36 } 37 38 int main(){ 39 printf("测试1: int类型两维数据\n"); 40 test1(); 41 42 printf("\n测试2: char类型两维数据\n"); 43 test2(); 44 45 return 0; 46 }
运行结果
问题1
int型二维数组a,在内存中是"按行连续存放"的,每个元素占用4个内存字节单元,int型二维数组a, 数组名a的值和&a[0][0]的值,在数字字面值上,是一样的
问题2
char型二维数组b,在内存中是"按行连续存放"的,每个元素占用1个内存字节单元,char型二维数组b, 数组名b的值&b[0][0]的值,在数字字面值上,是一样的
问题3
a[0], a[1]的值相差12,也就是三个int的内存大小,a数组的二维恰好有3个
b[0], b[1]的值相差3, 是三个char的内存大小 ,b数组的二维恰好3个
实验任务2
代码task2.c
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 80 5 6 void swap_str(char s1[N], char s2[N]); 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 }
运行结果
总结:test1中,views1和views2是两个一维数组;test2中,views[0], views[1]是二维数组第一维的两个数组,所以后者加中括号,传入swap_str的实际上都是一维数组
实验任务3
代码task3_1.c
1 #include <stdio.h> 2 3 #define N 80 4 5 int count(char x[]); 6 7 int main() { 8 char words[N+1]; 9 int n; 10 11 while(gets(words) != NULL) { 12 n = count(words); 13 printf("单词数: %d\n\n", n); 14 } 15 16 return 0; 17 } 18 19 int count(char x[]) { 20 int i; 21 int word_flag = 0; 22 int number = 0; 23 24 for(i = 0; x[i] != '\0'; i++) { 25 if(x[i] == ' ') 26 word_flag = 0; 27 else if(word_flag == 0) { 28 word_flag = 1; 29 number++; 30 } 31 } 32 33 return number; 34 }
运行结果
代码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 while(line[i] == ' ') { 19 word_len = 0; 20 i++; 21 } 22 23 while(line[i] != '\0' && line[i] != ' ') { 24 word_len++; 25 i++; 26 } 27 28 if(max_len < word_len) { 29 max_len = word_len; 30 end = i; 31 } 32 33 if(line[i] == '\0') 34 break; 35 } 36 37 printf("最长单词: "); 38 for(i = end - max_len; i < end; ++i) 39 printf("%c", line[i]); 40 printf("\n\n"); 41 } 42 43 return 0; 44 }
运行结果
实验任务4
代码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); 11 dec_to_n(x, 8); 12 dec_to_n(x, 16); 13 14 printf("\n输入一个十进制整数: "); 15 } 16 17 return 0; 18 } 19 20 void dec_to_n(int x, int n){ 21 char map[] = {"0123456789ABCDEF"}; 22 char ans[N]; 23 int r; 24 int cnt = 0, i; 25 26 do{ 27 r = x %n; 28 ans[cnt++] = map[r]; 29 x = x / n; 30 }while(x != 0); 31 32 for(i = cnt - 1;i >= 0; --i) 33 printf("%c", ans[i]); 34 35 printf("\n"); 36 }
运行结果
实验任务5
代码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 void input(int x[], int n) { 31 int i; 32 33 for(i = 0; i < n; ++i) 34 scanf("%d", &x[i]); 35 } 36 37 void output(int x[], int n) { 38 int i; 39 40 for(i = 0; i < n; ++i) 41 printf("%d ", x[i]); 42 printf("\n"); 43 } 44 45 double average(int x[], int n){ 46 int i; 47 double t, s = 0; 48 49 for(i = 0; i < n; ++i) 50 s = s + x[i]; 51 t = s / n; 52 return t; 53 } 54 55 void bubble_sort(int x[], int n){ 56 int i, a, b, c; 57 58 for(i = 1; i <= n - 1; ++i){ 59 for(a = 0; a <= n - 1 - i; ++a){ 60 if(x[a] < x[a + 1]){ 61 b = x[a]; 62 x[a] = x[a + 1]; 63 x[a + 1] = b; 64 } 65 } 66 } 67 for(c = 0; c < n; ++c) 68 printf("&d", x[c]); 69 }
运行结果
实验任务6
代码task6.c
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 5 5 #define M 20 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 void output(char str[][M], int n) { 27 int i; 28 29 for(i = 0; i < n; ++i) 30 printf("%s\n", str[i]); 31 } 32 33 void bubble_sort(char str[][M], int n) { 34 char temp[M]; 35 int i, j; 36 37 for(i = 0; i < n - 1; i++){ 38 for(j = 0; j < n - i - 1; j++) { 39 if (str[j][0] > str[j + 1][0]){ 40 strcpy(temp, str[j]); 41 strcpy(str[j], str[j + 1]); 42 strcpy(str[j + 1], temp); 43 } 44 } 45 } 46 }
运行结果
实验任务7
代码task7.c
1 #include <stdio.h> 2 #include <string.h> 3 int main(){ 4 char a[10000]; 5 int i, s; 6 7 while (scanf("%s", a) != EOF) { 8 int x = 0; 9 for(i = 0; i < strlen(a); i++) { 10 for(s = i + 1; s < strlen(a); s++){ 11 if(a[i] == a[s]){ 12 x = 1; 13 break; 14 } 15 } 16 if (x == 1){ 17 break; 18 } 19 } 20 if(x == 1) 21 printf("YES\n\n"); 22 else if(x == 0) 23 printf("NO\n\n"); 24 } 25 return 0; 26 }
运行结果
实验任务8
代码task8.c
1 #include <stdio.h> 2 #define N 100 3 #define M 4 4 5 void output(int x[][N], int n); 6 void rotate_to_right(int x[][N], int n); 7 8 9 int main() { 10 int t[][N] = {{21, 12, 13, 24}, 11 {25, 16, 47, 38}, 12 {29, 11, 32, 54}, 13 {42, 21, 33, 10}}; 14 15 printf("原始矩阵:\n"); 16 output(t, M); 17 18 rotate_to_right(t, M); 19 20 printf("变换后矩阵\n"); 21 output(t, M); 22 23 return 0; 24 } 25 26 void output(int x[][N], int n) { 27 int i, j; 28 29 for (i = 0; i < n; ++i) { 30 for (j = 0; j < n; ++j) 31 printf("%4d", x[i][j]); 32 33 printf("\n"); 34 } 35 } 36 37 void rotate_to_right(int x[][N], int n){ 38 int i,t; 39 40 for(i=0;i<4;++i){ 41 t=x[i][0]; 42 x[i][0]=x[i][3]; 43 x[i][3]=t; 44 t=x[i][1]; 45 x[i][1]=x[i][3]; 46 x[i][3]=t; 47 t=x[i][3]; 48 x[i][3]=x[i][2]; 49 x[i][2]=t; 50 } 51 }
运行结果