学生信息管理系统(C++)

1 系统介绍

如下图所见,学生管理系统总共包含四个大模块,七个小模块。本系统是一个用于管理学生个人信息的软件系统,实现对学生个人信息的增加、查询、删除和更新四大操作。

.系统需求分析:

  1. 学生信息管理:能够添加学生信息,包含有学号、姓名、性别、年龄、地址和专业。
  2. 添加学生信息:(能逐条增加学生记录)

A: 学号(string)是学生的唯一标识,在添加新的学生信息要做好唯一性判断,同时学号表示学生是否存在,所以还需要做非空限制,学号的长度是固定的(12)并且学号前四位数字表示入学年份,应该做合理限制,限制在【1935,2023】

B: 姓名(string)也做非空限制,只有当清楚学号和姓名才能进行添加操作,姓名字符长度限制在【2,7】

C:性别(string)限制在“男”或“女”,当性别不清楚是,用户可以不输入,跳到下一信息的录入操作,该字段会被赋值NULL。

D:年龄(string)限制在【17,45】,同样用户也能选择跳过,字段被赋值NULL。

E: 剩下地址和专业(string),也能置空,字段被赋值NULL。

  1. 数据查询:(信息用表格形式展示)

A:单条件查询

能够通过给定条件查询学生的信息,给定的条件如学号、姓名、地址、专业。

B:多条件查询

能够支持同时指定多个条件的查询:例如查询“18岁的浙江省男生信息”规范用户输入格式(缺失用“/”,间隔用“*”)

C: 模糊查询:

能进行模糊查询,比如查询名字中包含“小”字的学生;

D:统计查询

能统计满足某个条件的学生人数;(统计年龄、性别、地址)

  1. 删除学生信息

可以删除指定条件的学生信息,学号作为学生唯一性标识,通过学号删除,避免误删。

5.更新学生信息:

通过学号查找到需要更新信息的学生,通过用户录入新的信息更新,可更新姓名,性别,年龄,住址和专业。规范用户输入(姓名*性别*年龄*住址*专业   (不改变则用/代替并使用*作为间隔))。

  • 系统技术平台和标准:
  1. 编程语言:采用C++语言实现系统的开发
  2. 开发平台 :Visual Studio 2022
  3. 数据存储:文本文件TXT格式ANSI编码
  4. 用户界面:Visual Studio 2022 控制台

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <codecvt>
using namespace std;

// 学生信息结构体
struct Student {
    string id;
    string name;
    string gender;
    string age;
    string address;
    string major;
};

// 从字符串解析出一个学生对象
Student parse_student(const string& line) {
    Student student;
    stringstream ss(line);
    getline(ss, student.id, ',');
    getline(ss, student.name, ',');
    getline(ss, student.gender, ',');
    getline(ss, student.age, ',');
    getline(ss, student.address, ',');
    getline(ss, student.major, ',');
    return student;
}

// 将一个学生对象转换为字符串
string to_string(const Student& student) {
    stringstream ss;
    ss << student.id << "," << student.name << "," << student.gender << "," << student.age << ","
        << student.address << "," << student.major;
    return ss.str();
}

//输出查找学生的结果
void print_studnet(vector<Student>stu) {
    cout << "+--------------+-----------+--------+-----+-------------------+-------------------+" << endl;
    cout << "|      学号    |   姓名    | 性别   | 年龄|         住址      |         专业      |" << endl;
    cout << "+--------------+-----------+--------+-----+-------------------+-------------------+" << endl;
    for (const auto& s : stu) {
        cout << "|" << left << setw(14) << s.id << "|" << left << setw(11) << s.name << "|" << left << setw(7) << s.gender << " |" << left << setw(5) << s.age << "|" << left << setw(19) << s.address << "|" << left << setw(19) << s.major << "|" << endl;
        cout << "+--------------+-----------+--------+-----+-------------------+-------------------+" << endl;
    }


}

