C++ student information management system

Basic functions: Define the student class, including name, gender, student number, class and contact number.
1. Design menu to realize function selection;

2. Input function: input student information and save it to a file;

3. Ability to sort students according to their student numbers

4. Ability to modify student information based on student number

5. Able to delete student information based on student ID number
6. Query function:
1) Able to query student information based on student ID number; 2) Able to query student information based on name, student ID number, class, etc. 3) Count the number of students according to class.

If it’s helpful to you, just click three links~ If you need anything, please post it in the comment area in the next issue! !

 I only intercepted one code for the running results as follows: Please get it yourself if you need it.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;



// 学生信息管理类
class StudentManager {
public:
    // 将学生信息写入文件中
    void writeStudentToFile(const vector<Student> &students, const string &filename) {
        ofstream fout(filename);
        for (auto &s : students) {
            fout << s << endl;
        }
        fout.close();
    }
    
    // 从文件中读取学生信息
    vector<Student> readStudentsFromFile(const string &filename) {
        vector<Student> students;
        ifstream fin(filename);
        string line;
        while (getline(fin, line)) {
            Student s;
            sscanf(line.c_str(), "%s %s %s %s %s", s.name.c_str(),
                s.gender.c_str(), s.ID.c_str(), s.classname.c_str(), s.phone.c_str());
            students.push_back(s);
        }
        fin.close();
        return students;
    }
    
    // 添加学生信息
    void addStudent(vector<Student> &students) {
        Student s;
        cout << "请输入学生姓名:";
        cin >> s.name;
        cout << "请输入学生性别:";
        cin >> s.gender;
        cout << "请输入学生学号:";
        cin >> s.ID;
        cout << "请输入学生班级:";
        cin >> s.classname;
        cout << "请输入学生联系电话:";
        cin >> s.phone;
        students.push_back(s);
    }
    
    // 按照学号排序学生信息
    void sortStudentsByID(vector<Student> &students) {
        sort(students.begin(), students.end());
    }
    
    // 根据学号修改学生信息
    void modifyStudentByID(vector<Student> &students, const string &ID) {
        auto it = find(students.begin(), students.end(), Student{ "", "", ID, "", "" });
        if (it != students.end()) {
            Student &s = *it;
            cout << "请输入学生姓名:";
            cin >> s.name;
            cout << "请输入学生性别:";
            cin >> s.gender;
            cout << "请输入学生班级:";
            cin >> s.classname;
            cout << "请输入学生联系电话:";
            cin >> s.phone;
        } else {
            cout << "找不到该学生!" << endl;
        }
    }
    
    // 根据学号删除学生信息
    void deleteStudentByID(vector<Student> &students, const string &ID) {
        auto it = find(students.begin(), students.end(), Student{ "", "", ID, "", "" });
        if (it != students.end()) {
            students.erase(it);
        } else {
            cout << "找不到该学生!" << endl;
        }
    }
    
    // 根据学号查询学生信息
    void findStudentByID(const vector<Student> &students, const string &ID) {
        auto it = find(students.begin(), students.end(), Student{ "", "", ID, "", "" });
        if (it != students.end()) {
            cout << "找到该学生:" << *it << endl;
        } else {
            cout << "找不到该学生!" << endl;
        }
    }
    
    // 根据姓名、学号、班级等查询学生信息
    void findStudents(const vector<Student> &students) {
        int option;
        cout << "请选择查询方式:1.按姓名查询 2.按学号查询 3.按班级查询" << endl;
        cin >> option;
        switch (option) {
        case 1:
            cout << "请输入学生姓名:";
            break;
        case 2:
            cout << "请输入学生学号:";
            break;
        case 3:
            cout << "请输入学生班级:";
            break;
        default:
            cout << "无效的选项!" << endl;
            return;
        }
        string keyword;
        cin >> keyword;
        bool found = false;
        for (auto &s : students) {
            if ((option == 1 && s.name == keyword) ||
                (option == 2 && s.ID == keyword) ||
                (option == 3 && s.classname == keyword)) {
                cout << s << endl;
                found = true;
            }
        }
        if (!found) {
            cout << "找不到匹配的学生!" << endl;
        }
    }
    
    // 按照班级统计学生人数
    void countStudentsByClass(const vector<Student> &students) {
        string classname;
        cout << "请输入班级:";
        cin >> classname;
        int count = 0;
        for (auto &s : students) {
            if (s.classname == classname) {
                count++;
            }
        }
        cout << "该班级共有" << count << "名学生。" << endl;
    }
    
public:
    const string DATA_FILENAME = "students.txt";
};

// 显示菜单
void showMenu() {
    cout << "=====================" << endl;
    cout << "= 学生信息管理系统  =" << endl;
    cout << "=====================" << endl;
    cout << "1.新增学生信息" << endl;
    cout << "2.按学号排序" << endl;
    cout << "3.根据学号修改学生信息" << endl;
    cout << "4.根据学号删除学生信息" << endl;
    cout << "5.根据学号查询学生信息" << endl;
    cout << "6.查询学生信息" << endl;
    cout << "7.按班级统计学生人数" << endl;
    cout << "0.退出系统" << endl;
    cout << "=====================" << endl;
}

// 主函数
int main() {
    StudentManager manager;
    vector<Student> students = manager.readStudentsFromFile(manager.DATA_FILENAME);
    
    while (true) {
        showMenu();
        int option;
        cout << "请选择功能:";
        cin >> option;
        switch (option) {
        case 0:
            manager.writeStudentToFile(students, manager.DATA_FILENAME);
            cout << "再见!" << endl;
            return 0;
        case 1:
            manager.addStudent(students);
            cout << "添加成功!" << endl;
            break;
        case 2:
            manager.sortStudentsByID(students);
            cout << "排序完成!" << endl;
            break;
            case 3: {
                string ID;
                cout << "请输入要修改的学生的学号:";
                cin >> ID;
                manager.modifyStudentByID(students, ID);
                break;
            }
            case 4: {
                string ID;
                cout << "请输入要删除的学生的学号:";
                cin >> ID;
                manager.deleteStudentByID(students, ID);
                break;
            }
            case 5: {
                string ID;
                cout << "请输入要查询的学生的学号:";
                cin >> ID;
                manager.findStudentByID(students, ID);
                break;
            }
        case 6:
            manager.findStudents(students);
            break;
        case 7:
            manager.countStudentsByClass(students);
            break;
        default:
            cout << "无效的选项!" << endl;
            break;
        }
    }
    return 0;
}

If you need all the codes, please contact me: lxt123lxp456. It’s not easy to make, so please don’t bother me.

Guess you like

Origin blog.csdn.net/qq_62088638/article/details/131255673