实验4

任务1

 

#include <stdio.h>
#include<stdlib.h>
#define N 4

void test1() {
    int a[N] = {1, 9, 8, 4};          
    int i;

    // 输出数组a占用的内存字节数
    printf("sizeof(a) = %d\n", sizeof(a));

    // 输出int类型数组a中每个元素的地址、值 
    for (i = 0; i < N; ++i)
        printf("%p: %d\n", &a[i], a[i]);

    // 输出数组名a对应的值 
    printf("a = %p\n", a); 
}

void test2() {        
    char b[N] = {'1', '9', '8', '4'}; 
    int i;

    // 输出数组b占用的内存字节数
    printf("sizeof(b) = %d\n", sizeof(b));

    // 输出char类型数组b中每个元素的地址、值 
    for (i = 0; i < N; ++i)
        printf("%p: %c\n", &b[i], b[i]);
    
    // 输出数组名b对应的值 
    printf("b = %p\n", b); 
}

int main() {
    printf("测试1: int类型一维数组\n");
    test1();

    printf("\n测试2: char类型一维数组\n");
    test2();

    system("pause");
    return 0;
}

地址是连续存放的,并且a与&a[0]地址相同

 

 

#include <stdio.h>
#include<stdlib.h>
#define N 2
#define M 4

void test1() {
    int a[N][M] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
    int i, j;

    // 输出int类型二维数组a占用的内存字节数
    printf("sizeof(a) = %d\n", sizeof(a));

    // 输出int类型二维数组a中每个元素的地址、值 
    for (i = 0; i < N; ++i)
        for (j = 0; j < M; ++j)
            printf("%p: %d\n", &a[i][j], a[i][j]);
    printf("\n");

    // 输出int类型二维数组名a, 以及,a[0], a[1]的值
    printf("a = %p\n", a);
    printf("a[0] = %p\n", a[0]);
    printf("a[1] = %p\n", a[1]);
    printf("\n");
}

void test2() {
    char b[N][M] = {{'1', '9', '8', '4'}, {'2', '0', '4', '9'}};
    int i, j;

    // 输出char类型二维数组b占用的内存字节数
    printf("sizeof(b) = %d\n", sizeof(b));

    // 输出char类型二维数组b中每个元素的地址、值
    for (i = 0; i < N; ++i)
        for (j = 0; j < M; ++j)
            printf("%p: %c\n", &b[i][j], b[i][j]);
    printf("\n");

    // 输出char类型二维数组名b, 以及,b[0], b[1]的值
    printf("b = %p\n", b);
    printf("b[0] = %p\n", b[0]);
    printf("b[1] = %p\n", b[1]);
}

int main() {
    printf("测试1: int型两维数组");
    test1();

    printf("\n测试2: char型两维数组");
    test2();
    system("pause");

    return 0;
}

地址a,a[0],&a[0][0]以及b,b[0],&b[0][0]都是一致的

是行连续存放,4个内存单元

a相差4个字节

b相差一个字节

 

任务2

 

#include <stdio.h>

#include<stdlib.h>
#include <string.h>

#define N 80

void swap_str(char s1[N], char s2[N]);
void test1();
void test2();

int main() {
    printf("测试1: 用两个一维char数组,实现两个字符串交换\n");
    test1();

    printf("\n测试2: 用二维char数组,实现两个字符串交换\n");
    test2();

    system("pause");
    return 0;
}

void test1() {
    char views1[N] = "hey, C, I hate u.";
    char views2[N] = "hey, C, I love u.";

    printf("交换前: \n");
    puts(views1);
    puts(views2);

    swap_str(views1, views2);

    printf("交换后: \n");
    puts(views1);
    puts(views2);
}

void test2() {
    char views[2][N] = {"hey, C, I hate u.", 
                        "hey, C, I love u."};

    printf("交换前: \n");
    puts(views[0]);
    puts(views[1]);

    swap_str(views[0], views[1]);

    printf("交换后: \n");
    puts(views[0]);
    puts(views[1]);
}

void swap_str(char s1[N], char s2[N]) {
    char tmp[N];

    strcpy(tmp, s1);
    strcpy(s1, s2);
    strcpy(s2, tmp);
}

test1需要的是两个数组的开头地址,test2需要的是一个数组的不同位置地址

任务3

 

#include <stdio.h>

#include<stdlib.h>
#define N 80

int count(char x[]);

int main() {
    char words[N+1];
    int n;

    while(gets(words) != NULL) {
        n = count(words);
        printf("单词数: %d\n\n", n);
    }
    system("pause");
    return 0;
}

int count(char x[]) {
    int i;
    int word_flag = 0;  // 用作单词标志,一个新单词开始,值为1;单词结束,值为0
    int number = 0;  // 统计单词个数

    for(i = 0; x[i] != '\0'; i++) {
        if(x[i] == ' ')
            word_flag = 0;
        else if(word_flag == 0) {
            word_flag = 1;
            number++;
        }
    }

    return number;
}

 

#include <stdio.h>

#include<stdlib.h>
#define N 1000

