1. 程式人生 > >C++向量容器實現職工管理

C++向量容器實現職工管理

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


using namespace std;


class Worker //職工基本資訊類
{
private:
int id; //職工號
int age;//年齡
int postcode;//郵編
int salary;//工資


string name;//姓名
string sex;//性別
string department;//部門


public:
void setid(int id);
void setage(int age);
void setpostcode(int postcode);
void setsalary(int salary);


void setname(string name);
void setsex(string sex);
void setdepartment(string department);


int getid();
int getage();
int getpostcode();
int getsalary();


string getname();
string getsex();
string getdepartment();


friend ostream &operator <<(ostream &out, Worker &worker);//過載輸出
bool operator==(Worker &worker);//過載等於
bool operator<(Worker &worker);//過載小於
};


ostream &operator <<(ostream &out, Worker &worker)
{

out <<worker.getid()<<"\t"<<worker.getage()<<"\t"<<worker.getpostcode()<<"\t"<<worker.getsalary()<<"\t"<<worker.getname()<<"\t"<<worker.getsex()<<"\t"<<worker.getdepartment();



return out;
}


bool Worker :: operator==(Worker &worker)
{
return this->salary == worker.salary;
}


bool Worker :: operator<(Worker &worker)
{
return this->salary > worker.salary;
}


//賦值操作
void Worker :: setid(int id)
{
this->id = id;
}


void Worker :: setage(int age)
{
this->age = age;
}


void Worker :: setpostcode(int postcode)
{
this->postcode = postcode;
}


void Worker :: setsalary(int salary)
{
this->salary = salary;
}


void Worker :: setname(string name)
{
this->name = name;
}


void Worker :: setsex(string sex)
{
this->sex = sex;
}


void Worker :: setdepartment(string department)
{
this->department = department;
}


//取值操作
int Worker :: getid()
{
return id;
}


int Worker :: getage()
{
return age;
}


int Worker :: getpostcode()
{
return postcode;
}


int Worker :: getsalary()
{
return salary;
}


string Worker :: getname()
{
return name;
}


string Worker :: getsex()
{
return sex;
}


string Worker :: getdepartment()
{
return department;
}


vector<Worker> s; //建立一個名為s的連結串列容器
Worker worker;//定義一個物件


void add() //註冊職工資訊
{
int id; //職工號
int age;//年齡
int postcode;//郵編
int salary;//工資


string name;//姓名
string sex;//性別
string department;//部門


cout <<"請輸入職工的職工號 :"<< endl;
cin >> id;
worker.setid(id);


cout <<"請輸入職工的年齡 :"<< endl;
cin >> age;
worker.setage(age);


cout <<"請輸入職工的郵編 :"<< endl;
cin >> postcode;
worker.setpostcode(postcode);


cout <<"請輸入職工的工資 :"<< endl;
cin >> salary;
worker.setsalary(salary);


cout <<"請輸入職工的姓名 :"<< endl;
cin >> name;
worker.setname(name);


cout <<"請輸入職工的性別 :"<< endl;
cin >> sex;
worker.setsex(sex);


cout <<"請輸入職工的部門 :"<< endl;
cin >> department;
worker.setdepartment(department);


s.push_back(worker); //將資訊插入容器內
}


void dele() //刪除職工資訊(通過id刪除)
{
int w_id;
int flag = 0;
int i;


cout << "請輸入你要刪除的職工id :" << endl;


cin >> w_id; //輸入你要刪除的職工id;


for (i = 0; i < s.size(); i++)
{
if (w_id == s[i].getid())
{
s.erase(s.begin() + i);
cout << "刪除成功" << endl;
flag = 1;


}
}
if (flag == 0)
{
cout << "不存在此職工號,請重新輸入id" << endl;
}
}


void search()
{
string w_name; //定義要查詢的名字
string w_department; //定義要查詢的部門


int flag = 0;
int i;
int num; //功能選項


cout << "**   1.通過名字查詢   **" << endl;
cout << "**   2.通過部門查詢   **" << endl;


do 
{
cout <<"********************************"<< endl;
cout <<"請輸入你的選擇: " << endl;
cin >> num;


} while (num < 1 || num > 2 );


switch(num)
{
case 1:
{
cout << "請輸入名字 : " << endl;
cin >> w_name;
            cout <<"職工號\t"<<"年齡\t"<<"郵編\t"<<"工資\t"<<"姓名\t"<<"性別\t"<<"部門"<< endl; 


for (i = 0; i < s.size(); i++)
{
if (w_name == s.at(i).getname())
{
cout << s[i] << endl;
flag = 1;
}
}

if (flag == 0)
{
cout << "沒有此職工名字,請重新輸入" << endl;
}
break;
}


case 2:
{
cout << "請輸入部門 : " << endl;
cin >> w_department;
            cout <<"職工號\t"<<"年齡\t"<<"郵編\t"<<"工資\t"<<"姓名\t"<<"性別\t"<<"部門"<< endl; 


for (i = 0; i < s.size(); i++)
{
if (w_name == s.at(i).getdepartment())
{
cout << s[i] << endl;
flag = 1;
}
}

if (flag == 0)
{
cout << "沒有此職工部門,請重新輸入" << endl;
}
break;
}


}
}


