某公司需要存储雇员的编号、姓名、性别、所在部门,级别,并进行工资的计算。其中,雇员分为经理、技术人员、销售人员和销售经理。四类人员的月薪计算方法如下:经理拿固定月薪;技术人员按小时领取月薪;销售人员按其当月销售额的提成领取工资;销售经理既拿固定月薪也领取销售提成。 设计一程序能够对公司人员进行管理,应用到继承、抽象类、虚函数、虚基类、多态和文件的输入/输出等内容。
该程序实现了以下功能:
(1)添加功能:程序能够任意添加上述四类人员的记录,可提供选择界面供用户选择所要添加的人员类别,要求员工的编号要唯一,如果添加了重复编号的记录时,则提示数据添加重复并取消添加。 (2)查询功能:可根据编号、姓名等信息对已添加的记录进行查询,如果未找到,给出相应的提示信息,如果找到,则显示相应的记录信息. (3)显示功能:可显示当前系统中所有记录,每条记录占据一行。 (4)编辑功能:可根据查询结果对相应的记录进行修改,修改时注意编号的唯一性。 (5)删除功能:主要实现对已添加的人员记录进行删除。如果当前系统中没有相应的人员记录,则提示 “记录为空!” 并返回操作;否则,输入要删除的人员的编号或姓名,根据所输入的信息删除该人员记录,如果没有找到该人员信息,则提示相应的记录不存。 (6)统计功能:能根据多种参数进行人员的统计。例如,统计四类人员数量以及总数, 或者统计男、女员工的数量,或者统计平均工资、最高工资、最低工资等信息。 (7)保存功能:可将当前系统中各类人员记录存入文件中,存入方式任意。 (8)读取功能:可将保存在文件中的人员信息读入到当前系统中,供用户进行使用。
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <string>
#include <sstream>
using namespace std;
class Employee {
protected:
int id;
string name;
char gender;
string department;
string level;
public:
Employee(int id, string name, char gender, string department, string level)
: id(id), name(name), gender(gender), department(department), level(level) {}
int getId() const { return id; }
string getName() const { return name; }
char getGender() const { return gender; }
string getDepartment() const { return department; }
string getLevel() const { return level; }
virtual double calculateSalary() const = 0;
virtual void displayInfo() const {
cout << "员工编号:" << id << endl;
cout << "名字: " << name << endl;
cout << "性别: " << gender << endl;
cout << "部门: " << department << endl;
cout << "级别: " << level << endl;
}
};
class Manager : public Employee {
private:
double fixedSalary;
public:
Manager(int id, string name, char gender, string department, string level, double fixedSalary)
: Employee(id, name, gender, department, level), fixedSalary(fixedSalary) {}
double calculateSalary() const override {
return fixedSalary;
}
void displayInfo() const override {
Employee::displayInfo();
cout << "月薪: " << fixedSalary << endl;
}
};
class Technician : public Employee {
private:
double hourlyWage;
int hoursWorked;
public:
Technician(int id, string name, char gender, string department, string level, double hourlyWage, int hoursWorked)
: Employee(id, name, gender, department, level), hourlyWage(hourlyWage), hoursWorked(hoursWorked) {}
double calculateSalary() const override {
return hourlyWage * hoursWorked;
}
void displayInfo() const override {
Employee::displayInfo();
cout << "时薪: " << hourlyWage << endl;
cout << "工作时间:" << hoursWorked << endl;
}
};
class Salesperson : public Employee {
private:
double salesAmount;
double commissionRate;
public:
Salesperson(int id, string name, char gender, string department, string level, double salesAmount, double commissionRate)
: Employee(id, name, gender, department, level), salesAmount(salesAmount), commissionRate(commissionRate) {}
double calculateSalary() const override {
return salesAmount * commissionRate;
}
void displayInfo() const override {
Employee::displayInfo();
cout << "销售额: " << salesAmount << endl;
cout << "佣金率: " << commissionRate << endl;
}
};
class SalesManager : public Employee {
private:
double fixedSalary;
double salesAmount;
double commissionRate;
public:
SalesManager(int id, string name, char gender, string department, string level, double fixedSalary, double salesAmount, double commissionRate)
: Employee(id, name, gender, department, level), fixedSalary(fixedSalary), salesAmount(salesAmount), commissionRate(commissionRate) {}
double calculateSalary() const override {
return fixedSalary + (salesAmount * commissionRate);
}
void displayInfo() const override {
Employee::displayInfo();
cout << "月薪: " << fixedSalary << endl;
cout << "销售额: " << salesAmount << endl;
cout << "佣金率: " << commissionRate << endl;
}
};
class EmployeeManagementSystem {
private:
vector<Employee*> employees;
public:
void addEmployee() {
int id;
string name;
char gender;
string department;
string level;
int type;
cout << "输入员工编号: ";
cin >> id;
cin.ignore();
cout << "输入员工姓名: ";
getline(cin, name);
cout << "输入员工性别 (M/F): ";
cin >> gender;
cout << "输入员工部门: ";
cin >> department;
cout << "输入员工级别(M/T/S/SM): ";
cin >> level;
cout << "选择员工类型:" << endl;
cout << "1. 经理" << endl;
cout << "2. 技术人员" << endl;
cout << "3. 销售人员" << endl;
cout << "4. 销售经理" << endl;
cout << "输入选项:";
cin >> type;
switch (type) {
case 1: {
double fixedSalary;
cout << "输入经理的固定工资: ";
cin >> fixedSalary;
employees.push_back(new Manager(id, name, gender, department, level, fixedSalary));
cout << "经理添加成功!" << endl;
break;
}
case 2: {
double hourlyWage;
int hoursWorked;
cout << "输入技术人员的时薪: ";
cin >> hourlyWage;
cout << "输入技术人员的工作时间: ";
cin >> hoursWorked;
employees.push_back(new Technician(id, name, gender, department, level, hourlyWage, hoursWorked));
cout << "技术人员添加成功!" << endl;
break;
}
case 3: {
double salesAmount;
double commissionRate;
cout << "输入销售人员的销售额:";
cin >> salesAmount;
cout << "输入销售人员的佣金率: ";
cin >> commissionRate;
employees.push_back(new Salesperson(id, name, gender, department, level, salesAmount, commissionRate));
cout << "销售人员添加成功!" << endl;
break;
}
case 4: {
double fixedSalary;
double salesAmount;
double commissionRate;
cout << "输入销售经理的固定工资: ";
cin >> fixedSalary;
cout << "输入销售经理的销售额: ";
cin >> salesAmount;
cout << "输入销售经理的佣金率: ";
cin >> commissionRate;
employees.push_back(new SalesManager(id, name, gender, department, level, fixedSalary, salesAmount, commissionRate));
cout << "销售经理添加成功!" << endl;
break;
}
default:
cout << "无效的选择!" << endl;
}
}
void displayAllEmployees() const {
if (employees.empty()) {
cout << "没有员工可以展示!" << endl;
return;
}
for (const auto& employee : employees) {
employee->displayInfo();
cout << "工资: " << employee->calculateSalary() << endl;
cout << "--------------------------------------" << endl;
}
}
void saveToFile() const {
ofstream outfile("D:\\employees.txt", ios::out);
for (const auto& employee : employees) {
outfile << employee->getId() << "," << employee->getName() << ","
<< employee->getGender() << "," << employee->getDepartment() << ","
<< employee->getLevel() << "," << employee->calculateSalary() << endl;
}
outfile.close();
cout << "数据保存到文件!" << endl;
}
void loadFromFile() {
ifstream infile("D:\\employees.txt", ios::in);
if (!infile) {
cout << "无法打开文件!" << endl;
return;
}
string line;
while (getline(infile, line)) {
int id;
string name;
char gender;
string department;
string level;
double salary;
stringstream ss(line);
string item;
getline(ss, item, ',');
id = stoi(item);
getline(ss, name, ',');
getline(ss, item, ',');
gender = item[0];
getline(ss, department, ',');
getline(ss, level, ',');
getline(ss, item, ',');
salary = stod(item);
switch (level[0]) {
case 'M':
employees.push_back(new Manager(id, name, gender, department, level, salary));
break;
case 'T': {
int hoursWorked;
cout << "输入技术人员的工作时间: ";
cin >> hoursWorked;
employees.push_back(new Technician(id, name, gender, department, level, salary, hoursWorked));
break;
}
case 'S': {
double salesAmount;
double commissionRate;
cout << "输入销售人员的销售额:";
cin >> salesAmount;
cout << "输入销售人员的佣金率: ";
cin >> commissionRate;
employees.push_back(new Salesperson(id, name, gender, department, level, salesAmount, commissionRate));
break;
}
case 'A': {
double salesAmount;
double commissionRate;
cout << "输入销售经理的销售额: ";
cin >> salesAmount;
cout << "输入销售经理的佣金率: ";
cin >> commissionRate;
employees.push_back(new SalesManager(id, name, gender, department, level, salary, salesAmount, commissionRate));
break;
}
default:
cout << "无效的员工级别!" << endl;
}
}
infile.close();
cout << "从文件加载数据!" << endl;
}
void sortEmployeesById() {
sort(employees.begin(), employees.end(), [](Employee* a, Employee* b) { return a->getId() < b->getId(); });
cout << "按编号排序的员工!" << endl;
}
void sortEmployeesByName() {
sort(employees.begin(), employees.end(), [](Employee* a, Employee* b) { return a->getName() < b->getName(); });
cout << "员工按姓名排序!" << endl;
}
void sortEmployeesBySalary() {
sort(employees.begin(), employees.end(), [](Employee* a, Employee* b) { return a->calculateSalary() > b->calculateSalary(); });
cout << "员工按工资排序!" << endl;
}
void deleteEmployee() {
int id;
cout << "输入要删除的员工编号: ";
cin >> id;
auto it = find_if(employees.begin(), employees.end(), [id](Employee* e) { return e->getId() == id; });
if (it == employees.end()) {
cout << "未找到员工!" << endl;
return;
}
delete* it;
employees.erase(it);
cout << "员工删除成功!" << endl;
}
void statistics() {
// 统计四类人员数量以及总数
int managerCount = 0;
int technicianCount = 0;
int salespersonCount = 0;
int salesManagerCount = 0;
int totalCount = employees.size();
// 统计男、女员工的数量
int maleCount = 0;
int femaleCount = 0;
// 统计平均工资、最高工资、最低工资
double totalSalary = 0.0;
double averageSalary = 0.0;
double maxSalary = 0.0;
double minSalary = std::numeric_limits<double>::max();
for (const auto& employee : employees) {
// 统计各类人员数量
if (dynamic_cast<Manager*>(employee)) {
managerCount++;
}
else if (dynamic_cast<Technician*>(employee)) {
technicianCount++;
}
else if (dynamic_cast<Salesperson*>(employee)) {
salespersonCount++;
}
else if (dynamic_cast<SalesManager*>(employee)) {
salesManagerCount++;
}
// 统计男、女员工数量
if (employee->getGender() == 'M') {
maleCount++;
}
else if (employee->getGender() == 'F') {
femaleCount++;
}
// 统计工资信息
double salary = employee->calculateSalary();
totalSalary += salary;
if (salary > maxSalary) {
maxSalary = salary;
}
if (salary < minSalary) {
minSalary = salary;
}
}
// 计算平均工资
if (totalCount != 0) {
averageSalary = totalSalary / totalCount;
}
// 输出统计结果
cout << "四类人员数量统计:" << endl;
cout << "经理数量: " << managerCount << endl;
cout << "技术人员数量: " << technicianCount << endl;
cout << "销售人员数量: " << salespersonCount << endl;
cout << "销售经理数量: " << salesManagerCount << endl;
cout << "总人数: " << totalCount << endl;
cout << "男、女员工数量统计:" << endl;
cout << "男性员工数量: " << maleCount << endl;
cout << "女性员工数量: " << femaleCount << endl;
cout << "工资统计:" << endl;
cout << "平均工资: " << averageSalary << endl;
cout << "最高工资: " << maxSalary << endl;
cout << "最低工资: " << minSalary << endl;
}
void displayMenu() {
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 << "9. 统计" << endl;
cout << "0. 退出" << endl;
cout << "输入选项: ";
}
};
int main() {
EmployeeManagementSystem ems;
int choice;
while (true) {
ems.displayMenu();
cin >> choice;
switch (choice) {
case 1:
ems.addEmployee();
break;
case 2:
ems.displayAllEmployees();
break;
case 3:
ems.saveToFile();
break;
case 4:
ems.loadFromFile();
break;
case 5:
ems.sortEmployeesById();
break;
case 6:
ems.sortEmployeesByName();
break;
case 7:
ems.sortEmployeesBySalary();
break;
case 8:
ems.deleteEmployee();
break;
case 9:
ems.statistics();
break;
case 0:
cout << "退出..." << endl;
return 0;
default:
cout << "无效的选择!" << endl;
}
}
return 0;
}