职工系统(容器版)

继上次之后的另一版本

#include <iostream>

#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <fstream>


using namespace std;


class staff
{
public:
string employee_number;//职工号
string name;//姓名
string age;//年龄
string sex;//性别
string zip_code;//邮编
string department;//部门
string wage;//工资

};
bool up_wage(const staff &a, const staff &b)
{
return a.wage < b.wage;
}
bool down_wage(const staff &a, const staff &b)
{
return a.wage > b.wage;
}
void read(vector<staff> &A)
{
ofstream outfile;
outfile.open("职工系统new.txt", ios::app);
if(!outfile)
{
cout << "open error" << endl;
//return 0;
}
else
{
outfile.close();
ifstream infile;
infile.open("职工系统new.txt", ios::in);
while(!infile.eof())
{
staff temp;
string employee_number;//职工号
string name;//姓名
string age;//年龄
string sex;//性别
string zip_code;//邮编
string department;//部门
string wage;//工资
infile >> employee_number;
if(employee_number.length() == 0)
{
break;
}
infile >> name;
infile >> age;
infile >> sex;
infile >> zip_code;
infile >> department;
infile >> wage;
staff temp1 = {employee_number, name, age, sex, zip_code, department, wage};
A.push_back(temp1);
}
infile.close();
}


}
void write(vector<staff> &A)
{
ofstream outfile;
outfile.open("职工系统new.txt",ios::out);
for(auto it = A.begin(); it!= A.end(); it++)
{
outfile << it->employee_number << '\t';
outfile << it->name << '\t';
outfile << it->age << '\t';
outfile << it->sex << '\t';
outfile << it->zip_code << '\t';
outfile << it->department << '\t';
outfile << it->wage << endl;
}
outfile.close();
}
void show_all(vector<staff> &A)
{
for(auto it = A.begin(); it != A.end(); it++)
{
cout <<"工号:";
cout << it->employee_number <<'\t';


cout << "名字:";
cout <<it->name <<'\t';


cout << "性别:" ;
cout << it->sex <<'\t';


cout << "年龄:" ;
cout <<  it->age <<'\t';


cout << "邮编:" ;
cout << it->zip_code << '\t';


cout << "部门:";
cout << it->department << '\t';


cout << "工资:" ;
cout << it->wage << '\t';
cout << endl;
}
}
void registion(vector<staff> & A)
{
string employee_number;//职工号
string name;//姓名
string age;//年龄
string sex;//性别
string zip_code;//邮编
string department;//部门
string wage;//工资
cout << "请输入员工信息" << endl;
cout << "职工号: " << endl;
cin >> employee_number;
cout << "职工姓名: " << endl;
cin >> name;
cout << "职工年龄: " << endl;
cin >> age;
cout << "职工性别: " << endl;
cin >> sex;
cout << "职工邮编: " << endl;
cin >> zip_code;
cout << "职工部门: " << endl;
cin >> department;
cout << "职工工资: " << endl;
cin >> wage;
staff temp = {employee_number, name, age, sex, zip_code, department, wage};
A.push_back(temp);
}
void query_name(vector<staff> &A)
{
string s;
int count = 0;
cout << "请输入你想查找的员工姓名:" << endl;
cin >> s;
for(auto it = A.begin(); it != A.end(); it++)
{
if(it->name == s)
{
cout <<"工号:";
cout << it->employee_number <<'\t';


cout << "名字:";
cout <<it->name <<'\t';


cout << "性别:" ;
cout << it->sex <<'\t';


cout << "年龄:" ;
cout <<  it->age <<'\t';


cout << "邮编:" ;
cout << it->zip_code << '\t';


cout << "部门:";
cout << it->department << '\t';


cout << "工资:" ;
cout << it->wage << '\t';
cout << endl;
count++;
}


}
if(0 == count)
{
cout << "查无此人!" << endl;
}
}
void query_depart(vector<staff> &A)
{
string s2;
int count = 0;
cout << "请输入你想查找的员工部门:" << endl;
cin >> s2;
for(auto it = A.begin(); it != A.end(); it++)
{
if(it->department == s2)
{
cout <<"工号:";
cout << it->employee_number <<'\t';


cout << "名字:";
cout <<it->name <<'\t';


cout << "性别:" ;
cout << it->sex <<'\t';


cout << "年龄:" ;
cout <<  it->age <<'\t';


cout << "邮编:" ;
cout << it->zip_code << '\t';


cout << "部门:";
cout << it->department << '\t';


cout << "工资:" ;
cout << it->wage << '\t';
cout << endl;
count++;
}
}
if(0 == count)
{
cout << "查无此人!" << endl;
}
}
void delete_num(vector<staff> &A)
{
string s;
int count = 0;
cout << "请输入你想删除员工的工号:" << endl;
cin >> s;
for(auto it = A.begin(); it != A.end(); it++)
{
if(it->employee_number == s)
{
A.erase(it);
count++;
cout << "删除成功!" << endl;
break;
}
}
if(0 == count)
{
cout << "查无此人!" << endl;
}
}
void delete_name(vector<staff> &A)
{
string s;
int count = 0;
cout << "请输入你想删除员工的姓名:" << endl;
cin >> s;
for(auto it = A.begin(); it != A.end(); it++)
{
if(it->name == s)
{
A.erase(it);
count++;
cout << "删除成功!" << endl;
break;
}
}
if(0 == count)
{
cout << "查无此人!" << endl;
}
}
void update_num(vector<staff> &A)
{
cout << "请输入你要修改的员工号:" << endl;
string temp;
cin >> temp;
int count = 0;
for(auto it = A.begin(); it != A.end(); it++)
{
if(it->employee_number == temp)
{
count = 1;
cout << "请输入新工号!" << endl;
string temp2;
cin >> temp2;
it->employee_number = temp2;
cout << "修改成功" << endl;
break;
}
}
if(0 == count)
{
cout << "查无此人" << endl;
}
}
void update_name(vector<staff> &A)
{
cout << "请输入你要修改的员工号:" << endl;
string temp;
cin >> temp;
int count = 0;
for(auto it = A.begin(); it != A.end(); it++)
{
if(it->employee_number == temp)
{
count = 1;
cout << "请输入新员工姓名!" << endl;
string temp2;
cin >> temp2;
it->name = temp2;
cout << "修改成功" << endl;
break;
}
}
if(0 == count)
{
cout << "查无此人" << endl;
}
}
void update_age(vector<staff> &A)
{
cout << "请输入你要修改的员工号:" << endl;
string temp;
cin >> temp;
int count = 0;
for(auto it = A.begin(); it != A.end(); it++)
{
if(it->employee_number == temp)
{
count = 1;
cout << "请输入新员工年龄!" << endl;
string temp2;
cin >> temp2;
it->age = temp2;
cout << "修改成功" << endl;
break;
}
}
if(0 == count)
{
cout << "查无此人" << endl;
}
}
void update_sex(vector<staff> &A)
{
cout << "请输入你要修改的员工号:" << endl;
string temp;
cin >> temp;
int count = 0;
for(auto it = A.begin(); it != A.end(); it++)
{
if(it->employee_number == temp)
{
count = 1;
cout << "请输入新员工性别!" << endl;
string temp2;
cin >> temp2;
it->sex = temp2;
cout << "修改成功" << endl;
break;
}
}
if(0 == count)
{
cout << "查无此人" << endl;
}
}
void update_zip(vector<staff> &A)
{
cout << "请输入你要修改的员工号:" << endl;
string temp;
cin >> temp;
int count = 0;
for(auto it = A.begin(); it != A.end(); it++)
{
if(it->employee_number == temp)
{
count = 1;
cout << "请输入新员工邮编!" << endl;
string temp2;
cin >> temp2;
it->zip_code = temp2;
cout << "修改成功" << endl;
break;
}
}
if(0 == count)
{
cout << "查无此人" << endl;
}
}
void update_depart(vector<staff> &A)
{
cout << "请输入你要修改的员工号:" << endl;
string temp;
cin >> temp;
int count = 0;
for(auto it = A.begin(); it != A.end(); it++)
{
if(it->employee_number == temp)
{
count = 1;
cout << "请输入新员工部门!" << endl;
string temp2;
cin >> temp2;
it->department = temp2;
cout << "修改成功" << endl;
break;
}
}
if(0 == count)
{
cout << "查无此人" << endl;
}
}
void update_wage(vector<staff> &A)
{
cout << "请输入你要修改的员工号:" << endl;
string temp;
cin >> temp;
int count = 0;
for(auto it = A.begin(); it != A.end(); it++)
{
if(it->employee_number == temp)
{
count = 1;
cout << "请输入新员工薪水!" << endl;
string temp2;
cin >> temp2;
it->wage = temp2;
cout << "修改成功" << endl;
break;
}
}
if(0 == count)
{
cout << "查无此人" << endl;
}
}
void update_all(vector<staff> &A)
{
cout<<"输入要修改对象的工号:"<<flush;
int count = 0;
string tmp;
cin >> tmp;
for(auto it = A.begin(); it!= A.end(); it++)
{
if(it->employee_number == tmp)
{
count = 1;
cout << "输入新工号:"<< endl;
string temp;
cin >> temp;
it->employee_number = temp;


cout<< "输入新名字:"<< endl;
cin >> temp;
it->name = temp;


cout<< "输入新性别:"<< endl;
cin >> temp;
it->sex = temp;


cout<< "输入新年龄:"<< endl;
cin >> temp;
it->age = temp;


cout<< "输入新邮编:"<< endl;
cin >> temp;
it->zip_code = temp;


cout<< "输入新部门:"<< endl;
cin >> temp;
it->department = temp;


cout<< "输入新工资:"<< endl;
cin >> temp;
it->wage = temp;
cout << "修改成功" << endl;
break;
}
}
if(count == 0)
{
cout << "查无此人" << endl;
}
}
void update(vector<staff> &A)
{
int num = 0;
cout << "1.修改员工号" << endl;
cout << "2.修改员工姓名" << endl;
cout << "3.修改员工年龄" << endl;
cout << "4.修改员工性别" << endl;
cout << "5.修改员工邮编" << endl;
cout << "6.修改员工部门" << endl;
cout << "7.修改员工工资" << endl;
cout << "8.修改全部" << endl;
cin >> num;
switch(num)
{
case 1:
update_num(A);
break;
case 2:
update_name(A);
break;
case 3:
update_age(A);
break;
case 4:
update_sex(A);
break;
case 5:
update_zip(A);
break;
case 6:
update_depart(A);
break;
case 7:
update_wage(A);
break;
case 8:
update_all(A);
break;
default:
cout << "请重新输入正确的指令!" << endl;


}
}
int main()
{
vector<staff> L;
read(L);
cout << "欢迎来到Coder职工系统!" << endl;
while(1)
{
cout << "---------------------------------------------------" << endl;
   show_all(L);
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 << "0.退出" << endl;
cout << endl;
cout << "请输入你想选择的功能:" << endl;
int number;
cin >> number;
switch(number)
{
case 1:
{
registion(L);
break;
}
case 2:
{
   query_name(L);
break;
}
case 3:
{
   query_depart(L);
break;
}
case 4 :
{
delete_num(L);
break;
}
case 5:
{
delete_name(L);
break;
}
case 6:
{
sort(L.begin(), L.end(), up_wage);
break;
}
case 7:
{
sort(L.begin(), L.end(), down_wage);
break;
}
case 8:
{
update(L);
break;
}
case 0:
{
write(L);
return 0;
}
default: cout << "请重新输入正确的操作指令" << endl;


}
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值