int main() {
    char line[N];
    int word_len;   // 记录当前单词长度
    int max_len;    // 记录最长单词长度
    int end;        // 记录最长单词结束位置
    int i;

    while(gets(line) != NULL) {
        word_len = 0;
        max_len = 0;
        end = 0;

        i = 0;
        while(1) {
            // 跳过连续空格
            while(line[i] == ' ') {
                word_len = 0;  // 单词长度置0,为新单词统计做准备
                i++;
            }

            // 在一个单词中,统计当前单词长度
            while(line[i] != '\0' && line[i] != ' ') {
                word_len++;
                i++;
            }
        
            // 更新更长单词长度,并,记录最长单词结束位置
            if(max_len < word_len) {
                max_len = word_len;
                end = i;   // end保存的是单词结束的下一个坐标位置
            }

            // 遍历到文本结束时,终止循环
            if(line[i] == '\0')
                break;
        }

        // 输出最长单词
        printf("最长单词: ");
        for(i = end - max_len; i < end; ++i)
            printf("%c", line[i]);
        printf("\n\n");
    }

    return 0;
}

任务4

#include <stdio.h>

#include<stdlib.h>
#define N 100
void dec_to_n(int x, int n); // 函数声明

int main() {
    int x;

    printf("输入一个十进制整数: ");
    while(scanf("%d", &x) != EOF) {
        dec_to_n(x, 2);  // 函数调用: 把x转换成二进制输出
        dec_to_n(x, 8);  // 函数调用: 把x转换成八进制输出
        dec_to_n(x, 16); // 函数调用: 把x转换成十六进制输出

        printf("\n输入一个十进制整数: ");
    }
    system("pause");
    return 0;
}
void dec_to_n(int x,int n){
    
    int i,k,b;
    int a[100];
    i=0;
    while(1){
        
        a[i]=x%n;
        x=x/n;
        i++;
        if(x==0)
            break;
        
    }
    for(k=i-1;k>=0;k--){
        
    
        if(a[k]<10){
            printf("%d",a[k]);
            
        }
        else{
        
                if(a[k]==10){
                    printf("A");
                }
                else if(a[k]==11){
                    printf("B");
                }
                else if(a[k]==12){
                    printf("C");
                }
                else if(a[k]==13){
                    printf("D");
                }
                else if(a[k]==14){
                    printf("E");
                }
                else if(a[k]==15){
                    printf("F");
                }
            }
    }

    printf("\n");
    
}

 

 

任务5

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

 任务6

 

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

任务7

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <string.h>
 4 
 5 
 6 int compare(char str[]);
 7 int main() {
 8     char str[100];
 9 
10 
11     while (gets_s(str, 100) != NULL) {
12     
13         if (compare(str))
14         {
15             printf("YES\n");
16         }
17         else
18         {
19             printf("NO\n");
20         }
21     
22     }
23 
24     return 0;
25 }
26 int compare(char str[]) {
27     int cycle, onceIncyc,length;
28     length = strlen(str);
29     for ( cycle = 0; cycle <= length-2; cycle++)
30     {
31         for (onceIncyc = cycle+1; onceIncyc <= length - 1; onceIncyc++) {
32         
33             if (str[cycle]==str[onceIncyc])
34             {
35                 return 1;
36             }
37 
38         }
39     }
40     return 0;
41 }

 

 任务8

 1 #define N 100
 2 #define M 4
 3 #include <stdio.h>
 4 #include <string.h>
 5 
 6 void output(int x[][N],int n);
 7 void rotate_to_right(int x[][N],int n);
 8 
 9 int main() {
10 
11     int t[][N] = { {21,12,13,24},
12                    {25,16,47,38},
13                    {29,11,32,54},
14                    {42,21,33,10}};
15     printf("原始矩阵:\n");
16     output(t, M);
17 
18     rotate_to_right(t, M);
19 
20     printf("变换后矩阵:\n");
21     output(t, M);
22 
23 
24                     
25     return 0;
26 }
27 
28 void output(int x[][N], int n) {
29 
30     int i,j;
31 
32     for ( i = 0; i < n; i++)
33     {
34         for (j = 0; j < n; ++j) {
35             printf("%4d",x[i][j]);
36         }
37         printf("\n");
38     }
39 
40 }
41 void rotate_to_right(int x[][N], int n) {
42     //x[0][2]:13 x[detail][head]
43     int stoCop[N][N];
44     int head, detail;
45     for ( head = 0; head <= n - 1; head++)
46     {
47         for (detail = 0; detail <= n - 1; detail++) {
48         
49             stoCop[detail][head] = x[detail][head];
50 
51         }
52     }
53     for (head = 0; head <= n - 1; head++)
54     {
55         for (detail = 0; detail < n; detail++)
56         {
57             if (head == 0) {
58 
59                 x[detail][head] = stoCop[detail][n - 1];
60             }
61             
62             else
63             {
64                 x[detail][head] = stoCop[detail][head - 1];
65             }
66             
67         }
68     }
69     
70 }

 

posted @ 2023-11-13 22:28  Sn002  阅读(20)  评论(0)    收藏  举报