实验4 C语言数组应用编程

发布时间 2023-11-19 17:24:45作者: 侯岩宏

实验任务1

task 1_1.c

源代码:

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

运行截图:

task 1_2.c

源代码:

 1 #include <stdio.h>
 2 #define N 2
 3 #define M 4
 4 
 5 void test1()
 6 {
 7     int a[N][M]={{1,9,8,4},{2,0,4,9}};
 8     int i,j;
 9     
10     printf("sizeof(a)=%d\n",sizeof(a));
11     
12     for(i=0;i<N;++i){
13         for(j=0;j<M;++j)
14             printf("%p:%d\n",&a[i][j],a[i][j]);
15     }
16     printf("\n");
17         
18     printf("a=%p\n",a);
19     printf("a[0]=%p\n",a[0]);
20     printf("a[1] = %p\n", a[1]); 
21     printf("\n");
22 }
23 
24 void test2()
25 {
26     char b[N][M] = {{'1', '9', '8', '4'}, {'2', '0', '4', '9'}}; 
27     int i,j;
28     
29     printf("sizeof(b)=%d\n",sizeof(b));
30     
31     for (i = 0; i < N; ++i){
32         for (j = 0; j < M; ++j)
33             printf("%p: %c\n", &b[i][j], b[i][j]);
34     }
35     printf("\n");
36     
37     printf("b = %p\n", b);
38     printf("b[0] = %p\n", b[0]);
39     printf("b[1] = %p\n", b[1]);
40 }
41 
42 int main()
43 {
44     printf("测试1:int类型一维数组\n");
45     test1();
46     
47     printf("测试2:char类型一维数组\n");
48     test2();
49     
50     return 0;
51 }

运行截图:

实验任务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 {
12     printf("测试1:用两个一维char数组,实现两个字符串交换\n");
13     test1();
14     
15     printf("测试2:用二维char数组,实现两个字符串交换\n");
16     test2();
17     
18     return 0;
19 }
20 
21 void test1()
22 {
23     char views1[N]="hey,C,I hate u.";
24     char views2[N]="hey,C,I love u.";
25     
26     printf("交换前:\n");
27     puts(views1);
28     puts(views2);
29     
30     swap_str(views1,views2);
31     
32     printf("交换后:\n");
33     puts(views1);
34     puts(views2);
35 }
36 
37 void test2()
38 {
39     char views[2][N]={"hey,C,I hate u.",
40                       "hey,C,I love u."};
41     
42     printf("交换前:\n");
43     puts(views[0]);
44     puts(views[1]);
45     
46     swap_str(views[0],views[1]);
47     
48     printf("交换后:\n");
49     puts(views[0]);
50     puts(views[1]); 
51 }
52 
53 void swap_str(char s1[N],char s2[N])
54 {
55     char tmp[N];
56     
57     strcpy(tmp,s1);
58     strcpy(s1,s2);
59     strcpy(s2,tmp);
60 }

运行截图:

实验任务3

task 3_1.c

