学生成绩管理系统(C++实现)

学生成绩表信息包括学号、姓名、各科课程成绩(语文、数学、英语、政治)和总分。用带头结点的单链表管理学生成绩表,每个学生的信息依次从键盘输入,并根据需要进行插入、删除、排序、输出等操作。

#include 
#include 
#include 
#include 

const int MAX_STUDENTS = 100;

struct Student {
    char ID[20];
    char Name[10];
    float Math;
    float English;
    float Chinese;
    float Total;
    float Average;
};

int num = 0;
Student students[MAX_STUDENTS];

void saveToFile() {
    std::ofstream file("C:/Users/Lenovo/Desktop/111.txt");

    if (!file.is_open()) {
        std::cout << "无法打开文件。" << std::endl;
        return;
    }

    file << num << std::endl;

    for (int i = 0; i < num; ++i) {
        file << students[i].ID << " " << students[i].Name << " " << students[i].Math << " "
            << students[i].English << " " << students[i].Chinese << " " << students[i].Total << " "
            << students[i].Average << std::endl;
    }

    file.close();
}

void loadFromFile() {
    std::ifstream file("C:/Users/Lenovo/Desktop/111.txt");

    if (!file.is_open()) {
        std::cout << "无法打开文件。" << std::endl;
        return;
    }

    file >> num;

    for (int i = 0; i < num; ++i) {
        file >> students[i].ID >> students[i].Name >> students[i].Math
            >> students[i].English >> students[i].Chinese >> students[i].Total >> students[i].Average;
    }

    file.close();
}

void input() {
    if (num >= MAX_STUDENTS) {
        std::cout << "学生人数已达到上限,无法继续添加。" << std::endl;
        return;
    }

    std::cout << "请输入学号:";
    std::cin >> students[num].ID;
    std::cout << "请输入姓名:";
    std::cin >> students[num].Name;
    std::cout << "请输入数学成绩:";
    std::cin >> students[num].Math;
    std::cout << "请输入英语成绩:";
    std::cin >> students[num].English;
    std::cout << "请输入语文成绩:";
    std::cin >> students[num].Chinese;

    students[num].Total = students[num].Math + students[num].English + students[num].Chinese;
    students[num].Average = students[num].Total / 3;

    num++;
    std::cout << "录入成功。" << std::endl;
    saveToFile();
}

void deleteStudent() {
    char deleteID[20];
    std::cout << "请输入要删除的学生学号:";
    std::cin >> deleteID;

    for (int i = 0; i < num; ++i) {
        if (strcmp(students[i].ID, deleteID) == 0) {
            for (int j = i; j < num - 1; ++j) {
                students[j] = students[j + 1];
            }
            num--;
            std::cout << "删除成功。" << std::endl;
            saveToFile();
            return;
        }
    }

    std::cout << "未找到学号为" << deleteID << "的学生。" << std::endl;
}

void modify() {
    char modifyID[20];
    std::cout << "请输入要修改的学生学号:";
    std::cin >> modifyID;

    for (int i = 0; i < num; ++i) {
        if (strcmp(students[i].ID, modifyID) == 0) {
            std::cout << "请输入新的数学成绩:";
            std::cin >> students[i].Math;
            std::cout << "请输入新的英语成绩:";
            std::cin >> students[i].English;
            std::cout << "请输入新的语文成绩:";
            std::cin >> students[i].Chinese;

            students[i].Total = students[i].Math + students[i].English + students[i].Chinese;
            students[i].Average = students[i].Total / 3;

            std::cout << "修改成功。" << std::endl;
            saveToFile();
            return;
        }
    }

    std::cout << "未找到学号为" << modifyID << "的学生。" << std::endl;
}

