问题描述(功能要求):某小型公司,主要有四类人员:经理、兼职技术人员、销售经理和兼职推销员。现在,需要存储这些人员的姓名、编号、级别、当月薪水,计算月薪总额并显示全部信息
#include <iostream.h>
#include<string.h>
#include <fstream.h>
#include"employee.h"
#include<ctype.h>
class employee
{
protected:
char *name;//姓名
int num; //个人编号
int grade;//级别
float mouthpay; //月薪总额
static int employeeNo;//本公司职员编号目前最大值
public:
employee();//构造函数
~employee();//析构函数
virtual void pay()=0;//计算月薪函数(纯虚函数)
virtual void displayStatus()=0;//显示人员信息(纯虚函数)
};
class manager:virtual public employee //经理类
{
protected:
float monthlyPay;//固定月薪数
public:
manager(); //构造函数
manager::manager()
{monthlyPay=8000;} //固定月薪8000元
void pay(); //计算月薪函数
void displayStatus(); //显示人员信息
};
class technician:public employee //兼职技术人员类
{
private:
float hourlyRate;//每小时酬金
int workHours;//当月工作时数
public:
technician();//定义构造函数
techniCian::technician()
{ hourlyRate=100; } //每小时酬金100元
void pay();//计算月薪总额
void displayStatus();//显示人员信息
};
class salesman:virtual public employee //兼职推销员类
{
protected:
float CommRate;//按销售额提取酬金的百分比
float sales;//当月销售额
public:
salesman();//构造函数
salesman::salesman()
{ CommRate=0.04; } //销售提成比例4%
void pay(); //计算月薪函数
void displayStatus(); //显示人员信息
};
class salesmanager:public manager,public salesman//销售经理类
{
public:
salesmanager(); //构造函数
salesmanager::salesmanager()
{
monthlyPay=5000;
commRate=0.005;
}
void pay(); //计算月薪函数
void displayStatus(); //显示人员信息
};
int employee::employeeNo=1000; //员工编号基数为1000
employee::employee()
{
char namestr[50];//输人雇员姓名时首先临时存放在namestr
cout<<"请输入下一个雇员的姓名:";
cin>>namestr;
name=new char[strlen(namestr)+1];//动态申请用于存放姓名的内存空间
strcpy(name,namestr);//将临时存放的姓名复制到name
individualEmpNo=emptoyeeNo++;//新输入的员工,其编号为目前最大编号加l
grade=1;//级别初值为l
accumPay=0.0; //月薪总额初值为0
}
employee::~employee()
{
delete name;//在析构函数中删除为存放姓名动态分配的内存空间
}
void manager::pay()
{
accumPay=monthlyPay; //月薪总额即固定月薪数
cout<<"经理"<<name<<"编号"<<individualEmpNo
<<"本月工资"<<accumPay<<endl;
}
void manager::displayStatus()
{
cout<<"经理"<<name<<"编号"<<individualEmpNo
<<"级别为"<<grade<<"级,已付本月工资"
<<accumPay<<endl;
}
void technician::pay()
{
cout<<"请输入"<<name<<"本月的工作时数:";
cin>>workHours;
accumPay=hourlyRate*workHours;//计算月薪,按小时计酬
cout<<"兼职技术人员"<<name<<"编号"<<individualEmpNo <<"本月工资"<<accumPay<<endl;
}
void technician::displayStatus()
{
cout<<"兼职技术人员"<<name<<"编号"<<individualEmpNo<<"级别为"<<grade
<<"级,已付本月工资"<<accumPay<<endl;
}
void salseman::pay()
{
cout<<"请输人"<<name<<"本月的销售额:";
cin>>sales;
accumPay=sales*CommRate; //月薪=销售提成
cout<<"推销员"<<name<<"编号"<<individualEmpNo
<<"本月工资"<<accumPay<<endl;
}
void salesman::displaystatus()
{
cout<<"推销员"<<name<<"编号"<<individualEmpNo
<<"级别为"<<grade<<"级,已付本月工资"
<<accumPay<<endl;
}
void salesmanager::pay()
{
cout<<"请输入"<<employee::name
<<"所管辖部门本月的销售总额:";
cin>>sales;
accumPay=monthlyPay+CommRate*sales;//月薪=固定月薪+销售提成
cout<<"销售经理"<<name<<"编号"<<individualEmpNo
<<"本月工资"<<accumPay<<endl;
}
void salesmanager::displayStatus()
{
cout<<"销售经理"<<name<<"编号"<<individualEmpNo
<<"级别为"<<grade<<"级,已付本月工资"
<<accumPay<<endl;
}
void out()
{
cout<<"请输入职工的编号:";
cin>>number;
cout<<"请输入职工的姓名:";
cin>>name;
cout<<"请输入职工的级别:";
cin>>grade;
cout<<"该职工的月薪为:"<
}
void in()
{
cout<<"编号:"<<endl;
{
switch(ch)
{
case '1':write1();break;
case '2':write2();break;
case '3':write3();break;
case '4':write4();break;
default:cout<<"ERROR!!请从1~4中选择:";
}
cout<<"是否继续输入:(Y/N)?";
cin>>ch;
if(ch=='Y'||ch=='y')
{
cout<<"请输入1~4:"<
cin>>ch;
}
}
}
void show1()//*************************************显示函数
{
ifstream myfile("f1.txt",ios::binary|ios::out|ios::in);
manager p;
myfile.read((char *)&p,sizeof p);
while(!myfile.eof())
{
p.in();
myfile.read((char *)&p,sizeof p);
}
myfile.close();
}
void show2()
{
ifstream myfile("f2.txt",ios::binary|ios::out|ios::in);
technician p;
myfile.read((char *)&p,sizeof p);
while(!myfile.eof())
{
p.in();
myfile.read((char *)&p,sizeof p);
}
myfile.close();
}
void show3()
{
ifstream myfile("f3.txt",ios::binary|ios::out|ios::in);
salesmanager p;
myfile.read((char *)&p,sizeof p);
while(!myfile.eof())
{ p.in();
myfile.read((char *)&p,sizeof p);
}
myfile.close();
}
void show4()
{
ifstream myfile("f4.txt",ios::binary|ios::out|ios::in);
salesman p;
myfile.read((char *)&p,sizeof p);
while(!myfile.eof())
{ p.in();
myfile.read((char *)&p,sizeof p);
}
myfile.close();
}
void show0()
{
show1();
show2();
show3();
show4();
}
void show()
{
cout<<" **********************你正在显示********************"<
cout<<" * 1.经理 * "<
cout<<" * 2.兼职技术人员 * "<
cout<<" * 3.销售经理 * "<
cout<<" * 4.兼职推销员 * "<
cout<<" * 0.全显 * "<
cout<<" ************请输入你要显示的职工类(0~4)*************"<
char c;
cin>>c;
while(c!='N'&&c!='n')
{
switch(c)
{
case '1':show1();break;
case '2':show2();break;
case '3':show3();break;
case '4':show4();break;
case '0':show0();break;
default:cout<<"ERROR!!请从0~4中选择"<;
}
cout<<"是否继续显示(Y/N)?"<
cin>>c;
if(c=='Y'||c=='y')
{
cout<<"请选择你要显示的职工类:"<
cin>>c;
}
}
void search1()//************************查找函数
{
cout<<"请输入你要查找的编号:"<
int number;
cin>>number;
ifstream myfile("f1.txt",ios::binary|ios::out|ios::in);
manager p;
myfile.read((char *)&p,sizeof p);
while(!myfile.eof())
{
if(number==p.number)
{
p.in();
myfile.read((char *)&p,sizeof p);
myfile.close();
}
else
cout<<"查无此人!!!!"<
}
}
void search2()
{
cout<<"请输入你要查找的编号:"<
int number;
cin>>number;
ifstream myfile("f2.txt",ios::binary|ios::out|ios::in);
technician p;
myfile.read((char *)&p,sizeof p);
while(!myfile.eof())
{
if(number==p.number)
{
p.in();
myfile.read((char *)&p,sizeof p);
myfile.close();
}
else
cout<<"查无此人!!!!"<
}
}
void search3()
{
cout<<"请输入你要查找的编号:"<
int number;
cin>>number;
ifstream myfile("f3.txt",ios::binary|ios::out|ios::in);
salesmanager p;
myfile.read((char *)&p,sizeof p);
while(!myfile.eof())
{
if(number==p.number)
{
p.in();
myfile.read((char *)&p,sizeof p);
myfile.close();
}
else
cout<<"查无此人!!!!"<
}
}
void search4()
{
cout<<"请输入你要查找的编号:"<
int number;
cin>>number;
ifstream myfile("f4.txt",ios::binary|ios::out|ios::in);
salesman p;
myfile.read((char *)&p,sizeof p);
while(!myfile.eof())
{
if(number==p.number)
{
p.in();
myfile.read((char *)&p,sizeof p);
myfile.close();
}
else
cout<<"查无此人!!!!"<
}
}
void search()
{
cout<<" *********************你正在查找*********************"<
cout<<" ** 1.经理 **"<
cout<<" ** 2.兼职技术人员 **"<
cout<<" ** 3.兼职经理 **"<
cout<<" ** 4.兼职推销员 **"<
cout<<" ***************请选你的职称类型(1~4)****************"<
char c;
cin>>c;
while(c!='N'&&c!='n')
{
switch(c)
{
case '1':search1();break;
case '2':search2();break;
case '3':search3();break;
case '4':search4();break;
default:cout<<"ERROR!!请从1~4中选择:"<
cout<<"是否继续查找(Y/N)?";
cin>>c;
if(c=='Y'||c=='y')
{
cout<<"请选你的职称类型:"<
cin>>c;
}
}
}
void repair1(void)///*****************修改函数
{
int num,i=0,k=0;
manager *p1= new manager[50];
cout<<"请输入你要修改的编号:";
cin>>num;
ifstream h1("f1.txt",ios::binary|ios::in);
h1.read((char*)&p1[i],sizeof p1[i]);
while(!h1.eof())
{
i++;
h1.read((char*)&p1[i],sizeof p1[i]);
}
h1.close();
k=i;
for(i=0;i<=99;i++)
{
if(num==p1[i].number)
{
p1[i].out();
cout<<"修改完毕!"<
}
}
ofstream h1("f1.txt",ios::binary|ios::out);
for(i=0;i<=999;i++)
{h1.write((char*)&p1[i],sizeof p1[i]);}
h1.close();
delete []p1;
}
void repair2(void)
{
int num,i=0,k=0;
technician *p2=new technician[50];
cout<<"请输入你要修改的编号:";
cin>>num;
ifstream h2("f2.txt",ios::binary|ios::in);
h2.read((char*)&p2[i],sizeof p2[i]);
while(!h2.eof())
{i++; h2.read((char*)&p2[i],sizeof p2[i]); }
h2.close();
k=i;
for(i=0;i<=999;i++)
{
if(num==p2[i].number)
{
p2[i].out();
cout<<"修改完毕!"<
}
}
ofstream h2("f2.txt",ios::binary|ios::out);
for(i=0;i<=999;i++)
{h2.write((char*)&p2[i],sizeof p2[i]);}
h2.close();
delete []p2;
}
void repair3(void)
{
int num,i=0,k=0;
salesmanager *p3=new salesmanager[50];
cout<<"请输入你要修改的编号:";
cin>>num;
fstream h3("f3.txt",ios::binary|ios::out|ios::in);
h6.read((char*)&p3[i],sizeof p3[i]);
while(!h3.eof())
{
i++;
h3.read((char*)&p3[i],sizeof p3[i]);
}
k=i;
h3.close();
for(i=0;i<=999;i++)
if(num==p3[i].number)
{
p3[i].in();
cout<<"修改完毕!"<
}
ofstream h3("f3.txt",ios::binary|ios::out);
for(i=0;i<=999;i++)
{ h3.write((char*)&p3[i],sizeof p3[i]); }
h3.close();
delete []p3;
}
void repair4(void)
{
int num,i=0,k=0;
salesman *p4=new salesman[50];
cout<<"请输入你要修改的编号:";
cin>>num;
fstream h4("f4.txt",ios::binary|ios::out|ios::in);
h4.read((char*)&p4[i],sizeof p4[i]);
while(!h4.eof())
{
i++;
h4.read((char*)&p4[i],sizeof p4[i]);
}
k=i;
h4.close();
for(i=0;i<=999;i++)
{
if(num==p4[i].number)
{
p4[i].out();
cout<<"修改完毕!"<
}
}
ofstream h4("f4.txt",ios::binary|ios::out);
for(i=0;i<=999;i++)
{h4.write((char*)&p4[i],sizeof p4[i]);}
h4.close();
delete []p4;
}
void repair()
{
cout<<" *********************你正在修改*********************"<
cout<<" ** 1.经理 **"<
cout<<" ** 2.兼职技术人员 **"<
cout<<" ** 3.销售经理 **"<
cout<<" ** 4.兼职推销员 **"<
cout<<" ****************请选你的职称类型(1~4)***************"<
char c;
cin>>c;
while(c!='N'&&c!='n')
{
switch(c)
{
case '1':repair1();break;
case '2':repair2();break;
case '3':repair3();break;
case '4':repair4();break;
default:cout<<"ERROR!!请从1~4中选择"<
}
cout<<"是否继续修改(Y/N)?"<
cin>>c;
if(c=='Y'||c=='y')
{
cout<<"请输入你要修改的职称类型:"<
cin>>c;
}
}
}
void delete1()
{
int i=0,number;
ifstream myfile("f1.txt",ios::binary|ios::in);
manager p[30];
myfile.read((char *)(p+i),sizeof *p);
cout<<"请输入你要删除的对象的编号:"<
cin>>number;
while(!myfile.eof())
{
if(number==p[i].number)
{i--;}
i++;
myfile.read((char *)(p+i),sizeof *p);
}
int k=i;
myfile.close();
ofstream file1("f1.txt",ios::binary|ios::out);
for(i=0;i<=999;i++)
{
file1.write((char *)(p+i),sizeof *p);
}
}
void delete2()
{
int i=0,number;
ifstream myfile("f2.txt",ios::binary|ios::in);
technician p[30];
myfile.read((char *)(p+i),sizeof *p);
cout<<"请输入你要删除的对象的编号:"<
cin>>number;
while(!myfile.eof())
{
if(number==p[i].number)
{ i--; }
i++;
myfile.read((char *)(p+i),sizeof *p);
}
int k=i;
myfile.close();
ofstream file2("f2.txt",ios::binary|ios::out);
for(i=0;i<=999;i++)
{file2.write((char *)(p+i),sizeof *p);}
}
void delete3()
{
int i=0,number;
ifstream myfile("f3.txt",ios::binary|ios::in);
salesmanager p[30];
myfile.read((char *)(p+i),sizeof *p);
cout<<"请输入你要删除的对象的编号:"<
cin>>number;
while(!myfile.eof())
{
if(number==p[i].number)
{ i--; }
i++;
myfile.read((char *)(p+i),sizeof *p);
}
int k=i;
myfile.close();
ofstream file3("f3.txt",ios::binary|ios::out);
for(i=0;i<=999;i++)
{file3.write((char *)(p+i),sizeof *p);}
}
void delete4()
{
int i=0,number;
ifstream myfile("f7.txt",ios::binary|ios::in);
salesman p[30];
myfile.read((char *)(p+i),sizeof *p);
cout<<"请输入你要删除的对象的编号:"<
cin>>number;
while(!myfile.eof())
{
{if(number==p[i].number}
{ i--; }
i++;
myfile.read((char *)(p+i),sizeof *p);
}
int k=i;
myfile.close();
ofstream file4("f4.txt",ios::binary|ios::out);
for(i=0;i<=999;i++)
{
file4.write((char *)(p+i),sizeof *p);
}
}
void delete()
{
cout<<" *********************你正在删除*********************"<
cout<<" § 1.经理 §"<
cout<<" § 2.兼职技术人员 §"<
cout<<" § 3.销售经理 §"<
cout<<" § 4.兼职推销员 §"<
cout<<" ****************请选你的职称类型(1~4)***************"<
char c;
cin>>c;
while(c!='N'&&c!='n')
{
switch(c)
{
case '1':delete1();break;
case '2':delete2();break;
case '3':delete3();break;
case '4':delete4();break;
default:cout<<"ERROR!!请从1~4中选择"<
}
cout<<"是否继续删除(Y/N)?"<
cin>>c;
if(c=='Y'||c=='y')
{ cout<<"请输入你要删除对象的类型:"<>c; }
}
}
void list()
{
cout<<" *************★★公司人员管理系统★★***************"<
cout<<" § §"<
cout<<" ****** ☆输入(1) ☆显示(2) ☆查找(3) *****"<
cout<<" § §"<
cout<<" ****** ☆修改(4) ☆删除(5) ☆退出(6) *****"<
cout<<" § §"<
cout<<" ************* 请输入你要选择的功能(1-6) ************"<
char c;
cin>>c;
while(c!='N'&&c!='n')
{
switch(c)
{
case '1':build();break;
case '2':show();break;
case '3':search();break;
case '4':repair();break;
case '5':delete();break;
case '6':exit(0);break;
default:break;
}
cout<<"是否继续实现功能:(Y/N)?";
cin>>c;
if(c=='Y'||c=='y') list();
else exit(0);
}
}
void main()
{
ifstream file("f4.txt",ios::binary|ios::in);
cout<<"请输入管理员密码:";
char c[10];
cin>>c;
while(!file.eof())
{
while(strcmp(c,"lin")!=0)
{
cout<<"密码错误!!!!!请重新输入:";
cin>>c;
}
list(); //注意不能用void list();
}
}