实验四

task1-1.c
 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 }
View Code

 int一维数组连续存放,站四个字节,一样

char一维数组连续存放,占一个字节,一样

int二维数组连续存放,站四个字节,一样

char二维数组连续存放,占一个字节,一样

相差16个字节,相差4个字节

相差的都是第一行元素所占内存

 

task1-2.c

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

 

tast2.c

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

 

tast3-1.c

 1 #include <stdio.h>
 2 
 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 
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

tast3-2.c

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

tast4.c

 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);  // 函数调用: 把x转换成二进制输出
11         dec_to_n(x, 8);  // 函数调用: 把x转换成八进制输出
12         dec_to_n(x, 16); // 函数调用: 把x转换成十六进制输出
13 
14         printf("\n输入一个十进制整数: ");
15     }
16 
17     return 0;
18 }
19 
20 void dec_to_n(int x, int n)
21 {
22     int a[N],i,j;
23     
24     for(i=0;i<=100;i++)
25     {
26         a[i]=x%n;
27         x/=n;
28         if(x==0)
29         break;
30         
31         
32     }
33     for(j=i;j>=0;j--)
34     {
35         if(a[j]<10)
36         printf("%d",a[j]);
37         else if(a[j]>=10)
38         printf("%c",'A'+a[j]-10);
39         
40         
41     }
42     printf("\n");
43     
44  } 
View Code

 

task5.c

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

task6.c

 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 double average(int x[], int n)
50 {
51     int i;
52     double sum;
53     for(i=0;i<N;i++)
54     {
55         sum+=x[i];
56     }
57     return sum/N;
58 } 
59 
60 
61 
62 // 对整型数组x中的n个元素降序排序 
63 // 补足函数bubble_sort()实现
64 
65 void bubble_sort(int x[], int n)
66 {
67     int i,j,t;
68     for(i=0;i<N-1;i++)
69     {
70         for(j=0;j<N-1-i;j++)
71         {
72             if(x[j]<x[j+1])
73             {
74                 t=x[j];
75                 x[j]=x[j+1];
76                 x[j+1]=t;
77             }
78         }
79     }
80  }
View Code

task7.c

 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 void output(int x[][N], int n) {
29     int i, j;
30 
31     for (i = 0; i < n; ++i) {
32         for (j = 0; j < n; ++j)
33             printf("%4d", x[i][j]);
34 
35         printf("\n");
36     }
37 }
38 
39 void rotate_to_right(int x[][N], int n)
40 {
41     int i,j;
42     int b[100];
43     for(i=0;i<n;i++)
44     {
45         b[i]=x[i][0];
46         x[i][0]=x[i][n-1];
47         for(j=n-1;j>0;j--)
48         {
49             if(j==1)
50             x[i][j]=b[i];
51         
52         else
53         x[i][j]=x[i][j-1]; 
54         }
55      } 
56     
57     
58     
59     
60 }
View Code

task8.c

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

 

posted @ 2023-11-15 20:52  FrederickJACK  阅读(26)  评论(0)    收藏  举报