实验4 C语言数组应用编程
实验任务1(1)
1 #include <stdio.h> 2 #define N 4 3 4 void test1() { 5 int a[N] = {1, 9, 8, 4}; 6 int i; 7 printf("sizeof(a) = %d\n", sizeof(a)); 8 9 for (i = 0; i < N; ++i) 10 printf("%p: %d\n", &a[i], a[i]); 11 12 printf("a = %p\n", a); 13 } 14 void test2() { 15 char b[N] = {'1', '9', '8', '4'}; 16 int i; 17 18 printf("sizeof(b) = %d\n", sizeof(b)); 19 20 for (i = 0; i < N; ++i) 21 printf("%p: %c\n", &b[i], b[i]); 22 23 printf("b = %p\n", b); 24 } 25 26 int main() { 27 printf("测试1: int类型一维数组\n"); 28 test1(); 29 30 printf("\n测试2: char类型一维数组\n"); 31 test2(); 32 33 return 0; 34 }
结果演示

答:(1)是,4个,是
(2)是,1个,是
实验任务1(2)
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; ++j) 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; ++j) 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型两维数组"); 40 test1(); 41 42 printf("\n测试2: char型两维数组"); 43 test2(); 44 45 return 0; 46 }
结果演示

答:(1)是,4个,是
(2)是,1个,否
(3)int相差16个字节,char相差4个字节,都相差所在数组的一半字节
实验任务2
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 }
结果演示

答:因为第一个实参是一维数组,直接写入就行,第二个是二维数组,存在了views[0]和views[1]两个里面
实验任务3(1)
1 #include <stdio.h> 2 #define N 80 3 4 int count(char x[]); 5 int main() { 6 char words[N+1]; 7 int n; 8 while(gets(words) != NULL) { 9 n = count(words); 10 printf("单词数: %d\n\n", n); 11 } 12 13 return 0; 14 } 15 16 int count(char x[]) { 17 int i; 18 int word_flag = 0; 19 int number = 0; 20 21 for(i = 0; x[i] != '\0'; i++) { 22 if(x[i] == ' ') 23 word_flag = 0; 24 else if(word_flag == 0) { 25 word_flag = 1; 26 number++; 27 } 28 } 29 30 return number; 31 }
运行结果

实验任务3(2)
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 }
结果演示

答:定义一个新函数,用于判断当前字符是否为字母,若不是字母返回0,若是返回1。将函数运用于判断语句中。
实验任务4
1 #include <stdio.h> 2 #define N 100 3 void dec_to_n(int x, int n){ 4 int m[N]; 5 int i=1; 6 while(x!=0){ 7 m[i]=x%n; 8 x=x/n; 9 i++; 10 } 11 for(int j=i-1;j>=1;j--){ 12 if(m[j]<=9){ 13 printf("%d",m[j]); 14 }else{ 15 printf("%c",m[j]+55) ; 16 } 17 18 } 19 printf("\n"); 20 } 21 22 int main() { 23 int x; 24 25 printf("输入一个十进制整数: "); 26 while(scanf("%d", &x) != EOF) { 27 dec_to_n(x, 2); 28 dec_to_n(x, 8); 29 dec_to_n(x, 16); 30 31 printf("\n输入一个十进制整数: "); 32 } 33 34 return 0; 35 }
结果演示