// 写入学生信息
void write_students(const vector<Student>& students) {
    ofstream ofs("student.txt",ios::app);
    if (!ofs) {
        cerr << "打开文件写入失败!" << endl;
        exit(1);
    }

    for (const auto& student : students) {
        ofs << to_string(student) << endl;
    }

    ofs.close();
}
//直接查询文件数据,看学号是否重复
bool is_same_student_id(const string& id) {
    ifstream ifs("student.txt");
    if (!ifs) {
        cerr << "打开文件读取失败!" << endl;
        exit(1);
    }
    string line;
    while (getline(ifs, line)) {
        Student student = parse_student(line);
        if (student.id == id) {
            ifs.close(); // 关闭资源
            return true;
        }
    }
    ifs.close();
    return false;
}
void muti_check2() {
    vector<Student>stu;
    string order, age, gen, add;
    cout << "请输入你的查询条件:  (用*作为间隔) " << endl;
    cout << "样例:  18*浙江省*男" << "(年龄,地址,性别)缺失用/代替" << endl;
    cin >> order;
    stringstream ss(order);

    getline(ss, age, '*');
    getline(ss, add, '*');
    getline(ss, gen, '*');

    ifstream ifs("student.txt");
    if (!ifs) {
        cerr << "打开文件读取失败!" << endl;
        exit(1);
    }
    string line;
    while (getline(ifs, line)) {
        Student student = parse_student(line);
        bool r1 = age==student.age;
        bool r2 = student.address.find(add) != string::npos;
        bool r3 = student.gender==gen;
        if (age=="/")
        {
            r1 = 1;
        }
         if (add=="/")
        {
            r2=1;
        }
        if (gen=="/")
        {
            r3 = 1;
        }
        if (r1 == 1 && r2 == 1 && r3 == 1) {
            stu.push_back(student);

        }
    }
    ifs.close();
    print_studnet(stu);
    system("pause");
}

//模糊
void fuzzy_check() {
    cout << "名字模糊查询:" << endl;
    cout << "查询名字包含X的学生:" << endl;
    cout << "X:" << endl;
    string x;
    cin >> x;
    vector<Student>stu;
    ifstream ifs("student.txt");
    if (!ifs) {
        cerr << "打开文件读取失败!" << endl;
        exit(1);
    }
    string line;
    while (getline(ifs, line)) {
        Student student = parse_student(line);
        bool r1 = student.name.find(x) != string::npos;
        if (r1 == 1) {
            stu.push_back(student);
        }
    }
    ifs.close();
    print_studnet(stu);
    system("pause");
}

// 添加学生信息
void add_student() {
    Student student;
    vector<Student> students;
 outer:
    while (true) {
        cin.ignore();
        cout << "正在进行学生添加操作,退出请输入0" << endl;
        cout << "请输入学生学号:" << endl;
        getline(cin, student.id);
        if (student.id=="0")
        {
            return;
        }

        if (student.id.length() != 12 || student.id == " " || (stoi(student.id.substr(0, 4)) < 1935 || stoi(student.id.substr(0, 4)) > 2023))
        {
            cout << "学号输入错误!" << endl;
            continue;
        }

        // 检查学号是否唯一

        ifstream ifs("student.txt");
        if (!ifs) {
            cerr << "打开文件读取失败!" << endl;
            exit(1);
        }
        if (is_same_student_id(student.id)) {
            cout << "学号已经存在!" << endl;
            continue;
        }
        else {
            break;
        }
        goto next1;
        ifs.close();
    }
    
next1:
    while (true)
    {
        cout << "请输入学生姓名:" << endl;
        getline(cin, student.name);
        if (student.name.length() < 2 || student.name.length() > 7 || student.name.empty())
        {
            cout << "姓名输入错误!" << endl;
            continue;
        }
        else
        {
            goto next2;
        }
    }
      
next2:
    while (true)
    {
        cout << "请输入学生性别:" << endl;
        getline(cin, student.gender);
        if (student.gender=="男"||student.gender=="女")
        {
            goto next3;
           
        }
        else if(student.gender == " "||student.gender.empty())
        {
            student.gender = "NULL";
            goto next3;
        }
        else
        {
            cout << "性别输入错误!" << endl;
            continue;
        }
    }
    next3:
    while (true)
    {
        cout << "请输入学生年龄:  未知则输入0" << endl;
        cin >> student.age;

        if (student.age=="0")
        {
            student.age = "NULL";
            goto next4;
        }
        if ((stoi(student.age) >= 17 && stoi(student.age) <= 45)||stoi(student.age)==0)
        {
            goto next4;
        } 
        else {
            continue;
        }
    }
    next4:
            cin.ignore();
            cout << "请输入学生地址:" << endl;
            getline(cin, student.address);
            if (student.address.empty())
            {
                student.address = "NULL";
            }    
            cout << "请输入学生专业:" << endl;
            getline(cin, student.major);
            if (student.major.empty())
            {
                student.major = "NULL";
            }
    students.push_back(student);
    write_students(students);
    cout << "添加学生成功." << endl;
    system("pause");
    system("cls");
}