void change()//根據ID修改職工資訊
{


int w_id; //職工號
int w_age;//年齡
int w_postcode;//郵編
int w_salary;//工資


string w_name;//姓名
string w_sex;//性別
string w_department;//部門


int num;
int i;
int flag = 0;


cout <<"請輸入職工號:"<<endl;
cin >> w_id;


for (i = 0; i < s.size(); i++)
{
if (w_id == s.at(i).getid())
{
flag = 1;


cout <<"--------------------------"<< endl;
cout <<"|       1.修改年齡       |"<< endl;
cout <<"|       2.修改郵編       |"<< endl; 
cout <<"|       3.修改工資       |"<< endl;
cout <<"|       4.修改姓名       |"<< endl;
cout <<"|       5.修改性別       |"<< endl;
cout <<"|       6.修改部門       |"<< endl;
cout <<"|       7.返回選單       |"<< endl;
cout <<"--------------------------"<< endl;


cout << endl;


do 
{
cout << "請輸入你要選擇的操作(1-7) :" << endl;
cin >> num;


} while (num < 1 || num > 7 );


switch(num)
{
case 1:
{
cout <<"請輸入你要修改的年齡:"<<endl;
cin >> w_age;
s[i].setage(w_age);
break;
}
case 2:
{
cout << "請輸入你要修改的郵編:"<<endl;
cin >> w_postcode;
   s[i].setpostcode(w_postcode);
break;
}
case 3:
{
cout <<"請輸入你要更改的工資:"<<endl;
cin >> w_salary;
s[i].setsalary(w_salary);
break;
}
case 4:
{
cout <<"請輸入你要更改的姓名:"<<endl;
cin >> w_name;
s[i].setname(w_name);
break;
}
case 5:
{
cout <<"請輸入你要更改的性別:"<<endl;
cin >> w_sex;
s[i].setsex(w_sex);
break;
}
case 6:
{
cout <<"請輸入你要更改的部門:"<<endl;
cin >> w_department;
s[i].setdepartment(w_department);
break;
}
case 7:
{
return;
}
}


}


}


if (flag == 0)
{
cout << "不存在此職工ID,請重新輸入!" << endl;
}
}


void visitworker() //顯示職工資訊
{
int i;
cout << endl;
cout <<"職工號\t"<<"年齡\t"<<"郵編\t"<<"工資\t"<<"姓名\t"<<"性別\t"<<"部門"<< endl; 


for (i = 0; i < s.size(); i++)
{
cout << s[i] << endl;
}
}


void save() //寫入檔案
{
ofstream outfile("C:\\Users\\周鍵鈞\\Desktop\\1.txt", ios::app);
if (!outfile)
{
cerr<<"open erro"<<endl;
exit(1);
}


int i;
outfile  <<"職工號\t"<<"年齡\t"<<"郵編\t"<<"工資\t"<<"姓名\t"<<"性別\t"<<"部門"<< endl;


    for (i = 0; i < s.size(); i++)

outfile  << s[i] << endl;
}


outfile.close();
}


void sal_sort()//按工資排序
{
int i;


sort(s.begin(), s.end());


for (i = 0; i < s.size(); i++)
{
cout << s[i] << endl;
}

}


void menu() //選單
{
cout<<endl;


cout<<endl;


cout<<"***********************************************"<<endl;


cout<<"** **"<<endl;


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<<"** 0.退出 **"<<endl;


cout<<"** **"<<endl;


cout<<"***********************************************"<<endl;


cout<<endl;
}


int main()
{
int num;


while (1)
{
menu();
cout << "請輸入你要選擇的功能選項(0-7):" << endl;
cin >> num;


switch(num)
{
case 1:
{
add();
break;
}
case 2:
{
change();
break;
}
case 3:
{
dele();
break;
}
case 4:
{
sal_sort();
break;
}
case 5:
{
search();
break;
}
case 6:
{
visitworker();
break;
}
case 7:
{
save();
break;
}
case 0:
{
exit(1);
}
default:
{
cout << "輸入值無效,請重新輸入" << endl;
}
}
}
return 0;
}