实验任务5
1 #include <stdio.h> 2 #define N 5 3 void input(int x[], int n); 4 void output(int x[], int n); 5 double average(int x[], int n); 6 void bubble_sort(int x[], int n); 7 8 int main() { 9 int scores[N]; 10 double ave; 11 12 printf("录入%d个分数:\n", N); 13 input(scores, N); 14 15 printf("\n输出课程分数: \n"); 16 output(scores, N); 17 18 printf("\n课程分数处理: 计算均分、排序...\n"); 19 ave = average(scores, N); 20 bubble_sort(scores, N); 21 22 printf("\n输出课程均分: %.2f\n", ave); 23 printf("\n输出课程分数(高->低):\n"); 24 output(scores, N); 25 26 return 0; 27 } 28 29 void input(int x[], int n) { 30 int i; 31 32 for(i = 0; i < n; ++i) 33 scanf("%d", &x[i]); 34 } 35 36 void output(int x[], int n) { 37 int i; 38 39 for(i = 0; i < n; ++i) 40 printf("%d ", x[i]); 41 printf("\n"); 42 } 43 44 double average(int x[],int n){ 45 double m=0; 46 for(int i=0;i<n;++i){ 47 m+=x[i]; 48 } 49 return m/n; 50 } 51 52 void bubble_sort(int x[],int n){ 53 for(int i=0;i<n-1;i++){ 54 for(int j=i;j<n-1;j++){ 55 if(x[j]<x[j+1]){ 56 int m; 57 m=x[j]; 58 x[j]=x[j+1]; 59 x[j+1]=m; 60 } 61 } 62 n--; 63 } 64 }
结果演示

实验任务6
1 #include<stdio.h> 2 #include<string.h> 3 #define N 5 4 #define M 20 5 void output(char str[][M],int n){ 6 int i; 7 for(i=0;i<n;++i){ 8 printf("%s\n",str[i]); 9 } 10 } 11 void bubble_sort(char str[][M],int n){ 12 for(int i=0;i<n-1;i++){ 13 for(int j=i;j<n-1-i;j++){ 14 if(strcmp(str[j],str[j+1])>0){ 15 char x[N]; 16 strcpy(x,str[j]); 17 strcpy(str[j],str[j+1]); 18 strcpy(str[j+1],x); 19 } 20 } 21 } 22 } 23 24 int main(){ 25 char name[][M]={"Bob","Bill","Joseph","Taylor","George"}; 26 int i; 27 printf("输出初始名单:\n"); 28 output(name,N); 29 30 printf("\n排序中...\n"); 31 bubble_sort(name,N); 32 33 printf("\n按字典序输出名单:\n"); 34 output(name,N); 35 return 0; 36 }
结果演示

实验任务7
1 #include<stdio.h> 2 #include<math.h> 3 #define N 100 4 int pdd(char x[]){ 5 int p=0; 6 for(int i=0;x[i]!='\0';i++){ 7 for(int j=i+1;x[j]!='\0';j++){ 8 if(x[i]==x[j]){ 9 return 1; 10 } 11 } 12 } 13 return 0; 14 } 15 int main(){ 16 char a[N]; 17 while(gets(a)!=NULL){ 18 if(pdd(a)){ 19 printf("YES\n"); 20 }else{ 21 printf("NO\n"); 22 } 23 } 24 return 0; 25 }
结果演示

实验任务8
1 #include <stdio.h> 2 #define N 100 3 #define M 4 4 void output(int x[][N], int n); 5 void rotate_to_right(int x[][N], int n); 6 int main() { 7 int t[][N] ={{21, 12, 13, 24}, 8 {25, 16, 47, 38}, 9 {29, 11, 32, 54}, 10 {42, 21, 33, 10}}; 11 printf("原始矩阵:\n"); 12 output(t, M); 13 rotate_to_right(t, M); 14 printf("变换后矩阵:\n"); 15 output(t, M); 16 return 0; 17 } 18 19 void output(int x[][N], int n) { 20 int i, j; 21 for (i = 0; i < n; ++i) { 22 for (j = 0; j < n; ++j) 23 printf("%4d", x[i][j]); 24 25 printf("\n"); 26 } 27 } 28 29 void rotate_to_right(int x[][N],int n){ 30 int j,i; 31 for (i = 0; i < n; ++i) { 32 int a; 33 a=x[i][0]; 34 for (j = 0; j < n-1; ++j){ 35 x[i][j]=x[i][j+1]; 36 } 37 x[i][n-1]=a; 38 } 39 }
结果演示

浙公网安备 33010602011771号