//查询学生信息
void check_student() {
 ou:
    while (true) {
       cin.clear();
        system("cls");
        int choice_1;
        vector<Student>stu;
        cout << "=====================查询学生信息=====================" << endl;
        cout << "===                 请选择你的操作                 ===" << endl;
        cout << "===                  0.单条件查询                  ===" << endl;
        cout << "===                  1.多条件查询                  ===" << endl;
        cout << "===                  2.模糊查询                    ===" << endl;
        cout << "===                  3.统计查询                    ===" << endl;
        cout << "===                  4.退出查询                    ===" << endl;
        cout << "======================================================" << endl;
        cin >> choice_1;
        switch (choice_1)
        {
        
        case 0:
            while (true)
            {
                cout << "请选择你的条件:" << endl;
                cout << "1:学号查询" << endl;
                cout << "2:姓名查询" << endl;
                cout << "3:住址查询" << endl;
                cout << "4:专业查询" << endl;
                cout << "5:返回查询" << endl;
                int choice_2;
                cin >> choice_2;
                ifstream ifs("student.txt");
                if (!ifs) {
                    cerr << "打开文件读取失败!" << endl;
                    exit(1);
                }
                switch (choice_2)
                {
                    outer:
                case 1: {   string line;
                    cout << "请输入学号:" << endl;
                    string id;
                    cin >> id;
                    if (id.length()!=12)
                    {
                        cout << "错误输入!" << endl;
                        goto outer;
                    }
                    while (getline(ifs, line)) {
                        Student student = parse_student(line);
                        if (student.id == id) {
                            stu.push_back(student);
                            break;
                        }
                    }
                    ifs.close();
                    print_studnet(stu);
                    break; }

                case 2:
                {   string line;
                cout << "请输入姓名:" << endl;
                string name;
                cin >> name;
                while (getline(ifs, line)) {
                    Student student = parse_student(line);
                    if (student.name ==name ) {
                        stu.push_back(student);
                    }
                }
                ifs.close();
                print_studnet(stu);
                break;  }
                case 3:
                 {    string line;
                 cout << "请输入住址:" << endl;
                 string address;
                 cin >> address;
                 while (getline(ifs, line)) {
                     Student student = parse_student(line);
                     if (student.address == address) {
                         stu.push_back(student);  
                     }
                 }
                 ifs.close();
                 print_studnet(stu);
                 break;
                }
                case 4: {
                    string line;
                    cout << "请输入专业:" << endl;
                    string _maj;
                    cin >> _maj;
                    while (getline(ifs, line)) {
                        Student student = parse_student(line);
                        if (student.major == _maj) {
                            stu.push_back(student);
                        }
                    }
                    ifs.close();
                    print_studnet(stu);
                    break;
                }
                case 5:
                    system("cls");
                    goto ou;
                default:
                    cout << "非法输入!" << endl;
                    system("pause");
                    goto to;
                   // break;
                }
            }
        case 1:muti_check2();
            break;
        case 2:fuzzy_check();
            break;
        case 3:
       to: while (true)
            {
        
           system("cls");
                
                int choice_3;
                vector<Student>stu;
                cout << "=====================统计查询入口=====================" << endl;
                cout << "===                 请选择你的操作                 ===" << endl;
                cout << "===                  1.统计年龄                    ===" << endl;
                cout << "===                  2.统计地址                    ===" << endl;
                cout << "===                  3.统计性别                    ===" << endl;
                cout << "===                  4.退出查询                    ===" << endl;
                cout << "======================================================" << endl;
                while (true)
                {
                    cin.clear();
                    cin >> choice_3;
                    switch (choice_3)
                    {
                    case 1: {
                        ifstream ifs("student.txt");
                        if (!ifs) {
                            cerr << "打开文件读取失败!" << endl;
                            exit(1);
                        }
                        cout << "请输入年龄  只需要数字" << endl;
                        string _age;
                        cin >> _age;
                        if (stoi(_age)<17||stoi(_age)>45)
                        {
                            cout << "年龄输入错误!" << endl;
                            system("pause");
                            goto to;
                        }
                        string line;
                        int count = 0;
                        while (getline(ifs, line)) {
                            Student student = parse_student(line);
                            bool r1 = _age==student.age;
                            if (r1 == 1) {
                                ++count;
                                stu.push_back(student);
                            }
                        }
                        ifs.close();
                        cout << "符合条件的学生共有" << count << "名" << endl;
                        print_studnet(stu);
                        system("pause");
                        goto to;
                    }
                    case 2: {
                        ifstream ifs("student.txt");
                        if (!ifs) {
                            cerr << "打开文件读取失败!" << endl;
                            exit(1);
                        }
                        cout << "请输入地址" << endl;
                        string _add;
                        cin >> _add;
                        string line;
                        int count = 0;
                        while (getline(ifs, line)) {
                            Student student = parse_student(line);
                            bool r1 = student.address.find(_add) != string::npos;
                            if (r1 == 1) {
                                ++count;
                                stu.push_back(student);
                            }
                        }
                        ifs.close();
                        cout << "符合条件的学生共有" << count << "名" << endl;
                        print_studnet(stu);
                        system("pause");
                        goto to;
                    }

                    case 3: {
                        ifstream ifs("student.txt");
                        if (!ifs) {
                            cerr << "打开文件读取失败!" << endl;
                            exit(1);
                        }
                        cout << "请输入性别" << endl;
                        string _gen;
                        cin >> _gen;
                        if (_gen!="男"&&_gen!="女")
                        {
                            cout << "性别输入错误"<< endl;
                            system("pause");
                            goto to;
                        }


                        string line;
                        int count = 0;
                        while (getline(ifs, line)) {
                            Student student = parse_student(line);
                            bool r1 = student.gender.find(_gen) != string::npos;
                            if (r1 == 1) {
                                ++count;
                                stu.push_back(student);
                            }
                        }
                        ifs.close();
                        cout << "符合条件的学生共有" << count << "名" << endl;
                        print_studnet(stu);
                        system("pause");
                        goto to;
                    }
                    case 4: {  return; }

                    default:
                        cout << "非法输入!" << endl;
                        system("pause");
                        goto to;
                    }
                }
            }
            break;
        case 4:system("cls");
            return;

        default:
            cout << "非法输入!" << endl;
            system("pause");
            goto ou;
        }
    }
}

