实验四

task1_1.c源代码

 1 #include <stdio.h>
 2 #define N 4
 3 void test1() {
 4     int a[N] = { 1, 9, 8, 4 };
 5     int i;
 6     // 输出数组a占用的内存字节数
 7     printf("sizeof(a) = %d\n", sizeof(a));
 8     // 输出int类型数组a中每个元素的地址、值
 9     for (i = 0; i < N; ++i)
10         printf("%p: %d\n", &a[i], a[i]);
11     // 输出数组名a对应的值
12     printf("a = %p\n", a);
13 }
14 void test2() {
15     char b[N] = { '1', '9', '8', '4' };
16     int i;
17     // 输出数组b占用的内存字节数
18     printf("sizeof(b) = %d\n", sizeof(b));
19     // 输出char类型数组b中每个元素的地址、值
20     for (i = 0; i < N; ++i)
21         printf("%p: %c\n", &b[i], b[i]);
22     // 输出数组名b对应的值
23     printf("b = %p\n", b);
24 }
25 int main() {
26     printf("测试1: int类型一维数组\n");
27     test1();
28     printf("\n测试2: char类型一维数组\n");
29     test2();
30     return 0;
31 }

运行结果

 回答问题

① int型数组a,在内存中是否是连续存放的?每个元素占用几个内存字节单元? 数组名a对应
的值,和&a[0]是一样的吗?
是;4;一样
② char型数组b,在内存中是否是连续存放的?每个元素占用几个内存字节单元? 数组名b对
应的值,和&b[0]是一样的吗?
是;1;一样
task1_2.c源代码
 1 #include <stdio.h>
 2 #define N 2
 3 #define M 4
 4 void test1() {
 5     int a[N][M] = { {1, 9, 8, 4}, {2, 0, 4, 9} };
 6     int i, j;
 7     // 输出int类型二维数组a占用的内存字节数
 8     printf("sizeof(a) = %d\n", sizeof(a));
 9     // 输出int类型二维数组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     // 输出int类型二维数组名a, 以及,a[0], a[1]的值
15     printf("a = %p\n", a);
16     printf("a[0] = %p\n", a[0]);
17     printf("a[1] = %p\n", a[1]);
18     printf("\n");
19 }
20 void test2() {
21     char b[N][M] = { {'1', '9', '8', '4'}, {'2', '0', '4', '9'} };
22     int i, j;
23     // 输出char类型二维数组b占用的内存字节数
24     printf("sizeof(b) = %d\n", sizeof(b));
25     // 输出char类型二维数组b中每个元素的地址、值
26     for (i = 0; i < N; ++i)
27         for (j = 0; j < M; ++j)
28             printf("%p: %c\n", &b[i][j], b[i][j]);
29     printf("\n");
30     // 输出char类型二维数组名b, 以及,b[0], b[1]的值
31     printf("b = %p\n", b);
32     printf("b[0] = %p\n", b[0]);
33     printf("b[1] = %p\n", b[1]);
34 }
35 int main() {
36     printf("测试1: int型两维数组");
37     test1();
38     printf("\n测试2: char型两维数组");
39     test2();
40     return 0;
41 }

运行结果

 回答问题

① int型二维数组a,在内存中是否是"按行连续存放"的?每个元素占用几个内存字节单元?
数组名a的值、a[0]的值、&a[0][0]的值,在数字字面值上,是一样的吗?
是;4;一样
② char型二维数组b,在内存中是否是"按行连续存放"的?每个元素占用几个内存字节单元?
数组名b的值、b[0]的值、&b[0][0]的值,在数字字面值上,是一样的吗?
是;1;一样
③ 对于二维数组, 观察a[0], a[1]的值,它们之间相差多少?观察b[0]和b[1]的值,它们之间
相差多少?有什么规律吗?
16;4;差的是一行的值而一行有四列

task.2c源码

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

运行结果

 思考

