排列员工信息:可根据年龄从小到大的顺序对员工信息进行排序,员工信息包括:工号、姓名、年龄、工作、性别、地址、电话、入职时间。
时间: 2023-08-31 19:47:24 浏览: 152
实现某部门职工信息管理。职工信息包括:职工号、姓名、性别、年龄、学历、基本工资、岗位津贴、住址、电话等(职工号不重复)。
4星 · 用户满意度95%
好的,你可以使用结构体存储员工信息,然后使用快速排序或其他排序算法按照年龄从小到大的顺序对员工信息进行排序。
以下是一个示例代码:
```c++
#include <iostream>
#include <algorithm>
using namespace std;
struct Employee {
int id;
string name;
int age;
string job;
char gender;
string address;
string phone;
string hire_date;
};
bool cmp(Employee a, Employee b) {
return a.age < b.age; // 按照年龄从小到大排序
}
int main() {
Employee employees[5] = {
{1001, "Tom", 25, "Engineer", 'M', "Shanghai", "13888888888", "2020-01-01"},
{1002, "Jerry", 30, "Manager", 'M', "Beijing", "13999999999", "2019-01-01"},
{1003, "Alice", 22, "Intern", 'F', "Guangzhou", "13666666666", "2021-06-01"},
{1004, "Bob", 28, "Sales", 'M', "Hangzhou", "13777777777", "2018-01-01"},
{1005, "Eva", 35, "Director", 'F', "Shenzhen", "13555555555", "2017-01-01"}
};
// 排序前的员工信息
cout << "排序前的员工信息:" << endl;
for(int i=0; i<5; i++) {
cout << "工号:" << employees[i].id << ",姓名:" << employees[i].name << ",年龄:" << employees[i].age << ",工作:" << employees[i].job << ",性别:" << employees[i].gender << ",地址:" << employees[i].address << ",电话:" << employees[i].phone << ",入职时间:" << employees[i].hire_date << endl;
}
// 对员工信息按照年龄排序
sort(employees, employees+5, cmp);
// 排序后的员工信息
cout << "排序后的员工信息:" << endl;
for(int i=0; i<5; i++) {
cout << "工号:" << employees[i].id << ",姓名:" << employees[i].name << ",年龄:" << employees[i].age << ",工作:" << employees[i].job << ",性别:" << employees[i].gender << ",地址:" << employees[i].address << ",电话:" << employees[i].phone << ",入职时间:" << employees[i].hire_date << endl;
}
return 0;
}
```
输出结果为:
```
排序前的员工信息:
工号:1001,姓名:Tom,年龄:25,工作:Engineer,性别:M,地址:Shanghai,电话:13888888888,入职时间:2020-01-01
工号:1002,姓名:Jerry,年龄:30,工作:Manager,性别:M,地址:Beijing,电话:13999999999,入职时间:2019-01-01
工号:1003,姓名:Alice,年龄:22,工作:Intern,性别:F,地址:Guangzhou,电话:13666666666,入职时间:2021-06-01
工号:1004,姓名:Bob,年龄:28,工作:Sales,性别:M,地址:Hangzhou,电话:13777777777,入职时间:2018-01-01
工号:1005,姓名:Eva,年龄:35,工作:Director,性别:F,地址:Shenzhen,电话:13555555555,入职时间:2017-01-01
排序后的员工信息:
工号:1003,姓名:Alice,年龄:22,工作:Intern,性别:F,地址:Guangzhou,电话:13666666666,入职时间:2021-06-01
工号:1001,姓名:Tom,年龄:25,工作:Engineer,性别:M,地址:Shanghai,电话:13888888888,入职时间:2020-01-01
工号:1004,姓名:Bob,年龄:28,工作:Sales,性别:M,地址:Hangzhou,电话:13777777777,入职时间:2018-01-01
工号:1002,姓名:Jerry,年龄:30,工作:Manager,性别:M,地址:Beijing,电话:13999999999,入职时间:2019-01-01
工号:1005,姓名:Eva,年龄:35,工作:Director,性别:F,地址:Shenzhen,电话:13555555555,入职时间:2017-01-01
```
阅读全文