实验4

taks1

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

 

int连续存放 占用4字节 一样

char连续存放 占用1字节 一样

task1.2

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

1 是按行连续存放的 占用4个字节 a a[0] &a[0][0] 字面值相同

2是按行连续存放的 占用一个字节 b b[0] &b[0][0]字面值相同

3int相差4 char相差1 每行相邻元素地址存放相差一个元素的内存大小。

task2

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

 

test1 是对两个一维数组进行调用而test2调用的是一个二维数组的两行,需要加[0][1]区分是第一行还是第二行。

总结 对一维数组调用时可以省略[]对二维数组调用时应该加上[]区分是哪一行

 1 /* 
 2 从键盘输入一行英文文本,统计英文单词总数
 3 为了简化问题处理,只考虑单词以空格间隔的情形
 4 对教材例5.22代码做了些微改动:
 5 1. 统计单词个数,编写成函数模块;增加了多组输入
 6 2. 去掉了不必要的中间变量
 7 */
 8 
 9 #include <stdio.h>
10 
11 #define N 80
12 
13 int count(char x[]);
14 
15 int main() {
16     char words[N+1];
17     int n;
18 
19     while(gets(words) != NULL) {
20         n = count(words);
21         printf("单词数: %d\n\n", n);
22     }
23 
24     return 0;
25 }
26 
27 int count(char x[]) {
28     int i;
29     int word_flag = 0;  // 用作单词标志,一个新单词开始,值为1;单词结束,值为0
30     int number = 0;  // 统计单词个数
31 
32     for(i = 0; x[i] != '\0'; i++) {
33         if(x[i] == ' ')
34             word_flag = 0;
35         else if(word_flag == 0) {
36             word_flag = 1;
37             number++;
38         }
39     }
40 
41     return number;
42 }

 

task3.2

 1 /*
 2 输入一行英文文本,统计最长单词,并打印输出。
 3 为简化问题,只考虑单词之间用空格间隔的情形。
 4 相较于教材例5.24,做了以下改动:
 5 1. 增加了多组输入,因此,一些变量初始化放到了第一层循环里面
 6 2. 微调了代码书写逻辑和顺序
 7 */
 8 
 9 #include <stdio.h>
10 #define N 1000
11 
12 int main() {
13     char line[N];
14     int word_len;   // 记录当前单词长度
15     int max_len;    // 记录最长单词长度
16     int end;        // 记录最长单词结束位置
17     int i;
18 
19     while(gets(line) != NULL) {
20         word_len = 0;
21         max_len = 0;
22         end = 0;
23 
24         i = 0;
25         while(1) {
26             // 跳过连续空格
27             while(line[i] == ' ') {
28                 word_len = 0;  // 单词长度置0,为新单词统计做准备
29                 i++;
30             }
31 
32             // 在一个单词中,统计当前单词长度
33             while(line[i] != '\0' && line[i] != ' ') {
34                 word_len++;
35                 i++;
36             }
37         
38             // 更新更长单词长度,并,记录最长单词结束位置
39             if(max_len < word_len) {
40                 max_len = word_len;
41                 end = i;   // end保存的是单词结束的下一个坐标位置
42             }
43 
44             // 遍历到文本结束时,终止循环
45             if(line[i] == '\0')
46                 break;
47         }
48 
49         // 输出最长单词
50         printf("最长单词: ");
51         for(i = end - max_len; i < end; ++i)
52             printf("%c", line[i]);
53         printf("\n\n");
54     }
55 
56     return 0;
57 }

task4

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 100
 4 void dec_to_n(int x, int n); // 函数声明
 5 
 6 int main() {
 7     int x;
 8 
 9     printf("输入一个十进制整数: ");
10     while(scanf("%d", &x) != EOF) {
11         dec_to_n(x, 2);  // 函数调用: 把x转换成二进制输出
12         dec_to_n(x, 8);  // 函数调用: 把x转换成八进制输出
13         dec_to_n(x, 16); // 函数调用: 把x转换成十六进制输出
14 
15         printf("\n输入一个十进制整数: ");
16     }
17 
18     return 0;
19 }
20 
21 // 函数定义
22 // 功能: 把十进制数x转换成n进制,打印输出
23 // 补足函数实现
24 // ×××
25 void dec_to_n(int x, int n){
26     int s[10] = {0};
27     int i = 0;
28     
29     if(n == 2){
30         while(x != 0){
31             s[i] = x % 2;
32             x = x / 2;
33             i++;
34         } 
35         i = i - 1;
36         while(i > -1){
37             printf("%d",s[i]);
38             i--;
39         }
40         printf("\n");
41         memset(s,0,sizeof(s));
42         return ;
43     }
44     if(n == 8){
45         while(x != 0){
46             s[i] = x % 8;
47             x = x / 8;
48             i++;
49         } 
50         i = i - 1;
51         while(i > -1){
52             printf("%d",s[i]);
53             i--;
54         }
55         printf("\n");
56         memset(s,0,sizeof(s));
57         return ;
58     }
59     if(n == 16){
60         while(x != 0){
61             s[i] = x % 16;
62             x = x / 16;
63             i++;
64         } 
65         i = i - 1;
66         while(i > -1){
67             if(s[i] < 10)
68             printf("%d",s[i]);
69             if(s[i] == 10)
70             printf("A");
71             if(s[i] == 11)
72             printf("B");
73             if(s[i] == 12)
74             printf("C");
75             if(s[i] == 13)
76             printf("D");
77             if(s[i] == 14)
78             printf("E");
79             if(s[i] == 15)
80             printf("F");
81             i--;
82         }
83         printf("\n");
84         memset(s,0,sizeof(s));
85         return ;    
86     }
87     
88 
89 } 

