员工管理系统之C++ 单链表

来源:互联网 发布:淘宝怎样免费开店 编辑:程序博客网 时间:2024/05/17 07:07
#include <iostream>
#include <string>
#include <fstream>




using namespace std;


class Worker //定义一个员工类
{
//friend func;
private:
int id;//用来保存员工的职工号
int age;//年龄
int postcode;//邮编
int salary;//工资


string name;//姓名
string sex;//性别
string department;//部门


public:
void setid(int id);
void setage(int age);
void setpostcod(int postcode);
void setsalary(int salary);


void setname(string name);
void setsex(string sex);
void setdepartment(string department);


void getid(int *pid);
void getage(int *page);
void getpostcod(int *ppostcod);
void getsalary(int *psalary);


void getname(string *pname);
void getsex(string *psex);
void getdepartment(string *pdepartment);
};


void Worker :: setid(int id)
{
this -> id = id ;
}
void Worker :: setage(int age)
{
this -> age = age;  
}
void Worker :: setpostcod(int postcode)
{
this -> postcode = postcode;
}
void Worker :: setsalary(int salary)
{
this -> salary = salary;
}


void Worker :: setname(string name)
{
this -> name = name;
}
void Worker :: setsex(string sex)
{
this -> sex = sex;
}
void Worker :: setdepartment(string department)
{
this -> department = department;
}


void Worker :: getid(int *pid)
{
*pid = id ;
}
void Worker :: getage(int *page)
{
*page = age;  
}
void Worker :: getpostcod(int *ppostcode)
{
*ppostcode  = postcode;
}
void Worker :: getsalary(int *psalary)
{
*psalary = salary;
}


void  Worker :: getname(string *pname)
{
*pname = name;
}
void  Worker :: getsex(string *psex)
{
*psex =  sex;
}
void  Worker :: getdepartment(string *pdepartment)
{
*pdepartment = department;
}


class func //定义一个功能类
{
private:
Worker worker;//定义一个值域,保存职工信息
class func *next;//定义一个指针域
class func *head;//定义一个头结点


public:
void input_msg();//用于把信息输入链表
void init();//初始化链表
void setmsg();//注册
void updatemsg();//修改
void deletemsg();//删除
//void querymsg_name();//按名字查询
//void querymsg_dep();//按部门查询
void querymsg();
void rank_salary();//按工资排名
void printf_msg();//浏览全部员工的信息
void savemsg();//保存信息到文本
};


typedef class func Node;//用Node代表class func
typedef class func *Pnode;//定义一个指向func类的指针


void func :: init()
{
head = new Node[sizeof(Node)];//创建一个头结点


if (NULL == head)
{
cout <<"申请空间失败130"<< endl;
exit(1);
}
head -> next = NULL;


}


void func :: setmsg()//注册员工的信息,注册后要保存
{
int id;//用来保存员工的职工号
int age;//年龄
int postcode;//邮编
int salary;//工资


string name;//姓名
string sex;//性别
string department;//部门


Pnode pnew = new Node[sizeof(Node)];//创建一个新的结点
if(NULL == pnew)
{
cout <<"申请空间失败147"<< endl;
exit(1);
}


cout <<"请输入员工的职工号 :"<< endl;
cin >> id;
pnew -> worker.setid(id);


cout <<"请输入员工的年龄 :"<< endl;
cin >> age;
pnew -> worker.setage(age);


cout <<"请输入员工的邮编 :"<< endl;
cin >> postcode;
pnew -> worker.setpostcod(postcode);


cout <<"请输入员工的工资 :"<< endl;
cin >> salary;
pnew -> worker.setsalary(salary);


cout <<"请输入员工的姓名 :"<< endl;
cin >> name;
pnew -> worker.setname(name);


cout <<"请输入员工的性别 :"<< endl;
cin >> sex;
pnew -> worker.setsex(sex);


cout <<"请输入员工的部门 :"<< endl;
cin >> department;
pnew -> worker.setdepartment(department);


//用头插法把信息保存到链表里面
if (head -> next == NULL)
{
head -> next = pnew;
pnew -> next = NULL;
}
else
{
pnew -> next = head -> next;
head -> next = pnew;
}


}    


