1、问题描述
某公司需要存储雇员的编号、姓名、性别、所在部门,级别,并进行工资的计算。其中,雇员分为经理、技术人员、销售人员和销售经理。
设计一程序能够对公司人员进行管理,应用到继承、抽象类、虚函数、虚基类、多态和文件的输入/输出等内容。
2、功能要求
(1)添加功能:程序能够任意添加上述四类人员的记录,可提供选择界面供用户选择所要添加的人员类别,要求员工的编号要唯一,如果添加了重复编号的记录时,则提示数据添加重复并取消添加。
(2)查询功能:可根据编号、姓名等信息对已添加的记录进行查询,如果未找到,给出相应的提示信息,如果找到,则显示相应的记录信息;
(3)显示功能:可显示当前系统中所有记录,每条记录占据一行。
(4)编辑功能:可根据查询结果对相应的记录进行修改,修改时注意编号的唯一性。
(5)删除功能:主要实现对已添加的人员记录进行删除。如果当前系统中没有相应的人员记录,则提示“记录为空!”并返回操作;否则,输入要删除的人员的编号或姓名,根据所输入的信息删除该人员记录,如果没有找到该人员信息,则提示相应的记录不存。
(6)统计功能:能根据多种参数进行人员的统计。例如,统计四类人员数量以及总数,
或者统计男、女员工的数量等信息。
(7)保存功能:可将当前系统中各类人员记录存入文件中,存入方式任意。
(8)读取功能:可将保存在文件中的人员信息读入到当前系统中,供用户进行使用。
3、问题的解决方案
根据系统功能要求,可以将问题解决分为以下步骤:
(1)应用系统分析,建立该系统的功能模块框图以及界面的组织和设计;
(2)分析系统中的各个实体及它们之间的关系;
(3)根据问题描述,设计系统的类层次;
(4)完成类层次中各个类的描述;
(5)完成类中各个成员函数的定义;
(6)完成系统的应用模块;
(7)功能调试;
(8)完成系统总结报告。
4、程序功能介绍
5、程序源代码
//***************
//** 1.cpp **
//***************
#include <iostream>
#include <string.h>
#include <string>
#include <cstdio>
#include "1.h"
using namespace std;
System::System() //初始化:数据归零
{
for(int i = 0; i < 30; i ++)
{
pCompany[i] = 0;
}
}
void System:: PushTechnician() //添加技术人员
{
int n;
for(n = 0; pCompany[n] != 0&&n < 30; n ++);
Loop:
pCompany[n] = new Technician;
pCompany[n]->Add(); //查看添加数据的ID是否重复
for(int i = 0; i < 30; i ++)
{
if(i != n&&pCompany[i] != 0&&pCompany[i]->id == pCompany[n]->id)
{
cout<<"The ID has been recorded, please try again."<<endl<<endl;
delete pCompany[n];
goto Loop;
}
}
}
void System:: PushManager() //添加经理
{
int n;
for(n = 0; pCompany[n] != 0&&n < 30; n ++);
Loop:
pCompany[n] = new Manager;
pCompany[n]->Add(); //查看添加数据的ID是否重复
for(int i = 0; i < 30; i ++)
{
if(i != n&&pCompany[i] != 0&&pCompany[i]->id == pCompany[n]->id)
{
cout<<"The ID has been recorded, please try again."<<endl<<endl;
delete pCompany[n];
goto Loop;
}
}
}
void System:: PushSaleman() //添加销售人员
{
int n;
for(n = 0; pCompany[n] != 0&&n < 30; n ++);
Loop:
pCompany[n] = new Saleman;
pCompany[n]->Add(); //查看添加数据的ID是否重复
for(int i = 0; i < 30; i ++)
{
if(i != n&&pCompany[i] != 0&&pCompany[i]->id == pCompany[n]->id)
{
cout<<"The ID has been recorded, please try again."<<endl<<endl;
delete pCompany[n];
goto Loop;
}
}
}
void System:: PushSalemanager() //添加销售经理
{
int n;
for(n = 0; pCompany[n] != 0&&n < 30; n ++);
Loop:
pCompany[n] = new Salemanager;
pCompany[n]->Add(); //查看添加数据的ID是否重复
for(int i = 0; i < 30; i ++)
{
if(i != n&&pCompany[i] != 0&&pCompany[i]->id == pCompany[n]->id)
{
cout<<"The ID has been recorded, please try again."<<endl<<endl;
delete pCompany[n];
goto Loop;
}
}
}
// 通过ID查
int System::QueryById()
{
string id;
cout<<"please input the ID: ";
cin>>id;
cout<<endl;
int i;
for(i = 0; i < 30; i ++)
{
if(pCompany[i] != 0&&id == pCompany[i]->id)
break;
}
if(i < 30)
{
pCompany[i]->showInform();
cout<<endl;
EditMenu: //可以查询到,前往编辑功能
cout<<"Do you want to edit about it?(Y/N)"<<endl;
cout<<"1.Yes"<<endl;
cout<<"2.No"<<endl;
int select;
cout<<"your choice: ";
cin>>select;
cout<<endl;
switch(select)
{
case 1:
Edit(i);
return 0;
break;
case 2:
break;
default:
cout<<"error!!!"<<endl<<endl;
goto EditMenu;
}
DeleteMenu:
cout<<"Do you want to delete it?(Y/N)"<<endl;
cout<<"1.Yes"<<endl;
cout<<"2.No"<<endl;
int judge;
cout<<"your choice: ";
cin>>judge;
cout<<endl;
switch(judge)
{
case 1:
Delete(i);
break;
case 2:
break;
default:
cout<<"error!!!"<<endl<<endl;
goto DeleteMenu;
}
}
else
cout<<"Please retry the ID."<<endl<<endl;
return 0;
}
// 通过名字查
int System::QueryByName()
{
string name;
cout<<"please input the name: ";
cin>>name;
cout<<endl;
int i;
for(i = 0; i < 30; i ++)
{
if(pCompany[i] != 0&&name == pCompany[i]->name)
break;
}
if(i < 30)
{
pCompany[i]->showInform();
cout<<endl;
// 在能够查询到的情况下进行编辑功能
EditMenu:
cout<<"Do you want to edit about it?(Y/N)"<<endl;
cout<<"1.Yes"<<endl;
cout<<"2.No"<<endl;
int select;
cout<<"your choice: ";
cin>>select;
cout<<endl;
switch(select)
{
case 1:
Edit(i);
break;
case 2:
break;
case 3:
cout<<"error!!!"<<endl<<endl;
goto EditMenu;
}
DeleteMenu:
cout<<"Do you want to delete it?(Y/N)"<<endl;
cout<<"1.Yes"<<endl;
cout<<"2.No"<<endl;
int judge;
cout<<"your choice: ";
cin>>judge;
cout<<endl;
switch(judge)
{
case 1:
Delete(i);
break;
case 2:
break;
default:
cout<<"error!!!"<<endl<<endl;
goto DeleteMenu;
}
}
else
cout<<"Please retry the name."<<endl<<endl;
return 0;
}
// 简单显示全部信息功能
void System::Display()
{
for(int i = 0; i < 30; i ++)
{
if(pCompany[i] != 0)
pCompany[i]->showInform();
}
}
// 编辑功能
void System::Edit(int n)
{
Company* editor;
// 判断是哪种职员
if(pCompany[n]->Type() == 1)
{
editor = new Technician;
}
if(pCompany[n]->Type() == 2)
{
editor = new Manager;
}
if(pCompany[n]->Type() == 3)
{
editor = new Saleman;
}
if(pCompany[n]->Type() == 0)
{
editor = new Salemanager;
}
editor->Add();
for(int i = 0; i < 30; i ++)
{
if(pCompany[i] != 0&&editor->id == pCompany[i]->id)
{
cout<<"Please try again."<<endl<<endl;
delete editor;
goto End;
}
}
delete pCompany[n];
pCompany[n] = editor;
End:;
}
// 删除功能
void System::Delete(int n)
{
delete pCompany[n];
pCompany[n] = 0;
}
// 统计系统现有职员总数
void System::TotalNum()
{
int n = 0;
for(int i = 0; i < 30; i ++)
{
if(pCompany[i] != 0)
n ++;
}
cout<<"the total number of the offices: "<<n<<endl<<endl;
}
// 统计系统中男女职员人数
void System::StatGender()
{
int man = 0, woman = 0;
for(int i = 0; i < 30; i ++)
{
if(pCompany[i] == 0)
continue;
else if(pCompany[i]->gender == 77) // “M”的ASC码是77
man ++;
else if(pCompany[i]->gender == 70) // “F”的ASC码是70
woman ++;
}
cout<<"the number of men: "<<man<<endl;
cout<<"the number of women: "<<woman<<endl<<endl;
}
// 统计各个级别的职员人数
void System::StatLevel()
{
int technician = 0,manager = 0, saleman = 0, salemanager = 0;
for(int i = 0; i < 30; i ++)
{
if(pCompany[i] == 0)
continue;
else if(pCompany[i]->Type() == 1)
technician ++;
else if(pCompany[i]->Type() == 2)
manager ++;
else if(pCompany[i]->Type() == 3)
saleman ++;
else if(pCompany[i]->Type() == 3)
salemanager ++;
}
cout<<"the number of Technicians: "<<technician<<endl;
cout<<"the number of Managers: "<<manager<<endl;
cout<<"the number of Salemans: "<<saleman<<endl;
cout<<"the number of Salemanagers: "<<salemanager<<endl<<endl;
}
// 将文件里的数据写入到系统中
int System::FileIn()
{
ifstream technicianin("technician.txt",ios::binary);
ifstream managerin("manager.txt",ios::binary);
ifstream salemanin("saleman.txt",ios::binary);
ifstream salemanagerin("salemanager.txt",ios::binary);
if( (!technicianin) || (!managerin) || (!salemanin) || (!salemanagerin))
{
cout<<"The loading file process is error. There are no information."<<endl<<endl;
return 0;
}
Technician technician_temp;
Managerin managerin_temp;
College_Student college_temp;
int i = 0;
while(technicianin.peek() != EOF)
{
technicianin.read((char*)&technician_temp, sizeof(Technician));
pStudent[i] = &technician_temp;
i ++;
}
while(managerin.peek() != EOF)
{
managerin.read((char*)&manager_temp, sizeof(Manager));
pCompany[i] = &manager_temp;
i ++;
}
while(salemanin.peek() != EOF)
{
salemanin.read((char*)&saleman_temp, sizeof(Salemanin));
pCompany[i] = &saleman_temp;
i ++;
}
while(salemanagerin.peek() != EOF)
{
salemanagerin.read((char*)&salemanager_temp, sizeof(Salemanagerin));
pCompany[i] = &salemanager_temp;
i ++;
}
technicianin.close();
managerin.close();
salemanin.close();
salemanagerin.close();
return 0;
}
// 将系统中的数据保存到文件中
int System::Save()
{
remove("technician.txt");
remove("manager.txt");
remove("saleman.txt");
remove("salemanager.txt");
ofstream technicianout("technician.txt",ios::binary);
ofstream middleout("middle.txt",ios::binary);
ofstream collegeout("saleman.txt",ios::binary);
ofstream collegeout("salemanager.txt",ios::binary);
if( (!technicianout) || (!managerout) || (!salemanout) || (!salemanager))
{
cout<<"error!!!"<<endl<<endl;
return 0;
}
for(int i = 0; i < 30; i ++)
{
if(pCompany[i] == 0)
continue;
else if(pCompany[i]->Type() == 1)
{
technicianout.write((char*)pCompany[i], sizeof(Technician));
}
else if(pCompany[i]->Type() == 2)
{
managerout.write((char*)pCompany[i], sizeof(Manager);
}
else if(pCompany[i]->Type() == 3)
{
salemanout.write((char*)pCompany[i], sizeof(Saleman));
}
else if(pCompany[i]->Type() == 0)
{
salemanagerout.write((char*)pCompany[i], sizeof(Salemanager));
}
}
technicianout.close();
managerout.close();
salemanout.close();
salemanagerout.close();
cout<<"The information has been saved."<<endl<<endl;
return 0;
}
// 技术人员成员函数定义
void Technician::Add()
{
cout<<"the office's ID: ";
cin>>id;
cout<<"the office's name: ";
cin>>name;
cout<<"the gender(M/F): ";
cin>>gender;
cout<<"which sector: ";
cin>>sector;
cout<<"the rank: ";
cin>>rank;
cout<<"wages: ";
cin>>wages;
cout<<"The information has been recorded!"<<endl<<endl;
}
void Technician::showInform()
{
cout<<"ID: "<<id<<" "<<"name: "<<name<<" ";
cout<<"gender: "<<gender<<" "<<"sector: "<<sector<<" ";
cout<<"Rank: "<<rank<<" "<<"wages: "<<wages<<" "<<endl;
}
// 经理成员函数的定义
void Manager::Add()
{
cout<<"the office's ID: ";
cin>>id;
cout<<"the office's name: ";
cin>>name;
cout<<"the gender(M/F): ";
cin>>gender;
cout<<"which sector: ";
cin>>sector;
cout<<"the rank: ";
cin>>rank;
cout<<"wages: ";
cin>>wages;
cout<<"The information has been recorded!"<<endl<<endl;
}
void Manager::showInform()
{
cout<<"ID: "<<id<<" "<<"name: "<<name<<" ";
cout<<"gender: "<<gender<<" "<<"sector: "<<sector<<" ";
cout<<"Rank: "<<rank<<" "<<"wages: "<<wages<<" "<<endl;
}
// 销售人员成员函数定义
void Saleman::Add()
{
cout<<"the office's ID: ";
cin>>id;
cout<<"the office's name: ";
cin>>name;
cout<<"the gender(M/F): ";
cin>>gender;
cout<<"which sector: ";
cin>>sector;
cout<<"the rank: ";
cin>>rank;
cout<<"wages: ";
cin>>wages;
cout<<"The information has been recorded!"<<endl<<endl;
}
void Saleman::showInform()
{
cout<<"ID: "<<id<<" "<<"name: "<<name<<" ";
cout<<"gender: "<<gender<<" "<<"sector: "<<sector<<" ";
cout<<"Rank: "<<rank<<" "<<"wages: "<<wages<<" "<<endl;
}
// 销售经理成员函数定义
void Salemanager::Add()
{
cout<<"the office's ID: ";
cin>>id;
cout<<"the office's name: ";
cin>>name;
cout<<"the gender(M/F): ";
cin>>gender;
cout<<"which sector: ";
cin>>sector;
cout<<"the rank: ";
cin>>rank;
cout<<"wages: ";
cin>>wages;
cout<<"The information has been recorded!"<<endl<<endl;
}
void Salemanager::showInform()
{
cout<<"ID: "<<id<<" "<<"name: "<<name<<" ";
cout<<"gender: "<<gender<<" "<<"sector: "<<sector<<" ";
cout<<"Rank: "<<rank<<" "<<"wages: "<<wages<<" "<<endl;
}
//***************
//** 2.cpp **
//***************
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include "1.cpp"
using namespace std;
//UI用户界面
{
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<<"请选择0-5(输入相应的数字): ";
}
// 添加功能
void AddStudent(System& Frontier)
{
AddMenu:
cout<<"please choose the type want to add:"<<endl;
cout<<"1.Technician"<<endl;
cout<<"2.Manager"<<endl;
cout<<"3.Saleman"<<endl;
cout<<"4.Salemanager"<<endl;
cout<<"5.Back."<<endl;
cout<<"Please enter your choice here: ";
int choice;
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
Frontier.PushTechnician();
break;
case 2:
Frontier.PushManage();
break;
case 3:
Frontier.PushSaleman();
case 4:
Frontier.PushSalemanager();
case 5:
break;
default:
cout<<"Incorrectly input!!!"<<endl<<endl;
goto AddMenu;
}
}
// 查询功能
void FindStudent(System& Frontier)
{
FindMenu:
cout<<"In which way you want to query: "<<endl;
cout<<"1.The ID."<<endl;
cout<<"2.The name."<<endl;
cout<<"3.Back."<<endl;
cout<<"Please enter choice: ";
int choice;
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
Frontier.QueryById();
break;
case 2:
Frontier.QueryByName();
break;
case 3:
break;
default:
cout<<"Incorrectly input!!!"<<endl<<endl;
goto FindMenu;
}
}
// 统计功能
void Statistic(System& Frontier)
{
StatMenu:
cout<<"Please input the way you want statistic: "<<endl;
cout<<"1.The total number."<<endl;
cout<<"2.The different genders."<<endl;
cout<<"3.The different levels."<<endl;
cout<<"4.Back."<<endl;
int choice;
cout<<"Please enter your choice here: ";
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
Frontier.TotalNum();
break;
case 2:
Frontier.StatGender();
break;
case 3:
Frontier.StatLevel();
break;
case 4:
break;
default:
cout<<"error!!!"<<endl<<endl;
goto StatMenu;
}
}
int main()
{
System Frontier;
Frontier.FileIn(); //将文件数据输入到系统中
int option;
Menu:
UserInterface();
cin>>option;
cout<<endl;
switch(option)
{
case 1:
AddCompany(Frontier);
break;
case 2:
FindCompany(Frontier);
break;
case 3:
Frontier.Display();
cout<<endl;
break;
case 4:
Statistic(Frontier);
break;
case 5:
Frontier.Save();
break;
case 6:
exit(0);
break;
default:
cout<<"error!!!"<<endl<<endl;
break;
}
goto Menu;
return 0;
}
//***************
//** 1.h **
//***************
#include <string>
using namespace std;
class Company;
class System{
private:
Company* pCompany[30];
public:
System();
int FileIn(); //读取功能
void PushTechnician(); //添加技术人员
void PushManager(); //添加经理
void PushSaleman(); //添加销售人员
void PushSalemanager(); //添加销售经理
int QueryById(); //根据id查询
int QueryByName(); //根据name查询
void Display(); //显示功能
void Edit(int); //编辑功能
void Delete(int); //删除功能
void TotalNum(); //统计人数功能
void StatGender(); //统计性别功能
void StatLevel(); //统计职业功能
int Save(); //保存功能
};
class Company{
protected:
string id; //编号
string name; //姓名
char gender; //性别:用M代表男,用F代表女
string sector; //所在部门
int rank; //级别
double wages; //工资
public:
virtual ~Company(){};
void virtual Add() = 0;
void virtual showInform() = 0;
int virtual Type() = 0; //Edit功能出现问题后用来判断指针指向是哪个类的一个纯虚函数,
friend System;
};
class Technician: public Company{
public:
virtual ~Technician(){}; //基类定义虚析构
void virtual Add();
void showInform();
int Type() {return 0;};
friend System;
};
class Manager: public Company{
public:
virtual ~Manager(){}; //基类定义虚析构
void Add();
void showInform();
int Type() {return 1;};
friend System;
};
class Saleman: public Company{
public:
virtual ~Saleman(){}; //基类定义虚析构
void Add();
void showInform();
int Type() {return 1;};
friend System;
};
class Salemanager: public Company{
public:
virtual ~Salemanager(){}; //基类定义虚析构
void Add();
void showInform();
int Type() {return 1;};
friend System;
};
6、 程序执行结果
7、心得体会
编辑功能:直接在原数据上覆盖是不可行的(因为ID不可重复),要先new一个用来存储暂时数据,之后再看是否覆盖
先删除文件(因为write函数默认接着写入,而不会覆盖写入)所以用remove函数
很难想象,竟然写出来了!
C++语言课伊始时,迷迷糊糊的感觉听懂老师讲的东西。简单初级的自己觉着不用看了%¥#&然后没这些简单的东西铺垫就更搞不懂后面复杂的了……
最初的烦恼,我上课应该有认真听啊,作业做得很是难受(但作为一个初学者来说,只听老师讲的东西,课下不巩固记忆,怎么可能搞得懂)……要还非常担心之后的考试,感觉要凉……
第一次开窍是想把基础要打扎实,好好学习的(没得办法的)我把之前上过的课本全部整理了一遍。各种我初学时不太理解的东西基本都再看了一遍,然后我就顿悟了一下hhh。然后就就养成了课前预习,课后整理的好习惯,万万没想到因此课上效率也高了不少,能够理解老师所讲的同时,还能自己联系到类似的例子。