// 删除符合条件的学生信息
void delete_student( const string& id) {
   
    // 打开输入输出文件流
    ifstream ifs("student.txt");
    if (!ifs) {
        cerr << "打开文件失败!" << endl;
        return;
    }

    ofstream ofs("temp.txt");
    if (!ofs) {
        cerr << "创建临时文件失败!" << endl;
        return;
    }
    string line;
    bool found = false;
    while (getline(ifs, line)) {
        Student student = parse_student(line);
        if (student.id == id) {
            found = true;
            cout << "+--------------+-----------+--------+-----+-------------------+-------------------+" << endl;
            cout << "|      学号    |   姓名    | 性别   | 年龄|         住址      |         专业      |" << endl;
            cout << "+--------------+-----------+--------+-----+-------------------+-------------------+" << endl;
                cout << "|" << left << setw(14) << student.id << "|" << left << setw(11) << student.name << "|" << left << setw(7) << student.gender << " |" << left << setw(5) << student.age << "|" << left << setw(19) << student.address << "|" << left << setw(19) << student.major << "|" << endl;
                cout << "+--------------+-----------+--------+-----+-------------------+-------------------+" << endl;
         
            cout << "已找到该学生信息,是否确认删除?(Y/N)";
            cout<<endl;
            char choice;
            cin >> choice;
            if (choice == 'Y' || choice == 'y') {
                continue; // 不写入该行记录,从而删除
            }
        }
        ofs << line << endl;
    }
    ifs.close();
    ofs.close();

    if (!found) {
        cout << "未找到该学生信息!" << endl;
    }
    else {
        //删除旧文件,改临时为正式文件
        remove("student.txt");
        rename("temp.txt", "student.txt");
        cout << "已删除该学生信息!" << endl;
        system("pause");
        return;
    }
}