void func :: printf_msg()
{
int id;//用来保存员工的职工号
int age;//年龄
int postcode;//邮编
int salary;//工资


string name;//姓名
string sex;//性别
string department;//部门


Pnode temp = head -> next; 


cout <<"----------------------------------------------------------------------------"<<endl;
cout <<"职工号\t"<<"年龄\t"<<"邮编\t"<<"工资\t"<<"姓名\t"<<"性别\t"<<"部门\t"<<endl;


while( temp != NULL)
{
temp -> worker.getid(&id);
temp -> worker.getage(&age);
temp -> worker.getpostcod(&postcode);
temp -> worker.getsalary(&salary);
temp -> worker.getname(&name);
temp -> worker.getsex(&sex);
temp -> worker.getdepartment(&department);


cout <<"----------------------------------------------------------------------------"<<endl;
//outfile <<id<<"\t\t"<<age<<"\t\t"<<postcode<<"\t\t"<<salary<<"\t\t"<<name<<"\t\t"<<sex<<"\t\t"<<department<<"\t\t"<<endl;


cout <<id<<"\t"<<age<<"\t"<<postcode<<"\t"<<salary<<"\t"<<name<<"\t"<<sex<<"\t"<<department<<"\t"<<endl;
temp = temp -> next;
}


}


void func :: input_msg()
{
ifstream infile("E:\\c++\\worker_manage\\worker_manage\\msg.txt",ios ::in);
if(!infile)
{
cerr <<"open error 221"<< endl;
exit(1);
}


int id;//用来保存员工的职工号
int age;//年龄
int postcode;//邮编
int salary;//工资


string name;//姓名
string sex;//性别
string department;//部门


int i;
int j;
cout <<"请输入总共职工的人数 :"<<endl;
cin >> i;


//把文本里面的信息按照头插法插入到链表里面
for(j = 1;j <= i;j++)
{
Pnode pnew = new Node[sizeof(Node)];//创建一个新的结点
if(NULL == pnew)
{
cout <<"申请空间失败245"<< endl;
exit(1);
}


infile >> id >> age >> postcode >> salary >> name >> sex >> department;


pnew -> worker.setid(id);
pnew -> worker.setage(age);
pnew -> worker.setpostcod(postcode);
pnew -> worker.setsalary(salary);
pnew -> worker.setname(name);
pnew -> worker.setsex(sex);
pnew -> worker.setdepartment(department);


if (head -> next == NULL)
{
head -> next = pnew;
pnew -> next = NULL;
}
else
{
pnew -> next = head -> next;
head -> next = pnew;
}
}
cout <<"员工的信息已经录入链表"<<endl;
infile.close();
}


void func :: savemsg()
{
ofstream outfile("E:\\c++\\worker_manage\\worker_manage\\msg.txt",ios ::app);


if(!outfile)
{
cerr <<"打开文件失败280"<< endl;
exit(1);
}


int id;//用来保存员工的职工号
int age;//年龄
int postcode;//邮编
int salary;//工资


string name;//姓名
string sex;//性别
string department;//部门


Pnode temp = head -> next; 


outfile <<"----------------------------------------------------------------------------"<<endl;
outfile <<"职工号\t"<<"年龄\t"<<"邮编\t"<<"工资\t"<<"姓名\t"<<"性别\t"<<"部门\t"<<endl;


while( temp != NULL)
{
temp -> worker.getid(&id);
temp -> worker.getage(&age);
temp -> worker.getpostcod(&postcode);
temp -> worker.getsalary(&salary);
temp -> worker.getname(&name);
temp -> worker.getsex(&sex);
temp -> worker.getdepartment(&department);


outfile <<"----------------------------------------------------------------------------"<<endl;
outfile <<id<<"\t\t"<<age<<"\t\t"<<postcode<<"\t\t"<<salary<<"\t\t"<<name<<"\t\t"<<sex<<"\t\t"<<department<<"\t\t"<<endl;


//outfile <<id<<"\t"<<age<<"\t"<<postcode<<"\t"<<salary<<"\t"<<name<<"\t"<<sex<<"\t"<<department<<"\t"<<endl;
temp = temp -> next;
}
outfile.close();
}


