<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">本人只提供部分代码,意在学习交流,切勿拿来主义复制粘贴。转载需指明出处!</span>
#define MAXSIZE 100 //存储员工的人数的最大值
int i; //计数器,全局变量
//生日类
class Birthday
{
private:
int year,month;
public:
void setBirthday()
{
cin>>year;
cin>>month;
};
friend class Staff;
friend class Staff_List;
};
//职员信息类
class Staff
{
private:
int num; //编号
string name; //姓名
string sex; //男、女
Birthday birthday;
string ProfessionalTitle; //正高级、副高级、中级、助理级、技术员级5个级别;
string Last_Education; //小学、初中、高中、本科、硕士、博士6个级别;
int salary; //工资
string marriage; //已婚、未婚
public:
Staff(){} //构造函数
void show_message();
~Staff(){} //析构函数
friend class Staff_List;
};
//职员列表类,定义对职员的操作函数
class Staff_List
{
private:
Staff S[MAXSIZE];
int NUM; //用来计算职工数目
public:
void add(); //职工信息录入/增加
void dele(); //职工信息删除?
void modify(); //职工信息修改
void search(); //职工信息查找
void save(); //职工信息以文件形式保存?
void show_Staff(int &j); //显示职工信息在屏幕上
void Lode_In();
Staff_List(){NUM=0;} //构造函数
~Staff_List(){}
};
string p; //控制菜单开关
//菜单函数的实现
void menu_play()
{
Staff_List S;
S.Lode_In();
while(1)
{
system("cls");
menu();
int temp1;
string temp2;
cout<<"请输入你要选择的功能前的编号:";
cin>>temp1;
while(temp1<0||temp1>7)
{
cout<<"输入有误,请重新输入:"<<endl;
cin>>temp1;
}
switch(temp1)
{
case 1:
S.add();break;
case 2:
S.modify();break;
case 3:
S.dele();break;
case 4:
S.search();break;
case 0:
cout<<"是否保存本次操作?是/否: ";
cin>>temp2;
if(temp2=="是")
{
S.save();
}
cout<<"\n系统退出!\n";
exit(1);
default:
break;
}
system("pause");
}
}
//职工信息添加函数的实现
void Staff_List::add()
{
int num;
cout<<"请输入您要添加的职工编号:";
cin>>num;
for(i=0;i<=NUM-1;i++)
while(num==S[i].num)
{
cout<<"此职工编号已存在,请重新输入:";
cin>>num;
}
++NUM;
S[NUM-1].num=num;
cout<<"姓名:";cin>>S[NUM-1].name;
cout<<"性别:";cin>>S[NUM-1].sex;
cout<<"出生年月:";cin>>S[NUM-1].birthday.year>>S[NUM-1].birthday.month;
cout<<"职称:";cin>>S[NUM-1].ProfessionalTitle;
cout<<"学历:";cin>>S[NUM-1].Last_Education;
cout<<"工资(元/月):";cin>>S[NUM-1].salary;
cout<<"婚姻状况(已婚/未婚):";cin>>S[NUM-1].marriage;
cout<<"职工信息录入成功!"<<endl;
}
//职工信息删除函数的实现
/*。。。。。。。。。。。。*/
//职工信息修改
void Staff_List::modify()
{
if(NUM<=0)
{
cout<<"没有职工信息存入!"<<endl;
}
else
{
cout<<"请输入需要修改信息职工的编号:";
int num_temp;
cin>>num_temp;
for(i=0;i<=NUM-1;i++)
{
if(num_temp==S[i].num)
break;
}
if(i>NUM-1||i<0)
{
cout<<"此职工编号不存在,无需修改!";
}
else
{
cout<<"您要修改的职工信息如下:\n";
Staff_List::show_Staff(i);
char KEY='Y'; //控制修改次数的开关;
while(KEY=='Y')
{
int modify_temp; //存放需要修改的内容;
printf("\t1:修改编号;\n");
printf("\t2:修改姓名;\n");
printf("\t3:修改性别;\n");
printf("\t4:修改出生年月;\n");
printf("\t5:修改职称;\n");
printf("\t6:修改学历;\n");
printf("\t7:修改工资;\n");
printf("\t8:修改婚姻状况;\n");
cout<<"请选择你要修改的信息编号:";
cin>>modify_temp;
switch(modify_temp)
{
case 1:
cout<<"请输入新的员工编号:";
cin>>S[i].num;
printf("职员信息修改成功!\n");
break;
case 2:
cout<<"请输入新的员工姓名:";
cin>>S[i].name;
printf("职员信息修改成功!\n");
break;
case 3:
cout<<"请输入新的员工性别:";
cin>>S[i].sex;
printf("职员信息修改成功!\n");
break;
case 4:
cout<<"请输入新的员工出生年月:";
cin>>S[i].birthday.year>>S[i].birthday.month;
printf("职员信息修改成功!\n");
break;
case 5:
cout<<"请输入新的员工职称:";
cin>>S[i].ProfessionalTitle;
printf("职员信息修改成功!\n");
break;
case 6:
cout<<"请输入新的员工学历:";
cin>>S[i].Last_Education;
printf("职员信息修改成功!\n");
break;
case 7:
cout<<"请输入新的员工工资:";
cin>>S[i].salary;
printf("职员信息修改成功!\n");
break;
case 8:
cout<<"请输入新的员工婚姻状况:";
cin>>S[i].marriage;
printf("职员信息修改成功!\n");
break;
default:
break;
}
printf("输入Y继续修改该员工信息;输入其他字母退出!");
cin>>KEY;
}
}
}
}
//职工信息查找
void Staff_List::search()
{
if(NUM<=0)
{
cout<<"没有职工信息存入!"<<endl;
}
else
{
cout<<"1.按员工的编号查找;"<<endl;
cout<<"2.按员工的姓名查找;"<<endl;
cout<<"3.显示全体职工信息;"<<endl;
cout<<"4.显示某职称的职工信息;"<<endl;
cout<<"5.显示某年月前出生的职工信息;"<<endl;
cout<<"6.显示某年月后出生的职工信息;"<<endl;
cout<<"7.未婚职工信息:"<<endl<<endl;
cout<<"请输入您需要功能前的序号:";
int number_temp;
cin>>number_temp;
int num_temp;
string name_temp;
string sex_temp;
int year_temp;
int month_temp;
string ProfessionalTitle_temp;
string Last_Education_temp;
int salary_temp;
string marriage_temp;
int SAME_NAME=0;
int a[10];
switch(number_temp)
{
case 1:
cout<<"请输入您要查找的员工编号:";
cin>>num_temp;
for(i=0;i<=NUM-1;i++)
{
if(num_temp==S[i].num)
break;
}
if(i>NUM-1||i<0)
{
cout<<"此职工编号不存在!";
}
else
{
cout<<"您要查找的职工信息如下:"<<endl;
Staff_List::show_Staff(i);
}
break;
case 2:
cout<<"请输入您要查找的员工姓名:";
cin>>name_temp;
for(i=0;i<=NUM-1;i++)
{
if(name_temp==S[i].name)
{
SAME_NAME++;
a[SAME_NAME-1]=i;
}
}
if(SAME_NAME<=0)
{
cout<<"此职工姓名不存在!";
}
else
{
for(i=0;i<SAME_NAME;i++)
{
cout<<"您要查找的职工信息如下:"<<endl;
Staff_List::show_Staff(a[i]);
}
}
break;
case 3:
cout<<"全体职工信息如下:"<<endl;
for(i=0;i<=NUM-1;i++)
Staff_List::show_Staff(i);
break;
case 4:
cout<<"请输入您要查找的员工职称:";
cin>>ProfessionalTitle_temp;
cout<<"您要查找的职工信息如下:"<<endl;
for(i=0;i<=NUM-1;i++)
{
if(ProfessionalTitle_temp==S[i].ProfessionalTitle)
Staff_List::show_Staff(i);
}
break;
case 5:
cout<<"请输入您要查找的员工出生年月界限:";
cin>>year_temp>>month_temp;
cout<<year_temp<<"年"<<month_temp<<"月前出生的职工信息如下:"<<endl;
for(i=0;i<=NUM-1;i++)
{
if(year_temp>S[i].birthday.year)
Staff_List::show_Staff(i);
else if(year_temp==S[i].birthday.year)
{
if(month_temp>=S[i].birthday.month)
Staff_List::show_Staff(i);
}
}
break;
case 6:
cout<<"请输入您要查找的员工出生年月界限:";
cin>>year_temp>>month_temp;
cout<<year_temp<<"年"<<month_temp<<"月后出生的职工信息如下:"<<endl;
for(i=0;i<=NUM-1;i++)
{
if(year_temp<S[i].birthday.year)
Staff_List::show_Staff(i);
else if(year_temp==S[i].birthday.year)
{
if(month_temp<S[i].birthday.month)
Staff_List::show_Staff(i);
}
}
break;
case 7:
for(i=0;i<=NUM-1;i++)
if(S[i].marriage=="未婚")
Staff_List::show_Staff(i);
break;
default:
break;
}
}
}
<pre name="code" class="cpp">//将数据存储在文本文档中
void Staff_List::save()
{
int j;
if(NUM<=0)
{
cout<<"没有职工信息存入!"<<endl;
}
else
{
ofstream save("G:\Staff.txt",ios::out);
if(!save)
{
cout<<"不能保存:G:\Staff.txt!! "<<endl;
system("pause");
exit(1);
}
for(j=0;j<NUM;j++)
{
save<<S[j].num<<' '<<S[j].name<<' '<<S[j].sex<<' '
<<S[j].birthday.year<<' '<<S[j].birthday.month<<' '
<<S[j].ProfessionalTitle<<' '<<S[j].Last_Education<<' '<<S[j].salary<<' '<<S[j].marriage<<"\n";
}
//模仿保存画面纯属无聊之举
for(int i=1;i<=3;i++){<span style="font-family: Arial, Helvetica, sans-serif;">system("cls");printf("文件正在保存,请稍后");Sleep(100);A();Sleep(200);A();Sleep(200);A();Sleep(200);A();}cout<<"\n数据保存成功!"<<endl; save.close();}}//从文本文档中读取已经保存的职工信息</span>
void Staff_List::Lode_In() {
//模仿载入画面纯属无聊之举
for(int i=1;i<=3;i++){system("cls");printf("\n\n\n\n\n\n\n\n\n 读取文件中");Sleep(100);A();Sleep(200);A();Sleep(200);A();Sleep(200);A();Sleep(200);A();}printf("\n");system("cls");int j;ifstream fin("G:\\Staff.txt",ios::in);if(!fin){cout<<"未找到该文件,系统将创建一个新文件!\n";Sleep(1000);return;}for(j=0;!fin.eof();j++){fin>>S[j].num>>S[j].name>>S[j].sex>>S[j].birthday.year>>S[j].birthday.month>>S[j].ProfessionalTitle>>S[j].Last_Education>>S[j].salary>>S[j].marriage;}NUM=j-1;fin.close();printf("\n\n\n\n\n\n\n\n\n 载入成功!正在进入主菜单...");Sleep(3000);}//密码设置void code(){int times=0;int i;char ch;char str[20];char AdmCode[7]={"123456"};while(1){i=0;system("cls");cout<<"\n\n\n\n\n\n\n\n\n\n\n\t\t\t请输入登录密码:";ch=getch();while(ch!=13) { if(ch==8&&i>0){i--;str[i]='\0';system("cls");cout<<"\n\n\n\n\n\n\n\n\n\n\n\t\t\t请输入登录密码:";for(int j=0;j<i;j++){cout<<"*";}ch=getch();}else{printf("*"); str[i]=ch; i++;ch=getch(); }}str[i]='\0';if(!strcmp(str,AdmCode)){system("cls");cout<<"\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t登陆成功!\n";Sleep(1000);break;}else{system("cls");cout<<"\n\n\n\n\n\n\n\n\n\n\n\t\t\t密码错误!";times++;Sleep(1000);}if(times>=3){system("cls");cout<<"\n\n\n\n\n\n\n\n\n\n\n\t\t\t您已输错三次密码,系统将自动退出...\n";Sleep(1000);exit(1);}}}