用c++完成下列要求:定义员工数据类型包括:姓名,性別,年龄,部门,电话,月工资等数据。 输入5名员工信息,输出工资最高的姓名、 部门、电话。
时间: 2023-11-18 13:05:18 浏览: 28
以下是用 C++ 完成的代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
struct Employee {
char name[20];
char gender[10];
int age;
char department[20];
char phone[20];
double salary;
};
int main() {
Employee employees[5];
double maxSalary = 0;
int maxIndex = 0;
for (int i = 0; i < 5; i++) {
cout << "请输入第 " << i + 1 << " 名员工的信息:" << endl;
cout << "姓名:";
cin >> employees[i].name;
cout << "性别:";
cin >> employees[i].gender;
cout << "年龄:";
cin >> employees[i].age;
cout << "部门:";
cin >> employees[i].department;
cout << "电话:";
cin >> employees[i].phone;
cout << "月工资:";
cin >> employees[i].salary;
if (employees[i].salary > maxSalary) {
maxSalary = employees[i].salary;
maxIndex = i;
}
}
cout << "工资最高的员工是:" << endl;
cout << "姓名:" << employees[maxIndex].name << endl;
cout << "部门:" << employees[maxIndex].department << endl;
cout << "电话:" << employees[maxIndex].phone << endl;
return 0;
}
```
在上面的代码中,我们首先定义了一个 `Employee` 结构体,表示一个员工的信息。然后在 `main` 函数中,我们创建了一个数组 `employees`,用于存储 5 名员工的信息。接着,我们依次输入每个员工的信息,并且在输入时,判断该员工的工资是否是目前最高的,如果是,则记录下该员工的索引。最后,我们输出工资最高的员工的姓名、部门和电话即可。