#include <vector>
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "stdlib.h"//用其中的system()函数设置控制台样式;
using namespace std;
class CEmploy{//员工类
public://便于vector直接访问
int num;
char name[10];
char sex[4];
int birth;//生日,没有考虑用类实现
char xueli[10];
char zhiwu[10];
char tel[20];
char addr[20];
};
vector <CEmploy> RegisEmploy(){//程序刚开始用到,后来没有调用
vector <CEmploy> vce;
CEmploy t;
int i,total;
cout<<"请输入所要登记的员工总数:";
cin>>total;
cout<<endl;
for(i=0;i<total;i++){
cout<<"请输入第"<<i+1<<"员工的信息(编号,姓名,性别,生日,学历,职务,住址,电话)"<<endl;
cin>>t.num>>t.name>>t.sex>>t.birth>>t.xueli>>t.zhiwu;
cin>>t.addr>>t.tel;
vce.push_back(t);
}
return vce;
}
void StoreFile(vector <CEmploy> vec){//存为外部文件
int i;
int total=vec.size();
fstream ofile;
ofile.open("d:\\2.txt",ios::out);
ofile.write((char *)&total,sizeof(int));
for(i=0;i<total;i++){
ofile.write((char *)&vec[i].num,sizeof(int));
ofile.write((char *)&vec[i].name,sizeof(vec[i].name));
ofile.write((char *)&vec[i].sex,sizeof(vec[i].sex));
ofile.write((char *)&vec[i].birth,sizeof(vec[i].birth));
ofile.write((char *)&vec[i].xueli,sizeof(vec[i].xueli));
ofile.write((char *)&vec[i].zhiwu,sizeof(vec[i].zhiwu));
ofile.write((char *)&vec[i].addr,sizeof(vec[i].addr));
ofile.write((char *)&vec[i].tel,sizeof(vec[i].tel));
}
ofile.close();
cout<<"信息已保存..."<<endl;
}
vector <CEmploy> ReadFile(){//读文件
int i,total;
CEmploy t;
vector <CEmploy> vec;
fstream ifile;
ifile.open("d:\\2.txt",ios::in);
ifile.read((char *)&total,sizeof(int));
for(i=0;i<total;i++){
ifile.read((char *)&t.num,sizeof(t.num));
ifile.read((char *)&t.name,sizeof(t.name));
ifile.read((char *)&t.sex,sizeof(t.sex));
ifile.read((char *)&t.birth,sizeof(t.birth));
ifile.read((char *)&t.xueli,sizeof(t.xueli));
ifile.read((char *)&t.zhiwu,sizeof(t.zhiwu));
ifile.read((char *)&t.addr,sizeof(t.addr));
ifile.read((char *)&t.tel,sizeof(t.tel));
vec.push_back(t);//加入vector;
}
ifile.close();
return vec;
}
void Display(vector <CEmploy> t){//显示
int i;
cout<<endl;
cout<<setw(6)<<"编号"<<setw(8)<<"姓名"
<<setw(6)<<"性别"<<setw(10)<<"生日"
<<setw(8)<<"学历"<<setw(10)<<"职务"
<<setw(10)<<"住址"<<setw(10)<<"电话"<<endl;
for(i=0;i<t.size();i++){
cout<<setw(6)<<t[i].num<<setw(8)<<t[i].name
<<setw(6)<<t[i].sex<<setw(10)<<t[i].birth
<<setw(8)<<t[i].xueli<<setw(10)<<t[i].zhiwu;
cout<<setw(10)<<t[i].addr<<setw(13)<<t[i].tel<<endl<<endl;
}
}
void Search(vector <CEmploy> t,char mode){//查询
int i,age1,age2,count=0;
char jiguan[30];
switch(mode){
case '1':
cout<<"请输入你所要查询的年龄段(中间用空格分开):"<<endl;
cin>>age1>>age2;
age1 *=10000;
age2 *=10000;
for(i=0;i<=t.size();i++){
if(((20080905-age1)>t[i].birth&&t[i].birth>(20080905-age2))||((20080905-age1)<t[i].birth&&t[i].birth<(20080905-age2))){
count++;
cout<<"符合查询条件的第"<<count<<"个员工为"<<t[i].name<<endl;
}
if(i==t.size()+1) cout<<"NO MESSAGE..."<<endl;
}
break;
case '2':
cout<<"请输入你所要查询的籍贯:"<<endl;
cin>>jiguan;
for(i=0;i<=t.size();i++){
if(!strcmp(t[i].addr,jiguan)){
count++;
cout<<"符合查询条件的第"<<count<<"个员工为"<<t[i].name<<endl;
}
if(i==t.size()+1) cout<<"NO MESSAGE..."<<endl;
}
break;
case '3':
cout<<"请先输入你所要查询的年龄段(中间用空格分开):"<<endl;
cin>>age1>>age2;
age1 *=10000;
age2 *=10000;
cout<<"请再输入你所要查询的籍贯:"<<endl;
cin>>jiguan;
for(i=0;i<=t.size();i++)
if(((20080905-age1)>t[i].birth&&t[i].birth>(20080905-age2))||((20080905-age1)<t[i].birth&&t[i].birth<(20080905-age2)))
{ if(!strcmp(t[i].addr,jiguan)){
count++;
cout<<"符合查询条件的第"<<count<<"个员工为"<<t[i].name<<endl;
}
}
if(i==(t.size()+2)) cout<<"NO MESSAGE..."<<endl;
break;
case '4':break;
default:break;
}
}
char SearchMenu(){//查询功能选择菜单
char choice;
cout<<"*******************************"<<endl;
cout<<" 1 按年龄段查询"<<endl;
cout<<" 2 按籍贯查询"<<endl;
cout<<" 3 复合查询(年龄段+籍贯) "<<endl;
cout<<" 4 返回"<<endl;
cout<<"*******************************"<<endl;
cout<<"your choice(1--4):";
cin>>choice;
while(choice!='1'&&choice!='2'&&choice!='3'&&choice!='4'){
cout<<"输入不合法,请重新选择:"<<endl;
cin>>choice;
}
return choice;
}
vector <CEmploy> InsertEmploy(vector <CEmploy> tmp){//插入员工
vector <CEmploy> vec;
vec=tmp;
CEmploy t;
char YesOrNo;
do{
cout<<endl;
cout<<"请输入所要插入的员工的信息(编号,姓名,性别,生日,学历,职务,住址,电话)"<<endl;
cin>>t.num>>t.name>>t.sex>>t.birth>>t.xueli>>t.zhiwu>>t.addr>>t.tel;
vec.push_back(t);//插到末尾
cout<<"还要继续插入员工信息吗?(Y/N):";
cin>>YesOrNo;
}while(YesOrNo=='Y'||YesOrNo=='y');
return vec;
}
vector <CEmploy> DeleteEmploy(vector <CEmploy> tmp){//删除
vector <CEmploy> vec;
CEmploy t;
vec=tmp;
char YesOrNo;
int i,DelNum;
do{
cout<<"请输入你要删除的员工的编号:";
cin>>DelNum;
for(i=0;i<vec.size();i++){
if(DelNum==vec[i].num){//所要删除元素与末尾元素交换
t=vec[i];
vec[i]=vec[(vec.size()-1)];
vec[(vec.size()-1)]=t;
vec.pop_back();//末尾元素弹出
}
if(i==(vec.size()+1))
cout<<"Don't have this NO."<<endl;
}
cout<<"继续删除员工信息吗?(Y/N):";
cin>>YesOrNo;
}while(YesOrNo=='Y'||YesOrNo=='y');
return vec;
}
vector <CEmploy> Update(vector <CEmploy> t){//更新员工信息
vector <CEmploy> vec;
vec=t;
int num,i;
// do{
cout<<"请输入你所要更新的员工的编号:";
cin>>num;
cout<<endl;
for(i=0;i<vec.size();i++){
if(num==vec[i].num){
cout<<"此员工的现有信息如下:"<<endl;
cout<<setw(6)<<"编号"<<setw(8)<<"姓名"<<setw(6)<<"性别"<<setw(10)<<"生日"
<<setw(8)<<"学历"<<setw(10)<<"职务"<<setw(10)<<"住址"<<setw(10)<<"电话"<<endl;
cout<<setw(6)<<vec[i].num<<setw(8)<<vec[i].name<<setw(6)<<vec[i].sex<<setw(10)<<vec[i].birth
<<setw(8)<<vec[i].xueli<<setw(10)<<vec[i].zhiwu<<setw(10)<<vec[i].addr<<setw(13)<<vec[i].tel<<endl;
cout<<"请输入此员工修改后的完整信息(编号,姓名,性别,生日,学历,职务,住址,电话):"<<endl;
cin>>vec[i].num>>vec[i].name>>vec[i].sex>>vec[i].birth>>vec[i].xueli>>vec[i].zhiwu>>vec[i].addr>>vec[i].tel;
}
}
if(i==(vec.size()+1))
cout<<"Sorry,没有此编号的员工..."<<endl;
//}
return vec;
}
vector <CEmploy> Sort(vector <CEmploy> t){//排序:按员工编号
vector <CEmploy> vec;
CEmploy tmp;
vec=t;
int i,j;
for(i=0;i<vec.size();i++)//冒泡排序
for(j=i+1;j<vec.size();j++){
if(vec[i].num>vec[j].num){
tmp=vec[i];
vec[i]=vec[j];
vec[j]=tmp;
}
}
cout<<"员工已经按编号从小到大排列....."<<endl;
return vec;
}
int ChoiceMenu(){//选择菜单
int c;
int flag=1;
system("color 3f");
while(flag)
{
cout<<endl;
cout<<" 欢迎使用员工管理系统 "<<endl;
cout<<"┌——————————————————————┐"<<endl;
cout<<"│ 1.读取文件 2.显示信息 3.查询员工 4.更新员工│ "<<endl;
cout<<"│ 5.插入员工 6.删除员工 7.员工排序 8.保存信息│ "<<endl;
cout<<"│ 按0退出系统... │"<<endl;
cout<<"└——————————————————————┘"<<endl<<endl;
cout<<"your choice(1--8):";
cin>>c;
if(c==1||c==2||c==3||c==4||c==5||c==6||c==7||c==8||c==0)
flag=0;
else
{cout<<"非法操作,请看清后再做选择!!!!\n按任意键继续....."<<endl;
flag=1;
getchar();
getchar();
system("cls");
}
}
return c;
}
void zanting(){//程序暂停一下,给用户查看时间
cout<<"按回车键继续......"<<endl;
getchar();
getchar();
system("cls");
}
void main()
{
vector <CEmploy> vec;
int c=ChoiceMenu();
char s;
// system("color 3f");
while(c!=0){
switch ?{
case 1:
vec=ReadFile();
Display(vec);
zanting();break;
case 2:
Display(vec);
zanting();break;
case 3:
s=SearchMenu();
while(s!='4'){
Search(vec,s);
system("pause....");
s=SearchMenu();
}
break;
case 4:
vec=Update(vec);
zanting();b