不做教程,这玩意一般都是大学生实训课的时候要求的,你都到这来找了,还指望会看教程?废话不多说,直接上代码。
#include <iostream>
#include<fstream>
using namespace std;
//员工类
class emplee {
public:
virtual void get_massege() = 0;//获取员工信息
int person_number;//员工编号
int function;//部门编号
string name;
};
class worker :public emplee {
public:
worker(int m_id, int f_id, string e_name) {
this->person_number = m_id;
this->function = f_id;
this->name = e_name;
}
void get_massege() override {
cout << "职工姓名" << this->name
<< "\t岗位编号" << this->function
<< "\t职工编号" << this->func_name();
}
string func_name() {
return string("yaungong");
}
};
//经理类
class mananager :public emplee {
public:
mananager(int m_id, int f_id, string e_name) {
this->person_number = m_id;
this->function = f_id;
this->name = e_name;
}
void get_massege() override {
cout << "职工姓名" << this->name
<< "\t岗位编号" << this->function
<< "\t职位名称" << this->func_name();
}
string func_name() {
return string("jingli");
}
};
//老板类
class bosser :public emplee {
public:
bosser(int m_id, int f_id, string e_name) {
this->person_number = m_id;
this->function = f_id;
this->name = e_name;
}
void get_massege() override {
cout << "职工姓名" << this->name
<< "\t岗位编号" << this->function
<< "\t职位名称" << this->func_name();
}
string func_name() {
return string("laoban");
}
};
class workermanager {
public:
workermanager() {
ifstream ifs;
ifs.open("TextFile1.txt", ios::in);
//如果文件打开失败
if (!ifs.is_open()) {
cout << "文件不存在" << endl;
this->emp_number = 0;
this->emp_array = NULL;
this->is_null = true;
ifs.close();
return;
}
//文件存在但没有数据
char ch;
ifs >> ch;
if (ifs.eof()) {
cout << "没有数据" << endl;
this->emp_number = 0;
this->emp_array = NULL;
this->is_null = true;
ifs.close();
return;
}
//文件存在并记录数据
int num = this->get_allperson();
cout << "当前有" << num << "人" << endl;
this->emp_number = num;
this->emp_array = new emplee*[this->emp_number];
this->initarray();
}//构造函数
~workermanager();//析构函数
void show_menue();//菜单
void exitsystem();//退出
void add_emp();//添加职工
void save();//保存文件
void initarray();//初始化员工数组
int emp_number;//职工人数
emplee** emp_array;
int get_allperson();//获取所有人数
void showinto();//展示信息
int is_in(string name);//判断职工是否存在
void del_person();//删除职工
void change();//更改信息
void find_x();//查找员工信息
void clean_f();//清空文件
bool is_null;//判断文件是否为空
};
void workermanager::show_menue() {
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.mdzz..................." << endl;
}
workermanager::~workermanager() {
if (this->emp_array != NULL) {
delete[]this->emp_array;
this->emp_array = NULL;
}
}
void workermanager::add_emp() {
cout << "输入职工数量" << endl;
int addnumber = 0;
cin >> addnumber;
if (addnumber > 0) {
int newsize = this->emp_number + addnumber;
emplee** newspace = new emplee * [newsize];
if (this->emp_array != NULL) {
for (int i = 0; i < this->emp_number; i++) {
newspace[i] = this->emp_array[i];
}
}
//添加新数据
for (int j = 0; j < addnumber; j++) {
int spaceid;
int id;
string name;
cout << "请输入第" << j + 1 << "个员工的编号" << endl;
cin >> id;
cout << "请输入第" << j + 1 << "个职工的姓名" << endl;
cin >> name;
cout << "请输入第" << j + 1 << "个职工的岗位编号" << endl;
cin >> spaceid;
emplee* pworker = NULL;
switch (spaceid) {
case 408:
pworker = new worker(id, spaceid, name);
break;
case 407:
pworker = new mananager(id, spaceid, name);
break;
case 406:
pworker = new bosser(id, spaceid, name);
break;
default:
cout << "你tm输错了" << endl;
break;
}
cout << "添加成功!" << endl;
newspace[this->emp_number + j] = pworker;
}
delete this->emp_array;//删除原有员工数组
this->emp_array = newspace;//将员工数组指向newspace
emp_number = newsize;
this->is_null = false;
this->save();
}
else {
cout << "输错了!" << endl;
}
}
//菜单界面离开功能
void workermanager::exitsystem() {
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
//保存文件功能
void workermanager::save() {
ofstream ofs;
ofs.open("TextFile1.txt", ios::out);
if (ofs.is_open()) {
for (int i = 0; i < this->emp_number; i++) {
ofs << this->emp_array[i]->name << " "
<< this->emp_array[i]->person_number << " "
<< this->emp_array[i]->function << endl;
cout << "第" << i+1 << "个员工保存成功!" << endl;
}
}
else {
cout << "文件打开失败" << endl;
}
ofs.close();
}
int workermanager::get_allperson() {
ifstream ifs;
ifs.open("TextFile1.txt", ios::in);
string newname;
int newid;
int newspaceid;
int number=0;
while (ifs >> newname&&ifs>> newid&&ifs>> newspaceid ){
number++;
}
return number;
}
void workermanager::initarray() {
ifstream ifs;
ifs.open("TextFile1.txt", ios::in);
int id2;
int sid2;
string name2;
int index = 0;
while (ifs >> name2 && ifs >> id2 && ifs >> sid2) {
emplee * worker1 = NULL;
if (sid2 == 406) {
worker1=new worker( id2, sid2,name2);
}
else if (sid2 == 407) {
worker1=new mananager( id2, sid2,name2);
}
else {
worker1=new bosser( id2, sid2,name2);
}
this->emp_array[index] = worker1;
index++;
}
}
void workermanager::showinto() {
for (int i = 0; i < emp_number; i++) {
cout << this->emp_array[i]->name << "/t" << this->emp_array[i]->person_number << "/t" << this->emp_array[i]->function << endl;
}
}
int workermanager::is_in(string name) {
int index = -1;
for (int i = 0; i < emp_number; i++) {
if (this->emp_array[i]->name==name) {
index = i;
break;
}
}
return index;
}
void workermanager::del_person() {
string Name;
cout << "输入查询名字" << endl;
cin >> Name;
int i = is_in(Name);
if (i != -1) {
for (i; i < emp_number-1; i++) {
this->emp_array[i] = this->emp_array[i + 1];
}
}
emp_number--;
save();
cout << "删除成功!" << endl;
cout << "当前有" << emp_number << "人" << endl;
}
void workermanager::change() {
string Name;
int number=0;
cout << "你要更改几个人?" << endl;
cin >> number;
for (int j = 0; j < number ; j++) {
cout << "输入查询名字" << endl;
cin >> Name;
int i = is_in(Name);
string newname = "";
int id = 0;
int sid = 0;
if (i != -1) {
cout << "输入新名字" << endl;
cin >> newname;
emp_array[i]->name = newname;
cout << "输入新编号" << endl;
cin >> id;
emp_array[i]->person_number = id;
cout << "输入新部门编号" << endl;
cin >> sid;
emp_array[i]->function = sid;
}
}
save();
}
void workermanager::find_x() {
string Name;
cout << "输入查询名字" << endl;
cin >> Name;
int i = is_in(Name);
if (i != -1) {
cout << emp_array[i]->name << "/t" << emp_array[i]->function << "/t" << emp_array[i]->person_number << endl;
}
}
void workermanager::clean_f() {
cout << "你确定清空吗?" << "/t" << "1.确定" << "/t" << "2.返回" << endl;
int index = 0;
cin >> index;
if (index == 1) {
ofstream ofs("TextFile1.txt",ios::trunc);
ofs.close();
if (this->emp_array != NULL) {
delete[]this->emp_array;
this->emp_array = NULL;
this->emp_number = 0;
this->is_null = true;
}
}
else {
cout << "输错了,算了反正都一样" << endl;
}
}
int main()
{
int chioce = 0;
workermanager obj;
while (true) {
obj.show_menue();
cout << "请选出你的选择" << endl;
cin >> chioce;
switch (chioce)
{
case 0:
obj.exitsystem();
system("pause");
system("cls");
break;
case 1:
obj.add_emp();
break;
case 2:
obj.showinto();
break;
case 3:
obj.del_person();
break;
case 4:
obj.change();
break;
case 5:
obj.find_x();
break;
case 6:
obj.clean_f();
break;
default://清屏
system("pause");
system("cls");
break;
}
}
}
里面有个文件名,自己建一个新的,改改名字这代码就能跑了。