函数模块swap_str()的形参是一维数组。为什么test1()和test2()模块中调用时,实参的书写形
式一个不加[]、一个加[]?
    test1中是一维数组,test2中是二维数组。
类似地,test1()和test2()模块中,调用标准库函数puts()实现输出时,实参书写形式也类同。
思考并归纳、总结其用法
task3_1.c源代码
 1 #include <stdio.h>
 2 #define N 80
 3 int count(char x[]);
 4 int main() {
 5     char words[N + 1];
 6     int n;
 7     while (gets(words) != NULL) {
 8         n = count(words);
 9         printf("单词数: %d\n\n", n);
10     }
11     return 0;
12 }
13 int count(char x[]) {
14     int i;
15     int word_flag = 0; // 用作单词标志,一个新单词开始,值为1;单词结束,值为0
16     int number = 0; // 统计单词个数
17     for (i = 0; x[i] != '\0'; i++) {
18         if (x[i] == ' ')
19             word_flag = 0;
20         else if (word_flag == 0) {
21             word_flag = 1;
22             number++;
23         }
24     }
25     return number;
26 }

运行结果

 task3_2.c源代码

 1 #include <stdio.h>
 2 #define N 1000
 3 int main() {
 4 char line[N];
 5 int word_len; // 记录当前单词长度
 6 int max_len; // 记录最长单词长度
 7 int end; // 记录最长单词结束位置
 8 int i;
 9 while(gets(line) != NULL) {
10 word_len = 0;
11 max_len = 0;
12 end = 0;
13 i = 0;
14 while(1) {
15 // 跳过连续空格
16 while(line[i] == ' ') {
17 word_len = 0; // 单词长度置0,为新单词统计做准备
18 i++;
19 }
20 // 在一个单词中,统计当前单词长度
21 while(line[i] != '\0' && line[i] != ' ') {
22 word_len++;
23 i++;
24 }
25 // 更新更长单词长度,并,记录最长单词结束位置
26 if(max_len < word_len) {
27 max_len = word_len;
28 end = i; // end保存的是单词结束的下一个坐标位置
29 }
30 // 遍历到文本结束时,终止循环
31 if(line[i] == '\0')
32 break;
33 }
34 // 输出最长单词
35 printf("最长单词: ");
36 for(i = end - max_len; i < end; ++i)
37 printf("%c", line[i]);
38 printf("\n\n");
39 }
40 return 0;
41 }

运行结果

 task4.c源代码

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #define N 100
 4 void dec_to_n(int x, int n); // 函数声明
 5 int main() {
 6     int x;
 7     printf("输入一个十进制整数: ");
 8     while (scanf("%d", &x) != EOF) {
 9         dec_to_n(x, 2); // 函数调用: 把x转换成二进制输出
10         dec_to_n(x, 8); // 函数调用: 把x转换成八进制输出
11         dec_to_n(x, 16); // 函数调用: 把x转换成十六进制输出
12         printf("\n输入一个十进制整数: ");
13     }
14     return 0;
15 }
16 // 函数定义
17 void dec_to_n(int x, int n)
18 {
19     int i=0, b;
20     char map[] = "0123456789ABCDEF";
21     int ans[N];
22     do
23     {
24         b = x % n;
25         ans[i++] = map[b];
26         x = x / n;
27     } while (x != 0);
28     for (int j=i - 1; j >= 0; --j)
29         printf("%c", ans[j]);
30     printf("\n");
31 
32 }
33 // 功能: 把十进制数x转换成n进制,打印输出
34 // 补足函数实现
35 // ×××

运行结果

 

