实验4C语言数组应用编程

实验任务1

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     
 9     printf("sizeof(a) = %d\n", sizeof(a));
10     
11     
12     for (i = 0; i < N; ++i)
13     printf("%p: %d\n", &a[i], a[i]);
14     
15     
16     printf("a = %p\n", a);    
17 } 
18 
19 void test2() {
20     char b[N] = {'1', '9', '8', '4'};
21     int i;
22     
23     
24     printf("sizeof(b) = %d\n", sizeof(b));
25     
26     
27     for (i = 0; i < N; ++i)
28     printf("%p: %c\n", &b[i], b[i]);
29     
30     
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型数组a,在内存中是连续存放的,每个元素占用4个内存字节单元,数组名a与&a[0]对应的值一样。

答:char型数组b,在内存中是连续存放的,每个元素占用1个内存字节单元,数组名b与&b[0]对应的值一样。

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

运行截图

回答问题

答:int型二维数组a,在内存中是按行连续存放的,每个元素占用4个内存字节单位,数组名a的值,a[0]的值,&a[0][0]的值,在数字面值上一样。

答:int型二维数组b,在内存中是按行连续存放的,每个元素占用1个内存字节单位,数组名b的值,b[0]的值,&b[0][0]的值,在数字面值上一样。

答:a[0],a[1]之间差16;b[0],b[1]之间差4;差值等于元素类型大小 * 列数;

实验任务2

程序源码

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

运行截图

回答问题

答:test1()是用两个一维数组,test2()是用二维数组中的两个一维数组。

实验任务3

task3_1.c

程序源码

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

运行截图

task3_2.c

程序源码

 1 #include<stdio.h>
 2 #define N 1000
 3 
 4 int main()
 5 {
 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         i = 0;
17         while (1){
18             
19             while(line[i] == ' '){
20                 word_len = 0;
21                 i++;
22             }
23             while(line[i] != '\0' && line[i] != ' '){
24                 word_len++;
25                 i++;
26             }
27             
28             if(max_len < word_len){
29                 max_len = word_len;
30                 end = i;
31             }
32             if(line[i] == '\0')
33             break;
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

运行截图

实验任务4

程序源码

 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         
14         printf("\n输入一个十进制整数:"); 
15     } 
16     
17     return 0;
18 }
19 
20 void dec_to_n(int x, int n){
21     char fun[N];
22     char mid[7] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
23     int i = 0;
24     int m;
25     int j,t;
26     while(x !=0){
27         t = x % n;
28         x /= n;
29         fun[i++] = t + '0';
30     }
31     for(j = i - 1; j >= 0; j--){
32         if(fun[j] > '9'){
33             m = fun[j] - '9';
34             fun[j] = mid[m];
35         }
36         printf("%c",fun[j]);
37     }
38     printf("\n");
39 }
40     
View Code

运行截图

实验任务5

程序源码

 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 void input(int x[], int n){
32     int i;
33     
34     for(i = 0; i < n; ++i){
35         scanf("%d", &x[i]);
36     }
37 }
38 
39 void output(int x[], int n){
40     int i;
41     
42     for(i = 0; i < n; ++i)
43     printf("%d ", x[i]);
44     printf("\n");
45     
46 }
47 
48 double average(int x[], int n){
49     int i,sum = 0;
50     double ave1 = 0.0;
51     for(i = 0; i < n; i++){
52         sum += x[i]; 
53     } 
54     return ave1 =(double)sum / n;
55     
56 }
57 
58 void bubble_sort(int x[], int n){
59     int i,j,t;
60     for(j = 0; j < N - 1; j++) 
61     for(i = 0; i < N - j - 1; i++){
62         if(x[i] < x[i + 1]){
63             t = x[i];
64             x[i] = x[i + 1];
65             x[i + 1] = t;
66         }
67     }
68 }
View Code

运行截图

实验任务6

程序源码

 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 // ×××
40 void bubble_sort(char str[][M], int n){
41     int i,j;
42     char t[M]={0};
43     for(j = 0; j  < n - 1; j++)
44         for(i = 0; i < n - j - 1; i++){
45             if(strcmp(str[i], str[i + 1]) > 0){
46                 strcpy(t, str[i]);
47                 strcpy(str[i],str[i + 1]);
48                 strcpy(str[i + 1], t);
49                 
50             }
51         }
52 }
View Code

运行截图

实验任务7

程序源码

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

运行截图

实验任务8

程序源码

 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 a[N];
45     int i, j;
46     for(i = 0; i < n; i++){
47         a[i] = x[i][n - 1];
48     } 
49     for(i = n - 1; i > 0; i--){
50         for(j = 0; j < n; j++){
51             x[j][i] = x[j][i - 1];
52         } 
53     }
54     for(i = 0; i < n; i++){
55         x[i][0] = a[i];
56     } 
57 }
View Code

运行截图

 

posted @ 2023-11-19 09:58  Eternity//  阅读(30)  评论(0)    收藏  举报