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

发布时间 2023-11-17 16:35:20作者: zxy溢

1.task_1

(1)

 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     printf("sizeof(a)=%d\n",sizeof(a));
 9     for (i=0;i<N;++i)
10         printf("&p:%d\n",&a[i],a[i]);
11     printf("a=%p\n",a);
12 }
13 
14 void test2()
15 {
16     char b[N]={'1','9','8','4'};
17     int i;
18     printf("sizeof(b)=%d\n",sizeof(b));
19     for (i=0;i<N;++i)
20         printf("%p:%c\n",&b[i],b[i]);
21     printf("b=%p\n",b);
22 }
23 
24 int main()
25 {
26     printf("测试1:int类型一维数组\n");
27     test1();
28     printf("测试2:char类型一维数组\n");
29     test2();
30     
31     return 0;
32 }

①答:连续存放。占用4个。一样。

②答:连续存放。占用1个。一样。

(2)

 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     printf("sizeof(a)=%d\n",sizeof(a));
10     for (i=0;i<N;++i)
11         for (j=0;j<M;j++)
12             printf("%p:%d\n",&a[i][j],a[i][j]);
13     printf("\n");
14     printf("a=%p\n",a);
15     printf("a[0]=%p\n",a[0]);
16     printf("a[1]=%p\n",a[1]);
17     printf("\n");
18 }
19 
20 void test2()
21 {
22     char b[N][M]={{'1','9','8','4'},{'2','0','4','9'}};
23     int i,j;
24     printf("sizeof(b)=%d\n",sizeof(b));
25     for (i=0;i<N;++i)
26         for (j=0;j<M;++j)
27             printf("%p:%c\n",&b[i][j],b[i][j]);
28     printf("\n");
29     printf("b=%p\n",b);
30     printf("b[0]=%p\n",b[0]);
31     printf("b[1]=%p\n",b[1]);
32 }
33 
34 int main()
35 {
36     printf("测试1:int型两维数组");
37     test1();
38     printf("测试2:char型两维数组");
39     test2();
40      
41     return 0;
42 }

①答:连续存放。占用4个。不一样。

②答:连续存放。占用1个。不一样。

③答:a[0]与a[1]相差4。b[0]与b[1]相差1。都为一个定义类型的长度。

 

2.task_2

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 80
 4 
 5 void swap_str(char s1[N],char s2[N]);
 6 void test1();
 7 void test2();
 8 
 9 int main()
10 {
11     printf("测试1:用两个一维char数组,实现两个字符串交换\n");
12     test1();
13     printf("测试2:用二维char数组,实现两个字符串交换\n");
14     test2();
15     
16     return 0;
17 }
18 
19 void test1()
20 {
21     char views1[N]="hey,C,I,hate u.";
22     char views2[N]="hey,C,I love u.";
23     printf("交换前:\n");
24     puts(views1);
25     puts(views2);
26     swap_str(views1,views2);
27     printf("交换后:\n");
28     puts(views1);
29     puts(views2);
30 } 
31  
32 void test2()
33 {
34     char views[2][N]={"hey,C,I hate,u.","hey,C,I love u."};
35     printf("交换前:\n");
36     puts(views[0]);
37     puts(views[1]);
38     swap_str(views[0],views[1]);
39     printf("交换后:\n");
40     puts(views[0]);
41     puts(views[1]);
42 }
43 
44 void swap_str(char s1[N],char s2[N])
45 {
46     char tmp[N];
47     strcpy(tmp,s1);
48     strcpy(s1,s2);
49     strcpy(s2,tmp);
50 }

①答:test1()模块为一维数组的函数,数组名即位置;test2()模块为二维数组的函数,需加上[]来确定位置。

②答:puts()输出该数组名下的全部数据。

 

3.task_3

(1)

 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     while (gets(words)!=NULL)
11     {
12         n=count(words);
13         printf("单词数:%d\n\n",n);
14     }
15     return 0;
16 }
17 
18 int count(char x[])
19 {
20     int i;
21     int word_flag=0;
22     int number=0;
23     for (i=0;x[i]!='\0';i++)
24     {
25         if (x[i]==' ')
26             word_flag=0;
27         else if (word_flag==0)
28         {
29             word_flag=1;
30             number++;
31         }
32     }
33     return number;
34 }

(2)

 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     while (gets(line)!=NULL)
12     {
13         word_len=0;
14         max_len=0;
15         end=0;
16         i=0;
17         while (1)
18         {
19             while (line[i]==' ')
20             {
21                 word_len=0;
22                 i++;
23             }
24             while (line[i]!='\0'&&line[i]!=' ')
25             {
26                 word_len++;
27                 i++;
28             }
29             if (max_len<word_len)
30             {
31                 max_len=word_len;
32                 end=i;
33             }
34             if (line[i]=='\0')
35                 break;
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.task_4

 1 #include <stdio.h>
 2 #define N 100
 3 
 4 void dec_to_n(int x,int n);
 5 
 6 int main()
 7 {
 8     int x;
 9     printf("输入一个十进制整数:");
10     while (scanf("%d",&x)!=EOF)
11     {
12         dec_to_n(x,2);
13         dec_to_n(x,8);
14         dec_to_n(x,16);
15         printf("\n输入一个十进制整数:");
16     }
17     return 0;
18 }
19 
20 void dec_to_n(int x,int n)
21 {
22     int i=0,count=0,t;
23     char list[N]={0},map[16]={"0123456789ABCDEF"};
24     do
25     {
26         list[i]=map[x%n];
27         x/=n;
28         i++;
29         count++;
30     }while (x!=0);
31     for (i=count-1;i>=0;i--)
32     {
33         printf("%c",list[i]);
34     }
35     printf("\n");
36 }

 

5.task_5

 

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

 

6.task_6

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

 

7.task_7

 1 #include <stdio.h>
 2 #define N 100
 3 
 4 void judge(char str[N]);
 5 
 6 int main()
 7 {
 8     char list[N];
 9     while (gets(list)!=NULL)
10         judge(list);
11     
12     return 0;
13 }
14 
15 void judge(char str[N])
16 {
17     int i,k,flag=0;
18     for (i=0;str[i]!='\0';++i)
19     {
20         for (k=i+1;str[k]!='\0';++k)
21         {
22             if (str[i]==str[k])
23                 flag=1;
24         } 
25     }
26     if (flag==1)
27         printf("YES\n");
28     else
29         printf("NO\n");
30     printf("\n");
31 }

 

8.task_8

 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 int main()
 9 {
10     int t[][N]={{21,12,13,24},{25,16,47,38},{29,11,32,54},{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     
17     return 0;
18 }
19 
20 void output(int x[][N],int n)
21 {
22     int i,j;
23     for (i=0;i<n;++i)
24     {
25         for (j=0;j<n;++j)
26             printf("%4d",x[i][j]);
27         printf("\n");
28     }
29 }
30 
31 void rotate_to_right(int x[][N],int n)
32 {
33     int list[n];
34     int i,k;
35     for (i=0;i<n;++i)
36         list[i]=x[i][n-1];
37     for (i=0;i<n;++i)
38     {
39         for (k=n-1;k>0;--k)
40         {
41             x[i][k]=x[i][k-1];
42         }
43     }
44     for (i=0;i<n;++i)
45         x[i][0]=list[i];
46 }