#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int N;
struct worker
{
char name[20],sex[3];
double salary;
int age,id;
}worker[100];
void menu1();
void menu2();
void menu3();
void menu4();
void search_id();
void search_name();
void _intput();
void save();
void read();
void display();
void add();
void del();
void sort_name();
void sort_age();
void sort_salary();
int main()
{
int select,l;
while(1)
{
int flag=1;
system("cls");
menu1();
scanf("%d",&select);
switch(select)
{
case 1:while(flag)
{
system("cls");
menu2();
cin>>l;
switch(l)
{
case 1:_intput();
save();
break;
case 2:add();
break;
case 3:del();
system("pause");
break;
case 4:read();
display();
system("pause");
break;
case 5:flag=0;break;
default:cout<<"输入不合法!请输入1~4的命令!"<<endl;break;
}
}flag=1;break;
case 2:while(flag)
{
system("cls");
menu3();
cin>>l;
switch(l)
{
case 1:search_id();break;
case 2:search_name();break;
case 3:flag=0;break;
default:cout<<"输入不合法!请输入1~4的命令!"<<endl;break;
}
}flag=1;break;
case 3:while(flag)
{
system("cls");
menu4();
cin>>l;
switch(l)
{
case 1:sort_name();break;
case 2:sort_age();break;
case 3:sort_salary();break;
case 4:flag=0;break;
default:cout<<"输入不合法!请输入1~4的命令!"<<endl;break;
}
}flag=1;break;
case 4:exit(0);
default:cout<<"输入不合法!请输入1~4的命令!"<<endl;break;
}
}
}
void menu1() /*菜单1*/
{
printf("\n\n\n");
printf("\t============================================================\n");
printf("\t* * * * * * * * * * * *员工档案管理系统* * * * * * * * * * *\n");
printf("\t============================================================\n");
printf("\n");
printf("\t============================================================\n");
printf("\t 1.员工信息管理 2.员工信息查询 \n");
printf("\t 3.员工信息排序 4.退出系统 \n");
printf("\t============================================================\n");
printf("\n 请输入菜单选项(1~4):");
}
void menu2() /*菜单2*/
{
printf("\n\n\n");
printf("\t============================================================\n");
printf("\t* * * * * * * * * * * * 1.员工信息管理* * * * * * * * * * * *\n\n");
printf("\t 1.录入员工信息 2.增加员工信息 \n");
printf("\t 3.删除员工信息 4.查看所有员工信息 \n");
printf("\t * * * * * * * * * * * 5.返回 * * * * * * * * * * * * * * * *\n");
printf("\t============================================================\n");
printf("\n 请输入菜单选项(1~5):");
}
void menu3() /*菜单3*/
{
printf("\n\n\n");
printf("\t============================================================\n");
printf("\t* * * * * * * * * * * * 2.员工信息查询* * * * * * * * * * * *\n\n");
printf("\t 1.按工号查询 2.按姓名查询 \n");
printf("\t * * * * * * * * * * * 3.返回 * * * * * * * * * * * * * * * *\n");
printf("\t============================================================\n");
printf("\n 请输入菜单选项(1~3):");
}
void menu4()
{
printf("\n\n\n");
printf("\t============================================================\n");
printf("\t* * * * * * * * * * * * 3.员工信息排序* * * * * * * * * * * *\n\n");
printf("\t 1.按姓名排序 2.按年龄排序 \n");
printf("\t 3.按工资排序 4.返回 \n");
printf("\t============================================================\n");
printf("\n 请输入菜单选项(1~4):");
}
void _intput() /*录入职工信息*/
{
int i,f,j;
cout<<"请输入职工个数: ";
cin>>N;
fflush(stdin);
for(i=0;i<N;i++)
{
loop:
cout<<"请输入第"<<i+1<<"个员工的信息: "<<endl;
cout<<"工号: ";
cin>>worker[i].id;
fflush(stdin);
f=1;
if(worker[i].id<0)
cout<<"请输入正整数: "<<endl;
else
{
for(j=0;j<i;j++)
{
if(worker[i].id==worker[j].id)
{
f=0;
cout<<"工号重复,请重新输入: "<<endl;
goto loop;
}
}
}
cout<<"姓名:";
cin>>worker[i].name;
fflush(stdin);
cout<<"性别:";
cin>>worker[i].sex;
fflush(stdin);
cout<<"工资:";
cin>>worker[i].salary;
fflush(stdin);
cout<<"年龄:";
cin>>worker[i].age;
fflush(stdin);
}
cout<<"录入完毕!"<<endl;
system("pause");
}
void save() /*保存职工信息到文档*/
{
FILE *fp;
if((fp=fopen("worker.txt","wb"))==NULL)
{
cout<<"创建失败!";
exit(-1);
}
for(int i=0;i<N;i++)
{
fwrite(&worker[i],sizeof(struct worker),1,fp);
}
fclose(fp);
}
void read() /*从文档读取职工信息*/
{
FILE *fp;
int i=0;
if((fp=fopen("worker.txt","rb"))==NULL)
{
cout<<"创建失败!"<<endl;
exit(-1);
}
do
{
fread(&worker[i],sizeof(struct worker),1,fp);
i++;
}while(!feof(fp));
fclose(fp);
N=i-1;
}
void display() /*显示现在所有职工的信息*/
{
if(N==0)
{
cout<<"现在没有任何信息啊!"<<endl;
return;
}
cout<<"共有"<<N<<"个职工。信息如下:"<<endl<<endl;
for(int i=0;i<N;i++)
{
cout<<"工号: ";
cout<<worker[i].id<<endl;
cout<<"姓名: ";
cout<<worker[i].name<<endl;
cout<<"性别: ";
cout<<worker[i].sex<<endl;
cout<<"年龄: ";
cout<<worker[i].age<<endl;
cout<<"工资: ";
cout<<worker[i].salary<<endl;
cout<<endl;
}
}
void add() /*增加新职工以及其信息*/
{
FILE *fp;
struct worker p;
double t;
cout<<"请输入新增加职工的信息:"<<endl;
cout<<"请输入职工号: ";
cin>>p.id;
for(int i=0;i<N;i++)
{
while(worker[i].id==p.id)
{
cout<<"工号重复,请重新输入:"<<endl;
cin>>p.id;
}
}
cout<<"姓名:";
cin>>p.name;
fflush(stdin);
cout<<"性别:";
cin>>p.sex;
fflush(stdin);
cout<<"工资:";
cin>>p.salary;
fflush(stdin);
cout<<"年龄:";
cin>>p.age;
fflush(stdin);
cout<<"录入完毕!"<<endl;
system("pause");
if((fp=fopen("worker.txt","ab"))==NULL)
{
cout<<"创建失败!"<<endl;
exit(-1);
}
fwrite(&p,sizeof(struct worker),1,fp);
fclose(fp);
}
void search_name() /*按姓名查找职工*/
{
int f=0;
char s[20];
cout<<"请输入你要查的姓名:"<<endl;
read();
cin>>s;
for(int i=0;i<N;i++)
{
if(strcmp(s,worker[i].name)==0)
{
cout<<"工号: ";
cout<<worker[i].id;
cout<<"姓名: ";
cout<<worker[i].name;
cout<<"性别: ";
cout<<worker[i].sex;
cout<<"年龄: ";
cout<<worker[i].age;
cout<<"工资: ";
cout<<worker[i].salary;
f++;
break;
}
}
if(f==0)
cout<<"对不起!没有找到!"<<endl;
system("pause");
}
void search_id() /*按工号查找*/
{
int f=0,s;
cout<<"请输入你想要查的工号: "<<endl;
cin>>s;
read();
for(int i=0;i<N;i++)
{
if(s==worker[i].id)
{
cout<<"工号: ";
cout<<worker[i].id;
cout<<"姓名: ";
cout<<worker[i].name;
cout<<"性别: ";
cout<<worker[i].sex;
cout<<"年龄: ";
cout<<worker[i].age;
cout<<"工资: ";
cout<<worker[i].salary;
f++;
break;
}
}
if(f==0)
cout<<"对比起!没有找到!"<<endl;
system("pause");
}
void del() /*删除职工信息*/
{
int i,j;
FILE *fp;
char name[20];
char c;
if((fp=fopen("worker.txt","wb"))==NULL)
{
puts("创建失败!");
exit(-1);
}
printf("请输入要删除的职工的姓名:");
cin>>name;
for(i=0;i<N;i++)
{
if(strcmp(name,worker[i].name)==0)
{
cout<<"找到该职工,是否删除?(y/n)"<<endl;
fflush(stdin);
cin>>c;
if(c=='y'||c=='Y')
{
for(j=i;j<N;j++)
worker[j]=worker[j+1];
printf("删除成功!!!\n");
}
}
}
N-=1;
for(i=0;i<N;i++)
if(fwrite(&worker[i],sizeof(struct worker),1,fp)!=1)
{
puts("保存失败!");
system("pause");
}
fclose(fp);
}
void sort_name() /*按姓名排序*/
{
struct worker w;
read();
cout<<"共有"<<N<<"个职工。按姓名从大到小排序如下:"<<endl<<endl;
for(int i=0;i<N;i++)
{
for(int j=0;j<N-i;j++)
{
if(strcmp(worker[j].name,worker[j+1].name)<0)
{
w=worker[j];
worker[j]=worker[j+1];
worker[j+1]=w;
}
}
}
for(int i=0;i<N;i++)
{
cout<<"姓名: ";
cout<<worker[i].name<<endl;
cout<<"工号: ";
cout<<worker[i].id<<endl;
cout<<"性别: ";
cout<<worker[i].sex<<endl;
cout<<"年龄: ";
cout<<worker[i].age<<endl;
cout<<"工资: ";
cout<<worker[i].salary<<endl;
cout<<endl;
}
system("pause");
}
void sort_age() /*按年龄排序*/
{
struct worker w;
read();
cout<<"共有"<<N<<"个职工。按年龄从小到大排序如下:"<<endl<<endl;
for(int i=0;i<N;i++)
{
for(int j=0;j<N-i;j++)
{
if(worker[j].age>worker[j+1].age)
{
w=worker[j];
worker[j]=worker[j+1];
worker[j+1]=w;
}
}
}
for(int i=0;i<N;i++)
{
cout<<"年龄: ";
cout<<worker[i].age<<endl;
cout<<"工号: ";
cout<<worker[i].id<<endl;
cout<<"姓名: ";
cout<<worker[i].name<<endl;
cout<<"性别: ";
cout<<worker[i].sex<<endl;
cout<<"工资: ";
cout<<worker[i].salary<<endl;
cout<<endl;
}
system("pause");
}
void sort_salary() /*按工资排序*/
{
struct worker w;
read();
cout<<"共有"<<N<<"个职工。按工资从少到多排序如下:"<<endl<<endl;
for(int i=0;i<N;i++)
{
for(int j=0;j<N-i;j++)
{
if(worker[j].salary>worker[j+1].salary)
{
w=worker[j];
worker[j]=worker[j+1];
worker[j+1]=w;
}
}
}
for(int i=0;i<N;i++)
{
cout<<"工资: ";
cout<<worker[i].salary<<endl;
cout<<"工号: ";
cout<<worker[i].id<<endl;
cout<<"姓名: ";
cout<<worker[i].name<<endl;
cout<<"性别: ";
cout<<worker[i].sex<<endl;
cout<<"年龄: ";
cout<<worker[i].age<<endl;
cout<<endl;
}
system("pause");
}
员工管理系统
最新推荐文章于 2022-07-17 20:28:29 发布