博客
关于我
C++实现控制台学生学籍管理系统
阅读量:458 次
发布时间:2019-03-06

本文共 12444 字,大约阅读时间需要 41 分钟。

学生学籍管理系统开发文档

操作流程

本系统的操作流程主要包括以下几个方面:

  • 创建文件

    系统在初始化时会检查文件状态,分为以下几种情况:

    • 文件不存在:系统会提示文件不存在,并初始化文件。
    • 文件为空:系统会提示文件为空,并进行适当处理。
    • 文件正常:系统会读取文件中的数据并进行初始化。
  • 创建管理类

    系统创建了一个StudentManager类,负责与用户的交互及数据的管理。该类的主要职责包括:

    • 提供与用户的沟通菜单界面。
    • 实现对职工的增删改查操作。
    • 数组数据与文件的读写交互。
  • 菜单功能实现

    StudentManager.h中定义了Show_Menu()函数,在StudentManager.cpp中实现了菜单界面的显示功能。通过显示所有功能选项,便于用户选择后续操作。

    void StudentManager::Show_Menu() {    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;    cout << "~~~~~~~~~~~~~~~~学生学籍管理系统~~~~~~~~~~~~~~~~" << endl;    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;    cout << "******************(0)退出系统*****************" << endl;    cout << "******************(1)录入学生*****************" << endl;    cout << "******************(2)显示学籍*****************" << endl;    cout << "******************(3)删除学生*****************" << endl;    cout << "******************(4)修改学生*****************" << endl;    cout << "******************(5)查找学生*****************" << endl;    cout << "******************(6)学生排序*****************" << endl;    cout << "******************(7)分类显示*****************" << endl;    cout << "******************(8)清空系统*****************" << endl;    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;}
  • 退出功能

    StudentManager.h中定义了Exit_System()函数,在StudentManager.cpp中实现了退出系统的功能,通过调用exit(0)函数退出程序。

    void StudentManager::Exit_System() {    cout << "欢迎下次使用" << endl;    system("pause");    exit(0);}
  • 系统功能实现

    为各种功能提供接口

    main函数中,为管理操作的函数提供接口,创建实例对象,调用对象的成员函数。以下是实现代码:

    int main() {    StudentManager stu;    int choice;    while (true) {        system("color B1"); // 美化控制台        stu.Show_Menu();        cout << "请输入您的选择:";        cin >> choice;        switch (choice) {            case 0: stu.Exit_System(); // 退出系统                break;            case 1: stu.Add_Student(); // 录入学生                break;            case 2: stu.ShowStudent(); // 显示学籍                break;            case 3: stu.DeleteStudent(); // 删除学生                break;            case 4: stu.ModStudent(); // 修改学生                break;            case 5: stu.FindStudent(); // 查找学生                break;            case 6: stu.SortStudent(); // 学生排序                break;            case 7: stu.ClassifyStudent(); // 分类显示                break;            case 8: stu.CleanStudent(); // 清空系统                break;            default: system("cls"); // 处理未选项                break;        }        system("pause");    }    return 0;}

    退出功能实现

    StudentManager.h中定义了Exit_System()函数,用于退出系统。实现如下:

    void StudentManager::Exit_System() {    cout << "欢迎下次使用" << endl;    system("pause");    exit(0);}

    创建学生类

    系统创建了一个Student类作为基类,分别创建了Grade01Grade02Grade03Grade04四个子类,分别对应不同年级的学生。以下是部分实现代码:

    // Header file#pragma once#include "Student.h"class Grade01 : public Student {public:    Grade01(int id, string name, int grade);    void Show_Info(); // 显示个人信息    string Get_Grade(); // 获取年级名称};
    // Source file#include "Grade01.h"Grade01::Grade01(int id, string name, int grade) {    this->m_id = id;    this->m_name = name;    this->m_grade = grade;}void Grade01::Show_Info() {    cout << "学号:" << this->m_id << " ";    cout << "姓名:" << this->m_name << " ";    cout << "年级:" << this->Get_Grade() << endl;}string Grade01::Get_Grade() {    return "大一";}

    添加学生

    StudentManager.cpp中实现了Add_Student函数,用于添加学生信息。以下是实现代码:

    void StudentManager::Add_Student() {    cout << "请输入添加学生数量" << endl;    int addnum;    cin >> addnum;    if (addnum > 0) {        int newsize = this->Student_Num + addnum;        Student** newspace = new Student*[newsize];        if (this->Student_Array != NULL) {            for (int i = 0; i < this->Student_Num; i++) {                newspace[i] = this->Student_Array[i];            }        }        for (int i = 0; i < addnum; i++) {            int id;            string name;            int select;            cout << "请输入第" << i + 1 << "个学生学号" << endl;            cin >> id;            cout << "请输入第" << i + 1 << "个学生姓名" << endl;            cin >> name;            cout << "请输入第" << i + 1 << "个学生年级编号" << endl;            cout << "1、大一" << endl;            cout << "2、大二" << endl;            cout << "3、大三" << endl;            cout << "4、大四" << endl;            cin >> select;            Student* student = NULL;            switch (select) {                case 1: student = new Grade01(id, name, 1); break;                case 2: student = new Grade02(id, name, 1); break;                case 3: student = new Grade03(id, name, 1); break;                case 4: student = new Grade04(id, name, 1); break;                default: break;            }            newspace[this->Student_Num + i] = student;        }        delete[] this->Student_Array;        this->Student_Array = newspace;        this->Student_Num = newsize;        cout << "添加成功" << addnum << "个学生" << endl;    } else {        cout << "输入有误" << endl;    }    system("pause");    system("cls");}

    保存文件

    实现了Save函数,将学生数据保存到文件中。以下是实现代码:

    void StudentManager::Save() {    ofstream ofs;    ofs.open(FILENAME, ios::out);    for (int i = 0; i < this->Student_Num; i++) {        ofs << this->Student_Array[i]->m_id << " "            << this->Student_Array[i]->m_name << " "            << this->Student_Array[i]->m_grade << " ";        ofs << endl;    }    ofs.close();}

    初始化学生

    StudentManager.cpp中实现了InitStudent函数,用于初始化学生数据。以下是实现代码:

    void StudentManager::InitStudent() {    ifstream ifs;    ifs.open(FILENAME, ios::in);    int id, grade;    string name;    int index = 0;    while (ifs >> id >> name >> grade) {        Student* student = NULL;        if (id == 1) {            student = new Grade01(id, name, grade);        } else if (id == 2) {            student = new Grade02(id, name, grade);        } else if (id == 3) {            student = new Grade03(id, name, grade);        } else {            student = new Grade04(id, name, grade);        }        this->Student_Array[index] = student;        index++;    }    ifs.close();}

    显示学生

    实现了ShowStudent函数,用于显示学生信息。以下是实现代码:

    void StudentManager::ShowStudent() {    if (this->FileIsEmpty) {        cout << "文件为空或者不存在" << endl;    } else {        for (int i = 0; i < this->Student_Num; i++) {            this->Student_Array[i]->Show_Info();        }    }    system("pause");    system("cls");}

    删除学生

    实现了DeleteStudent函数,用于删除学生信息。以下是实现代码:

    void StudentManager::DeleteStudent() {    if (this->FileIsEmpty) {        cout << "文件不存在或者记录为空" << endl;    } else {        cout << "请输入要删除的学生学号" << endl;        int id;        cin >> id;        int index = this->IsExist(id);        if (index != -1) {            for (int i = index; i < this->Student_Num - 1; i++) {                this->Student_Array[i] = this->Student_Array[i + 1];            }            this->Student_Num--;            this->Save();            cout << "删除成功" << endl;        } else {            cout << "删除失败,未找到该职工" << endl;        }    }    system("pause");    system("cls");}

    查找学生

    实现了FindStudent函数,用于查找学生信息。以下是实现代码:

    void StudentManager::FindStudent() {    if (this->FileIsEmpty) {        cout << "文件不存在或者记录为空!" << endl;    } else {        cout << "请输入查找的方式:" << endl;        cout << "1、按学号查找" << endl;        cout << "2、按姓名查找" << endl;        int select = 0;        cin >> select;        if (select == 1) {            int id;            cout << "请输入查找的学号" << endl;            cin >> id;            int ret = this->IsExist(id);            if (ret != -1) {                cout << "查找成功,该学生信息如下" << endl;                this->Student_Array[ret]->Show_Info();            } else {                cout << "查找失败,查无此人" << endl;            }        } else if (select == 2) {            string name;            cout << "请输入查找的姓名" << endl;            cin >> name;            bool flag = false;            for (int i = 0; i < this->Student_Num; i++) {                if (this->Student_Array[i]->m_name == name) {                    cout << "查找成功,信息如下" << endl;                    flag = true;                    this->Student_Array[i]->Show_Info();                }            }            if (!flag) {                cout << "查找失败,查无此人!!" << endl;            }        } else {            cout << "输入选项有误" << endl;        }    }    system("pause");    system("cls");}

    修改学生信息

    实现了ModStudent函数,用于修改学生信息。以下是实现代码:

    void StudentManager::ModStudent() {    if (this->FileIsEmpty) {        cout << "文件不存在,或记录为空" << endl;    } else {        cout << "请输入要修改的学生学号" << endl;        int id;        cin >> id;        int ret = this->IsExist(id);        if (ret != -1) {            delete this->Student_Array[ret];            int newid;            string newname;            int select;            cout << "查到" << id << "号学生,请输入新学号" << endl;            cin >> newid;            cout << "请输入新姓名" << endl;            cin >> newname;            cout << "请输入年级" << endl;            cout << "1、大一" << endl;            cout << "2、大二" << endl;            cout << "3、大三" << endl;            cout << "4、大四" << endl;            cin >> select;            Student* student = NULL;            switch (select) {                case 1: student = new Grade01(newid, newname, 1); break;                case 2: student = new Grade02(newid, newname, 1); break;                case 3: student = new Grade03(newid, newname, 1); break;                case 4: student = new Grade04(newid, newname, 1); break;                default: break;            }            this->Student_Array[ret] = student;            cout << "修改成功" << endl;            this->Save();        } else {            cout << "修改失败,查无此人" << endl;        }    }    system("pause");    system("cls");}

    按学号排序

    实现了SortStudent函数,用于对学生按照学号进行排序。以下是实现代码:

    void StudentManager::SortStudent() {    if (this->FileIsEmpty) {        cout << "文件不存在或者记录为空" << endl;        system("pause");        system("cls");    } else {        cout << "请选择排序方式: " << endl;        cout << "1、按学号进行升序" << endl;        cout << "2、按学号进行降序" << endl;        int select = 0;        cin >> select;        for (int i = 0; i < this->Student_Num; i++) {            int minOrmax = i;            for (int j = i + 1; j < this->Student_Num; j++) {                if (select == 1) {                    if (this->Student_Array[minOrmax]->m_id > this->Student_Array[j]->m_id) {                        minOrmax = j;                    }                } else {                    if (this->Student_Array[minOrmax]->m_id < this->Student_Array[j]->m_id) {                        minOrmax = j;                    }                }            }            if (i != minOrmax) {                Student* temp = this->Student_Array[i];                this->Student_Array[i] = this->Student_Array[minOrmax];                this->Student_Array[minOrmax] = temp;            }        }        cout << "排序成功" << endl;        this->Save();    }}

    按年级分类查看

    实现了ClassifyStudent函数,用于按年级分类查看学生信息。以下是实现代码:

    void StudentManager::ClassifyStudent() {    cout << "大一:" << endl;    for (int i = 0; i < this->Student_Num; i++) {        if (this->Student_Array[i]->m_grade == 1) {            this->Student_Array[i]->Show_Info();        }    }    cout << endl;    cout << "大二:" << endl;    for (int i = 0; i < this->Student_Num; i++) {        if (this->Student_Array[i]->m_grade == 2) {            this->Student_Array[i]->Show_Info();        }    }    cout << endl;    cout << "大三:" << endl;    for (int i = 0; i < this->Student_Num; i++) {        if (this->Student_Array[i]->m_grade == 3) {            this->Student_Array[i]->Show_Info();        }    }    cout << endl;    cout << "大四:" << endl;    for (int i = 0; i < this->Student_Num; i++) {        if (this->Student_Array[i]->m_grade == 4) {            this->Student_Array[i]->Show_Info();        }    }    system("pause");    system("cls");}

    清空数据

    实现了CleanStudent函数,用于清空系统数据。以下是实现代码:

    void StudentManager::CleanStudent() {    cout << "确认清空?" << endl;    cout << "1、确认" << endl;    cout << "2、返回" << endl;    int select = 0;    cin >> select;    if (select == 1) {        ofstream ofs(FILENAME, ios::trunc);        ofs.close();        if (this->Student_Array != NULL) {            for (int i = 0; i < this->Student_Num; i++) {                if (this->Student_Array[i] != NULL) {                    delete this->Student_Array[i];                }            }            this->Student_Num = 0;            delete[] this->Student_Array;            this->Student_Array = NULL;            this->FileIsEmpty = true;        }        cout << "清空成功" << endl;    }    system("pause");    system("cls");}

    项目代码

    项目代码已 successful 提取码:s99z

    转载地址:http://pmhfz.baihongyu.com/

    你可能感兴趣的文章
    mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
    查看>>
    MySQL 存储过程参数:in、out、inout
    查看>>
    mysql 存储过程每隔一段时间执行一次
    查看>>
    mysql 存在update不存在insert
    查看>>
    Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
    查看>>
    Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
    查看>>
    Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
    查看>>
    Mysql 学习总结(89)—— Mysql 库表容量统计
    查看>>
    mysql 实现主从复制/主从同步
    查看>>
    mysql 审核_审核MySQL数据库上的登录
    查看>>
    mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
    查看>>
    mysql 导入导出大文件
    查看>>
    MySQL 导出数据
    查看>>
    mysql 将null转代为0
    查看>>
    mysql 常用
    查看>>
    MySQL 常用列类型
    查看>>
    mysql 常用命令
    查看>>
    Mysql 常见ALTER TABLE操作
    查看>>
    MySQL 常见的 9 种优化方法
    查看>>
    MySQL 常见的开放性问题
    查看>>