C++实现员工管理系统
定义对应的类
class employee //雇员
{
private:
string id;
string name;
string position;
string sex;
int age;
int salary;
public:
string Getid(){ return id;}
string Getname() {return name;}
string Getpost() {return position;}
string GetSex() {return sex;}
int GetAge() {return age;}
int Getsalary() {return salary;}
void SetSalary(float s)
{
salary=s;
}
employee();
employee(string id1,string name1,string sex1,int a,string post)
{
id=id1;
name=name1;
age=a;
position=post;
sex=sex1;
salary=0;
}
};
class technician : public employee //技术员
{
private:
int WorkHour; //工作时间
int HourSalary=100; //时薪
public:
void SetHour(int h) //输入工作时间时自动计算工资
{
WorkHour=h;
SetSalary(HourSalary*WorkHour);
}
technician(string id1,string name1,string sex1,int a,string post)
:employee(id1,name1,sex1,a,post)
{
SetSalary(0);
}
int GetHour() {return WorkHour;}
};
class manger : virtual public employee //经理
{
public:
manger(string id1,string name1,string sex1,int a,string post)
: employee(id1,name1,sex1,a,post) //初始化时直接记录工资
{
SetSalary(8000);
}
};
class saler : virtual public employee //销售
{
private:
int team; //部门
float sales; //销售额
public:
saler(string id1,string name1,string sex1,int a,string post)
: employee(id1,name1,sex1,a,post)
{
sales=0;
}
void SetTeam(int t)
{
team=t;
}
void SetSales(float s) //设置销售额计算提成
{
sales=s;
SetSalary(sales*0.04);
}
void pSetSales(float s)
{
sales=s;
}
float GetSales(){return sales;}
int GetTeam() {return team;}
};
class sale_manger : public manger, public saler
{
private:
float team_sales;//部门销售额
public:
sale_manger(string id1,string name1,string sex1,int a,string post)
: employee(id1,name1,sex1,a,post), manger(id1,name1,sex1,a,post), saler(id1,name1,sex1,a,post)
{
SetSalary(5000);
team_sales=0;
pSetSales(0);
}
void SetSales(float ps) //输入个人销售额
{
pSetSales(ps);
SetSalary(5000+team_sales*0.005+GetSales()*0.04);
}
void Set_team_sales(float ts)
{
team_sales=ts;
}
float GetTeamSales() {return team_sales;}
};
//类
int v1_n=0;
vector <employee> v1; //存放员工基本信息
int v2_n=0;
vector <saler> v2; //存放销售员信息
int v3_n=0;
vector <sale_manger> v3; //存放销售经理信息
int v4_n=0;
vector <manger> v4; //存放经理信息
int v5_n=0;
vector <technician> v5; //存放技术员信息
设置对应的函数
void add_employee() //录入职工基本信息,并记录各岗位数据
{
cout<<"请输入录入人员数:"<<endl;
int n;
cin>>n;
v1_n=n;
string id;
string name;
string position;
string sex;
int age;
for(int i=0;i<n;i++)
{
cout<<"请输入员工ID:"<<endl;
cin>>id;
cout<<"请输入员工姓名:"<<endl;
cin>>name;
cout<<"请输入员工性别:"<<endl;
cin>>sex;
cout<<"请输入员工年龄:"<<endl;
cin>>age;
cout<<"请输入员工职位:"<<endl;
cout<<"销售员:saler / 经理:manger / 销售经理:sale_manger / 技术员:technician"<<endl;
cin>>position;
employee temp(id,name,sex,age,position);
v1.push_back(temp);
system("cls");
}
for(vector<employee>::iterator it=v1.begin();it!=v1.end();it++)
{
if(it->Getpost()=="saler")
{
saler s(it->Getid(),it->Getname(),it->GetSex(),it->GetAge(),it->Getpost());
v2.push_back(s);
v2_n++;
}
if(it->Getpost()=="sale_manger")
{
sale_manger t(it->Getid(),it->Getname(),it->GetSex(),it->GetAge(),it->Getpost());
v3.push_back(t);
v3_n++;
it->SetSalary(5000);
}
if(it->Getpost()=="manger")
{
manger m(it->Getid(),it->Getname(),it->GetSex(),it->GetAge(),it->Getpost());
v4_n++;
v4.push_back(m);
it->SetSalary(8000);
}
if(it->Getpost()=="technician")
{
technician t(it->Getid(),it->Getname(),it->GetSex(),it->GetAge(),it->Getpost());
v5_n++;
v5.push_back(t);
}
}
}
void SetSaleTeams() //录入销售业绩
{
int nTeam;
cout<<"请输入部门个数:"<<endl;
cin>>nTeam;
system("cls");
float s[nTeam+1];
for(int i=1;i<nTeam+1;i++)
{
s[i]=0;
}
cout<<"开始录入销售数据:"<<endl;
cout<<"开始录入销售员信息:"<<endl;
for(vector<saler>::iterator it=v2.begin();it!=v2.end();it++) //遍历销售员
{
float xiaoshoue;
int team_id;
cout<<"销售员ID:"<<it->Getid()<<endl;
cout<<"请输入销售额:"<<endl;
cin>>xiaoshoue;
cout<<"请输入部门编号:"<<endl;
cin>>team_id;
it->SetSales(xiaoshoue);
it->SetTeam(team_id);
s[team_id]+=xiaoshoue;
system("cls");
}
for(vector<sale_manger>::iterator it=v3.begin();it!=v3.end();it++) //遍历销售经理
{
float xiaoshoue;
int team_id;
cout<<"销售经理ID:"<<it->Getid()<<endl;
cout<<"请输入个人销售额:"<<endl;
cin>>xiaoshoue;
it->SetSales(xiaoshoue);
cout<<"请输入所管理部门编号(1-10)"<<endl;
cin>>team_id;
it->SetTeam(team_id);
s[team_id]+=xiaoshoue;
it->Set_team_sales(s[team_id]);
system("cls");
//录入完毕后将工资数据传给相应employee的对象
for(vector<saler>::iterator it1=v2.begin();it1!=v2.end();it1++)
{
for(vector<employee>::iterator it2=v1.begin();it2!=v1.end();it2++)
{
if(it1->Getid()==it2->Getid())
{
it2->SetSalary(it1->Getsalary());
}
}
}
for(vector<sale_manger>::iterator it1=v3.begin();it1!=v3.end();it1++)
{
for(vector<employee>::iterator it2=v1.begin();it2!=v1.end();it2++)
{
if(it1->Getid()==it2->Getid())
{
it2->SetSalary(it1->Getsalary());
}
}
}
}
}
void SetWorkhour() //录入工作时间
{
for(vector<technician>::iterator it=v5.begin();it!=v5.end();it++)
{
cout<<"技术员工号:"<<it->Getid()<<endl;
cout<<"请输入工作时间:"<<endl;
int h;
cin>>h;
it->SetHour(h);
system("cls");
}
//录入完毕后将工资数据传给相应employee的对象
for(vector<technician>::iterator it1=v5.begin();it1!=v5.end();it1++)
{
for(vector<employee>::iterator it2=v1.begin();it2!=v1.end();it2++)
if(it1->Getid()==it2->Getid())
{
it2->SetSalary(it1->Getsalary());
}
}
}
void print1() //屏幕输出员工基本信息
{
cout<<setw(15)<<"职工号"<<setw(15)<<"姓名";
cout<<setw(15)<<"性别"<<setw(15)<<"年龄";
cout<<setw(15)<<"岗位"<<endl;
for(vector<employee>::iterator it=v1.begin();it!=v1.end();it++)
{
cout<<setw(15)<<it->Getid();
cout<<setw(15)<<it->Getname();
cout<<setw(15)<<it->GetSex();
cout<<setw(15)<<it->GetAge();
cout<<setw(15)<<it->Getpost();
cout<<endl;
}
cout<<"输出结束!"<<endl;
}
void print2() //文本文件输出员工基本信息
{
ofstream ofs;
ofs.open("employee.txt",ios::out);
ofs<<setw(14)<<"职工号"<<setw(14)<<"姓名";
ofs<<setw(14)<<"性别"<<setw(14)<<"年龄";
ofs<<setw(14)<<"岗位"<<endl;
for(vector<employee>::iterator it=v1.begin();it!=v1.end();it++)
{
ofs<<setw(14)<<it->Getid();
ofs<<setw(14)<<it->Getname();
ofs<<setw(13)<<it->GetSex();
ofs<<setw(14)<<it->GetAge();
ofs<<setw(14)<<it->Getpost();
ofs<<endl;
}
cout<<"备份完毕,已自动生成文本 employee.txt !"<<endl;
}
void print3() //屏幕输出销售部门业绩与工资
{
cout<<setw(15)<<"职工号"<<setw(15)<<"姓名";
cout<<setw(15)<<"职位"<<setw(15)<<"销售额";
cout<<setw(15)<<"工资"<<setw(15)<<"所属部门";
cout<<setw(15)<<"部门销售额"<<endl;
for(vector<sale_manger>::iterator it=v3.begin();it!=v3.end();it++)
{
cout<<setw(15)<<it->Getid();
cout<<setw(15)<<it->Getname();
cout<<setw(15)<<it->Getpost();
cout<<setw(15)<<fixed<<setprecision(1)<<it->GetSales();
cout<<setw(15)<<fixed<<setprecision(1)<<it->Getsalary();
cout<<setw(15)<<it->GetTeam();
cout<<setw(15)<<fixed<<setprecision(1)<<it->GetTeamSales();
cout<<endl;
}
for(vector<saler>::iterator it=v2.begin();it!=v2.end();it++)
{
cout<<setw(15)<<it->Getid();
cout<<setw(15)<<it->Getname();
cout<<setw(15)<<it->Getpost();
cout<<setw(15)<<fixed<<setprecision(1)<<it->GetSales();
cout<<setw(15)<<fixed<<setprecision(1)<<it->Getsalary();
cout<<setw(15)<<it->GetTeam();
cout<<endl;
}
}
void print4() //屏幕输出有排序的全体员工工资
{
int i=1;
cout<<setw(15)<<"职工号"<<setw(15)<<"姓名";
cout<<setw(15)<<"职位"<<setw(15)<<"工资";
cout<<setw(15)<<"排名"<<endl;
for(vector<employee>::iterator it=v1.begin();it!=v1.end();it++)
{
cout<<setw(15)<<it->Getid();
cout<<setw(15)<<it->Getname();
cout<<setw(15)<<it->Getpost();
cout<<setw(15)<<it->Getsalary();
cout<<setw(15)<<i++<<endl;
}
}
void print5() //屏幕输出无排序的全体员工工资
{
cout<<setw(15)<<"职工号"<<setw(15)<<"姓名";
cout<<setw(15)<<"职位"<<setw(15)<<"工资"<<endl;
for(vector<employee>::iterator it=v1.begin();it!=v1.end();it++)
{
cout<<setw(15)<<it->Getid();
cout<<setw(15)<<it->Getname();
cout<<setw(15)<<it->Getpost();
cout<<setw(15)<<it->Getsalary();
cout<<endl;
}
}
void print6() //文件备份技术员工时
{
ofstream ofs;
ofs.open("WorkHour.txt",ios::out);
ofs<<setw(15)<<"职工号"<<setw(15)<<"姓名";
ofs<<setw(15)<<"工时"<<endl;
for(vector<technician>::iterator it=v5.begin();it!=v5.end();it++)
{
ofs<<setw(15)<<it->Getid();
ofs<<setw(15)<<it->Getname();
ofs<<setw(15)<<it->GetHour();
ofs<<endl;
}
ofs.close();
cout<<"备份完毕,已自动生成文本 WorkHour.txt !"<<endl;
}
void print7() //文件备份销售部门业绩
{
ofstream ofs;
ofs.open("Salesdata.txt",ios::out);
ofs<<setw(15)<<"职工号"<<setw(15)<<"姓名";
ofs<<setw(15)<<"个人销售额"<<setw(15)<<"部门销售额";
ofs<<endl;
for(vector<sale_manger>::iterator it=v3.begin();it!=v3.end();it++)
{
ofs<<setw(15)<<it->Getid();
ofs<<setw(15)<<it->Getname();
ofs<<fixed<<setprecision(1)<<setw(15)<<it->GetSales();
ofs<<fixed<<setprecision(1)<<setw(15)<<it->GetTeamSales();
ofs<<endl;
}
for(vector<saler>::iterator it=v2.begin();it!=v2.end();it++)
{
ofs<<setw(15)<<it->Getid();
ofs<<setw(15)<<it->Getname();
ofs<<fixed<<setprecision(1)<<setw(15)<<it->GetSales();
ofs<<endl;
}
ofs.close();
cout<<"备份完毕,已自动生成文本 Salesdata.txt !"<<endl;
}
void salary_sort() //工资排序
{
for(int i=0;i<v1.size()-1;i++)
{
for(int j=0;j<v1.size()-i;j++)
{
if(v1[j].Getsalary()<v1[j+1].Getsalary())
{
employee temp=v1[j];
v1[j]=v1[j+1];
v1[j+1]=temp;
}
}
}
}
void ShowMenu()
{
cout<<"欢迎使用工资管理系统!"<<endl;
cout<<"请选择功能:"<<endl;
cout<<"1.录入数据"<<endl;
cout<<"2.统计工资"<<endl;
cout<<"3.打印数据"<<endl;
cout<<"4.备份数据"<<endl;
cout<<"5.使用说明"<<endl;
cout<<"0.退出"<<endl;
cout<<"Tips:"<<endl
<<"建议先查看使用说明"<<endl;
}
void Readme()
{
cout<<" 使用说明 "<<endl<<endl
<<"1.为保证系统正常运行,请先录入各项数据,在使用其他功能"<<endl<<endl
<<"2.暂不支持中文输入,职位输入请按照所提示格式"<<endl<<endl
<<"3.部门编号请与所设置部门个数相匹配"<<endl
<<" 例如设置2个销售部门,则部门编号只有1,2"<<endl<<endl
<<"4.为避免系统崩溃,请输入指定数字来进行功能选择"<<endl<<endl
<<"5.未输入相应工时信息和销售信息时,所查询的工资将为0或底薪"<<endl<<endl
<<"6.进行工资排序可能会打乱输出/备份时的id顺序,若想获得按id排序的数据请排序前输出/备份"<<endl;
}
进入主函数运行
#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
int main() {
int e; //暂存用户输入
do
{
system("cls");
ShowMenu();
cin>>e;
switch (e)
{
case 0:
{
system("cls");
break;
}
case 1:
system("cls");
cout<<"请选择录入内容:"<<endl;
cout<<"1.录入员工基本信息"<<endl;
cout<<"2.录入销售部门信息"<<endl;
cout<<"3.录入技术员工作时间"<<endl;
int t;
cin>>t;
if(t==1)
{
system("cls");
add_employee();
cout<<"录入完毕,稍后自动返回";
Sleep(1500);
}
else if(t==2)
{
system("cls");
SetSaleTeams();
cout<<"录入完毕,稍后自动返回";
Sleep(1500);
}
else if(t==3)
{
system("cls");
SetWorkhour();
cout<<"录入完毕,稍后自动返回";
Sleep(1500);
}
break;
case 2:
system("cls");
cout<<"1.销售部门业绩及工资计算"<<endl;
cout<<"2.全体工资排序"<<endl;
int r;
cin>>r;
if(r==1)
{
system("cls");
print3();
system("pause");
}
else if (r==2)
{
salary_sort();
print4();
cout<<"排序结束!";
system("pause");
}
break;
case 3:
system("cls");
cout<<"请选择输出内容:"<<endl;
cout<<"1.员工基本信息"<<endl;
cout<<"2.员工工资情况"<<endl;
int c;
cin>>c;
if(c==1)
{
system("cls");
print1();
do
{
system("pause");
} while (!getchar());
}
else if(c==2)
{
system("cls");
print5();
system("pause");
}
break;
case 4:
system("cls");
cout<<"请选择备份内容:"<<endl;
cout<<"1.员工基本信息"<<endl;
cout<<"2.技术员工时信息"<<endl;
cout<<"3.销售部分业绩信息"<<endl;
int z;
cin>>z;
if(z==1)
{
print2();
Sleep(1500);
}
if(z==2)
{
print6();
Sleep(1500);
}
if(z==3)
{
print7();
Sleep(1500);
}
break;
case 5:
system("cls");
Readme();
system("pause");
break;
}
} while (e!=0);
cout<<endl<<"感谢您的使用!";
Sleep(1000);
return 0;
}
偷偷加个验证码
156fafe0-09f6-4764-8eda-4b3954b0f53c