task5.c源代码

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #define N 5
 4 // 函数声明
 5 void input(int x[], int n);
 6 void output(int x[], int n);
 7 double average(int x[], int n);
 8 void bubble_sort(int x[], int n);
 9 int main() {
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 // 输入n个整数保存到整型数组x中
26 void input(int x[], int n) {
27     int i;
28     for (i = 0; i < n; ++i)
29         scanf("%d", &x[i]);
30 }
31 // 输出整型数组x中n个元素
32 void output(int x[], int n) {
33     int i;
34     for (i = 0; i < n; ++i)
35         printf("%d ", x[i]);
36     printf("\n");
37 }
38 // 计算整型数组x中n个元素均值,并返回
39 // 补足函数average()实现
40 double average(int x[], int n)
41 {
42     int sum = 0;
43     for (int i = 0; i < n; ++i)
44         sum += x[i];
45     double average = 0;
46     average = sum / n;
47     return average;
48 }
49 // 对整型数组x中的n个元素降序排序
50 // 补足函数bubble_sort()实现
51 void bubble_sort(int x[], int n)
52 {
53     int a = 0; int j;
54     int t,temp;
55     for (int i = 0; x[i] != '\0'; ++i)
56         a++;
57     for (j = 0; j < a - 1; j++)
58     {
59         for (t = 0; t < a - 1 - j; ++t)
60         {
61             if (x[t] < x[t + 1])
62             {
63                 temp = x[t];
64                 x[t] = x[t + 1];
65                 x[t + 1] = temp;
66             }
67 
68 
69         }
70     }
71 }

运行结果

 task6.c源代码

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <string.h>
 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 int main() {
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 // 功能:按行输出二维数组中的字符串
22 void output(char str[][M], int n) {
23     int i;
24     for (i = 0; i < n; ++i)
25         printf("%s\n", str[i]);
26 }
27 // 函数定义
28 // 功能:使用冒泡排序算法对二维数组str中的n个字符串按字典序排序
29 // 补足函数bubble_sort()实现
30 // ×××
31 void bubble_sort(char str[][M], int n)
32 {
33     int j, i;
34     char temp[10000];
35     for (j = 0; j < n - 1; j++)
36     {
37         for (i = 0; i < n - 1 - j; ++i)
38         {
39 
40             if (strcmp(str[i], str[i + 1]) > 0)
41             {
42                 strcpy(temp, str[i]);
43                 strcpy(str[i], str[i + 1]);
44                 strcpy(str[i + 1], temp);
45             }
46 
47 
48         }
49 
50     }
51 }

运行结果

 task7.c源代码 

 1 #include <stdio.h>
 2 #include <string.h>
 3 int chongfu(char a[],int n);
 4 #define N 100
 5 int main()
 6 {
 7     char a[N];
 8     while(gets_s(a)!=NULL)
 9     {
10         if(chongfu(a,strlen(a))==1)        
11             printf("YES\n");
12         else
13             printf("NO\n");
14     
15     }
16 }
17 int chongfu(char a[],int n)
18 {
19     int re=0,len;
20     len=strlen(a);
21     for(int i=0;i<len;i++)
22     {int j=i+1;
23         for(;j<len;++j)
24         {
25             if(a[i]==a[j]){
26                 re=1;
27                 break;        }    
28         }
29         if(re==1)
30         break;
31     }
32     if(re==1)
33         return 1;
34     else
35         return 0;
36 }

运行结果

 task8.c源代码

 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 // 功能: 输出一个n*n的矩阵x
20 void output(int x[][N], int n) {
21     int i, j;
22     for (i = 0; i < n; ++i) {
23         for (j = 0; j < n; ++j)
24             printf("%4d", x[i][j]);
25         printf("\n");
26     }
27 }
28 void rotate_to_right(int x[][N], int n)
29 {
30     int i, j;
31     int a[N];
32     for (i = 0; i < n; ++i)
33     {
34         a[i] = x[i][n - 1];
35         for (j = 1; j < n; j++)
36         {
37             x[i][j] = x[i][j - 1];
38         }
39         x[i][0] = a[i];
40     }
41 }

运行结果

 

 

posted @ 2023-11-16 17:36    阅读(15)  评论(0)    收藏  举报