实验四

task1_1

1.源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 4
 4 
 5 void test1() {
 6     int a[N] = {1, 9, 8, 4};          
 7     int i;
 8 
 9     // 输出数组a占用的内存字节数
10     printf("sizeof(a) = %d\n", sizeof(a));
11 
12     // 输出int类型数组a中每个元素的地址、值 
13     for (i = 0; i < N; ++i)
14         printf("%p: %d\n", &a[i], a[i]);
15 
16     // 输出数组名a对应的值 
17     printf("a = %p\n", a); 
18 }
19 
20 void test2() {        
21     char b[N] = {'1', '9', '8', '4'}; 
22     int i;
23 
24     // 输出数组b占用的内存字节数
25     printf("sizeof(b) = %d\n", sizeof(b));
26 
27     // 输出char类型数组b中每个元素的地址、值 
28     for (i = 0; i < N; ++i)
29         printf("%p: %c\n", &b[i], b[i]);
30     
31     // 输出数组名b对应的值 
32     printf("b = %p\n", b); 
33 }
34 
35 int main() {
36     printf("测试1: int类型一维数组\n");
37     test1();
38 
39     printf("\n测试2: char类型一维数组\n");
40     test2();
41 
42     system("pause");
43     return 0;
44 }
45 // 函数定义
46 // 功能: 输出一个n*n的矩阵x
47 void output(int x[][N], int n) {
48     int i, j;
49     for (i = 0; i < n; ++i) {
50         for (j = 0; j < n; ++j)
51             printf("%4d", x[i][j]);
52         printf("\n");
53     }
54 }
View Code

 

2.运行结果

回答:1.是     占4个  一样

           2.是     占1个  一样

task1_2

1源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include <stdio.h>
 4 #define N 2
 5 #define M 4
 6 
 7 void test1() {
 8     int a[N][M] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
 9     int i, j;
10 
11     // 输出int类型二维数组a占用的内存字节数
12     printf("sizeof(a) = %d\n", sizeof(a));
13 
14     // 输出int类型二维数组a中每个元素的地址、值 
15     for (i = 0; i < N; ++i)
16         for (j = 0; j < M; ++j)
17             printf("%p: %d\n", &a[i][j], a[i][j]);
18     printf("\n");
19 
20     // 输出int类型二维数组名a, 以及,a[0], a[1]的值
21     printf("a = %p\n", a);
22     printf("a[0] = %p\n", a[0]);
23     printf("a[1] = %p\n", a[1]);
24     printf("\n");
25 }
26 
27 void test2() {
28     char b[N][M] = {{'1', '9', '8', '4'}, {'2', '0', '4', '9'}};
29     int i, j;
30 
31     // 输出char类型二维数组b占用的内存字节数
32     printf("sizeof(b) = %d\n", sizeof(b));
33 
34     // 输出char类型二维数组b中每个元素的地址、值
35     for (i = 0; i < N; ++i)
36         for (j = 0; j < M; ++j)
37             printf("%p: %c\n", &b[i][j], b[i][j]);
38     printf("\n");
39 
40     // 输出char类型二维数组名b, 以及,b[0], b[1]的值
41     printf("b = %p\n", b);
42     printf("b[0] = %p\n", b[0]);
43     printf("b[1] = %p\n", b[1]);
44 }
45 
46 int main() {
47     printf("测试1: int型两维数组");
48     test1();
49 
50     printf("\n测试2: char型两维数组");
51     test2();
52     system("pause");
53     return 0;
54 }
View Code

 

2.运行结果

回答 1.是     4个     一样

         2.是     1个     一样

         3.a[0]a[1]差16       b[0]b[1]差4  相差四个存放对应数据的单位

 

task2

1源代码

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

 

2运行结果

test1是一维数组,自定义函数可以直接交换;test2是二维数组[n]中数字代表存放在第n行的字符串

task3