源代码:

 1 #include <stdio.h>
 2 #define N 80
 3 
 4 int count(char x[]);
 5 
 6 int main()
 7 {
 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 {
21     int i;
22     int word_flag=0;
23     int number=0;
24     
25     for(i=0;x[i]!='\0';i++){
26         if(x[i]==' ')
27            word_flag=0;
28         else if(word_flag==0){
29             word_flag=1;
30             number++;
31         }
32     }
33     
34     return number;
35 }

运行截图:

task 3_2.c

源代码:

 1 #include <stdio.h>
 2 #define N 100
 3 
 4 int main()
 5 {
 6     char line[N];
 7     int word_len;
 8     int max_len;
 9     int end;
10     int i;
11     
12     while(gets(line)!=NULL){
13         word_len=0;
14         max_len=0;
15         end=0;
16         
17         i=0;
18         while(1){
19             while(line[i]==' '){
20                 word_len=0;
21                 i++; 
22             }
23             
24             while(line[i]!='\0'&&line[i]!=' '){
25                 word_len++;
26                 i++;
27             }
28             
29             if(max_len<word_len){
30                 max_len=word_len;
31                 end=i;
32             }
33             
34             if(line[i]=='\0')
35                 break;
36         }
37         
38         printf("最长单词:");
39         for(i=end-max_len;i<end;++i)
40             printf("%c",line[i]);
41         printf("\n\n");
42     }
43     
44     return 0;
45 }

运行截图:

实验4

源代码:

 1 #include <stdio.h>
 2 #include <math.h>
 3 #define N 100
 4 void dec_to_n(int x,int n);
 5 
 6 int main()
 7 {
 8     int x;
 9     
10     printf("输入一个十进制整数:");
11     while(scanf("%d",&x)!=EOF){
12         dec_to_n(x,2);
13         dec_to_n(x,8);
14         dec_to_n(x,16);
15         printf("输入一个十进制整数:");
16     }
17     
18     return 0;
19 }
20 
21 void dec_to_n(int x,int n)
22 {
23     char s[10]={0};
24     int a[10];
25     int cnt=0;
26     
27     while(x>0){
28         a[cnt]=x%n;
29         x/=n;
30         cnt++;
31     }
32     
33     for(int i=0;i<cnt;i++){
34         s[i]=a[i]+48;
35         if(s[i]==':'){
36             s[i]='A';
37         }else if(s[i]==';'){
38             s[i]='B';
39         }else if(s[i]=='<'){
40             s[i]='C';
41         }else if(s[i]=='='){
42             s[i]='D';
43         }else if(s[i]=='>'){
44             s[i]='E';
45         }else if(s[i]=='?'){
46             s[i]='F';
47         }
48     }
49     
50     for(int cntt=cnt-1;cntt>=0;cntt--){
51         printf("%c",s[cntt]);
52     }
53     printf("\n");
54 }

运行截图:

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

运行截图:

实验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 void bubble_sort(char str[][M],int n);
 7 
 8 int main()
 9 {
10     char name[][M]={"Bob","Bill","Joseph","Taylor","George"};
11     int i;
12     printf("输出初始名单:\n");
13     output(name,N);
14     printf("\n排序中...\n");
15     bubble_sort(name,N);
16     printf("\n按字典序输出名单:\n");
17     output(name,N);
18     return 0;
19 }
20 
21 void output(char str[][M],int n)
22 {
23     int i;
24     for(i=0;i<n;i++)
25        printf("%s\n",str[i]);
26 }
27 
28 void bubble_sort(char str[][M],int n)
29 {
30     int round;
31     for(int cnt=0;cnt<n;cnt++){
32         round=n-cnt;
33         for(int cnt=0;cnt<round;cnt++){
34             if(strcmp(str[cnt],str[cnt+1])>0){
35                 char i[M];
36                 strcpy(i,str[cnt+1]);
37                 strcpy(str[cnt+1],str[cnt]);
38                 strcpy(str[cnt],i);
39             }
40         }
41     }
42 }

运行截图:

实验7

源代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main()
 5 {
 6     char a[110];
 7     while (scanf("%s", a) != EOF) {
 8           int flag = 0;
 9           for (int n = 0; n < strlen(a); n++) {
10               for (int s = n + 1; s < strlen(a); s++) {
11                 if (a[n] == a[s]) {
12                      flag = 1;
13                      break;
14                 }
15              }
16             if (flag == 1) {
17                  break;
18             }
19          }
20          if (flag == 1) {
21              printf("YES\n\n");
22          }
23          else if (flag == 0) {
24              printf("NO\n\n");
25          }
26      }
27      return 0;
28 }

运行截图:

实验8

源代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 100
 4 #define M 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 {
11     int t[][N] = { {21, 12, 13, 24},
12                    {25, 16, 47, 38},
13                    {29, 11, 32, 54},
14                    {42, 21, 33, 10} };
15     printf("原始矩阵:\n");
16     output(t, M); 
17     rotate_to_right(t, M); 
18     printf("变换后矩阵:\n");
19     output(t, M); 
20     return 0;
21 }
22 
23 void output(int x[][N], int n) 
24 {
25     int i, j;
26     for (i = 0; i < n; ++i) {
27         for (j = 0; j < n; ++j)
28             printf("%4d", x[i][j]);
29         printf("\n");
30    }
31 }
32  
33 void rotate_to_right(int x[][N], int n)
34 {
35     int mid[M];
36     for (int j = 0; j < n; j++) {
37         mid[j] = x[j][M - 1];
38     }
39     for (int j = 0; j < n; j++) {
40         for (int i = n-1; i >= 1; i--) {
41             x[j][i] = x[j][i - 1];
42         }
43     }
44     for (int i = 0; i < n; i++) {
45         x[i][0] = mid[i];
46     }
47 }

运行截图:

实验9

源代码:

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int a[110][110] = { 0 };
 6     int n;
 7     while (scanf("%d", &n) != EOF) {
 8         int nn = n * n;
 9         a[0][(n - 1) / 2] = 1;
10         int i, j;
11         int count = 2;
12         i = 0;
13         j = (n - 1) / 2;
14         while (count <= nn) {
15             i--;
16             j++;
17             if (i == -1) {
18                 i += n;
19             }
20             if (j == n) {
21                 j -= n;
22             }
23             if (a[i][j] != 0) {
24                 i++;
25                 j--;
26                 if (i == n) {
27                     i -= n;
28                }
29                if (j == -1) {
30                    j += n;
31                 }
32                i++;
33             }
34              
35             a[i][j] = count;
36             count++;
37     }
38      
39         for (i = 0; i < n; i++) {
40             for (j = 0; j < n; j++) {            
41                printf("%d\t", a[i][j]);
42             }
43             printf("\n");
44         }
45         int sum = 0;
46         for (i = 0; i < n; i++) {
47             sum += a[0][i];
48         }
49         printf("相同的和为:%d\n", sum);
50         for (i = 0; i < n; i++) {
51             for (j = 0; j < n; j++) {
52                 a[i][j] = 0;
53             }
54         }
55     }
56     return 0;
57 }

运行截图: