输入关键词开始检索,或点选下方高频主题

    跳到正文
    TranquilYu's Blog
    返回

    2025.12.23 C语言程序设计上机实习八

    约 9 分钟 · 105 字
    文章目录6 节
    1. 2025.12.23 C语言程序设计上机实习八
    2. 必做题
    3. 选做题
    4. 题目1:文件中字符串的模糊匹配
    5. 题目2:学生信息文件排序
    6. 附:完整代码
    文章目录6 节

    2025.12.23 C语言程序设计上机实习八

    必做题

    选做题

    题目源文件实验八.pdf

    题目1:文件中字符串的模糊匹配

    函数FuzzyMatchLine

    int FuzzyMatchLine(const char *filename, int i, const char *pattern)
    {
        if (pattern[0] == '\0') return 1;
        if (i < 1) return 0;
        char line[100];
        FILE *fp;
        fp = fopen(filename, "r");
        if (fp == NULL) return 0;
        for (int j = 1; j <= i; j++)
        {
            if (fgets(line, sizeof(line), fp) == NULL)
            {
                fclose(fp);
                return 0;
            }
        }
        for (int k = 0; line[k] != '\0'; k++)
        {
            line[k] = tolower((unsigned char)line[k]);
        }
        char *pattern_temp;
        pattern_temp = NULL;
        pattern_temp = (char *)malloc(strlen(pattern) * sizeof(char));
        memcpy(pattern_temp, pattern, strlen(pattern));
        for(int l = 0; pattern_temp[l] != '\0'; l++)
        {
            pattern_temp[l] = tolower((unsigned char)pattern_temp[l]);
        }
        if (strstr(line, pattern_temp) != NULL)
        {
            free(pattern_temp);
            fclose(fp);
            return 1;
        }
        free(pattern_temp);
        fclose(fp);
        return 0;
    }

    用相应测试例测试的运行结果截图

    题目2:学生信息文件排序

    函数cmpByNameThenId

    int cmpByNameThenId(const void *a, const void *b)
    {
        const struct Student *s1 = (const struct Student *)a;
        const struct Student *s2 = (const struct Student *)b;
    
        int nameCmp = strcmp(s1->name, s2->name);
        if (nameCmp != 0)
        {
            return nameCmp;
        }
    
        return s1->id - s2->id;
    }

    函数cmpByAgeThenId

    int cmpByAgeThenId(const void *a, const void *b)
    {
        const struct Student *s1 = (const struct Student *)a;
        const struct Student *s2 = (const struct Student *)b;
    
        if (s1->age != s2->age)
        {
            return s1->age - s2->age;
        }
    
        return s1->id - s2->id;
    }

    函数cmpById

    int cmpById(const void *a, const void *b)
    {
        const struct Student *s1 = (const struct Student *)a;
        const struct Student *s2 = (const struct Student *)b;
    
        return s1->id - s2->id;
    }

    函数LoadAndSortStudents

    void LoadAndSortStudents(char mode, struct Student **stus, int *n)
    {
        FILE *fp;
        fp = fopen("stuInfo.dat", "rb");
        if (fp == NULL) return;
        fread(n, sizeof(int), 1, fp);
        *stus = (struct Student *)malloc(sizeof(struct Student) * (*n));
        if (*stus == NULL)
        {
            fclose(fp);
            return;
        }
        fread(*stus, sizeof(struct Student), *n, fp);
    
        switch(mode) {
            case 'n': goto n;
            case 'a': goto a;
            case 'i': goto i;
            default: fclose(fp); free(*stus); return;
        }
    n:
        qsort(*stus, *n, sizeof(struct Student), cmpByNameThenId);
        fclose(fp);
        return;
    a:
        qsort(*stus, *n, sizeof(struct Student), cmpByAgeThenId);
        fclose(fp);
        return;
    i:
        qsort(*stus, *n, sizeof(struct Student), cmpById);
        fclose(fp);
        return;
    }

    用相应测试例测试的运行结果截图

    附:完整代码

    FuzzyMatch.c
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    // 你需要实现的函数
    int FuzzyMatchLine(const char *filename, int i, const char *pattern);
    
    int main()
    {
        int i;
        char pattern[51];  // 题目说明 pattern 长度 ≤ 50
    
        if (scanf("%d", &i) != 1)
        {
            printf("No\n");
            return 0;
        }
    
        // 跳过换行符
        getchar();
    
        if (fgets(pattern, sizeof(pattern), stdin) == NULL)
        {
            printf("No\n");
            return 0;
        }
    
        // 去除 fgets 读入的换行符(如果存在)
        size_t len = strlen(pattern);
        if (len > 0 && pattern[len - 1] == '\n')
        {
            pattern[len - 1] = '\0';
        }
    
        int result = FuzzyMatchLine("strings.txt", i, pattern);
    
        if (result == 1)
        {
            printf("Yes\n");
        }
        else
        {
            printf("No\n");
        }
    
        return 0;
    }
    
    
    int FuzzyMatchLine(const char *filename, int i, const char *pattern)
    {
        if (pattern[0] == '\0') return 1;
        if (i < 1) return 0;
        char line[100];
        FILE *fp;
        fp = fopen(filename, "r");
        if (fp == NULL) return 0;
        for (int j = 1; j <= i; j++)
        {
            if (fgets(line, sizeof(line), fp) == NULL)
            {
                fclose(fp);
                return 0;
            }
        }
        for (int k = 0; line[k] != '\0'; k++)
        {
            line[k] = tolower((unsigned char)line[k]);
        }
        char *pattern_temp;
        pattern_temp = NULL;
        pattern_temp = (char *)malloc(strlen(pattern) * sizeof(char));
        memcpy(pattern_temp, pattern, strlen(pattern));
        for(int l = 0; pattern_temp[l] != '\0'; l++)
        {
            pattern_temp[l] = tolower((unsigned char)pattern_temp[l]);
        }
        if (strstr(line, pattern_temp) != NULL)
        {
            free(pattern_temp);
            fclose(fp);
            return 1;
        }
        free(pattern_temp);
        fclose(fp);
        return 0;
    }
    SortStudents.c
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct Student
    {
        char name[21];  // 姓名,最多20字符 + '\0'
        int age;        // 年龄
        int id;         // 学号
    };
    
    // 你需要实现
    void LoadAndSortStudents(char mode, struct Student **stus, int *n);
    
    int main()
    {
        char mode = getchar();
        struct Student *students;
        int n;
        LoadAndSortStudents(mode, &students, &n);
        for (int i = 0; i < n; i++)
        {
            printf("id: %d, name: %s, age: %d\n", students[i].id, students[i].name, students[i].age);
        }
        return 0;
    }
    
    int cmpByNameThenId(const void *a, const void *b)
    {
        const struct Student *s1 = (const struct Student *)a;
        const struct Student *s2 = (const struct Student *)b;
    
        int nameCmp = strcmp(s1->name, s2->name);
        if (nameCmp != 0)
        {
            return nameCmp;
        }
    
        return s1->id - s2->id;
    }
    
    int cmpByAgeThenId(const void *a, const void *b)
    {
        const struct Student *s1 = (const struct Student *)a;
        const struct Student *s2 = (const struct Student *)b;
    
        if (s1->age != s2->age)
        {
            return s1->age - s2->age;
        }
    
        return s1->id - s2->id;
    }
    
    int cmpById(const void *a, const void *b)
    {
        const struct Student *s1 = (const struct Student *)a;
        const struct Student *s2 = (const struct Student *)b;
    
        return s1->id - s2->id;
    }
    
    
    void LoadAndSortStudents(char mode, struct Student **stus, int *n)
    {
        FILE *fp;
        fp = fopen("stuInfo.dat", "rb");
        if (fp == NULL) return;
        fread(n, sizeof(int), 1, fp);
        *stus = (struct Student *)malloc(sizeof(struct Student) * (*n));
        if (*stus == NULL)
        {
            fclose(fp);
            return;
        }
        fread(*stus, sizeof(struct Student), *n, fp);
    
        switch(mode) {
            case 'n': goto n;
            case 'a': goto a;
            case 'i': goto i;
            default: fclose(fp); free(*stus); return;
        }
    n:
        qsort(*stus, *n, sizeof(struct Student), cmpByNameThenId);
        fclose(fp);
        return;
    a:
        qsort(*stus, *n, sizeof(struct Student), cmpByAgeThenId);
        fclose(fp);
        return;
    i:
        qsort(*stus, *n, sizeof(struct Student), cmpById);
        fclose(fp);
        return;
    }
    

    — 蝶引记毕 · 愿君读有所得 —



    1. 2025.12.16 C语言程序设计上机实习七作业C语言程序设计上机实习七
    2. 2025.12.09 C语言程序设计上机实习六作业C语言程序设计上机实习六
    3. 2025.12.02 C语言程序设计上机实习五作业C语言程序设计上机实习五