本文共 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
类作为基类,分别创建了Grade01
、Grade02
、Grade03
和Grade04
四个子类,分别对应不同年级的学生。以下是部分实现代码:
// 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/