//根据ID更改信息,每个员工的ID都不一样
void func ::updatemsg()
{
int id;


int id_temp;
int age_temp;
int postcode_temp;
int salary_temp;


string name_temp;
string sex_temp;
string department_temp;


int num;


cout <<"请输入你的职工号:"<<endl;
cin >> id_temp;


Pnode temp_flag = new Node[sizeof(Node)];
Pnode temp = head -> next;


int count = 0;
while(temp != NULL)
{
temp -> worker.getid(&id);
if (id == id_temp)
{   
temp_flag = temp;
count = 1;
}
temp = temp -> next;
}
if (count == 0)
{
cout <<"没有此成员号,你可以重新输入"<<endl;
return;
}


cout <<"-------------------------------"<< endl;
cout <<"|       1.修改年龄            |"<< endl;
cout <<"|       2.修改邮编            |"<< endl; 
cout <<"|       3.修改工资            |"<< endl;
cout <<"|       4.修改姓名            |"<< endl;
cout <<"|       5.修改性别            |"<< endl;
cout <<"|       6.修改部门            |"<< endl;
cout <<"|       7.返回菜单            |"<< endl;
cout <<"-------------------------------"<< endl;


do 
{
cout <<"********************************"<< endl;
cout <<"请输入你的选择: " << endl;
cin >> num;


} while (num < 1 || num > 7 );


switch(num)
{
case 1:
{  
cout <<"请输入你要更改的年龄:"<<endl;
cin >> age_temp;
temp_flag -> worker.setage(age_temp);
break;
}
case 2:
{
cout <<"请输入你要更改的邮编:"<<endl;
cin >> postcode_temp;
temp_flag -> worker.setpostcod(postcode_temp);
break;
}
case 3:
{    
cout <<"请输入你要更改的工资:"<<endl;
cin >> salary_temp;
temp_flag -> worker.setsalary(salary_temp); 
break;
}
case 4:
{
cout <<"请输入你要更改的姓名:"<<endl;
cin >> name_temp;
temp_flag -> worker.setname(name_temp);
break;
}
case 5:
{
cout <<"请输入你要更改的性别:"<<endl;
cin >> sex_temp;
temp_flag -> worker.setsex(sex_temp);
break;
}
case 6:
{
cout <<"请输入你要更改的部门:"<<endl;
cin >> department_temp;
temp_flag -> worker.setdepartment(department_temp);
break;
}
case 7:
{
return;
}
}
}


