实验5

task1_1

#include <stdio.h>
#define N 5

void input(int x[], int n);
void output(int x[], int n);
void find_min_max(int x[], int n, int *pmin, int *pmax);

int main() {
    int a[N];
    int min, max;

    printf("录入%d个数据:\n", N);
    input(a, N);

    printf("数据是: \n");
    output(a, N);

    printf("数据处理...\n");
    find_min_max(a, N, &min, &max);

    printf("输出结果:\n");
    printf("min = %d, max = %d\n", min, max);

    return 0;
}

void input(int x[], int n) {
    int i;

    for(i = 0; i < n; ++i)
        scanf("%d", &x[i]);
}

void output(int x[], int n) {
    int i;
    
    for(i = 0; i < n; ++i)
        printf("%d ", x[i]);
    printf("\n");
}

void find_min_max(int x[], int n, int *pmin, int *pmax) {
    int i;
    
    *pmin = *pmax = x[0];

    for(i = 0; i < n; ++i)
        if(x[i] < *pmin)
            *pmin = x[i];
        else if(x[i] > *pmax)
            *pmax = x[i];
}

 

1.找出一组数据中的最小值和最大值

2.分别指向main函数中的min和max

 

task1_2

#include <stdio.h>
#define N 5

void input(int x[], int n);
void output(int x[], int n);
int *find_max(int x[], int n);

int main() {
    int a[N];
    int *pmax;

    printf("录入%d个数据:\n", N);
    input(a, N);

    printf("数据是: \n");
    output(a, N);

    printf("数据处理...\n");
    pmax = find_max(a, N);

    printf("输出结果:\n");
    printf("max = %d\n", *pmax);

    return 0;
}

void input(int x[], int n) {
    int i;

    for(i = 0; i < n; ++i)
        scanf("%d", &x[i]);
}

void output(int x[], int n) {
    int i;
    
    for(i = 0; i < n; ++i)
        printf("%d ", x[i]);
    printf("\n");
}

int *find_max(int x[], int n) {
    int max_index = 0;
    int i;

    for(i = 0; i < n; ++i)
        if(x[i] > x[max_index])
            max_index = i;
    
    return &x[max_index];
}

1.函数的功能是找到数据中的最大值,返回的是x[i]

2.可以,用执政变量代替                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                t        

task2_1

#include <stdio.h>
#include <string.h>
#define N 80

