主题:一道程序求调试
//职工管理系统
#include<iomanip>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include <string.h>
class Employee
{
protected:
double num;
char Name[20];
double Salary;
Employee *next;
int Duty;
public:
Employee(){next=0;}
virtual ~Employee(){}
virtual void Input()=0;
virtual void Input(ifstream& ifs)=0;
virtual void Output()=0;
virtual void Output(ofstream& ofs)=0;
virtual double Incoming()=0;
friend class Corperate;
};
class Technican:virtual public Employee
{
protected:
int Hours;
public:
Technican()
{
Salary=150;
Duty=1;
}
void Input()
{
cout<<"\n编号:";
cin>>num;
cout<<"\n姓名:";
cin>>Name;
cout<<"\n上班的钟数:";
cin>>Hours;
Salary=Incoming();
}
void Input(ifstream &ifs)
{
ifs>>num>>Name>>Duty>>Hours>>Salary;
}
void Output()
{
cout<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<Hours<<"\t"<<Salary<<"\t"<<endl;
}
void Output(ofstream &ofs)
{
ofs<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<Hours<<"\t"<<Salary<<"\t"<<endl;
double Incoming()
{
if(Hours>1)
return Salary+(Hours-1)*150;
else
return Salary;
}
};
class Salesman:virtual public Employee
{
protected:
int amount;
public:
Salesman()
{
Duty=2;
Salary=0;
amount=0;
}
void Input()
{
cout<<"\n编号:";
cin>>num;
cout<<"\n姓名:";
cin>>Name;
cout<<"\n当月销售额:";
cin>>amount;
Salary=Incoming();
}
void Input(ifstream &ifs)
{
ifs>>num>>Name>>Duty>>amount>>Salary;
}
void Output()
{
cout<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<amount<<"\t"<<Salary<<"\t"<<endl;
}
void Output(ofstream &ofs)
{
ofs<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<amount<<"\t"<<Salary<<"\t"<<endl;
}
double Incoming()
{
if(amount>0)
return Salary+amount*0.07;
else
return Salary;
}
};
class Manager:virtual public Employee
{
public:
Manager()
{
Duty=3;
Salary=8500;
}
void Input()
{
cout<<"\n编号:";
cin>>num;
cout<<"\n姓名:";
cin>>Name;
Salary=Incoming();
}
void Input(ifstream &ifs)
{
ifs>>num>>Name>>Duty>>Salary;
}
void Output()
{
cout<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<Salary<<endl;
}
void Output(ofstream &ofs)
{
ofs<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<Salary<<"\t"<<endl;
}
double Incoming()
{
return Salary;
}
};
class Salesmanager:public Salesman,public Manager
{
public:
Salesmanager()
{
Duty=4;
Salesman::amount=0;
Manager::Salary=4000;
}
void Input()
{
cout<<"\n编号:";
cin>>num;
cout<<"\n姓名:";
cin>>Name;
cout<<"\n本月销售额:";
cin>>Salesman::amount;
Salary=Incoming();
}
void Input(ifstream &ifs)
{
ifs>>num>>Name>>Duty>>Salesman::amount>>Salary;
}
void Output()
{
cout<<num<<"\t"<<Name<<"\t"<<"\t"<<Duty<<"\t"<<Salesman::amount<<"\t"<<Salary<<endl;
}
void Output(ofstream& ofs)
{
ofs<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<Salesman::amount<<"\t"<<Salary<<"\t"<<endl;
}
double Incoming()
{
return Manager::Salary+Salesman::amount*0.005;
}
};
class Corperate
{
private:
Employee *PL;
void Clear();
int Corperate::Cfind(int ID,Employee **p1,Employee **p2);
public:
Corperate();
~Corperate();
void Add();
void Delete();
void Modify();
void Print();
void Save();
void Find();
void Build();
};
Corperate::Corperate()
{
Employee *p=new Technican;
PL=p;
Build();
}
Corperate::~Corperate()//仅保留一个头结点
{
Employee *p=PL;
while(p)
{
PL=p->next;
delete p;
p=PL;
}
PL=0;
}
void Corperate::Add()
{
char c;
do
{
cout<<"\n*****************增加职工****\n";
Employee *p=PL;
while(p->next)p=p->next;
int ch;
cout<<"********************************\n";
cout<<"******请输入你要增加的类别******\n";
cout<<"********技术人员:****->1********\n";
cout<<"********销售人员:****->2********\n";
cout<<"********部门经理:****->3********\n";
cout<<"********销售经理:****->4********\n";
cout<<"********************************\n";
cin>>ch;
Technican *pt;
Salesman *pa;
Manager *pm;
Salesmanager *pn;
switch(ch)
{
case 1: pt=new Technican;
pt->Input();
p->next=pt;
break;
case 2: pa=new Salesman;
pa->Input();
p->next=pa;
break;
case 3: pm=new Manager;
pm->Input();
p->next=pm;
break;
case 4: pn=new Salesmanager;
pn->Input();
p->next=pn;
break;
default:return;
}
cout<<"是否继续增加职工?\n";
cin>>c;
}while(c=='y'||c=='Y');
Save();
}
void Corperate::Clear()
{
Employee *p=PL->next;
while(p)
{
PL->next=p->next;
delete p;
p=PL->next;
}
}
void Corperate::Find()
{
double ID;
Employee *p1;
Employee *p2;
cout<<"输入你要查的编号:";
cin>>ID;
p1=PL->next;
p2=PL;
while(p1)
{
if(p1->num==ID)
break;
else
{
p2=p1;
p1=p1->next;
}
}
if(!p1)
{
cout<<"找不到你要查询的人!!!\n请确认你输入的编号是否有错!!!\n";
return;
}
else
{
cout<<"\n**职工信息表****\n";
cout<<"编号 姓名 身份代码 工作时间/销售额 收入情况 \n ";
p1->Output();
getch();
}
}
int Corperate::Cfind(int ID,Employee **p1,Employee **p2)
{
*p1=PL->next;
*p2=PL;
while(*p1)
{
if((*p1)->num==ID)
break;
else
{
*p2=*p1;
*p1=(*p1)->next;
}
}
return *p1?1:0;
}
void Corperate::Delete()
{
cout<<"\n***删除职工****\n";
int num;
Employee *p1,*p2;
cout<<"编号:";
cin>>num;
if(!Cfind(num,&p1,&p2))
{
cout<<"没有指定的人!!!\n";
}
else
{
p2->next=p1->next;
delete p1;
cout<<"正确删除!!!\n";
}
Save();
getch();
}
void Corperate::Modify()
{
cout<<"\n***修改职工***\n";
int num;
Employee *p1,*p2;
cout<<"编号";
cin>>num;
if(!Cfind(num,&p1,&p2))
{
cout<<"没有指定的人!!!\n";
}
else
{
p1->Output();//输出原来的职工信息
p1->Input();//输入新的职工信息
cout<<"修改完成!!!\n";
}
Save();
}
void Corperate::Print()
{
cout<<"\n*********职工信息表************\n";
Employee *p=PL->next;
if(!p)
{
cout<<"无职工记录!!!\n";
return;
}
while(p)
{
p->Output();
p=p->next;
}
getch();
}
void Corperate::Save()
{
ofstream f("李伟.txt",ios::out);//打开文件,遍历输出文件
Employee *p=PL->next;
while(pe)
{
p->Output(f);
p=p->next;
}
f.close();//关闭文件
cout<<"职工的信息已保存在 李伟.txt.\n";
}
void Corperate::Build()//职工信息
{
char buf[81];//临时空间
int Duty;//人员类型
Employee *p2;//新建结点指针
long t;//读写位置//清除现有结点(保留头结点)
Clear();//打开文件
ifstream f("ms.txt",ios::in);
Employee *p=PL;//尾结点指针
while(1)
{
t=f.tellg();//读取人员类型
f>>buf>>buf>>Duty;
if(f)
{
switch(Duty)//根据人员类型创建新节点
{
case 1: p2=new Technican;
break;
case 2: p2=new Salesman;
break;
case 3: p2=new Manager;
break;
case 4: p2=new Salesmanager;
break;
default:
f.close();
return;
}
p->next=p2;
p=p->next;
f.seekg(t);
e->Input(f);
}
else
break;
}//关闭文件
f.close();
}
void Menu()
{
char ch;
Corperate C;
do
{
system("cls");
cout<<"********某企业工资管理********"<<endl;
cout<<"*******************************\n";
cout<<"*********输入你的选择**********\n";
cout<<"*********增加职工:选择1*******\n";
cout<<"*********删除职工:选择2*******\n";
cout<<"*********修改职工:选择3*******\n";
cout<<"*********查询职工:选择4*******\n";
cout<<"*********查看数据:选择5*******\n";
cout<<"*********系统退出:选择6*******\n";
cout<<"*******************************\n";
cout<<"*********请选择一个操作********\n";
cin>>ch;
switch(ch)
{
case'1':C.Add();
break;
case'2':C.Delete();
break;
case'3':C.Modify();
break;
case'4':C.Find();
break;
case'5':C.Print();
break;
}
}while(ch!='6');
}
void main()
{
Menu();
}
#include<iomanip>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include <string.h>
class Employee
{
protected:
double num;
char Name[20];
double Salary;
Employee *next;
int Duty;
public:
Employee(){next=0;}
virtual ~Employee(){}
virtual void Input()=0;
virtual void Input(ifstream& ifs)=0;
virtual void Output()=0;
virtual void Output(ofstream& ofs)=0;
virtual double Incoming()=0;
friend class Corperate;
};
class Technican:virtual public Employee
{
protected:
int Hours;
public:
Technican()
{
Salary=150;
Duty=1;
}
void Input()
{
cout<<"\n编号:";
cin>>num;
cout<<"\n姓名:";
cin>>Name;
cout<<"\n上班的钟数:";
cin>>Hours;
Salary=Incoming();
}
void Input(ifstream &ifs)
{
ifs>>num>>Name>>Duty>>Hours>>Salary;
}
void Output()
{
cout<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<Hours<<"\t"<<Salary<<"\t"<<endl;
}
void Output(ofstream &ofs)
{
ofs<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<Hours<<"\t"<<Salary<<"\t"<<endl;
double Incoming()
{
if(Hours>1)
return Salary+(Hours-1)*150;
else
return Salary;
}
};
class Salesman:virtual public Employee
{
protected:
int amount;
public:
Salesman()
{
Duty=2;
Salary=0;
amount=0;
}
void Input()
{
cout<<"\n编号:";
cin>>num;
cout<<"\n姓名:";
cin>>Name;
cout<<"\n当月销售额:";
cin>>amount;
Salary=Incoming();
}
void Input(ifstream &ifs)
{
ifs>>num>>Name>>Duty>>amount>>Salary;
}
void Output()
{
cout<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<amount<<"\t"<<Salary<<"\t"<<endl;
}
void Output(ofstream &ofs)
{
ofs<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<amount<<"\t"<<Salary<<"\t"<<endl;
}
double Incoming()
{
if(amount>0)
return Salary+amount*0.07;
else
return Salary;
}
};
class Manager:virtual public Employee
{
public:
Manager()
{
Duty=3;
Salary=8500;
}
void Input()
{
cout<<"\n编号:";
cin>>num;
cout<<"\n姓名:";
cin>>Name;
Salary=Incoming();
}
void Input(ifstream &ifs)
{
ifs>>num>>Name>>Duty>>Salary;
}
void Output()
{
cout<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<Salary<<endl;
}
void Output(ofstream &ofs)
{
ofs<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<Salary<<"\t"<<endl;
}
double Incoming()
{
return Salary;
}
};
class Salesmanager:public Salesman,public Manager
{
public:
Salesmanager()
{
Duty=4;
Salesman::amount=0;
Manager::Salary=4000;
}
void Input()
{
cout<<"\n编号:";
cin>>num;
cout<<"\n姓名:";
cin>>Name;
cout<<"\n本月销售额:";
cin>>Salesman::amount;
Salary=Incoming();
}
void Input(ifstream &ifs)
{
ifs>>num>>Name>>Duty>>Salesman::amount>>Salary;
}
void Output()
{
cout<<num<<"\t"<<Name<<"\t"<<"\t"<<Duty<<"\t"<<Salesman::amount<<"\t"<<Salary<<endl;
}
void Output(ofstream& ofs)
{
ofs<<num<<"\t"<<Name<<"\t"<<Duty<<"\t"<<Salesman::amount<<"\t"<<Salary<<"\t"<<endl;
}
double Incoming()
{
return Manager::Salary+Salesman::amount*0.005;
}
};
class Corperate
{
private:
Employee *PL;
void Clear();
int Corperate::Cfind(int ID,Employee **p1,Employee **p2);
public:
Corperate();
~Corperate();
void Add();
void Delete();
void Modify();
void Print();
void Save();
void Find();
void Build();
};
Corperate::Corperate()
{
Employee *p=new Technican;
PL=p;
Build();
}
Corperate::~Corperate()//仅保留一个头结点
{
Employee *p=PL;
while(p)
{
PL=p->next;
delete p;
p=PL;
}
PL=0;
}
void Corperate::Add()
{
char c;
do
{
cout<<"\n*****************增加职工****\n";
Employee *p=PL;
while(p->next)p=p->next;
int ch;
cout<<"********************************\n";
cout<<"******请输入你要增加的类别******\n";
cout<<"********技术人员:****->1********\n";
cout<<"********销售人员:****->2********\n";
cout<<"********部门经理:****->3********\n";
cout<<"********销售经理:****->4********\n";
cout<<"********************************\n";
cin>>ch;
Technican *pt;
Salesman *pa;
Manager *pm;
Salesmanager *pn;
switch(ch)
{
case 1: pt=new Technican;
pt->Input();
p->next=pt;
break;
case 2: pa=new Salesman;
pa->Input();
p->next=pa;
break;
case 3: pm=new Manager;
pm->Input();
p->next=pm;
break;
case 4: pn=new Salesmanager;
pn->Input();
p->next=pn;
break;
default:return;
}
cout<<"是否继续增加职工?\n";
cin>>c;
}while(c=='y'||c=='Y');
Save();
}
void Corperate::Clear()
{
Employee *p=PL->next;
while(p)
{
PL->next=p->next;
delete p;
p=PL->next;
}
}
void Corperate::Find()
{
double ID;
Employee *p1;
Employee *p2;
cout<<"输入你要查的编号:";
cin>>ID;
p1=PL->next;
p2=PL;
while(p1)
{
if(p1->num==ID)
break;
else
{
p2=p1;
p1=p1->next;
}
}
if(!p1)
{
cout<<"找不到你要查询的人!!!\n请确认你输入的编号是否有错!!!\n";
return;
}
else
{
cout<<"\n**职工信息表****\n";
cout<<"编号 姓名 身份代码 工作时间/销售额 收入情况 \n ";
p1->Output();
getch();
}
}
int Corperate::Cfind(int ID,Employee **p1,Employee **p2)
{
*p1=PL->next;
*p2=PL;
while(*p1)
{
if((*p1)->num==ID)
break;
else
{
*p2=*p1;
*p1=(*p1)->next;
}
}
return *p1?1:0;
}
void Corperate::Delete()
{
cout<<"\n***删除职工****\n";
int num;
Employee *p1,*p2;
cout<<"编号:";
cin>>num;
if(!Cfind(num,&p1,&p2))
{
cout<<"没有指定的人!!!\n";
}
else
{
p2->next=p1->next;
delete p1;
cout<<"正确删除!!!\n";
}
Save();
getch();
}
void Corperate::Modify()
{
cout<<"\n***修改职工***\n";
int num;
Employee *p1,*p2;
cout<<"编号";
cin>>num;
if(!Cfind(num,&p1,&p2))
{
cout<<"没有指定的人!!!\n";
}
else
{
p1->Output();//输出原来的职工信息
p1->Input();//输入新的职工信息
cout<<"修改完成!!!\n";
}
Save();
}
void Corperate::Print()
{
cout<<"\n*********职工信息表************\n";
Employee *p=PL->next;
if(!p)
{
cout<<"无职工记录!!!\n";
return;
}
while(p)
{
p->Output();
p=p->next;
}
getch();
}
void Corperate::Save()
{
ofstream f("李伟.txt",ios::out);//打开文件,遍历输出文件
Employee *p=PL->next;
while(pe)
{
p->Output(f);
p=p->next;
}
f.close();//关闭文件
cout<<"职工的信息已保存在 李伟.txt.\n";
}
void Corperate::Build()//职工信息
{
char buf[81];//临时空间
int Duty;//人员类型
Employee *p2;//新建结点指针
long t;//读写位置//清除现有结点(保留头结点)
Clear();//打开文件
ifstream f("ms.txt",ios::in);
Employee *p=PL;//尾结点指针
while(1)
{
t=f.tellg();//读取人员类型
f>>buf>>buf>>Duty;
if(f)
{
switch(Duty)//根据人员类型创建新节点
{
case 1: p2=new Technican;
break;
case 2: p2=new Salesman;
break;
case 3: p2=new Manager;
break;
case 4: p2=new Salesmanager;
break;
default:
f.close();
return;
}
p->next=p2;
p=p->next;
f.seekg(t);
e->Input(f);
}
else
break;
}//关闭文件
f.close();
}
void Menu()
{
char ch;
Corperate C;
do
{
system("cls");
cout<<"********某企业工资管理********"<<endl;
cout<<"*******************************\n";
cout<<"*********输入你的选择**********\n";
cout<<"*********增加职工:选择1*******\n";
cout<<"*********删除职工:选择2*******\n";
cout<<"*********修改职工:选择3*******\n";
cout<<"*********查询职工:选择4*******\n";
cout<<"*********查看数据:选择5*******\n";
cout<<"*********系统退出:选择6*******\n";
cout<<"*******************************\n";
cout<<"*********请选择一个操作********\n";
cin>>ch;
switch(ch)
{
case'1':C.Add();
break;
case'2':C.Delete();
break;
case'3':C.Modify();
break;
case'4':C.Find();
break;
case'5':C.Print();
break;
}
}while(ch!='6');
}
void main()
{
Menu();
}