//根据员工的ID删除员工的信息
void func :: deletemsg()
{
int id_temp;
int id;


cout <<"请输入你要删除的职工号:"<<endl;
cin >> id_temp;
//申请一个临时的结点用来保存temp
Pnode temp_flag = new Node[sizeof(Node)];
//Pnode temp = head -> next;
Pnode p = head;
int count = 0;


while(p -> next != NULL)
{
p -> next -> worker.getid(&id);
if (id_temp == id)
{   
temp_flag  = p -> next;
p -> next = p -> next -> next;
count = 1;
delete []temp_flag;
}
else
{
   p = p -> next;

}
if (count == 0)
{
cout <<"没有此成员号,你可以重新输入"<<endl;
return;
}




}


void func :: querymsg()
{
int id;//用来保存员工的职工号
int age;//年龄
int postcode;//邮编
int salary;//工资
string name;//姓名
string sex;//性别
string department;//部门


string name_temp;
string department_temp;


int num;


cout <<"-------------------------------"<< endl;
cout <<"|       1.按照名字查询信息    |"<< endl;
cout <<"|       2.按照部门查询信息    |"<< endl; 
cout <<"|       3.返回菜单            |"<< endl;
cout <<"-------------------------------"<< endl;


do 
{
cout <<"********************************"<< endl;
cout <<"请输入你的选择: " << endl;
cin >> num;


} while (num < 1 || num > 3 );


Pnode temp = head -> next;
int count = 0;


switch(num)
{


case 1 :
{
cout <<"请输入你要查询的名字:"<<endl;
cin >> name_temp;


while(temp != NULL)
{
temp -> worker.getname(&name);
if (name_temp == name)
{   
temp -> worker.getid(&id);
temp -> worker.getage(&age);
temp -> worker.getpostcod(&postcode);
temp -> worker.getsalary(&salary);
temp -> worker.getname(&name);
temp -> worker.getsex(&sex);
temp -> worker.getdepartment(&department);


cout <<"----------------------------------------------------------------------------"<<endl;
cout <<"职工号\t"<<"年龄\t"<<"邮编\t"<<"工资\t"<<"姓名\t"<<"性别\t"<<"部门\t"<<endl;
cout <<id<<"\t"<<age<<"\t"<<postcode<<"\t"<<salary<<"\t"<<name<<"\t"<<sex<<"\t"<<department<<"\t"<<endl;
count = 1;
}
temp = temp -> next;
}
if (count == 0)
{
cout <<"没有此名字,你可以重新输入"<<endl;
return;
}


break;
}
case 2 :
{
cout <<"请输入你要查询的部门:"<<endl;
cin >> department_temp;


while(temp != NULL)
{
temp -> worker.getdepartment(&department);
if (department_temp == department)
{   
temp -> worker.getid(&id);
temp -> worker.getage(&age);
temp -> worker.getpostcod(&postcode);
temp -> worker.getsalary(&salary);
temp -> worker.getname(&name);
temp -> worker.getsex(&sex);
temp -> worker.getdepartment(&department);


cout <<"----------------------------------------------------------------------------"<<endl;
cout <<"职工号\t"<<"年龄\t"<<"邮编\t"<<"工资\t"<<"姓名\t"<<"性别\t"<<"部门\t"<<endl;
cout <<id<<"\t"<<age<<"\t"<<postcode<<"\t"<<salary<<"\t"<<name<<"\t"<<sex<<"\t"<<department<<"\t"<<endl;
count = 1;
}
temp = temp -> next;
}
if (count == 0)
{
cout <<"没有此部门,你可以重新输入"<<endl;
return;
}


break;
}


case 3:
{
return;
}


}


}


void func :: rank_salary()
{
    
int salary;
int salary_temp;


Pnode temp_1 = head -> next;
Pnode temp_2 = head -> next -> next;
Pnode temp = new Node[sizeof(Node)];


while(temp_1 != NULL)
{  
temp_1 -> worker.getsalary(&salary);

while(temp_2 != NULL)
{  


temp_2 -> worker.getsalary(&salary_temp);

if (salary  >= salary_temp)
{
temp -> worker  = temp_1 -> worker;
temp_1 -> worker = temp_2 -> worker;
temp_2  -> worker = temp -> worker;
}

temp_1 = temp_1 -> next;

}


temp_2 = temp_2 -> next;
}


}


int menu_select();


int main()
{
Node head;//创建一个类
head.init();


while(1)
{
switch(menu_select())
{
case 1:
{   
system("cls");


head.setmsg();
break;
}


case 2:
{
system("cls");


head.updatemsg();
break;
}
case 3:
{
system("cls");


head.deletemsg();
break;
}
case 4:
{
system("cls");


head.querymsg();
break;
}
case 5:
{
system("cls");


head.rank_salary();
break;
}
case 6:
{   
system("cls");


head.printf_msg();
break;
}
case 7:
{
system("cls");


head.savemsg();
break;
}
case 8 :
{
exit(1);
}
}
}


return 0;
}


//按姓名查询职工信息   按部门查询职工信息 


int menu_select()
{
int c;
char str[30] = {0};
//cout <<"请输入你的选择 : " << endl;


cout <<"------欢迎来到员工管理系统-----"<< endl;
cout <<"|       1.注册新职工          |"<< endl;
cout <<"|       2.修改职工信息        |"<< endl; 
cout <<"|       3.删除职工信息        |"<< endl;
cout <<"|       4.查询职工信息        |"<< endl;
cout <<"|       5.按工资多少进行排名  |"<< endl;
cout <<"|       6.浏览全部职工信息    |"<< endl;
cout <<"|       7.保存信息到文本      |"<< endl;
cout <<"|       8.退出系统            |"<< endl;
cout <<"-------------------------------"<< endl;


do 
{
cout <<"********************************"<< endl;
cout <<"请输入你的选择: " << endl;
cin >> str ;
c = atoi(str);


} while (c < 1 || c > 8 );


return c;
}

0 0