int main() {
    char s1[N] = "Learning makes me happy";
    char s2[N] = "Learning makes me sleepy";
    char tmp[N];

    printf("sizeof(s1) vs. strlen(s1): \n");
    printf("sizeof(s1) = %d\n", sizeof(s1));
    printf("strlen(s1) = %d\n", strlen(s1));

    printf("\nbefore swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);

    printf("\nswapping...\n");
    strcpy(tmp, s1);
    strcpy(s1, s2);
    strcpy(s2, tmp);

    printf("\nafter swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);

    return 0;
}

.大小为4字节;计算的是数组s1的大小;统计的是字符串的长度

2.不能,  操作改变了s[0]的地址

tasl2_2

#include <stdio.h>
#include <string.h>
#define N 80

int main() {
    char *s1 = "Learning makes me happy";
    char *s2 = "Learning makes me sleepy";
    char *tmp;

    printf("sizeof(s1) vs. strlen(s1): \n");
    printf("sizeof(s1) = %d\n", sizeof(s1));
    printf("strlen(s1) = %d\n", strlen(s1));

    printf("\nbefore swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);

    printf("\nswapping...\n");
    tmp = s1;
    s1 = s2;
    s2 = tmp;

    printf("\nafter swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);

    return 0;
}

1.字符串的地址;s1占的字节数;字符串的字符个数

2.可以

3.交换指针变量,没有

task3

#include <stdio.h>

int main() {
    int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
    int i, j;
    int *ptr1;     // 指针变量,存放int类型数据的地址
    int(*ptr2)[4]; // 指针变量,指向包含4个int元素的一维数组

    printf("输出1: 使用数组名、下标直接访问二维数组元素\n");
    for (i = 0; i < 2; ++i) {
        for (j = 0; j < 4; ++j)
            printf("%d ", x[i][j]);
        printf("\n");
    }

    printf("\n输出2: 使用指针变量ptr1(指向元素)间接访问\n");
    for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) {
        printf("%d ", *ptr1);

        if ((i + 1) % 4 == 0)
            printf("\n");
    }
                         
    printf("\n输出3: 使用指针变量ptr2(指向一维数组)间接访问\n");
    for (ptr2 = x; ptr2 < x + 2; ++ptr2) {
        for (j = 0; j < 4; ++j)
            printf("%d ", *(*ptr2 + j));
        printf("\n");
    }

    return 0;
}

1.标志符ptr标示的语义是行指针变量

2.指针数组

task4

#include <stdio.h>
#define N 80

void replace(char *str, char old_char, char new_char); // 函数声明

int main() {
    char text[N] = "Programming is difficult or not, it is a question.";

    printf("原始文本: \n");
    printf("%s\n", text);

    replace(text, 'i', '*'); // 函数调用 注意字符形参写法,单引号不能少

    printf("处理后文本: \n");
    printf("%s\n", text);

    return 0;
}

// 函数定义
void replace(char *str, char old_char, char new_char) {
    int i;

    while(*str) {
        if(*str == old_char)
            *str = new_char;
        str++;
    }
}

                                                                                                                                               1.i换成*

   2.可以

task5

#include <stdio.h>
#define N 80

char *str_trunc(char *str, char x);

int main() {
    char str[N];
    char ch;

    while(printf("输入字符串:"), gets(str) != NULL) {
        printf("输入一个字符:");
        ch = getchar();

        printf("截断处理...\n");
        str_trunc(str, ch); // 函数调用

        printf("截断处理后的字符串:%s\n\n", str);
        getchar();
    }

    return 0;
}

// 函数str_trunc定义
char *str_trunc(char *str, char x) {
    char *p = str;
    // 查找字符x第一次出现的位置
    while (*p != '\0' && *p != x) {
        p++;
    }
    // 如果找到x,则截断字符串
    if (*p == x) {
        *p = '\0';
    }
    return str;
}

task6

#include <stdio.h>
#include <string.h>
#include <ctype.h> // For isdigit() function
#define N 5

int check_id(char *str); // 函数声明

int main()
{
    char *pid[N] = {"31010120000721656X",
                    "330106199600203301",
                    "53010220051126571",
                    "51010419921119797",
                    "53010220051126133Y"};
    int i;

    for (i = 0; i < N; ++i)
        if (check_id(pid[i])) // 函数调用
            printf("%s\t合法\n", pid[i]);
        else
            printf("%s\t不合法\n", pid[i]);
    return 0;
}

// 函数定义
// 功能:检查指针str指向的身份证号码形式是否合法
// 形式合法,返回1,否则,返回0
int check_id(char *str) {
    int len = strlen(str);
    
    // 检查长度是否为18
    if (len != 18) {
        return 0;
    }
    
    // 检查前17位是否为数字
    for (int i = 0; i < 17; i++) {
        if (!isdigit(str[i])) {
            return 0;
        }
    }
    
    // 检查最后一位是否为数字或X
    char last = str[17];
    if (!(isdigit(last) || last == 'X')) {
        return 0;
    }
    
    // 所有检查通过
    return 1;
}

       

task7

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 80

void encoder(char *str, int n); // Function declaration
void decoder(char *str, int n); // Function declaration

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

    printf("输入英文文本: ");
    fgets(words, N, stdin);
    words[strcspn(words, "\n")] = '\0'; // Remove newline character

    printf("输入n: ");
    scanf("%d", &n);

    printf("编码后的英文字符: ");
    encoder(words, n); // Function call
    printf("%s\n", words);

    printf("对编码后的英文字符解码: ");
    decoder(words, n); // Function call
    printf("%s\n", words);

    return 0;
}

/* Function definition */
// Encodes the string by shifting letters n positions forward in the alphabet
void encoder(char *str, int n) {
    for (int i = 0; str[i] != '\0'; i++) {
        if (isalpha(str[i])) {
            char base = isupper(str[i]) ? 'A' : 'a';
            str[i] = base + (str[i] - base + n) % 26;
        }
    }
}

/* Function definition */
// Decodes the string by shifting letters n positions backward in the alphabet
void decoder(char *str, int n) {
    for (int i = 0; str[i] != '\0'; i++) {
        if (isalpha(str[i])) {
            char base = isupper(str[i]) ? 'A' : 'a';
            str[i] = base + (str[i] - base - n + 26) % 26;
        }
    }
}

                     

task8

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

// 用于qsort的字符串比较函数
int compare_strings(const void *a, const void *b) {
    return strcmp(*(const char **)a, *(const char **)b);
}

int main(int argc, char *argv[]) {
    // 检查是否有参数输入
    if (argc < 2) {
        printf("用法: %s 姓名1 姓名2 ...\n", argv[0]);
        return 1;
    }

    // 对姓名进行排序(从argv[1]到argv[argc-1])
    qsort(argv + 1, argc - 1, sizeof(char *), compare_strings);

    // 按排序后的顺序打印问候语
    for (int i = 1; i < argc; ++i) {
        printf("hello, %s\n", argv[i]);
    }

    return 0;
}

                                                                                                                                                        

posted @ 2025-05-18 22:12  donk129  阅读(7)  评论(0)    收藏  举报