2301_81580952 2023-12-05 20:20 采纳率: 100%
浏览 15
已结题

改正代码,错误比较多

QQ上发代码,QQ2068551359

1.题目

学生GPA查询系统。

2.要求

(1)学生属性包括:学号、姓名、院系和GPA等,数据从文件( student_ data.txt) 中读取。

(2)提供菜单选项功能

(3)提供显示输出功能。

(4)提供排序功能,能够按学号和GPA排序,自由选择升序或降序

(5)提供查询功能,能够按姓名、年级和GPA区间查询。(6)提供统计功能,统计各学院人数。

(7)提供退出功能。

  • 写回答

6条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-12-05 22:33
    关注

    【以下回答由 GPT 生成】

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Student {
        char id[10];  // 学号
        char name[20];  // 姓名
        char department[30];  // 院系
        float gpa;  // GPA
    };
    
    int menu() {
        int choice;
        printf("\n学生GPA查询系统\n");
        printf("===========================\n");
        printf("1. 显示所有学生信息\n");
        printf("2. 按学号排序\n");
        printf("3. 按GPA排序\n");
        printf("4. 按姓名查询\n");
        printf("5. 按年级查询\n");
        printf("6. 按GPA区间查询\n");
        printf("7. 统计各学院人数\n");
        printf("8. 退出\n");
        printf("===========================\n");
        printf("请输入您的选择:");
        scanf("%d", &choice);
        return choice;
    }
    
    void displayStudents(struct Student students[], int count) {
        printf("\n学生信息\n");
        printf("===========================\n");
        printf("%-10s %-20s %-30s %-4s\n", "学号", "姓名", "院系", "GPA");
        printf("-----------------------------------------\n");
        for (int i = 0; i < count; i++) {
            printf("%-10s %-20s %-30s %-4.2f\n", students[i].id, students[i].name, students[i].department, students[i].gpa);
        }
        printf("===========================\n");
    }
    
    void sortById(struct Student students[], int count) {
        struct Student temp;
        for (int i = 0; i < count - 1; i++) {
            for (int j = 0; j < count - i - 1; j++) {
                if (strcmp(students[j].id, students[j + 1].id) > 0) {
                    temp = students[j];
                    students[j] = students[j + 1];
                    students[j + 1] = temp;
                }
            }
        }
        printf("按学号排序完成。\n");
    }
    
    void sortByGpa(struct Student students[], int count) {
        struct Student temp;
        for (int i = 0; i < count - 1; i++) {
            for (int j = 0; j < count - i - 1; j++) {
                if (students[j].gpa > students[j + 1].gpa) {
                    temp = students[j];
                    students[j] = students[j + 1];
                    students[j + 1] = temp;
                }
            }
        }
        printf("按GPA排序完成。\n");
    }
    
    void searchByName(struct Student students[], int count, char name[]) {
        printf("查询结果:\n");
        printf("===========================\n");
        printf("%-10s %-20s %-30s %-4s\n", "学号", "姓名", "院系", "GPA");
        printf("-----------------------------------------\n");
        for (int i = 0; i < count; i++) {
            if (strcmp(students[i].name, name) == 0) {
                printf("%-10s %-20s %-30s %-4.2f\n", students[i].id, students[i].name, students[i].department, students[i].gpa);
            }
        }
        printf("===========================\n");
    }
    
    void searchByGrade(struct Student students[], int count, int grade) {
        printf("查询结果:\n");
        printf("===========================\n");
        printf("%-10s %-20s %-30s %-4s\n", "学号", "姓名", "院系", "GPA");
        printf("-----------------------------------------\n");
        for (int i = 0; i < count; i++) {
            int studentGrade = atoi(students[i].id) / 10000;
            if (studentGrade == grade) {
                printf("%-10s %-20s %-30s %-4.2f\n", students[i].id, students[i].name, students[i].department, students[i].gpa);
            }
        }
        printf("===========================\n");
    }
    
    void searchByGpaRange(struct Student students[], int count, float minGpa, float maxGpa) {
        printf("查询结果:\n");
        printf("===========================\n");
        printf("%-10s %-20s %-30s %-4s\n", "学号", "姓名", "院系", "GPA");
        printf("-----------------------------------------\n");
        for (int i = 0; i < count; i++) {
            if (students[i].gpa >= minGpa && students[i].gpa <= maxGpa) {
                printf("%-10s %-20s %-30s %-4.2f\n", students[i].id, students[i].name, students[i].department, students[i].gpa);
            }
        }
        printf("===========================\n");
    }
    
    void countByDepartment(struct Student students[], int count) {
        printf("各学院人数统计:\n");
        printf("===========================\n");
        printf("%-30s %-4s\n", "院系", "人数");
        printf("-----------------------------------------\n");
    
        int numDepartments = 0;
        char departments[100][30];  // 修改数组大小以适应预计的最大学院数量
        int departmentCounts[100];  // 修改数组大小以适应预计的最大学院数量
    
        for (int i = 0; i < count; i++) {
            int found = 0;
            for (int j = 0; j < numDepartments; j++) {
                if (strcmp(students[i].department, departments[j]) == 0) {
                    departmentCounts[j]++;
                    found = 1;
                    break;
                }
            }
            if (!found) {
                strcpy(departments[numDepartments], students[i].department);
                departmentCounts[numDepartments] = 1;
                numDepartments++;
            }
        }
    
        for (int i = 0; i < numDepartments; i++) {
            printf("%-30s %-4d\n", departments[i], departmentCounts[i]);
        }
        printf("===========================\n");
    }
    
    int main() {
        FILE *file = fopen("student_data.txt", "r");
        if (file == NULL) {
            printf("无法打开文件!\n");
            return 1;
        }
        int count = 0;
        struct Student students[100];
        while (fscanf(file, "%s %s %s %f", students[count].id, students[count].name, students[count].department, &students[count].gpa) == 4) {
            count++;
        }
        fclose(file);
    
        int choice;
        char name[20];
        int grade;
        float minGpa, maxGpa;
    
        do {
            choice = menu();
    
            switch(choice) {
                case 1:
                    displayStudents(students, count);
                    break;
                case 2:
                    sortById(students, count);
                    break;
                case 3:
                    sortByGpa(students, count);
                    break;
                case 4:
                    printf("请输入要查询的姓名:");
                    scanf("%s", name);
                    searchByName(students, count, name);
                    break;
                case 5:
                    printf("请输入要查询的年级:");
                    scanf("%d", &grade);
                    searchByGrade(students, count, grade);
                    break;
                case 6:
                    printf("请输入GPA区间的最小值:");
                    scanf("%f", &minGpa);
                    printf("请输入GPA区间的最大值:");
                    scanf("%f", &maxGpa);
                    searchByGpaRange(students, count, minGpa, maxGpa);
                    break;
                case 7:
                    countByDepartment(students, count);
                    break;
                case 8:
                    printf("感谢使用学生GPA查询系统!再见!\n");
                    break;
                default:
                    printf("无效的选择!请重新输入。\n");
                    break;
            }
        } while (choice != 8);
    
        return 0;
    }
    

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 12月14日
  • 已采纳回答 12月6日
  • 创建了问题 12月5日

悬赏问题

  • ¥55 AD844 howland电流源如何驱动大额负载
  • ¥15 C++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码
  • ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题,但部署到linux环境报错
  • ¥15 vue页面,node封装接口
  • ¥15 求TMS320F280039C工程模板!
  • ¥15 delphi+fastreport实现分组补空打印问题
  • ¥15 使用python把两台mysql数据库服务器数据导出和导入
  • ¥15 NodeBB论坛配置Apache Solr中文搜索引擎的详细教程