1源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 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     system("pause");
16     return 0;
17 }
18 
19 int count(char x[]) {
20     int i;
21     int word_flag = 0;  // 用作单词标志,一个新单词开始,值为1;单词结束,值为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 }
View Code

 

2运行结果

发现问题,标点或其他符号后必须加空格,否则统计错误,修改如下

 1 #include<stdio.h>
 2 #include<stdlib.h>
 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     system("pause");
16     return 0;
17 }
18 
19 int count(char x[]) {
20     int i;
21     int word_flag = 0;  // 用作单词标志,一个新单词开始,值为1;单词结束,值为0
22     int number = 0;  // 统计单词个数
23 
24     for(i = 0; x[i] != '\0'; i++) {
25         if(x[i] == ' '||(x[i]<'A'||x[i]>'z')||(x[i]<'a'||x[i]>'z'))
26             word_flag = 0;
27         else if(word_flag == 0) {
28             word_flag = 1;
29             number++;
30         }
31     }
32 
33     return number;
34 }

在line25处做修改

task3_2

1.源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 1000
 4 
 5 int main() {
 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             // 跳过连续空格
20             while(line[i] == ' ') {
21                 word_len = 0;  // 单词长度置0,为新单词统计做准备
22                 i++;
23             }
24 
25             // 在一个单词中,统计当前单词长度
26             while(line[i] != '\0' && line[i] != ' ') {
27                 word_len++;
28                 i++;
29             }
30         
31             // 更新更长单词长度,并,记录最长单词结束位置
32             if(max_len < word_len) {
33                 max_len = word_len;
34                 end = i;   // end保存的是单词结束的下一个坐标位置
35             }
36 
37             // 遍历到文本结束时,终止循环
38             if(line[i] == '\0')
39                 break;
40         }
41 
42         // 输出最长单词
43         printf("最长单词: ");
44         for(i = end - max_len; i < end; ++i)
45             printf("%c", line[i]);
46         printf("\n\n");
47     }
48 
49     system("pause");
50     return 0;
51 }
View Code

 

2.运行结果

 

task4

1.源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 100
 4 void dec_to_n(int x, int n)
 5 {
 6     int a[N],i;
 7     for(i=0;x/n!=0;i++)
 8     {
 9         a[i]=x%n;
10         x/=n;
11     }
12     a[i]=x;
13     for(;i>=0;i--)
14     {
15         if(a[i]>=10)
16             printf("%c",a[i]+55);
17         else
18             printf("%d",a[i]);
19     }
20     printf("\n");
21 }
22 
23 int main() {
24     int x;
25 
26     printf("输入一个十进制整数: ");
27     while(scanf("%d", &x) != EOF) {
28         dec_to_n(x, 2);  // 函数调用: 把x转换成二进制输出
29         dec_to_n(x, 8);  // 函数调用: 把x转换成八进制输出
30         dec_to_n(x, 16); // 函数调用: 把x转换成十六进制输出
31 
32         printf("\n输入一个十进制整数: ");
33     }
34     system("pause");
35     return 0;
36 }

 

2.运行结果

 

task5

1.源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 5
 4 
 5 // 函数声明 
 6 void input(int x[], int n);
 7 void output(int x[], int n);
 8 double average(int x[], int n);
 9 void bubble_sort(int x[], int n);
