Playing with the student information management system——[c++]

Design a management system to realize the management of basic information of students (including at least name, student number, gender, date of birth, age of dormitory number (obtained by calculation);), with data entry, display, storage, query (according to student number query or name query), modify and delete functions.

one. Problem Description

C++ design a management system to realize the management of basic information of students (at least including name, student number, gender, date of birth, dormitory number, age (obtained by calculation) ; with data entry, display, storage, query (according to student number or Name query), modify and delete functions.

two. basic requirements

Data entry: Allow users to enter student information, including name, student number, gender, date of birth, dormitory number and other information.

Data display: student information can be displayed in tabular form through the interface.

Data saving: After entering or modifying student information, the information needs to be saved to a file so that it can be used next time the program is opened.

Query function: Allow users to query student information by student number or name, and display the query results in the form of a table.

Modify function: Allow users to modify existing student information and save the modified information to a file.

Delete function: Allow users to delete existing student information and save the deleted information to a file.

It should be noted that in order to realize the above functions, the system needs to perform some calculations and processing. For example, it is necessary to calculate the age of a student based on the date of birth, and to determine whether the data format is correct and whether the data is legal when entering or modifying student information.

three. demand analysis

When conducting requirements analysis, we need to describe in detail the functions and technologies used by the system, determine the input and output of the system, and consider possible special situations and exception handling.

The system needs to realize the following functions:

(1) Data entry

Users can enter student information through the interface, including name, student number, gender, date of birth, dormitory number and other information, and make legality judgments. If the data format is incorrect or the data is wrong, the program needs to give a corresponding prompt message and ask the user to re-enter.

(2) Display of data

The system can display the existing student information in tabular form, including name, student number, gender, date of birth, dormitory number, and calculated age.

(3) Data storage

After entering or modifying student information, the program needs to save the information to a file. In order to ensure the correct operation of the program, the saved file format needs to strictly meet the requirements.

(4) Query function

Users can query student information by student number or name, and the query results are displayed in a table. At the same time, the program also needs to judge the validity of the query results, and give corresponding prompt information if the information does not exist.

(5) Modify function

Allow users to modify existing student information and save the modified information to a file.

(6) Delete function

Allow users to delete existing student information and save the deleted information to a file.

Four. outline design

When doing the general design, we need to determine the composition structure and functional modules of the system, design the necessary data structures and algorithms, and plan the control flow and operation flow of the program.

The system can be divided into the following modules:

Data input module: responsible for receiving the student information input by the user, and verifying and judging its legality.

Data processing module: responsible for calculating, processing and saving the input student information, including age calculation, information entry, modification and deletion, etc.

Data output module: responsible for outputting the processed student information in the form of a table, including the display of the query results and the processing of the saved file format.

In the framework design of the program, a student class can be designed with object-oriented thinking, which includes attributes such as name, student number, gender, date of birth, dormitory number, and corresponding operation functions. At the same time, a student information management class needs to be established to manage, store, read and operate student information.

five. detailed design

When carrying out detailed design, we need to further design the inter-module interface, data structure and algorithm of the program, and carry out module coding and unit testing.

(1) Data input module

The input module needs to define a function to receive the student information input by the user, and the parameter is the object of the student information management class. Inside the function, the user needs to be prompted to input student information in a certain format, and the data format verification and legality judgment should be performed. If the input data is wrong, you need to return the corresponding error message. If the input data is correct, the student information needs to be added to the instance of the student information management class.

Example function skeleton:

void InputStudentInfo(StudentManagement& management){

    // Prompt user to enter student information

    //Receive user input student information

    // Perform data format verification and legality judgment

    //If the input data is wrong, return an error message

    //Add student information to the student information management class instance

}

(2) Data processing module

The data processing module needs to define a student information management class, including operation methods such as adding students, modifying students, deleting students and querying students, and also needs to calculate and process the age of students.

Example class framework:

class StudentManagement {

public:

    void AddStudent(Student student); //Add students

    void ModifyStudent(Student student); //Modify student information

    void DeleteStudent(Student student); //Delete students

    void QueryStudentByNum(std::string num); //Query student information by student number

    void QueryStudentByName(std::string name); //Query student information by name

    void ShowAllStudentInfo(); //Display all student information

    void SaveToFile(std::string filename); //Save student information to the file

    void ReadFromFile(std::string filename); //Read student information from the file

private:

    std::vector<Student> students; //container for storing student information

};

class Student {

public:

    std::string name; //student name

    std::string num; //student number

    std::string gender; //student gender

    std::string birth; //Student's date of birth (in the format of yyyy-mm-dd)

    std::string room; //student dormitory number

    int age; //student age

};

//Function to calculate the student's age

int CalculateAge(std::string birth) {

    //Calculate the student's age and return

}

(3) Data output module

The data output module needs to define a function for outputting student information in a form, including the display of query results and the processing of saved file formats.

Example function skeleton:

void OutputStudentInfo(Student student){

    // output student information in form

}

void SaveStudentInfoToFile(std::vector&lt;Student&gt; students, std::string filename){

    //Save the student information to the file (you can convert the student information into a string format first, and then write it into the file)

}

six. debug analysis

After the program is written, it needs to be tested and debugged to ensure the correctness and stability of the program. When testing and debugging, you need to pay attention to the following:

For each module, unit testing is required to ensure that each module functions correctly. At the same time, integration testing and system testing are required to test the functionality of the entire program.

For special and abnormal situations that may occur, corresponding processing and testing are required, such as illegal input, unreadable files, and non-existent files.

It is recommended to use debugging tools to debug the program, and to troubleshoot and solve problems in time.

seven. User Instructions

For users who use this system, it is necessary to provide corresponding instructions, including how to use each functional module, how to input and process data, how to save and read files, etc.

Example usage instructions:

Enter student information:

In the main interface, select the "Enter Student Information" button to enter the input interface, follow the prompts to enter the student's name, student number, gender, date of birth, dormitory number and other information, and click the "Confirm" button, the system will automatically send the student The information is saved to a file.

Query student information:

In the main interface, select the "Query Student Information" button to enter the query interface, enter the student's student number or name in the query interface, and click the "Query" button, the system will display the query results in the form of a table. If the query result does not exist, the system will give a corresponding prompt message.

Modify student information:

In the main interface, select the "Modify Student Information" button to enter the modification interface, enter the student's student number or name in the modification interface, and enter the information to be modified, and finally click the "Confirm" button, the system will automatically save the modified Save the student information to the file.

Delete student information:

In the main interface, select the "Delete Student Information" button to enter the deletion interface, enter the student's student number or name in the deletion interface, and click the "Delete" button, the system will automatically save the deleted student information to a file .

eight. Test Results

The test results show that the system has good functions and user experience, can effectively manage student information, and has high accuracy and stability in input, query, modification and deletion.

Nine. Program Design Summary

This program is developed with C++ language, using object-oriented thinking, and realizes the management and operation of student information through modular design and layered architecture. Through this program design, I learned some basic concepts and syntax of C++, which further improved my programming skills and practical ability.

ten. references

《C++ Primer》

"C++ Programming Thoughts"

"C++ Programming"

"Basics of C++ Programming"

After the above analysis, the following code is obtained:

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

using namespace std;


struct Student {
    string name;
    string id;
    string gender;
    string dorm;
    int age;
    string birth;
};


const string fileName = "students.txt";


vector<Student> readStudents() {
    vector<Student> students;
    ifstream infile(fileName);
    if (!infile) {
        cout << "无法打开文件 " << fileName << endl;
        return students;
    }
    string line;
    while (getline(infile, line)) {
       
        line += ",";
        Student s;
        size_t pos = 0;
        string token;
        int count = 0;
        while ((pos = line.find(',')) != string::npos) {
            token = line.substr(0, pos);
            switch (count) {
            case 0: s.name = token; break;
            case 1: s.id = token; break;
            case 2: s.gender = token; break;
            case 3: s.dorm = token; break;
            case 4: s.age = stoi(token); break;
            case 5: s.birth = token; break;
            }
            line.erase(0, pos + 1);
            count++;
        }
        students.push_back(s);
    }
    infile.close();
    return students;
}


void saveStudents(vector<Student>& students) {
    ofstream outfile(fileName);
    if (!outfile) {
        cout << "无法打开文件 " << fileName << endl;
        return;
    }
    for (auto&& s : students) {
      
        outfile << s.name << ',' << s.id << ',' << s.gender << ',' << s.dorm
            << ',' << s.age << ',' << s.birth << endl;
    }
    outfile.close();
}

void displayStudents(vector<Student>& students) {
    cout << setw(10) << "姓名" << setw(10) << "学号" << setw(5) << "性别"
        << setw(10) << "宿舍号" << setw(5) << "年龄" << setw(15) << "出生日期" << endl;
    for (auto&& s : students) {
        cout << setw(10) << s.name << setw(10) << s.id << setw(5) << s.gender
            << setw(10) << s.dorm << setw(5) << s.age << setw(15) << s.birth << endl;
    }
}

vector<Student> searchById(vector<Student>& students, string id) {
    vector<Student> result;
    for (auto&& s : students) {
        if (s.id == id) {
            result.push_back(s);
        }
    }
    return result;
}


vector<Student> searchByName(vector<Student>& students, string name) {
    vector<Student> result;
    for (auto&& s : students) {
        if (s.name == name) {
            result.push_back(s);
        }
    }
    return result;
}


void modifyStudent(vector<Student>& students, string id) {
    for (auto&& s : students) {
        if (s.id == id) {
            cout << "请输入新的姓名:";
            getline(cin, s.name);
            cout << "请输入新的性别:";
            getline(cin, s.gender);
            cout << "请输入新的宿舍号:";
            getline(cin, s.dorm);
            cout << "请输入新的出生日期:";
            getline(cin, s.birth);
            s.age = 2023 - stoi(s.birth.substr(0, 4));
            cout << "修改成功!" << endl;
            return;
        }
    }
    cout << "没有找到学号为 " << id << " 的学生。" << endl;
}


void deleteStudent(vector<Student>& students, string id) {
    for (auto it = students.begin(); it < students.end(); it++) {
        if ((*it).id == id) {
            students.erase(it);
            cout << "删除成功!" << endl;
            return;
        }
    }
    cout << "没有找到学号为 " << id << " 的学生。" << endl;
}

void addStudent(vector<Student>& students) {
    Student s;
    cout << "请输入姓名:";
    getline(cin, s.name);
    cout << "请输入学号:";
    getline(cin, s.id);
    cout << "请输入性别:";
    getline(cin, s.gender);
    cout << "请输入宿舍号:";
    getline(cin, s.dorm);
    cout << "请输入出生日期(格式为YYYY-MM-DD):";
    getline(cin, s.birth);
    s.age = 2023 - stoi(s.birth.substr(0, 4));
    students.push_back(s);
    cout << "添加成功!" << endl;
}


void displayMenu() {
    cout << "请选择操作:" << endl;
    cout << "1. 查看所有学生信息" << endl;
    cout << "2. 按学号查询学生信息" << endl;
    cout << "3. 按姓名查询学生信息" << endl;
    cout << "4. 修改学生信息" << endl;
    cout << "5. 删除学生信息" << endl;
    cout << "6. 添加学生信息" << endl;
    cout << "0. 退出" << endl;
    cout << "请选择操作:";
}

int main() {
    vector<Student> students = readStudents();

    while (true) {
        displayMenu();
        int choice;
        cin >> choice;
        cin.get();
        switch (choice) {
        case 1:
            displayStudents(students);
            break;
        case 2: {
            cout << "请输入要查询的学号:";
            string id;
            getline(cin, id);
            vector<Student> result = searchById(students, id);
            if (result.size() == 0) {
                cout << "没有找到学号为 " << id << " 的学生。" << endl;
            }
            else {
                cout << "查询结果:" << endl;
                displayStudents(result);
            }
            break;
        }
        case 3: {
            cout << "请输入要查询的姓名:";
            string name;
            getline(cin, name);
            vector<Student> result = searchByName(students, name);
            if (result.size() == 0) {
                cout << "没有找到姓名为 " << name << " 的学生。" << endl;
            }
            else {
                cout << "查询结果:" << endl;
                displayStudents(result);
            }
            break;
        }
        case 4: {
            cout << "请输入要修改的学号:";
            string id;
            getline(cin, id);
            modifyStudent(students, id);
            saveStudents(students);
            break;
        }
        case 5: {
            cout << "请输入要删除的学号:";
            string id;
            getline(cin, id);
            deleteStudent(students, id);
            saveStudents(students);
            break;
        }
        case 6:
            addStudent(students);
            saveStudents(students);
            break;
        case 0:
            return 0;
        default:
            cout << "无效的选项,请重新输入。" << endl;
        }
    }

    return 0;
}

 The code can add more functions. The blogger will give you a reference. We can add some collective entry, query of dormitory personnel, deletion regardless of the whole, etc. I hope everyone can improve.

Guess you like

Origin blog.csdn.net/m0_74755811/article/details/131106150