// 更新符合条件的学生信息
void update_student( const string& id) {
    system("cls");
    // 打开输入输出文件流
    ifstream ifs("student.txt",ios::in|ios::out);
    if (!ifs) {
        cerr << "打开文件失败!" << endl;
        return;
    }
    ofstream ofs("student.txt",ios::in|ios::out);
    if (!ofs) {
        cerr << "打开文件失败!" << endl;
        return;
    }
  

    string line;
    bool found = false;
ou:  while (getline(ifs, line)) {
        Student student = parse_student(line);
        if (student.id == id) {
            int pos = static_cast<int>(ifs.tellg()) - static_cast<int>(line.size())-2;
            found = true;
            cout << "|      学号    |   姓名    | 性别   | 年龄|         住址      |         专业      |" << endl;
            cout << "+--------------+-----------+--------+-----+-------------------+-------------------+" << endl;
                cout << "|" << left << setw(14) << student.id << "|" << left << setw(11) << student.name << "|" << left << setw(7) << student.gender << " |" << left << setw(5) << student.age << "|" << left << setw(19) << student.address << "|" << left << setw(19) << student.major << "|" << endl;
                cout << "+--------------+-----------+--------+-----+-------------------+-------------------+" << endl;
                cout << endl;
                cout << "请依次输入 姓名*性别*年龄*住址*专业   (不改变则用/代替并使用*作为间隔) 退出请输入0";
                string order, name,age, gen, add, maj;
                cout << endl;
                cin >> order;
                if (order=="0")
                {
                    return;
                }
                stringstream ss(order);
                getline(ss, name, '*');
                getline(ss, gen, '*');
                getline(ss, age, '*');
                getline(ss, add, '*');
                getline(ss, maj, '*');
           
                if ((name!="/")&& (name.length()>1&&name.length()<=7))
                {
                    student.name = name;
                   
                }
                else if ((name != "/") && (name.length() <= 1 || name.length() >= 7))
                {
                    cout << "姓名输入错误!" << endl;
                    goto ou;
                }
               
                if ((gen!="/")&&(gen=="男"||gen=="女"))
                {
                    student.gender = gen;
                }
                else if ((gen != "/") && (gen != "男" && gen != "女"))
                {
                    cout << "性别输入错误" << endl;
                    goto ou;
                }

                if ((age!="/")&&(stoi(age)<45&&stoi(age)>=17))
                {
                    student.age = age;
                }
                else if ((age != "/") && (stoi(age) >=45 || stoi(age) < 17))
                {
                    cout << "年龄输入错误" << endl;
                    goto ou;
                }
                if (add != "/") 
                {
                    student.address = add;
                } 
                if (maj != "/") 
                {
                    student.major = maj;
                }
                ofs.seekp(pos, ios::beg);
                ofs << to_string(student) << endl;
           
        }
       
    }  
       ifs.close();
      ofs.close();
      if (!found) {
          cout << "该学号不存在!" << endl;
          system("pause");
          return;
   }
  
     cout << "已更新该学生信息!" << endl;
     system("pause");
       return;
}

int main() {
   outer0:
    while (true) {
        system("cls");
        cout << "==============欢迎来到学生信息管理系统==============" << endl;
        cout << "===               请选择你的操作                 ===" << endl;
        cout << "===                1.添加学生                    ===" << endl;
        cout << "===                2.查询学生                    ===" << endl;
        cout << "===                3.删除学生                    ===" << endl;
        cout << "===                4.更新学生                    ===" << endl;
        cout << "===                5.退出系统                    ===" << endl;
        cout << "====================================================" << endl;
        int choice = 0;
        cin >> choice;
        switch (choice) {
        case 1:
            add_student();
            break;
        case 2: {
            check_student();
            break;
        }

        case 3: {  string id;
            while (true)
            {
                cout << "正在进行删除操作  返回则输入0" << endl;
                cout << "请输入学号:" << endl;
                cin >> id;
                if (id == "0")
                {
                    goto outer0;
                }
                else if (id.length()!=12)
                {
                    cout << "学号格式错误!" << endl;
                    continue;
                }
                else { break; }
                
            } 
            delete_student( id);
            break;
        }
        case 4: {
            string id;
            while (true)
            {
                cout << "正在进行修改信息操作   返回则输入0" << endl;
                cout << "请输入学号:" << endl;
                cin >> id;
                if (id == "0")
                {
                    goto outer0;
                }
                else if (id.length() != 12)
                {
                    cout << "学号格式错误!" << endl;
                    continue;
                }
                else { break; }
            }
            update_student( id);
            break;
        }
        case 5:
            return 0;
        default:
           cout<< "非法输入!" << endl;
           system("pause");
           system("cls");
            break;
        }
    }
    return 0;
}
  1. 删除学生模块:

通过学生学号来对指定学生进行删除,同样对用户输入的学号做提前过滤,避免无效输入。痛过ifstream和ofstream来对文件进行读写操作,建立临时文件“temp.txt”,使用 while循环不断逐行读取解析学生信息,拿到学号与用户所录入的学号做比照,若不符合,则将该行学生数据写进临时文件;若符合,则跳过该行学生数据,写入下一行数据,最后再将原学生信息文本数据删除,将临时文件重命名为正式文件“student.txt”,实现对于该学生数据的删除功能

2.更新学生模块:

同样通过学生学号查询到需要修改的学生。用户输入学号,过滤,查找,使用seekp方法对该行学生数据定位,并将该行学生文本数据读取解析为student,通过用户选择性更新字段信息,对学生进行重新赋值,当出现用户输入字段信息非法,则提示用户,并让用户重新操作,规范用户输入(姓名*性别*年龄*住址*专业   (不改变则用/代替并使用*作为间隔)

最后将student转为字符串,通过ofstream写进对应行,实现覆盖原行数据,达到修改操作。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值