#include<iostream>
#include<string>
#include<iomanip>
#include<conio.h>
#include<fstream>
#include<cstdlib>
using namespace std;
#define null 0
class Employee
{
public:
Employee *creat(); //建立资料库
Employee *search(Employee *); //查找模块
Employee *del(Employee *); //删除模块
Employee *add(Employee *); //添加模块
Employee *show_menu(Employee *); //显示模块
Employee *modify(Employee *); //修改模块
Employee *save(Employee *); //保存模块
Employee *open(); //读出模块,自动执行
public:
char name;
char sex;
char zhiwei;
char Address;
int age;
double pay;
int count;
Employee *next; //链接指针
};
Employee *Employee::show_menu(Employee *p)//显示信息
{
Employee *h;
h=p;
if(h==null)//链表为空
{
cout<<"系统里没有资料记录."<<endl;
}
else
{
cout<<"编号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"年龄"<<setw(10)<<"职位"<<setw(10)<<"工资"<<setw(10)<<"地址"<<setw(10)<<endl;
while(p->next!=null)
{
cout<<p->count<<setw(10)<<p->name<<setw(10)<<p->sex<<setw(10)<<p->age<<setw(10)<<p->zhiwei<<setw(10)<<p->pay<<setw(10)<<p->Address<<setw(10)<<endl;
p=p->next;
}
if(p->next==null)
{
cout<<p->count<<setw(10)<<p->name<<setw(10)<<p->sex<<setw(10)<<p->age<<setw(10)<<p->zhiwei<<setw(10)<<p->pay<<setw(10)<<p->Address<<setw(10)<<endl;
}
}
cout<<endl;
return(h);
}
void wellcome() //欢迎函数
{
char choice;
int i;
cout<<"*********************************员工管理系统**********************************/n/n/n";
for (i=1;i<6;i++)
cout<<"/t/t ***********/t/t/t***********/t/t/n/n";
cout<<"/n/n";
cout<<"*********************************欢 迎 光 临***********************************/n/n/n";
cout<<"/t/t/t输入 y 或 Y 进入功能区:";
cin>>choice;
if(choice!='y'&&choice!='Y')
exit(1);
}
int login() //登陆函数
{
char password[7]="111111";
char s[7];
int flag=0;
int n=3,i;
cout<<"/n/n*********************************欢 迎 登 录************************************/n";
cout<<"/n/n/n";
for (i=1;i<4;i++)
cout<<"/t/t/t **********************/t/t/n/n";
cout<<"/n";
do{
cout<<"/t/t/t 请输入密码:";
for(int j=0;j<6;j++)
{
s[j]=getch();
putch('*');
}
s[6]='/0';
if(!strcmp(s,password))
{
system("cls");
cout<<"/n/n/n";
flag=1;
break;
}
else{
cout<<"/t密码错误,请再次输入密码:/n";
n--;
}
}
while(n>0);
if(!flag)
{
cout<<"密码错误3次!退出程序!";
exit(0);
}
else
{
return 1;
}
}
void menu()//输出主菜单
{
cout<<endl<<endl;
cout<<" "<<"=============员工工资管理系统============"<<endl;
cout<<endl<<endl<<endl;
cout<<"********************************************************************************"<<endl<<endl;
cout<<"/t/t1建员工资料库/t/t/t2查找员工资料/n"<<endl;
cout<<"/t/t3删除员工资料/t/t/t4添加员工资料/n"<<endl;
cout<<"/t/t5显示员工资料/t/t/t6修改员工资料/n"<<endl;
cout<<"/t/t7导出员工资料/t/t/t8退出系统/n"<<endl;
cout<<"/t****************************************************************"<<endl;
}
void input(Employee *p)//录入信息
{
cout<<"请输入编号:";
cin>>p->count;
cout<<"请输入姓名:";
cin>>p->name;
cout<<"请输入性别:";
cin>>p->sex;
cout<<"请输入年龄:";
cin>>p->age;
while(p->age<0||p->age>200)
{
cout<<"你的输入有误,请重新输入."<<endl<<"===>提示:年龄最低为0岁,最高为200岁."<<endl;
cout<<"请输入年龄:";
cin>>p->age;
}
cout<<"请输入工资:";
cin>>p->pay;
cout<<"请输入职位:";
cin>>p->zhiwei;
cout<<"请输入地址:";
cin>>p->Address;
cout<<endl<<endl;
}
void minput(Employee *p)//修改信息时的重新录入
{
cout<<"请输入新的编号(原来是"<<p->count<<"):";
cin>>p->count;
cout<<"请输入新的姓名(原来是"<<p->name<<"):";
cin>>p->name;
cout<<"请输入新的性别(原来是"<<p->sex<<"):";
cin>>p->sex;
cout<<"请输入新的年龄(原来是"<<p->age<<"):";
cin>>p->age;
while(p->age<0||p->age>200)
{
cout<<"你的输入有误,请重新输入."<<endl<<"===>提示:年龄最低为0岁,最高为200岁."<<endl;
cout<<"请输入年龄:";
cin>>p->age;
}
cout<<"请输入新的工资(原来是"<<p->pay<<"):";
cin>>p->pay;
cout<<"请输入新的职位(原来是"<<p->zhiwei<<"):";
cin>>p->zhiwei;
cout<<"请输入新的地址(原来是"<<p->Address<<"):";
cin>>p->Address;
}
Employee *Employee::creat()//建立资料库
{
Employee *Employee::show_menu(Employee *p);
Employee *head,*p,*p1=null;
int n;
while(n<=0)
{
cout<<"请输入学生的个数:";
cin>>n;
if(n<=0)
{
cout<<"没有学生资料."<<endl;
}
else
{
head=p=new Employee;
input(p);
for(int i=1;i<n;i++)
{
Employee *r;
p1=new Employee;
r=p;
p=p->next=p1;
input(p);
}
}
}
p->next=null;
head->show_menu(head);
return(head);
}
Employee *Employee::search(Employee *p)//查找
{
Employee *h;
h=p;
int n;
cout<<"1 按编号查找 2按名字查找 3退出查找"<<endl;
cout<<"请选择:";
cin>>n;
int d=0;
char name;
if(n==1)
{
cout<<"请输入编号:";
cin>>d;//输入要查找的编号
}
else if(n==2)
{
cout<<"请输入姓名:";
cin>>name;
}
else if(n==3)
{
return(h);
}
else
{
cout<<"===>提示:你的输入有误,请重新输入."<<endl;
h->search(h);
}
if(h==null)
{
cout<<"查找失败."<<endl<<"===>提示:没有学生资料,请先输入学生资料."<<endl;
}
else
{
while(p->next!=null)
{
if(p->count==d||p->name==name)
{
break;
}
else
{
p=p->next;
}
}
if(p->next!=null)
{
cout<<"查找成功."<<endl;
cout<<setw(10)<<"编号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"年龄"<<setw(10)<<"职位"<<setw(10)<<"工资"<<setw(10)<<"地址"<<setw(10)<<endl;
cout<<p->count<<setw(10)<<p->name<<setw(10)<<p->sex<<setw(10)<<p->age<<setw(10)<<p->zhiwei<<setw(10)<<p->pay<<setw(10)<<p->Address<<setw(10)<<endl;
}
else if(p->next==null&&(p->count==d||p->name==name))
{
cout<<"查找成功."<<endl;
cout<<"编号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"年龄"<<setw(10)<<"职位"<<setw(10)<<"工资"<<setw(10)<<"地址"<<setw(10)<<endl;
cout<<p->count<<setw(10)<<p->name<<setw(10)<<p->sex<<setw(10)<<p->age<<setw(10)<<p->zhiwei<<setw(10)<<p->pay<<setw(10)<<p->Address<<setw(10)<<endl;
}
else
{
cout<<"查找不成功."<<endl;
cout<<"===>提示:你要查的资料,系统里没有记录."<<endl;
}
}
return(h);
}
Employee *Employee::del(Employee *p)//删除
{
Employee *Employee::show_menu(Employee *p);
Employee *h,*q;
q=h=p;
int n,d=0;
char name;
cout<<"1按编号删除 2按名字删除 3退出删除"<<endl;
cout<<"请输入你的选择:";
cin>>n;
if(n==1)
{
cout<<"请输入编号:";//按编号删除
cin>>d;
}
else if(n==2)
{
cout<<"请输入姓名:";//按姓名删除
cin>>name;
}
else if(n==3)
{
return(h);
}
else
{
cout<<"你的输入有误,请重新输入."<<endl;
cout<<"===>提示:只能选择1或者2来进行删除操作."<<endl;
h->del(h);
}
if(h==null)
{
cout<<"删除失败."<<endl<<"===>提示:没有学生资料,请先输入学生资料."<<endl;
}
else if((p->count==d||p->name==name)&&p->next!=null)
{
h=p->next;
cout<<"删除成功."<<endl;
delete p;
}
else if(p->next==null&&(p->count==d||p->name==name))
{
h=null;
delete p;
cout<<"删除成功."<<endl;
}
else
{
while(p->next!=null)
{
if(p->count==d||p->name==name)
{
break;
}
else
{
p=p->next;
}
}
if(p->next!=null)
{
while(q->next!=p)
{
q=q->next;
}
q->next=p->next;
cout<<"删除成功."<<endl;
delete p;
}
else if(p->next==null&&(p->count==d||p->name==name))
{
while(q->next!=p)
{
q=q->next;
}
q->next=null;
delete p;
cout<<"删除成功."<<endl;
}
else
{
cout<<"删除失败."<<endl<<"===>提示:你要删除的资料,系统里没有记录"<<endl;
}
}
h->show_menu(h);
return(h);
}
Employee *Employee::add(Employee *p)//添加新资料
{
Employee *Employee::show_menu(Employee *p);
Employee *h,*p1,*p2;
p2=h=p;
while(p->next!=null)
{
p=p->next;
}
p=p->next=p1=new Employee;
cout<<"请输入你想要添加的学生资料."<<endl;
input(p);
if(p2->count==p->count)
{
cout<<"你的输入有误,请重新输入."<<endl<<"===>提示:学号不能相同."<<endl;
cout<<"请重新输入编号:";
cin>>p->count;
}
p->next=null;
h->show_menu(h);
return(h);
}
Employee *Employee::modify(Employee *p)//修改资料
{
Employee *Employee::show_menu(Employee *p);
int n=0;
Employee *h;
h=p;
cout<<"请输入要修改的员工的编号:";
cin>>n;
if(p->count==n)
{
minput(p);
cout<<"修改成功."<<endl;
}
else
{
while(p->count!=n)
{
if(p->next==null)
{
cout<<"修改失败."<<endl<<"===>提示:系统里没有关于该学号的资料记录."<<endl;
return(h);
}
else
{
p=p->next;
}
}
if(p!=null)
{
minput(p);
cout<<"修改成功."<<endl;
}
}
h->show_menu(h);
return(h);
}
Employee *Employee::save(Employee *p)//生成文件保存资料,以便下次接着操作
{
Employee *h,*q;int c=0;
q=h=p;
ofstream outfile("emp.txt",ios::out|ios::trunc);
if(!outfile)
{
cerr<<"没有找到文件,导入记录失败."<<endl;
exit(1);
}
if(p==null)
{
cerr<<"没有资料导入,导入记录失败."<<endl;
return(h);
}
else
{
while(q!=null)
{
c++;//统计要保存的记录的条数
q=q->next;
}
outfile<<c<<endl;
while(p!=null)
{
outfile<<"编号:"<<p->count<<setw(10)<<"姓名:"<<p->name<<setw(10)<<"年龄:"<<p->sex<<setw(10)<<"编号:"<<p->age<<setw(10)<<"职位:"<<p->zhiwei<<setw(10)<<"月薪:"<<p->pay<<setw(10)<<"地址:"<<p->Address<<setw(10)<<endl;
p=p->next;
}
delete p;
}
outfile.close();
return(h);
}
int main()//主函数
{
system("color 2f");
Employee emp,*p;
int n;
char choice;
login(); //调用登陆函数
wellcome(); //调用欢迎函数
if(p==null)
{
menu();
cout<<"请你选择操作:";
cin>>n;
while(n<1&&n>7)
{
cout<<"===>提示:菜单里没有你输入的选项."<<endl;
cout<<"你的输入有误,请重新输入:";
cin>>n;
}
if(n==1)
{
p=emp.creat();
}
else
{
exit(0);
}
}
else
{;}
do
{
system("cls");
menu();
cout<<"请你选择操作:";
cin>>n;
while(n<=1&&n>=7)
{
cout<<"===>提示:菜单里没有你输入的选项."<<endl;
cout<<"你的输入有误,请重新输入:";
cin>>n;
}
switch(n)//选择操作
{
case 1:
system("cls");
p=emp.creat();
cout<<"是否继续?(Y/y)按任意键退出"<<endl;
cin>>choice;
if(choice!='y'&&choice!='Y')
exit(1);
break;
case 2:
system("cls");
p=emp.search(p);
cout<<"是否继续?(Y/y)按任意键退出"<<endl;
cin>>choice;
if(choice!='y'&&choice!='Y')
exit(1);
break;
case 3:
system("cls");
p=emp.del(p);
cout<<"是否继续?(Y/y)按任意键退出"<<endl;
cin>>choice;
if(choice!='y'&&choice!='Y')
exit(1);
break;
case 4:
system("cls");
p=emp.add(p);
cout<<"是否继续?(Y/y)按任意键退出"<<endl;
cin>>choice;
if(choice!='y'&&choice!='Y')
exit(1);
break;
case 5:
system("cls");
p=emp.show_menu(p);
cout<<"是否继续?(Y/y)按任意键退出"<<endl;
cin>>choice;
if(choice!='y'&&choice!='Y')
exit(1);
break;
case 6:
system("cls");
p=emp.modify(p);
cout<<"是否继续?(Y/y)按任意键退出"<<endl;
cin>>choice;
if(choice!='y'&&choice!='Y')
exit(1);
break;
case 7:
system("cls");
p=emp.save(p);
cout<<"文件已导出,请在根目录下查看!"<<endl;
cout<<"是否继续?(Y/y)按任意键退出"<<endl;
cin>>choice;
if(choice!='y'&&choice!='Y')
exit(1);
break;
case 8:
system("cls");
exit(1);break;
}
}
while(n<=7);
return 0;
}