• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
nuist0415
博客园    首页    新随笔    联系   管理    订阅  订阅
实验四

task1_1 code

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

task1_1 result

a数组是连续存放的,每个元素占4个字节,a与&a[0]一样。

b数组是连续存放的,每个元素占1个字节,b与&b[o]一样。

task1_2 code

 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 }
View Code

task1_2 result

1.是连续存放的,每个占4字节,一样。

2.是连续存放的,每个占1字节,一样。

3.a差16个字节,b差4个字节,都相差一行的字节总数。

task2 code

 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 }
View Code

task2 result

 1.不加【】是第一个一维数组第一个char的地址,加【】是 一个二维数组第一行的第一个char的地址。传递的都是地址。

task3_1 code

 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 
26     return number;
27 }
View Code

task3_1 result

 

task3_2 code

 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 }
View Code

task3_2 result

task4 code

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

task4 result

task5 code

 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     printf("录入%d个分数:\n", N);
12     input(scores, N);
13     printf("\n输出课程分数: \n");
14     output(scores, N);
15     printf("\n课程分数处理: 计算均分、排序...\n");
16     ave = average(scores, N);
17     bubble_sort(scores, N);
18     printf("\n输出课程均分: %.2f\n", ave);
19     printf("\n输出课程分数(高->低):\n");
20     output(scores, N);
21     return 0;
22 }
23 
24 // 输入n个整数保存到整型数组x中
25 void input(int x[], int n) {
26     int i;
27     for (i = 0; i < n; ++i)
28         scanf("%d", &x[i]);
29 }
30 // 输出整型数组x中n个元素
31 void output(int x[], int n) {
32     int i;
33     for (i = 0; i < n; ++i)
34         printf("%d ", x[i]);
35     printf("\n");
36 }
37 // 计算整型数组x中n个元素均值,并返回
38 double average(int x[], int n) {
39     double ave = 0;
40 
41     for (int i = 0; i < 5; i++)
42         ave += x[i];
43 
44     return ave / 5;
45 }
46 // 对整型数组x中的n个元素降序排序
47 void bubble_sort(int x[], int n) {
48     int i, j, t;
49 
50     for (i = 0; i < n - 1; i++)
51         for (j = 0; j < n - 1 - i; j++)
52             if (x[j] < x[j + 1]) {
53                 t = x[j];
54                 x[j] = x[j + 1];
55                 x[j + 1] = t;
56             }
57 }
58 */
View Code

task5 result

task6 code

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 #define M 20
 5 
 6 void output(char str[][M], int n);
 7 void bubble_sort(char str[][M], int n);
 8 
 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 // 功能:使用冒泡排序算法对二维数组str中的n个字符串按字典序排序
28 void bubble_sort(char str[][M], int n) {
29     int i, j, t;
30     char ex[M] = { '0' };
31     int a;
32 
33     for (i = 0; i < n - 1; i++)
34         for (j = 0; j < n - 1 - i; j++)
35             if (strcmp(str[j], str[j + 1]) > 0) {
36 
37 
38                 for (a = 0; a < strlen(str[j]); a++) //confi
39                     ex[a] = str[j][a];
40                 
41                 for (a = 0; a < M; a++) //refresh
42                     str[j][a] = '\0';
43                 
44                 for (a = 0; a < strlen(str[j + 1]); a++)
45                     str[j][a] = str[j + 1][a];
46                 for (a = 0; a < strlen(ex); a++)
47                     str[j + 1][a] = ex[a];
48 
49                 for (a = 0; a < M; a++) //refresh
50                     ex[a] = '\0';
51 
52             }
53 }
View Code

task6 result

 

task7 code

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

task7 result

task8 code

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

task8 result

 

posted on 2023-11-13 19:59  Haruto  阅读(18)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3