繼上次之后的另一版本
#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;}新聞熱點
疑難解答