task5

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

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 #define M 20
 5 
 6 // 函数声明
 7 void output(char str[][M], int n);
 8 void bubble_sort(char str[][M], int n);
 9 
10 int main() {
11     char name[][M] = {"Bob", "Bill", "Joseph", "Taylor", "George"};
12     int i;
13 
14     printf("输出初始名单:\n");
15     output(name, N);
16 
17     printf("\n排序中...\n");
18     bubble_sort(name, N);  // 函数调用 
19 
20     printf("\n按字典序输出名单:\n");
21     output(name, N);
22 
23     return 0;
24 }
25 
26 // 函数定义
27 // 功能:按行输出二维数组中的字符串
28 void output(char str[][M], int n) {
29     int i;
30 
31     for(i = 0; i < n; ++i)
32         printf("%s\n", str[i]);
33 }
34 
35 // 函数定义
36 // 功能:使用冒泡排序算法对二维数组str中的n个字符串按字典序排序
37 // 补足函数bubble_sort()实现 
38 // ×××
39 void bubble_sort(char str[][M], int n){
40     int i,j;
41     char s[M];
42     for(i = 0;i < n-1;i++)
43         for(j = 0;j < n-1-i;j++)
44              if(strcmp(str[j],str[j+1]) >= 0){
45                  strcpy(s,str[j]);
46                 strcpy(str[j],str[j+1]);
47                 strcpy(str[j+1],s);    
48              }
49              return ;
50 }

 task7

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

task8

 1 #include <stdio.h>
 2 #define N 100
 3 #define M 4
 4 
 5 // 函数声明
 6 void output(int x[][N], int n);          
 7 void rotate_to_right(int x[][N], int n); 
 8 
 9 
10 int main() {
11     int t[][N] = {{21, 12, 13, 24},
12                   {25, 16, 47, 38},
13                   {29, 11, 32, 54},
14                   {42, 21, 33, 10}};
15 
16     printf("原始矩阵:\n");
17     output(t, M); // 函数调用
18 
19     rotate_to_right(t, M); // 函数调用
20 
21     printf("变换后矩阵:\n");
22     output(t, M); // 函数调用
23 
24     return 0;
25 }
26 
27 // 函数定义
28 // 功能: 输出一个n*n的矩阵x
29 void output(int x[][N], int n) {
30     int i, j;
31 
32     for (i = 0; i < n; ++i) {
33         for (j = 0; j < n; ++j)
34             printf("%4d", x[i][j]);
35 
36         printf("\n");
37     }
38 }
39 
40 // 待补足3:函数rotate_to_right()定义
41 // 功能: 把一个n*n的矩阵x,每一列向右移, 最右边被移出去的一列绕回左边
42 // xxx
43 void rotate_to_right(int x[][N], int n){
44     int i,j;
45     int y[n][n];
46     
47     for(i = 0;i <= n-1;i++)
48         for(j = 1;j <= n-1;j++)
49              y[i][j] = x[i][j-1];
50              
51     for(i = 0;i <= n-1;i++)
52         y[i][0] = x[i][n-1];
53     
54     for(i = 0;i <= n-1;i++)
55         for(j = 0;j <= n-1;j++)
56         x[i][j] = y[i][j];
57         return ;
58 }

task9

 1 #include<stdio.h>
 2 void m(int s[][10],int n);
 3 int main(){
 4     int s[10][10] = {0};
 5     int n,i,j,sum;
 6     
 7     while(scanf("%d",&n) != EOF){
 8         m(s,n);    
 9         sum = 0;
10         
11         for(i = 0;i <= n-1;i++){
12             printf("\n");
13             for(j = 0;j <= n-1;j++)
14                 printf("%d ",s[i][j]);
15         }
16         
17         printf("\n");
18         for(i = 0;i <= n-1;i++)
19             sum = sum + s[0][i];
20         
21         printf("每行、每列、每条对角线上的和为:%d\n",sum);
22         
23         for(i = 0;i <= n-1;i++)
24             for(j = 0;j <= n-1;j++)
25                 s[i][j] = 0;
26     }
27     return 0;
28 }
29 void m(int s[][10],int n){
30     int i,x,y,x0,y0;
31     
32     s[0][n/2] = 1;
33     x = 0,y= n / 2;
34     for(i = 2;i <= n*n;i++){
35         x0 = x,y0 = y;
36         if(x == 0&&y == n-1)
37         {
38             x = n-1;
39             y = 0;
40         }
41         else if(y == n-1){
42             y = 0;
43             x--;
44         }
45         else if(x == 0){
46             x = n-1;
47             y++;
48         }
49         else{
50             x--;
51             y++;
52         }
53         if(s[x][y] != 0){
54             x = x0 + 1;
55             y = y0;
56         }
57         s[x][y] = i;
58     }
59 }

 

posted @ 2023-11-13 15:56  sad&bule  阅读(36)  评论(0)    收藏  举报