c语言实现检索未交作业学生名单

c语言实现检索未交作业学生名单

简介

C语言程序实现一个简单的学生作业管理,其主要功能是在指定的目录下检索学生是否提交了作业,如果没有提交则将其学号和姓名列出来。

  • 程序使用了C语言中文件操作、字符串处理和Windows API的知识,通过加载学生信息文件,并与指定目录下的文件名进行匹配,识别未提交作业的学生信息。此小项目可以帮助C语言学习者加深对文件处理、字符串处理和内存管理等方面的理解。

程序实现

  • 主要代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <dirent.h>
    #include <windows.h>
    
    #define MAX_STUDENTS 100
    #define MAX_NAME_LENGTH 100
    
    typedef struct {
          
          
        char id[20]; // 学号
        char name[MAX_NAME_LENGTH]; // 姓名
    } StudentInfo;
    
    StudentInfo studentDatabase[MAX_STUDENTS];
    int databaseSize = 0;
    
    // 将UTF-8字符串转换为GBK字符串的函数
    char* UTF8ToGBK(const char* utf8Str) {
          
          
        int len = MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, NULL, 0);
        wchar_t* wStr = (wchar_t*)malloc(sizeof(wchar_t) * len);
        MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, wStr, len);
    
        len = WideCharToMultiByte(CP_ACP, 0, wStr, -1, NULL, 0, NULL, NULL);  // CP_ACP 指定了ANSI编码
        char* gbkStr = (char*)malloc(len);
        WideCharToMultiByte(CP_ACP, 0, wStr, -1, gbkStr, len, NULL, NULL);
    
        free(wStr);
        return gbkStr; // 需由调用者释放内存
    }
    
    // 从文件中加载学生信息的函数
    void loadStudentDatabase(const char *filename) {
          
          
        FILE *file = fopen(filename, "r");
        if (file == NULL) {
          
          
            perror("File opening failed");
            exit(EXIT_FAILURE);
        }
    
        while (fscanf(file, "%19s %99[^\n]\n", studentDatabase[databaseSize].id, studentDatabase[databaseSize].name) == 2) {
          
          
            databaseSize++;
        }
    
        fclose(file);
    }
    
    // 检查作业提交情况的函数
    void checkHomework(const char *targetPath) {
          
          
        struct dirent *entry;
        DIR *dir = opendir(targetPath);
    
        if (dir == NULL) {
          
          
            perror("Directory opening failed");
            exit(EXIT_FAILURE);
        }
    
        int *submitted = (int *)calloc(databaseSize, sizeof(int));
        if (submitted == NULL) {
          
          
            perror("Memory allocation failed");
            closedir(dir);
            exit(EXIT_FAILURE);
        }
    
        while ((entry = readdir(dir)) != NULL) {
          
          
            for (int i = 0; i < databaseSize; i++) {
          
          
                if (strstr(entry->d_name, studentDatabase[i].id) != NULL) {
          
          
                    submitted[i] = 1; // Mark as submitted
                    break;
                }
            }
        }
        closedir(dir);
    
        printf("以下同学没有交作业:\n学号 姓名\n");
        for (int i = 0; i < databaseSize; i++) {
          
          
            if (!submitted[i]) {
          
          
                char* gbkName = UTF8ToGBK(studentDatabase[i].name);
                printf("%s %s\n", studentDatabase[i].id, gbkName);
                free(gbkName); // Do not forget to free the memory
            }
        }
    
        free(submitted);
    }
    
    int main() {
          
          
        char targetPath[256];
        printf("请输入windows电脑目标路径:");
        scanf("%255s", targetPath);
    
        loadStudentDatabase("stuInf.txt"); // 假设学生信息文件在程序当前目录下
        checkHomework(targetPath);
    
        return 0;
    }
    

    关于乱码解决:由于txt文档采用的编码是utf-8,shell里面的是gbk,为防止中文输出乱码,已经对编码进行了转化

  • stuInf.txt

    学生信息文件,存储格式为:学号+姓名

    image-20231225135255760

  • 程序检索方式

    • 将目标目录zip、rar文件、普通文件名字中与stuInf中学生的学号进行匹配。

    • 如果匹配上,说明该学生已经上交作业

    • 程序输出没有匹配上学生的学号+姓名

运行测试

image-20231225135850359

中与stuInf中学生的学号进行匹配。

  • 如果匹配上,说明该学生已经上交作业
  • 程序输出没有匹配上学生的学号+姓名

运行测试

image-20231225135850359

猜你喜欢

转载自blog.csdn.net/weixin_46290752/article/details/135198161