帮我使用C++写一份公司管理系统,需要复杂一点,再加上注释

时间: 2024-03-03 08:49:51 浏览: 23
好的,我可以帮你写一份公司管理系统。由于公司管理系统的功能比较复杂,我会尽可能地详细解释每个部分的实现方式和注释。下面是一份基于C++的公司管理系统: ```c++ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; // Employee类表示公司员工 class Employee { public: Employee(string name, int age, string position, int salary) { this->name = name; this->age = age; this->position = position; this->salary = salary; } string getName() const { return name; } int getAge() const { return age; } string getPosition() const { return position; } int getSalary() const { return salary; } void setName(string name) { this->name = name; } void setAge(int age) { this->age = age; } void setPosition(string position) { this->position = position; } void setSalary(int salary) { this->salary = salary; } // 显示员工信息 void displayEmployee() const { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Position: " << position << endl; cout << "Salary: " << salary << endl; } private: string name; int age; string position; int salary; }; // Company类表示公司 class Company { public: // 添加员工 void addEmployee() { string name, position; int age, salary; cout << "Enter employee name: "; cin >> name; cout << "Enter employee age: "; cin >> age; cout << "Enter employee position: "; cin >> position; cout << "Enter employee salary: "; cin >> salary; Employee employee(name, age, position, salary); employees.push_back(employee); cout << "Employee added successfully!" << endl; } // 显示所有员工信息 void displayAllEmployees() const { for (const auto& employee : employees) { employee.displayEmployee(); cout << endl; } } // 显示员工薪资排名 void displaySalaryRanking() const { vector<Employee> tempEmployees = employees; sort(tempEmployees.begin(), tempEmployees.end(), [](const Employee& employee1, const Employee& employee2) { return employee1.getSalary() > employee2.getSalary(); }); cout << "Salary Ranking:" << endl; int rank = 1; for (const auto& employee : tempEmployees) { cout << rank << ". " << employee.getName() << " (" << employee.getSalary() << ")" << endl; rank++; } } // 更新员工信息 void updateEmployeeInfo() { string name; cout << "Enter employee name: "; cin >> name; bool found = false; for (auto& employee : employees) { if (employee.getName() == name) { found = true; string position; int age, salary; cout << "Enter new position: "; cin >> position; cout << "Enter new age: "; cin >> age; cout << "Enter new salary: "; cin >> salary; employee.setPosition(position); employee.setAge(age); employee.setSalary(salary); cout << "Employee information updated successfully!" << endl; break; } } if (!found) { cout << "Employee not found!" << endl; } } // 删除员工 void deleteEmployee() { string name; cout << "Enter employee name: "; cin >> name; bool found = false; for (auto it = employees.begin(); it != employees.end(); ++it) { if (it->getName() == name) { found = true; employees.erase(it); cout << "Employee deleted successfully!" << endl; break; } } if (!found) { cout << "Employee not found!" << endl; } } private: vector<Employee> employees; }; // 主函数 int main() { Company company; int choice; while (true) { cout << "1. Add employee" << endl; cout << "2. Display all employees" << endl; cout << "3. Display salary ranking" << endl; cout << "4. Update employee information" << endl; cout << "5. Delete employee" << endl; cout << "6. Exit" << endl; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: company.addEmployee(); break; case 2: company.displayAllEmployees(); break; case 3: company.displaySalaryRanking(); break; case 4: company.updateEmployeeInfo(); break; case 5: company.deleteEmployee(); break; case 6: cout << "Goodbye!" << endl; return 0; default: cout << "Invalid choice!" << endl; break; } } return 0; } ``` 在上面的代码中,我们定义了一个`Employee`类来表示公司员工,每个员工有名字、年龄、职位和薪水四个属性。`Company`类表示整个公司,其中包含了添加员工、显示所有员工信息、显示员工薪资排名、更新员工信息和删除员工等五个功能。 在主函数中,我们使用一个循环来实现菜单选择功能。用户可以通过输入不同的数字来选择不同的功能,然后调用相应的`Company`类的函数来实现相应的功能。 注释已经添加到代码中,希望对你有所帮助!

相关推荐

最新推荐

recommend-type

MATLAB实验一二 数值计算

MATLAB实验一二 数值计算
recommend-type

Java毕业设计-ssm基于SSM的英语学习网站的设计与实现演示录像(高分期末大作业).rar

Java毕业设计-ssm基于SSM的英语学习网站的设计与实现演示录像(高分期末大作业)
recommend-type

平安保险-智富人生A的计算

平安保险-智富人生A的计算
recommend-type

MATLAB实验五六 图像相关应用

MATLAB实验五六 图像相关应用
recommend-type

AUTOSAR-SRS-FreeRunningTimer.pdf

AUTOSAR_SRS_FreeRunningTimer.pdf
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

确保MATLAB回归分析模型的可靠性:诊断与评估的全面指南

![确保MATLAB回归分析模型的可靠性:诊断与评估的全面指南](https://img-blog.csdnimg.cn/img_convert/4b823f2c5b14c1129df0b0031a02ba9b.png) # 1. 回归分析模型的基础** **1.1 回归分析的基本原理** 回归分析是一种统计建模技术,用于确定一个或多个自变量与一个因变量之间的关系。其基本原理是拟合一条曲线或超平面,以最小化因变量与自变量之间的误差平方和。 **1.2 线性回归和非线性回归** 线性回归是一种回归分析模型,其中因变量与自变量之间的关系是线性的。非线性回归模型则用于拟合因变量与自变量之间非
recommend-type

引发C++软件异常的常见原因

1. 内存错误:内存溢出、野指针、内存泄漏等; 2. 数组越界:程序访问了超出数组边界的元素; 3. 逻辑错误:程序设计错误或算法错误; 4. 文件读写错误:文件不存在或无法打开、读写权限不足等; 5. 系统调用错误:系统调用返回异常或调用参数错误; 6. 硬件故障:例如硬盘损坏、内存损坏等; 7. 网络异常:网络连接中断、网络传输中断、网络超时等; 8. 程序异常终止:例如由于未知原因导致程序崩溃等。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。