#include<iostream>#include<fstream>#include<iomanip>using namespace std;class person //基类{public:bool mark; //存在标记char name[20]; //职工姓名int num; //职工编号double basePay; //基本工资person(bool m,char nm[],int n,double b):mark(m),num(n),basePay(b){strcpy(name,nm);}};class teacher:virtual public person //教师类{public:double classPay; //课时费teacher(bool m,char nm[],int n,double b,double c):person(m,nm,n,b),classPay(c){}void show(){cout<<setw(15)<<"职工类型"<<setw(10)<<"职工编号"<<setw(10)<<"职工姓名"<<setw(10)<<"基本功资"<<setw(10)<<"课时费"<<setw(10)<<"总工资"<<endl;cout<<setw(15)<<"教师"<<setw(10)<<num<<setw(10)<<name<<setw(10)<<basePay<<setw(10)<<classPay<<setw(10)<<classPay+base Pay<<endl;}};class lab:virtual public person //实验员类{public:double labPay; //实验室补助lab(bool m,char nm[],int n,double b,double j):person(m,nm,n,b),labPay(j){}void show(){cout<<setw(15)<<"职工类型"<<setw(10)<<"职工编号"<<setw(10)<<"职工姓名"<<setw(10)<<"基本功资"<<setw(10)<<"实验补贴"<<setw(10)<<"总工资"<<endl;cout<<setw(15)<<"实验员"<<setw(10)<<num<<setw(10)<<name<<setw(10)<<basePay<<setw(10)<<labPay<<setw(10)<<labPay+basePay< <endl;}};class admin:virtual public person //行政人员类{public:double adminPay; //行政补贴admin(bool m,char nm[],int n,double b,double k):person(m,nm,n,b),adminPay(k){}void show(){cout<<setw(15)<<"职工类型"<<setw(10)<<"职工编号"<<setw(10)<<"职工姓名"<<setw(10)<<"基本功资"<<setw(10)<<"行政补贴"<<setw(10)<<"总工资"<<endl;cout<<setw(15)<<"行政人员"<<setw(10)<<num<<setw(10)<<name<<setw(10)<<basePay<<setw(10)<<adminPay<<setw(10)<<adminPay+base Pay<<endl;}};void add() //添加数据函数{ifstream ifile("E:\\data.txt");if(ifile.fail()){ofstream ofile("E:\\data.txt");if(ofile.fail())throw("打开文件失败!");ofile.close();}else ifile.close();fstream file;file.open("E:\\data.txt",ios::in|ios::out|ios::binary);int a,b,c,sign,bh;char xm[20];double jbgz,ksf;cout<<"\t*******************************************************************"<<endl;cout<<"\t* 1.教师 2.实验员 3.行政人员*"<<endl;cout<<"\t*******************************************************************"<<endl;cout<<"\t\t\t请选择添加类型:";cin>>a;switch(a){case 1:{cout<<"\t*******************************************************************"<<endl;cout<<"\t* 1.教授 2.副教授 3.讲师*"<<endl;cout<<"\t*******************************************************************"<<endl;cout<<"\t\t\t请选择添加类型:";cin>>c;switch(c){case 1:{cout<<"\t\t\t输入编号:";cin>>bh;cout<<"\t\t\t输入姓名:";cin>>xm;cout<<"\t\t\t输入基本工资:";cin>>jbgz;cout<<"\t\t\t输入上学期课时工作量:";cin>>b;ksf=b*20;cout<<"\t\t\t课时费:"<<ksf<<endl;cout<<"\t\t\t总工资:"<<ksf+jbgz<<endl;teacher temp(true,xm,bh,jbgz,ksf);sign=1;file.seekg(0,ios::end);file.write((char*)&sign,sizeof(int));file.write((char*)&temp,sizeof(teacher));cout<<"\t\t\t添加成功"<<endl;}break;case 2:{cout<<"\t\t\t输入编号:";cin>>bh;cout<<"\t\t\t输入姓名:";cin>>xm;cout<<"\t\t\t输入基本工资:";cin>>jbgz;cout<<"\t\t\t输入上学期课时工作量:";cin>>b;ksf=b*15;cout<<"\t\t\t课时费:"<<ksf<<endl;cout<<"\t\t\t总工资:"<<ksf+jbgz<<endl;teacher temp(true,xm,bh,jbgz,ksf);sign=2;file.seekg(0,ios::end);file.write((char*)&sign,sizeof(int));file.write((char*)&temp,sizeof(teacher));cout<<"\t\t\t添加成功"<<endl;}break;case 3:{cout<<"\t\t\t输入编号:";cin>>bh;cout<<"\t\t\t输入姓名:";cin>>xm;cout<<"\t\t\t输入基本工资:";cin>>jbgz;cout<<"\t\t\t输入上学期课时工作量:";cin>>b;ksf=b*10;cout<<"\t\t\t课时费:"<<ksf<<endl;cout<<"\t\t\t总工资:"<<ksf+jbgz<<endl;teacher temp(true,xm,bh,jbgz,ksf);sign=3;file.seekg(0,ios::end);file.write((char*)&sign,sizeof(int));file.write((char*)&temp,sizeof(teacher));cout<<"***********************"<<endl;cout<<"\t\t\t添加成功"<<endl;}break;} }break;case 2:{cout<<"\t*******************************************************************"<<endl; cout<<"\t* 1.工程师 2.高级工程师 *"<<endl; cout<<"\t*******************************************************************"<<endl; cout<<"\t\t\t请选择添加类型:";cin>>c;switch(c){case 1:{cout<<"\t\t\t输入编号:";cin>>bh;cout<<"\t\t\t输入姓名:";cin>>xm;cout<<"\t\t\t输入基本工资:";cin>>jbgz;ksf=150;cout<<"\t\t\t实验室补助:"<<ksf<<endl;cout<<"\t\t\t总工资:"<<ksf+jbgz<<endl;lab temp(true,xm,bh,jbgz,ksf);sign=1;file.seekg(0,ios::end);file.write((char*)&sign,sizeof(int));file.write((char*) &temp,sizeof(lab));cout<<"\t\t\t添加成功"<<endl;}break;case 2:{cout<<"\t\t\t输入编号:";cin>>bh;cout<<"\t\t\t输入姓名:";cin>>xm;cout<<"\t\t\t输入基本工资:";cin>>jbgz;ksf=200;cout<<"\t\t\t实验室补助:"<<ksf<<endl;cout<<"\t\t\t总工资:"<<ksf+jbgz<<endl;lab temp(true,xm,bh,jbgz,ksf);sign=2;file.seekg(0,ios::end);file.write((char*) &temp,sizeof(lab));cout<<"\t\t\t添加成功"<<endl;}break;}}break;case 3:{cout<<"\t*******************************************************************"<<endl; cout<<"\t*1.科员 2.科长 3.处长 *"<<endl;cout<<"\t*******************************************************************"<<endl; cout<<"\t\t\t请选择添加类型:";cin>>c;switch(c){case 1:{cout<<"\t\t\t输入编号:";cin>>bh;cout<<"\t\t\t输入姓名:";cin>>xm;cout<<"\t\t\t输入基本工资:";cin>>jbgz;cout<<"\t\t\t输入行政补贴:";cin>>b;cout<<"\t\t\t输入加班费时长:";cin>>ksf;ksf=ksf*20;cout<<"\t\t\t总工资:"<<b+jbgz+ksf<<endl;admin temp(true,xm,bh,jbgz,b);sign=1;file.seekg(0,ios::end);file.write((char*)&sign,sizeof(int));file.write((char*) &temp,sizeof(admin));cout<<"\t\t\t添加成功"<<endl;}break;case 2:{cout<<"\t\t\t输入编号:";cin>>bh;cout<<"\t\t\t输入姓名:";cin>>xm;cout<<"\t\t\t输入基本工资:";cin>>jbgz;cout<<"\t\t\t输入行政补贴:";cin>>b;cout<<"\t\t\t输入加班费时长:";cin>>ksf;ksf=ksf*25;cout<<"\t\t\t总工资:"<<b+jbgz+ksf<<endl;admin temp(true,xm,bh,jbgz,b);sign=2;file.seekg(0,ios::end);file.write((char*)&sign,sizeof(int));file.write((char*) &temp,sizeof(admin));cout<<"\t\t\t添加成功"<<endl;}break;case 3:{cout<<"\t\t\t输入编号:";cin>>bh;cout<<"\t\t\t输入姓名:";cin>>xm;cout<<"\t\t\t输入基本工资:";cin>>jbgz;cout<<"\t\t\t输入行政补贴:";cin>>b;cout<<"\t\t\t输入加班费时长:";cin>>ksf;ksf=ksf*30;cout<<"\t\t\t总工资:"<<b+jbgz+ksf<<endl;admin temp(true,xm,bh,jbgz,b);sign=3;file.seekg(0,ios::end);file.write((char*)&sign,sizeof(int));cout<<"\t\t\t添加成功"<<endl;}break;}}break;file.close();}}void search() //查询修改函数{ifstream ifile("E:\\data.txt");if(ifile.fail()){ofstream ofile("E:\\data.txt");if(ofile.fail())throw("打开失败");ofile.close();}else ifile.close();fstream file;file.open("E:\\data.txt",ios::in|ios::out|ios::binary);int a,b,sign,g=0;char tag,y[20];cout<<"\t*******************************************************************"<<endl;cout<<"\t* 1.职工编号查询 2.职工姓名查询 3.显示所有数据*"<<endl;cout<<"\t*******************************************************************"<<endl;cout<<"\t\t\t请选择:";cin>>b;while(b!=1 && b!=2 && b!=3 && b!=4 && b!=5){cout<<"\t\t\t输入不符,重新输入:";cin>>b;}if(b==1){cout<<"\t\t\t输入要查询的职工编号:";cin>>a;file.seekg(0);file.read((char*)&sign,sizeof(int));while(!file.eof()){if(sign==1){teacher temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(teacher));if(temp.num==a && temp.mark){temp.show();cout<<"\t\t\t是否要修改数据?(Y/N):";cin>>tag;tag=tolower(tag);while(tag!='y' && tag!='n'){cout<<"\t\t\t输入不符,重新输入(Y/N):";cin>>tag;tag=tolower(tag);}if(tag=='y'){temp.mark=false;file.seekg(sizeof(teacher),ios::cur);file.write((char*)&temp,sizeof(teacher));cout<<"\t*******************************************************************\n\t\t\t\t请输入修改后的数据:\n\t*******************************************************************"<<endl; add();}else{cout<<"**********"<<endl;cout<<"\t\t\t修改已取消"<<endl;}g=1;break;}}if(sign==2){lab temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(lab));if(temp.num==a && temp.mark){temp.show();cout<<"\t\t\t是否要修改数据?(Y/N):";cin>>tag;tag=tolower(tag);while(tag!='y' && tag!='n'){cout<<"\t\t\t输入不符,重新输入(Y/N):";cin>>tag;tag=tolower(tag);}if(tag=='y'){temp.mark=false;file.seekg(sizeof(lab),ios::cur);file.write((char*)&temp,sizeof(lab));cout<<"\t*******************************************************************\n\t\t\t\t请输入修改后的数据:\n\t*******************************************************************"<<endl; add();}else{cout<<"———————"<<endl;cout<<"\t\t\t修改已取消"<<endl;}g=1;break;}}if(sign==3){admin temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(admin));if(temp.num==a && temp.mark){temp.show();cout<<"\t\t\t是否要修改数据?(Y/N):";cin>>tag;tag=tolower(tag);while(tag!='y' && tag!='n'){cout<<"\t\t\t输入不符,重新输入(Y/N):";cin>>tag;tag=tolower(tag);}if(tag=='y'){temp.mark=false;file.seekg(sizeof(admin),ios::cur);file.write((char*)&temp,sizeof(admin));cout<<"\t*******************************************************************\n\t\t\t\t请输入修改后的数据:\n\t*******************************************************************"<<endl; add();}else{cout<<"************"<<endl;cout<<"\t\t\t修改已取消"<<endl;}g=1;break;}}file.read((char*)&sign,sizeof(int));}if(g==0){cout<<"*********"<<endl;cout<<"\t\t\t没有记录!"<<endl;}}if(b==2){cout<<"\t\t\t输入要查询的职工姓名:";cin>>y;file.seekg(0);file.read((char*)&sign,sizeof(int));while(!file.eof()){if(sign==1){teacher temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(teacher));if(strcmp(,y)==0 && temp.mark){temp.show();cout<<"\t\t\t是否要修改数据?(Y/N):";cin>>tag;tag=tolower(tag);while(tag!='y' && tag!='n'){cout<<"\t\t\t输入不符,重新输入(Y/N):";cin>>tag;tag=tolower(tag);}if(tag=='y'){temp.mark=false;file.seekg(sizeof(teacher),ios::cur);file.write((char*)&temp,sizeof(teacher));cout<<"\t*******************************************************************\n\t\t\t\t请输入修改后的数据:\n\t*******************************************************************"<<endl; add();}else{cout<<"***********"<<endl;cout<<"\t\t\t修改已取消"<<endl;}g=1;break;}}if(sign==2){lab temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(lab));if(strcmp(,y)==0 && temp.mark){temp.show();cout<<"\t\t\t是否要修改数据?(Y/N):";cin>>tag;tag=tolower(tag);while(tag!='y' && tag!='n'){cout<<"\t\t\t输入不符,重新输入(Y/N):";cin>>tag;tag=tolower(tag);}if(tag=='y'){temp.mark=false;file.seekg(sizeof(lab),ios::cur);file.write((char*)&temp,sizeof(lab));cout<<"\t*******************************************************************\n\t\t\t\t请输入修改后的数据:\n\t*******************************************************************"<<endl; add();}else{cout<<"***********"<<endl;cout<<"\t\t\t修改已取消"<<endl;}g=1;break;}}if(sign==3){admin temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(admin));if(strcmp(,y)==0 && temp.mark){temp.show();cout<<"\t\t\t是否要修改数据?(Y/N):";cin>>tag;tag=tolower(tag);while(tag!='y' && tag!='n'){cout<<"\t\t\t输入不符,重新输入(Y/N):";cin>>tag;tag=tolower(tag);}if(tag=='y'){temp.mark=false;file.seekg(sizeof(admin),ios::cur);file.write((char*)&temp,sizeof(admin));cout<<"\t*******************************************************************\n\t\t\t\t请输入修改后的数据:\n\t*******************************************************************"<<endl; add();}else{cout<<"*************"<<endl;cout<<"\t\t\t修改已经取消"<<endl;}g=1;break;}}file.read((char*)&sign,sizeof(int));}if(g==0){cout<<"**********"<<endl;cout<<"\t\t\t没有记录"<<endl;}}if(b==3){file.seekg(0);file.read((char*)&sign,sizeof(int));while(!file.eof()){if(sign==1){teacher temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(teacher));if(temp.mark){temp.show();g=1;}}if(sign==2){lab temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(lab));if(temp.mark){temp.show();g=1;}}if(sign==3){admin temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(admin));if(temp.mark){temp.show();g=1;}}file.read((char*)&sign,sizeof(int));}if(g==0){cout<<"**********"<<endl;cout<<"\t\t\t没有记录"<<endl;}}file.close();}void del() //删除数据函数{ifstream ifile("E:\\data.txt");if(ifile.fail()){ofstream ofile("E:\\data.txt");if(ofile.fail())throw("打开文件失败!");ofile.close();}else ifile.close();fstream file;file.open("E:\\data.txt",ios::in|ios::out|ios::binary);int a,sign,g=0;char tag;cout<<"\t*******************************************************************\n\t\t\t\t输入要删除的职工编号:\n\t*******************************************************************";cin>>a; file.seekg(0);file.read((char*)&sign,sizeof(int));while(!file.eof()){if(sign==1){teacher temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(teacher));if(temp.num==a && temp.mark){temp.show();cout<<"\t\t\t是否要删除?(Y/N):";cin>>tag;tag=tolower(tag);while(tag!='y' && tag!='n'){cout<<"\t\t\t输入不符,重新输入(Y/N):";cin>>tag;tag=tolower(tag);}if(tag=='y'){temp.mark=false;file.seekg(sizeof(teacher),ios::cur);file.write((char*)&temp,sizeof(teacher));cout<<"**********"<<endl;cout<<"\t\t\t删除成功!"<<endl;}else{cout<<"**********"<<endl;cout<<"\t\t\t删除已取消!"<<endl;}g=1;break;}}if(sign==2){lab temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(lab));if(temp.num==a && temp.mark){temp.show();cout<<"\t\t\t是否要删除?(Y/N):";cin>>tag;tag=tolower(tag);while(tag!='y' && tag!='n'){cout<<"\t\t\t输入不符,重新输入(Y/N):";cin>>tag;tag=tolower(tag);}if(tag=='y'){temp.mark=false;file.seekg(sizeof(lab),ios::cur);file.write((char*)&temp,sizeof(lab));cout<<"**********"<<endl;cout<<"\t\t\t删除成功"<<endl;}else{cout<<"**********"<<endl;cout<<"\t\t\t删除取消"<<endl;}g=1;break;}}if(sign==3){admin temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(admin));if(temp.num==a && temp.mark){temp.show();cout<<"*******************************************************************\n\t\t\t\t是否要删除?(Y/N):\n*******************************************************************";cin>>tag;tag=tolower(tag) ;while(tag!='y' && tag!='n'){cout<<"\t\t\t输入不符,重新输入(Y/N):";cin>>tag;tag=tolower(tag);}if(tag=='y'){temp.mark=false;file.seekg(sizeof(admin),ios::cur);file.write((char*)&temp,sizeof(admin));cout<<"**********"<<endl;cout<<"\t\t\t删除成功"<<endl;}else{cout<<"**********"<<endl;cout<<"\t\t\t删除成功"<<endl;}g=1;break;}}file.read((char*)&sign,sizeof(int));}if(g==0)cout<<"\t\t\t无记录!"<<endl;file.close();}void end() //重组退出函数{ifstream ifile("E:\\data.txt");if(ifile.fail()){ofstream ofile("E:\\data.txt");if(ofile.fail())throw("打开失败");ofile.close();}else ifile.close();fstream file;file.open("E:\\data.txt",ios::in|ios::out|ios::binary);ofstream outfile("E:\\tem.txt",ios::out|ios::binary);int sign;file.seekg(0);file.read((char*)&sign,sizeof(int));while(!file.eof()){if(sign==1){teacher temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(teacher));if(temp.mark==true){outfile.write((char*)&sign,sizeof(int));outfile.write((char*)&temp,sizeof(teacher));}}if(sign==2){lab temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(lab));if(temp.mark==true){outfile.write((char*)&sign,sizeof(int));outfile.write((char*)&temp,sizeof(lab));}}if(sign==3){admin temp(true,"s",0,0,0);file.read((char*)&temp,sizeof(admin));if(temp.mark==true){outfile.write((char*)&sign,sizeof(int));outfile.write((char*)&temp,sizeof(admin));}}file.read((char*)&sign,sizeof(int));}file.close();outfile.close();remove("E:\\data.txt");rename("E:\\tem.txt","E:\\data.txt");}void main(){int a;do{cout<<"\n\t*******************************************************************\n"; cout<<"\n\t * 高校工资管理系统 * \n"; cout<<"\n\t*******************************************************************\n"; cout<<"\n\t\t\t\t1.数据输入"<<endl;cout<<"\n\t\t\t\t2.数据查询与修改"<<endl;cout<<"\n\t\t\t\t3.删除相关数据"<<endl;cout<<"\n\t\t\t\t4.退出本程序"<<endl;cout<<"\n\t\t\t请输入序号实现相应功能:";cin>>a;switch(a){case 1:add();break;case 2:search();break;case 3:del();break;case 4:end();break;}}while(a!=4);}。