10 
11 int main() 
12 {
13     int scores[N];
14     double ave;
15     
16     printf("录入%d个分数:\n", N);
17     input(scores, N);
18     
19     printf("\n输出课程分数: \n");
20     output(scores, N);
21     
22     printf("\n课程分数处理: 计算均分、排序...\n");
23     ave = average(scores, N);
24     bubble_sort(scores, N);
25     
26     printf("\n输出课程均分: %.2f\n", ave);
27     printf("\n输出课程分数(高->低):\n");
28     output(scores, N);
29     system("pause");
30     return 0;
31 }
32 
33 // 函数定义
34 // 输入n个整数保存到整型数组x中 
35 void input(int x[], int n) 
36 {
37     int i;
38     
39     for(i = 0; i < n; ++i)
40         scanf("%d", &x[i]); 
41 }
42 
43 // 输出整型数组x中n个元素 
44 void output(int x[], int n) 
45 {
46     int i;
47     
48     for(i = 0; i < n; ++i)
49         printf("%d ", x[i]);
50     printf("\n");
51 }
52 
53 // 计算整型数组x中n个元素均值,并返回 
54 // 补足函数average()实现
55 // ××× 
56 double average(int x[], int n)
57 {
58     double ave,s=0;
59     for(int i=0;i<n;i++)
60         s+=x[i];
61     ave=s/n;
62     return ave;
63 }
64 // 对整型数组x中的n个元素降序排序 
65 // 补足函数bubble_sort()实现
66 // ××× 
67 void bubble_sort(int x[], int n)
68 {
69     int j,i,tem;
70     for (i = 0; i < n-1;i ++)
71     {
72         int count=0;
73         for (j=0;j<n-1-i;j++)
74         {
75             if (x[j]<x[j+1])
76             {
77                 tem = x[j];
78                 x[j+1] = tem;
79                 count = 1;
80             }
81         }
82         if (count == 0)
83                 break;    
84     }
85 }

2.运行结果

 

task6

1.源代码

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

 

2.运行结果

 

task7

1.源代码

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

 

2.运行结果

 

task8

1.源代码

 1 #include<stdio.h>
 2 #include<stdlib.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 int main() {
 8     int t[][N] = {{21, 12, 13, 24},
 9                   {25, 16, 47, 38},
10                   {29, 11, 32, 54},
11                   {42, 21, 33, 10}};
12     printf("原始矩阵:\n");
13     output(t, M);
14     rotate_to_right(t, M);
15     printf("变换后矩阵:\n");
16     output(t, M);
17     system("pause");
18     return 0;
19 }
20 // 函数定义
21 // 功能: 输出一个n*n的矩阵x
22 void output(int x[][N], int n) {
23     int i, j;
24     for (i = 0; i < n; ++i) {
25         for (j = 0; j < n; ++j)
26             printf("%4d", x[i][j]);
27         printf("\n");
28     }
29 }
30 // 待补足3:函数rotate_to_right()定义
31 void rotate_to_right(int x[][N], int n)
32 {
33     int y[N][N];
34     for(int i=0;i<n;i++)
35         for(int j=0;j<n;j++)
36             y[i][j]=x[i][j];
37     for(int i=n-1;i>0;i--)
38     {
39         for(int j=0;j<n;j++)
40             x[j][i]=y[j][i-1];
41     }
42     for(int j=0;j<n;j++)
43             x[j][0]=y[j][n-1];
44 }

 

2.运行结果

 

task9

1.源代码

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     int n;
 6     while(scanf("%d",&n)!=EOF)
 7     {
 8         int she[100][100]={0};
 9         int j=n/2,k=0,i=1,a,b;
10         while(1)
11         {
12             if(k<0)
13                 k=n-1;
14             if(j>n-1)
15                 j=0;
16             if(she[k][j]!=0)
17             {
18                 she[a][b]=i;
19                 k=a,j=b;
20             }
21             else
22                 she[k][j]=i;
23             a=k+1,b=j;
24             k--;
25             j++;
26             i++;
27             if(she[n-1][n/2]==n*n)
28                 break;
29         }
30         for(int x=0;x<n;x++)
31         {
32             for(int y=0;y<n;y++)
33                 printf("%4d",she[x][y]);
34             printf("\n");
35         }
36         printf("\n");
37     }
38     system("pause");
39     return 0;
40 }

 

2.运行结果

 

 

posted @ 2023-11-13 17:15  JUVBuffon  阅读(32)  评论(0)    收藏  举报