C++小小课程设计
最近,第一次接触c++,我花一个周末时间写了一个有关c++的人员管理系统,当然是带着模仿尝试性完成的,我是先学c语言和Java出来的所以对c++的最基础基础的内容还是可以简单完成的,这些代码全手动完成,我就当记录一下!!如果你选学c++课程,做一个小案例,可能这个对你会点有帮助!
案例任务
任务的要求
- 查找人员信息:按照学生的学号或者学生的姓名进行查找相关的学生信息
- 显示人员信息:显示学校内部所有学生的信息
- 增加人员信息:实现批量添加学生,老师,校长等功能,将信息录入到文件中,学生信息:学生学号,学生姓名,学生班级
- 退出管理程序:退出当前管理系统
- 删除退学人员:按照学号删除指定的学生
- 清除所有文档:清空文件中记录的所有学生的信息(清空前需要再次确定,防止误删)
任务的软件(推荐)
任务的方向
关系图
步骤思路
首先创建管理类
1.与用户的沟通菜单的界面
2.对学生增删改查的操作
3.与文件的读写
其次菜单功能
功能描述:与用户的沟通界面
添加人员
功能描述:批量添加人员,并且保存到文件中
用户在批量创建时,可能创建不同种类的人员(学生,老师,校长)
如果想要将所有种类人员都放入一个数组中,可以将所有人员的指针维护到一个数组里
如果想在程序中维护这个不定长度的数组,可以将数组创建在堆区,并利用Person**的指针来维护
文件交互-写文件
功能描述:对文件进行读写
在上一个添加功能中,我们只是将所有的数据添加到了内存中,一旦程序结束就无法保存
因此文件管理类需要一个与文件进行交互的功能,对于文件进行读写的功能
文件交互-读文件
功能描述:将文件中的内容读取到程序中
虽然我们能实现添加文件后的保存文件,但是每次开始运行程序,并没有将文件中的数据读取出来
而且我们程序的功能中还有清空文件的功能需求
1.第一次使用,文件未创建
2.文件存在,但是数据被清空
3.文件存在,并且保存人员的所有数据
获取记录人员人数
显示人员
功能描述:显示当前所有人员信息
删除人员
功能描述:按照人员的信息进行删除
修改人员信息
功能描述:能够按照人员的姓名对人员的信息进行修改
清空文件
功能描述:将文件记录的数据清空
核心代码介绍(教学小小白类)
不多说废话,直接上代码!!!
显示菜单
// 显示菜单
void Manager::ShowMenu() {
cout << "***********************************" << endl;
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 << "** " << endl;
cout << "***********************************" << endl;
cout << endl;
}
初始化文件信息
利用构造函数进行初始化文件信息
1.第一次使用,文件未创建
2.文件存在,但是数据被清空
3.文件存在,并且保存人员的所有数据
Manager::Manager() {
ifstream ifs;
ifs.open(FIFENAME,ios::in);
// 1.文件不存在的情况
if (!ifs.is_open()) {
cout << "***********************************" << endl;
cout << "** 文件不存在" << endl;
cout << "***********************************" << endl;
//初始化属性
//初始化记录人数
this->personNum = 0;
//初始化数组指针
this->PersonArray = NULL;
//初始化文件是否为空
this->FileIsEmpty = true;
ifs.close();
return;
}
// 2.文件存在,并且没有记录的情况
char ch;
ifs >> ch; // 读取到文件txt第一个字节数据
if (ifs.eof()) { // eof为文件txt的末尾标志
cout << "***********************************" << endl;
cout << "** 文件为空" << endl;
cout << "***********************************" << endl;
//初始化属性
//初始化记录人数
this->personNum = 0;
//初始化数组指针
this->PersonArray = NULL;
//初始化文件是否为空
this->FileIsEmpty = true;
ifs.close();
return;
}
// 3.文件存在,并且记录数据的情况
int num = this->get_PerNum();
cout << "***********************************" << endl;
cout << "**\t已经添加" << num << "个人员!" << endl;
cout << "***********************************" << endl;
this->personNum = num;
//开辟空间
this->PersonArray = new Person * [num];
//将文件中的数据存储到数组中
this->init_Person();
}
1.显示人员
事先我存入了3个人员,本着认真态度,信息属实,所以打上马赛克!
// 1.显示人员
// personNum 存放人员数量 PersonArray[] 存放人员信息
void Manager::showPerson() {
// 判断文件是否为空
if (this->FileIsEmpty) {
cout << "***********************************" << endl;
cout << "** 数据没有?怎么显示??" << endl;
cout << "***********************************" << endl;
}
else {
// 利用多态调用程序接口
for (int i = 0; i < this->personNum; i++) {
this->PersonArray[i]->showInfo();
}
}
//按任意键清屏
system("pause");
system("cls");
}
2.增加人员
// 2.增加人员
void Manager::Add_person() {
int AddNum = 0; //记录用户输入的人数
cout << endl;
cout << "***********************************" << endl;
cout << "胖友,请你输入你需要添加多少人(包括学生,老师,校长):" << endl;
cin >> AddNum;
//形式参数,无实际意义
string content1;
string content2;
string content3;
//判断数值是否有误
if (AddNum > 0) {
//动态扩展数组
//计算开辟新空间大小
int newSize = this->personNum + AddNum;
//开辟新空间
Person** newSpace = new Person * [newSize];
//将原来空间的数据,拷贝到新空间下
if (this->PersonArray != NULL) {
for (int i = 0; i < this->personNum; i++) {
newSpace[i] = this->PersonArray[i];
}
}
//批量添加新数据
for (int i = 0; i < AddNum; i++) {
int select; //身份的选择
cout << "***********************************" << endl;
cout << "请你输入添加的第" << i + 1 << "个人员" << endl;
cout << "请选择人员的身份:" << endl;
cout << " 1.学生" << endl;
cout << " 2.老师" << endl;
cout << " 3.校长" << endl;
cin >> select;
while (select != 1 && select != 2 && select != 3) {
cout << "兄弟,你是不是玩弄我的感情?" << endl;
cout << "请你重新选择:" << endl;
cin >> select;
}
Person* person = NULL;
switch (select) {
case 1: cout << "请问这位学生院系是?" << endl;
cin >> content2;
cout << "请问这位学生班级是?" << endl;
cin >> content3;
cout << "请问这位学生姓名是?" << endl;
cin >> content1;
person = new Student(1, content1, content2, content3); //开辟堆区
break;
case 2: cout << "请问这位老师课程是?" << endl;
cin >> content2;
cout << "请问这位老师姓名是?" << endl;
cin >> content1;
cout << "请问这位老师布置任务是?" << endl;
cin >> content3;
person = new Teacher(2, content1, content2, content3);
break;
case 3: cout << "请问这位校长姓名是?" << endl;
cin >> content1;
cout << "请问这位校长管理的学校是?" << endl;
cin >> content2;
cout << "请问这位校长的学校大发展方向是?" << endl;
cin >> content3;
person = new Headmaster(3, content1, content2, content3);
break;
default: break;
}
//保存到数组
newSpace[this->personNum + i] = person;
}
//释放原有空间
delete[] this->PersonArray;
//更改新空间的指针
this->PersonArray = newSpace;
//保存数据到文件中
this->FileSave(newSize);
//更新新的人员人数
this->personNum = newSize;
//更新职工不为空标志
this->FileIsEmpty = false;
//提示添加成功
cout << "成功添加" << AddNum << "个人员!!" << endl;
system("pause");
system("cls");
}
else {
cout << "兄弟,你是不是欺骗我的感情?" << endl;
}
cout << "***********************************" << endl;
cout << endl;
}
父类person
class Person {
public:
//显示个人信息
virtual void showInfo() = 0;
//姓名
string name;
//ID号 1为学生 2为老师 3为校长
int ID;
//信息1
string message;
//信息2
string message2;
};
子类 Student
类推子类Teacher Headmaster
//构造函数
Student::Student(int id, string name, string department,string className) {
this->name = name;
this->message = department;
this->ID = id;
this->message2 = className;
};
//显示个人信息
void Student::showInfo() {
cout << "\n***********************************"
<< "\n**\t学生编号:" << this->ID
<< "\n**\t学生姓名:" << this->name
<< "\n**\t学生院系:" << this->message
<< "\n**\t学生班级:" << this->message2
<< "\n***********************************" << endl;
};
3.删除人员
// 3.删除人员
void Manager::delPerson() {
if (this->FileIsEmpty) {
cout << "***********************************" << endl;
cout << "** 文件都为空?删撒嘞??" << endl;
cout << "***********************************" << endl;
}
else {
cout << "***********************************" << endl;
// 按照人员姓名进行删除
cout << "请输入想要删除人员的姓名: ";
string name;
cin >> name;
int index = this->personIsExist(name);
if (index != -1) { //说明人员存在,并且要删除
//数据前移
for (int i = index; i < this->personNum - 1; i++) {
this->PersonArray[i] = this->PersonArray[i + 1];
}
this->personNum--; // 更新数组中记录的人员个数
//数据同步更新到文件中
this->FileSave(-1);
cout << "删除成功!你真棒!" << endl;
}
else {
cout << " 删撒嘞??有木有??" << endl;
}
cout << "***********************************" << endl;
}
system("pause");
system("cls");
}
4.修改文件
// 4.修改文件
void Manager::modPerson() {
if (this->FileIsEmpty) {
cout << "***********************************" << endl;
cout << "** 文件都为空?删撒嘞??" << endl;
cout << "***********************************" << endl;
}
else {
cout << "***********************************" << endl;
int ID; //存放人员的ID
string name; //存放修改人的姓名
cout << "请输入修改人员的姓名:";
cin >> name;
int index = this->personIsExist(name);
if (index != -1) {
cout << "请选择人员的身份:" << endl;
cout << " 1.学生" << endl;
cout << " 2.老师" << endl;
cout << " 3.校长" << endl;
cin >> ID;
while (ID != 1 && ID != 2 && ID != 3) {
cout << "兄弟,你是不是玩弄我的感情?" << endl;
cout << "请你重新选择:" << endl;
cin >> ID;
}
this->PersonArray[index]->ID = ID ;
delete this->PersonArray[index];
string newMessage=""; //存放修改人的信息
string newMessage2=""; //存放修改人的信息2
Person* person = NULL;
switch (ID) {
case 1: cout << "请问这位学生院系是?" << endl;
cin >> newMessage;
cout << "请问这位学生班级是?" << endl;
cin >> newMessage2;
cout << "请问这位学生姓名是?" << endl;
cin >> name;
person = new Student(1, name, newMessage, newMessage2); //开辟堆区
break;
case 2: cout << "请问这位老师课程是?" << endl;
cin >> newMessage;
cout << "请问这位老师任务是?" << endl;
cin >> newMessage2;
cout << "请问这位老师姓名是?" << endl;
cin >> name;
person = new Teacher(2, name, newMessage, newMessage2);
break;
case 3: cout << "请问这位校长管理的学校是?" << endl;
cin >> newMessage;
cout << "请问这位校长的学校大发展方向是?" << endl;
cin >> newMessage2;
cout << "请问这位校长姓名是?" << endl;
cin >> name;
person = new Headmaster(3, name, newMessage, newMessage2);
break;
default: break;
}
//更改数据到数组中
this->PersonArray[index] = person;
cout << "修改成功" << endl;
//保存到文件中
this->FileSave(-1);
}
else {
cout << "兄弟,查无此人??" << endl;
}
cout << "***********************************" << endl;
}
//按任意键清屏
system("pause");
system("cls");
}
5.退出管理系统
// 5.退出管理系统
void Manager::ExitSystem() {
cout << endl;
cout << "***********************************" << endl;
cout << "** 后会有期!兄弟们!" << endl;
cout << "***********************************" << endl;
cout << endl;
system("pause");
exit(0);
}
6.清空文件
// 6.清空文件
void Manager::cleanFile() {
cout << "***********************************" << endl;
cout << "**\t确定清空文件数据?" << endl;
cout << "**\t1.确定" << endl;
cout << "**\t2.返回" << endl;
int select;
cin >> select;
while (select != 1 && select != 2) {
cout << "Are you really ?" << endl;
cin >> select;
}
if (select == 1) {
// 打开模式 ios::trunc 如果存在删除文件并重新创建
ofstream ofs(FIFENAME, ios::trunc);
ofs.close();
for (int i = 0; i < this->personNum; i++){
if (this->PersonArray[i] != NULL) {
delete this->PersonArray[i];
}
}
this->personNum = 0;
delete[] this->PersonArray;
this->PersonArray = NULL;
this->FileIsEmpty = true;
cout << "清除干净!" << endl;
}
cout << "***********************************" << endl;
system("pause");
system("cls");
}
全部代码(小白白嫖类)
有些人还是很希望看到这个的,这样就CTRL+C 和 CTRL+V就完成!
所有文件如下
main
#include <iostream>
#include "Manager.h"
#include "person.h"
#include "student.h"
#include "teacher.h"
#include "headmaster.h"
using namespace std;
int main() {
//实例化管理对象
Manager manager;
//选择功能
int choice = 0;
while (true) {
//调用展示菜单成员函数
manager.ShowMenu();
cout<<"兄嘚姐妹,请你输入你的选择(1~6):";
// 输入用户的选择
cin >> choice;
switch(choice){
// 1.显示人员信息
case 1: manager.showPerson();
break;
// 2.增加人员信息
case 2: manager.Add_person();
break;
// 3.删除人员信息
case 3: manager.delPerson();
break;
// 4.修改人员信息
case 4: manager.modPerson();
break;
// 5.退出管理系统
case 5: manager.ExitSystem();
break;
// 6.清空所有文档
case 6: manager.cleanFile();
break;
default: system("cls");
break;
}
}
}
manager.cpp
#include "Manager.h"
// 显示菜单
void Manager::ShowMenu() {
cout << "***********************************" << endl;
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 << "** " << endl;
cout << "***********************************" << endl;
cout << endl;
}
// 1.显示人员
void Manager::showPerson() {
// 判断文件是否为空
if (this->FileIsEmpty) {
cout << "***********************************" << endl;
cout << "** 数据没有?怎么显示??" << endl;
cout << "***********************************" << endl;
}
else {
// 利用多态调用程序接口
for (int i = 0; i < this->personNum; i++) {
this->PersonArray[i]->showInfo();
}
}
//按任意键清屏
system("pause");
system("cls");
}
// 2.增加人员
void Manager::Add_person() {
int AddNum = 0; //记录用户输入的人数
cout << endl;
cout << "***********************************" << endl;
cout << "胖友,请你输入你需要添加多少人(包括学生,老师,校长):" << endl;
cin >> AddNum;
//形式参数,无实际意义
string content1;
string content2;
string content3;
//判断数值是否有误
if (AddNum > 0) {
//动态扩展数组
//计算开辟新空间大小
int newSize = this->personNum + AddNum;
//开辟新空间
Person** newSpace = new Person * [newSize];
//将原来空间的数据,拷贝到新空间下
if (this->PersonArray != NULL) {
for (int i = 0; i < this->personNum; i++) {
newSpace[i] = this->PersonArray[i];
}
}
//批量添加新数据
for (int i = 0; i < AddNum; i++) {
int select; //身份的选择
cout << "***********************************" << endl;
cout << "请你输入添加的第" << i + 1 << "个人员" << endl;
cout << "请选择人员的身份:" << endl;
cout << " 1.学生" << endl;
cout << " 2.老师" << endl;
cout << " 3.校长" << endl;
cin >> select;
while (select != 1 && select != 2 && select != 3) {
cout << "兄弟,你是不是玩弄我的感情?" << endl;
cout << "请你重新选择:" << endl;
cin >> select;
}
Person* person = NULL;
switch (select) {
case 1: cout << "请问这位学生院系是?" << endl;
cin >> content2;
cout << "请问这位学生班级是?" << endl;
cin >> content3;
cout << "请问这位学生姓名是?" << endl;
cin >> content1;
person = new Student(1, content1, content2, content3); //开辟堆区
break;
case 2: cout << "请问这位老师课程是?" << endl;
cin >> content2;
cout << "请问这位老师姓名是?" << endl;
cin >> content1;
cout << "请问这位老师布置任务是?" << endl;
cin >> content3;
person = new Teacher(2, content1, content2, content3);
break;
case 3: cout << "请问这位校长姓名是?" << endl;
cin >> content1;
cout << "请问这位校长管理的学校是?" << endl;
cin >> content2;
cout << "请问这位校长的学校大发展方向是?" << endl;
cin >> content3;
person = new Headmaster(3, content1, content2, content3);
break;
default: break;
}
//保存到数组
newSpace[this->personNum + i] = person;
}
//释放原有空间
delete[] this->PersonArray;
//更改新空间的指针
this->PersonArray = newSpace;
//保存数据到文件中
this->FileSave(newSize);
//更新新的人员人数
this->personNum = newSize;
//更新职工不为空标志
this->FileIsEmpty = false;
//提示添加成功
cout << "成功添加" << AddNum << "个人员!!" << endl;
system("pause");
system("cls");
}
else {
cout << "兄弟,你是不是欺骗我的感情?" << endl;
}
cout << "***********************************" << endl;
cout << endl;
}
// 3.删除人员
void Manager::delPerson() {
if (this->FileIsEmpty) {
cout << "***********************************" << endl;
cout << "** 文件都为空?删撒嘞??" << endl;
cout << "***********************************" << endl;
}
else {
cout << "***********************************" << endl;
// 按照人员姓名进行删除
cout << "请输入想要删除人员的姓名: ";
string name;
cin >> name;
int index = this->personIsExist(name);
if (index != -1) { //说明人员存在,并且要删除
//数据前移
for (int i = index; i < this->personNum - 1; i++) {
this->PersonArray[i] = this->PersonArray[i + 1];
}
this->personNum--; // 更新数组中记录的人员个数
//数据同步更新到文件中
this->FileSave(-1);
cout << "删除成功!你真棒!" << endl;
}
else {
cout << " 删撒嘞??有木有??" << endl;
}
cout << "***********************************" << endl;
}
system("pause");
system("cls");
}
// 4.修改文件
void Manager::modPerson() {
if (this->FileIsEmpty) {
cout << "***********************************" << endl;
cout << "** 文件都为空?删撒嘞??" << endl;
cout << "***********************************" << endl;
}
else {
cout << "***********************************" << endl;
int ID; //存放人员的ID
string name; //存放修改人的姓名
cout << "请输入修改人员的姓名:";
cin >> name;
int index = this->personIsExist(name);
if (index != -1) {
cout << "请选择人员的身份:" << endl;
cout << " 1.学生" << endl;
cout << " 2.老师" << endl;
cout << " 3.校长" << endl;
cin >> ID;
while (ID != 1 && ID != 2 && ID != 3) {
cout << "兄弟,你是不是玩弄我的感情?" << endl;
cout << "请你重新选择:" << endl;
cin >> ID;
}
this->PersonArray[index]->ID=ID;
delete this->PersonArray[index];
string newMessage=""; //存放修改人的信息
string newMessage2=""; //存放修改人的信息2
Person* person = NULL;
switch (ID) {
case 1: cout << "请问这位学生院系是?" << endl;
cin >> newMessage;
cout << "请问这位学生班级是?" << endl;
cin >> newMessage2;
cout << "请问这位学生姓名是?" << endl;
cin >> name;
person = new Student(1, name, newMessage, newMessage2); //开辟堆区
break;
case 2: cout << "请问这位老师课程是?" << endl;
cin >> newMessage;
cout << "请问这位老师任务是?" << endl;
cin >> newMessage2;
cout << "请问这位老师姓名是?" << endl;
cin >> name;
person = new Teacher(2, name, newMessage, newMessage2);
break;
case 3: cout << "请问这位校长管理的学校是?" << endl;
cin >> newMessage;
cout << "请问这位校长的学校大发展方向是?" << endl;
cin >> newMessage2;
cout << "请问这位校长姓名是?" << endl;
cin >> name;
person = new Headmaster(3, name, newMessage, newMessage2);
break;
default: break;
}
//更改数据到数组中
this->PersonArray[index] = person;
cout << "修改成功" << endl;
//保存到文件中
this->FileSave(-1);
}
else {
cout << "兄弟,查无此人??" << endl;
}
cout << "***********************************" << endl;
}
//按任意键清屏
system("pause");
system("cls");
}
// 5.退出管理系统
void Manager::ExitSystem() {
cout << endl;
cout << "***********************************" << endl;
cout << "** 后会有期!兄弟们!" << endl;
cout << "***********************************" << endl;
cout << endl;
system("pause");
exit(0);
}
// 6.清空文件
void Manager::cleanFile() {
cout << "***********************************" << endl;
cout << "**\t确定清空文件数据?" << endl;
cout << "**\t1.确定" << endl;
cout << "**\t2.返回" << endl;
int select;
cin >> select;
while (select != 1 && select != 2) {
cout << "Are you really ?" << endl;
cin >> select;
}
if (select == 1) {
// 打开模式 ios::trunc 如果存在删除文件并重新创建
ofstream ofs(FIFENAME, ios::trunc);
ofs.close();
for (int i = 0; i < this->personNum; i++){
if (this->PersonArray[i] != NULL) {
delete this->PersonArray[i];
}
}
this->personNum = 0;
delete[] this->PersonArray;
this->PersonArray = NULL;
this->FileIsEmpty = true;
cout << "清除干净!" << endl;
}
cout << "***********************************" << endl;
system("pause");
system("cls");
}
// 保存文件
void Manager::FileSave(int newSize) {
ofstream ofs;
if (newSize == -1) {
ofs.open(FIFENAME, ios::out); //用输出的方式打开文件 --写文件
//将每个人的数据写入到文件中
for (int i = 0; i < this->personNum; i++) {
ofs << this->PersonArray[i]->ID << " "
<< this->PersonArray[i]->name << " "
<< this->PersonArray[i]->message << " "
<< this->PersonArray[i]->message2 << endl;
}
}
else {
ofs.open(FIFENAME, ios::app); //用输出的方式打开文件 --写文件
//将每个人的数据写入到文件中
for (int i = this->personNum; i < newSize; i++) {
ofs << this->PersonArray[i]->ID << " "
<< this->PersonArray[i]->name << " "
<< this->PersonArray[i]->message << " "
<< this->PersonArray[i]->message2 << endl;
}
}
cout << "保存文件成功!" << endl;
//关闭文件流
ofs.close();
}
// 统计文件人数
int Manager::get_PerNum() {
ifstream ifs;
ifs.open(FIFENAME,ios::in);
int ID;
string name;
string message;
string message2;
int num = 0;
while (ifs >> ID && ifs >> name && ifs >> message && ifs >> message2) {
//记录人数
num++;
}
ifs.close();
return num;
}
// 初始化人员
void Manager::init_Person() {
ifstream ifs;
ifs.open(FIFENAME, ios::in);
int index = 0; //下标
int ID;
string name;
string message;
string message2;
while (ifs >> ID && ifs >> name && ifs >> message && ifs >> message2) {
Person* person = NULL;
if (ID == 1) { //学生
person = new Student(ID, name, message, message2);
}
else if (ID == 2) { //老师
person = new Teacher(ID, name, message, message2);
}
else { //校长
person = new Headmaster(ID, name, message, message2);
}
this->PersonArray[index] = person;
index++;
}
// 关闭文件
ifs.close();
}
//判断人员是否存在 如果存在返回职工所在数组中的位置,不存在则返回-1
int Manager::personIsExist(string name) {
int index = -1;
for (int i = 0; i < this->personNum; i++) {
if (this->PersonArray[i]->name == name) {
index = i;
break;
}
}
return index;
}
Manager::Manager() {
ifstream ifs;
ifs.open(FIFENAME,ios::in);
// 1.文件不存在的情况
if (!ifs.is_open()) {
cout << "***********************************" << endl;
cout << "** 文件不存在" << endl;
cout << "***********************************" << endl;
//初始化属性
//初始化记录人数
this->personNum = 0;
//初始化数组指针
this->PersonArray = NULL;
//初始化文件是否为空
this->FileIsEmpty = true;
ifs.close();
return;
}
// 2.文件存在,并且没有记录的情况
char ch;
ifs >> ch; // 读取到文件txt第一个字节数据
if (ifs.eof()) { // eof为文件txt的末尾标志
cout << "***********************************" << endl;
cout << "** 文件为空" << endl;
cout << "***********************************" << endl;
//初始化属性
//初始化记录人数
this->personNum = 0;
//初始化数组指针
this->PersonArray = NULL;
//初始化文件是否为空
this->FileIsEmpty = true;
ifs.close();
return;
}
// 3.文件存在,并且记录数据的情况
int num = this->get_PerNum();
cout << "***********************************" << endl;
cout << "**\t已经添加" << num << "个人员!" << endl;
cout << "***********************************" << endl;
this->personNum = num;
//开辟空间
this->PersonArray = new Person * [num];
//将文件中的数据存储到数组中
this->init_Person();
//测试代码
/*
for (int i = 0; i < num; i++) {
cout << "** " ;
switch (this->PersonArray[i]->ID) {
case 1: cout << "学生编号:" << this->PersonArray[i]->ID << " * "
<< "学生姓名:" << this->PersonArray[i]->name << " * "
<< "学生院系:" << this->PersonArray[i]->message << " * "
<< "学生班级:" << this->PersonArray[i]->message2 << endl;
cout << "**" << endl;
break;
case 2: cout << "老师编号:" << this->PersonArray[i]->ID << " * "
<< "老师姓名:" << this->PersonArray[i]->name << " * "
<< "老师课程:" << this->PersonArray[i]->message << " * "
<< "老师任务:" << this->PersonArray[i]->message2 << endl;
cout << "**" << endl;
break;
case 3: cout << "校长编号:" << this->PersonArray[i]->ID << " * "
<< "校长姓名:" << this->PersonArray[i]->name << " * "
<< "管理的学校:" << this->PersonArray[i]->message << " * "
<< "学校的方向:" << this->PersonArray[i]->message2 << endl;
cout << "**" << endl;
break;
}
}
cout << "*********************************************************************************************" << endl;
*/
};
Manager::~Manager() {
if (this->PersonArray != NULL) {
delete[] this->PersonArray;
this->PersonArray = NULL;
}
};
manager.h
#pragma once //防止头文件重复包含
#include <iostream> //包含输入输出流
#include "person.h"
#include "student.h"
#include "teacher.h"
#include "headmaster.h"
#include "string.h"
#include <fstream>
#define FIFENAME "perFile.txt"
using namespace std; //使用标准的命名空间
class Manager {
public:
// 构造函数
Manager();
//析构函数
~Manager();
//显示菜单
void ShowMenu();
//退出管理系统
void ExitSystem();
//记录登记人数
int personNum;
//人员数组指针
Person **PersonArray;
//增加人员
void Add_person();
//保存文件
void FileSave(int newSize);
//判断文件是否为空标志
bool FileIsEmpty;
//统计文件人数
int get_PerNum();
//初始化员工
void init_Person();
//显示人员
void showPerson();
//删除人员
void delPerson();
//判断人员是否存在 如果存在返回职工所在数组中的位置,不存在则返回-1
int personIsExist(string name);
//修改文件
void modPerson();
//清空文件
void cleanFile();
};
person.h
#pragma once
#include <iostream>
#include <String>
using namespace std;
class Person {
public:
//显示个人信息
virtual void showInfo() = 0;
//姓名
string name;
//ID号 1为学生 2为老师 3为校长
int ID;
//信息1
string message;
//信息2
string message2;
};
student.cpp
#include <iostream>
#include "student.h"
//构造函数
Student::Student(int id, string name, string department,string className) {
this->name = name;
this->message = department;
this->ID = id;
this->message2 = className;
};
//显示个人信息
void Student::showInfo() {
cout << "\n***********************************"
<< "\n**\t学生编号:" << this->ID
<< "\n**\t学生姓名:" << this->name
<< "\n**\t学生院系:" << this->message
<< "\n**\t学生班级:" << this->message2
<< "\n***********************************" << endl;
};
student.h
#pragma once
#include <iostream>
#include <string.h>
#include "person.h"
using namespace std;
class Student :public Person {
public:
//构造函数
Student(int id, string name,string department,string className);
//显示个人信息
virtual void showInfo();
};
teacher.cpp
#include <iostream>
#include "teacher.h"
//构造函数
Teacher::Teacher(int id, string name, string lesson, string maketask) {
this->ID = id;
this->message = lesson;
this->name = name;
this->message2 = maketask;
}
//显示个人信息
void Teacher::showInfo() {
cout << "\n***********************************"
<< "\n**\t老师编号:" << this->ID
<< "\n**\t老师姓名:" << this->name
<< "\n**\t老师课程:" << this->message
<< "\n**\t老师任务:" << this->message2
<< "\n***********************************" << endl;
}
teacher.h
#pragma once
#include <iostream>
#include <string.h>
#include "person.h"
using namespace std;
//老师类
class Teacher :public Person {
public:
//构造函数
Teacher(int id,string name,string lesson,string maketask);
//显示个人信息
virtual void showInfo();
};
headmaster.cpp
#include <iostream>
#include "headmaster.h"
using namespace std;
//构造函数
Headmaster::Headmaster(int ID,string name, string schoolName, string schoolAim) {
this->ID = ID;
this->name = name;
this->message = schoolAim;
this->message2= schoolName;
};
//显示个人信息
void Headmaster::showInfo() {
cout << "\n***********************************"
<< "\n**\t校长编号:" << this->ID
<< "\n**\t校长姓名:" << this->name
<< "\n**\t学校名称:" << this->message2
<< "\n**\t学校方向:" << this->message
<< "\n***********************************" << endl;
};
headmaster.h
#pragma once
#include <iostream>
#include <String>
#include "person.h"
using namespace std;
class Headmaster :public Person {
public:
//构造函数
Headmaster(int ID,string name,string schoolName,string schoolAim );
//显示个人信息
virtual void showInfo();
};