void insert() {
    if (num >= MAX_STUDENTS) {
        std::cout << "学生人数已达到上限,无法插入。" << std::endl;
        return;
    }

    int position;
    std::cout << "请输入要插入的位置(1-" << num + 1 << "):";
    std::cin >> position;

    if (position < 1 || position > num + 1) {
        std::cout << "位置无效,请重新输入。" << std::endl;
        return;
    }

    for (int i = num; i >= position
        ; --i) {
        students[i] = students[i - 1];
    }

    std::cout << "请输入学号:";
    std::cin >> students[position - 1].ID;
    std::cout << "请输入姓名:";
    std::cin >> students[position - 1].Name;
    std::cout << "请输入数学成绩:";
    std::cin >> students[position - 1].Math;
    std::cout << "请输入英语成绩:";
    std::cin >> students[position - 1].English;
    std::cout << "请输入语文成绩:";
    std::cin >> students[position - 1].Chinese;

    students[position - 1].Total = students[position - 1].Math + students[position - 1].English + students[position - 1].Chinese;
    students[position - 1].Average = students[position - 1].Total / 3;

    num++;
    std::cout << "插入成功。" << std::endl;
    saveToFile();
}

void sort() {
    for (int i = 0; i < num - 1; ++i) {
        for (int j = 0; j < num - i - 1; ++j) {
            if (students[j].Average < students[j + 1].Average) {
                std::swap(students[j], students[j + 1]);
            }
        }
    }

    std::cout << "排序完成。" << std::endl;
    saveToFile();
}

void display() {

    if (num == 0) {
        std::cout << "暂无学生信息。" << std::endl;
        return;
    }

    std::cout << "学号\t姓名\t数学\t英语\t语文\t总分\t平均分" << std::endl;

    for (int i = 0; i < num; ++i) {
        std::cout << students[i].ID << '\t' << students[i].Name << '\t' << students[i].Math << '\t'
            << students[i].English << '\t' << students[i].Chinese << '\t' << students[i].Total
            << '\t' << students[i].Average << std::endl;
    }
}

void searchByIndex() {
    char searchID[20];
    std::cout << "请输入要查询的学生学号:";
    std::cin >> searchID;

    for (int i = 0; i < num; ++i) {
        if (strcmp(students[i].ID, searchID) == 0) {
            std::cout << "学号:" << students[i].ID << ",姓名:" << students[i].Name << ",数学:" << students[i].Math << ",英语:" << students[i].English << ",语文:" << students[i].Chinese << ",总分:" << students[i].Total << ",平均分:" << students[i].Average << std::endl;
            return;
        }
    }

    std::cout << "未找到学号为" << searchID << "的学生。" << std::endl;
}

int main() {
    loadFromFile();  // 在程序开始时加载学生数据

    int choice;

    while (true) {
        std::cout << "=========学生成绩管理系统=========" << std::endl;
        std::cout << "1. 录入学生成绩" << std::endl;
        std::cout << "2. 删除学生成绩" << std::endl;
        std::cout << "3. 修改学生成绩" << std::endl;
        std::cout << "4. 插入学生成绩" << std::endl;
        std::cout << "5. 排序学生成绩" << std::endl;
        std::cout << "6. 显示学生成绩" << std::endl;
        std::cout << "7. 查询学生成绩" << std::endl;
        std::cout << "0. 退出" << std::endl;
        std::cout << "=================================" << std::endl;
        std::cout << "请选择操作:";
        std::cin >> choice;

        switch (choice) {
        case 1:
            input();
            break;
        case 2:
            deleteStudent();
            break;
        case 3:
            modify();
            break;
        case 4:
            insert();
            break;
        case 5:
            sort();
            break;
        case 6:
            display();
            break;
        case 7:
            searchByIndex();
            break;
        case 0:
            std::cout << "感谢使用学生成绩管理系统,再见!" << std::endl;
            saveToFile();  // 在程序结束时保存学生数据
            return 0;
        default:
            std::cout << "输入无效,请重新输入。" << std::endl;
        }
    }

    return 0;
}


你可能感兴趣的:(C++相关学习,c++,课程设计,c语言)