C++职工工资管理 基本要求:定义职工(employee)类,其中至少包括姓名、性别、工号、电话、所在科室和 工资。功能要求:1、设计菜单实现功能选择;2、输入功能:输入职工信息,并保存到文件中;查询功能:1)能够根据工号精确查询职工信息;2)能够根据姓名

时间: 2023-07-10 19:14:30 浏览: 51
来模糊查询职工信息,并将查询结果输出;3、能够统计并输出所有职工的工资总和、平均工资和最高工资者的信息;4、能够按照工资从高到低排序并输出职工信息。 以下是一个实现职工工资管理系统的示例代码: ```c++ #include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> using namespace std; // 定义职工类 class Employee { public: string name; // 姓名 string gender; // 性别 string id; // 工号 string phone; // 电话 string dept; // 所在科室 double salary; // 工资 }; // 定义职工管理类 class EmployeeManager { public: vector<Employee> employees; // 所有职工信息 // 从文件中读取职工信息 void loadFromFile(string filename) { ifstream ifs(filename); if (!ifs) { cout << "Error: cannot open file " << filename << endl; return; } Employee emp; while (ifs >> emp.name >> emp.gender >> emp.id >> emp.phone >> emp.dept >> emp.salary) { employees.push_back(emp); } ifs.close(); cout << "Load " << employees.size() << " employees from file " << filename << endl; } // 将职工信息保存到文件中 void saveToFile(string filename) { ofstream ofs(filename); if (!ofs) { cout << "Error: cannot open file " << filename << endl; return; } for (auto &emp : employees) { ofs << emp.name << ' ' << emp.gender << ' ' << emp.id << ' ' << emp.phone << ' ' << emp.dept << ' ' << emp.salary << endl; } ofs.close(); cout << "Save " << employees.size() << " employees to file " << filename << endl; } // 添加职工信息 void addEmployee() { Employee emp; cout << "Enter employee's name: "; getline(cin, emp.name); cout << "Enter employee's gender (male or female): "; getline(cin, emp.gender); cout << "Enter employee's id: "; getline(cin, emp.id); cout << "Enter employee's phone: "; getline(cin, emp.phone); cout << "Enter employee's department: "; getline(cin, emp.dept); cout << "Enter employee's salary: "; cin >> emp.salary; cin.ignore(); // 忽略换行符 employees.push_back(emp); cout << "Add employee successfully!" << endl; } // 根据工号查询职工信息 void searchById() { string id; cout << "Enter employee's id to search: "; getline(cin, id); for (auto &emp : employees) { if (emp.id == id) { cout << "Name: " << emp.name << endl; cout << "Gender: " << emp.gender << endl; cout << "Id: " << emp.id << endl; cout << "Phone: " << emp.phone << endl; cout << "Department: " << emp.dept << endl; cout << "Salary: " << emp.salary << endl; return; } } cout << "Cannot find employee with id " << id << endl; } // 根据姓名模糊查询职工信息 void searchByName() { string name; cout << "Enter employee's name to search: "; getline(cin, name); vector<Employee> result; for (auto &emp : employees) { if (emp.name.find(name) != string::npos) { result.push_back(emp); } } if (result.empty()) { cout << "Cannot find employee with name " << name << endl; } else { for (auto &emp : result) { cout << "Name: " << emp.name << endl; cout << "Gender: " << emp.gender << endl; cout << "Id: " << emp.id << endl; cout << "Phone: " << emp.phone << endl; cout << "Department: " << emp.dept << endl; cout << "Salary: " << emp.salary << endl; } } } // 统计职工工资总和、平均工资和最高工资者的信息 void getStatistics() { double sum = 0, maxSalary = 0; for (auto &emp : employees) { sum += emp.salary; maxSalary = max(maxSalary, emp.salary); } double avgSalary = employees.empty() ? 0 : sum / employees.size(); cout << "Total salary: " << sum << endl; cout << "Average salary: " << avgSalary << endl; cout << "Employee(s) with highest salary: " << endl; for (auto &emp : employees) { if (emp.salary == maxSalary) { cout << "Name: " << emp.name << endl; cout << "Gender: " << emp.gender << endl; cout << "Id: " << emp.id << endl; cout << "Phone: " << emp.phone << endl; cout << "Department: " << emp.dept << endl; cout << "Salary: " << emp.salary << endl; } } } // 按照工资从高到低排序并输出职工信息 void sortBySalary() { sort(employees.begin(), employees.end(), [](const Employee &a, const Employee &b) { return a.salary > b.salary; }); for (auto &emp : employees) { cout << "Name: " << emp.name << endl; cout << "Gender: " << emp.gender << endl; cout << "Id: " << emp.id << endl; cout << "Phone: " << emp.phone << endl; cout << "Department: " << emp.dept << endl; cout << "Salary: " << emp.salary << endl; } } }; int main() { EmployeeManager manager; manager.loadFromFile("employees.txt"); while (true) { cout << "Menu:" << endl; cout << "1. Add employee" << endl; cout << "2. Search by id" << endl; cout << "3. Search by name" << endl; cout << "4. Get statistics" << endl; cout << "5. Sort by salary" << endl; cout << "6. Save to file" << endl; cout << "0. Exit" << endl; int choice; cout << "Enter your choice: "; cin >> choice; cin.ignore(); // 忽略换行符 switch (choice) { case 0: return 0; case 1: manager.addEmployee(); break; case 2: manager.searchById(); break; case 3: manager.searchByName(); break; case 4: manager.getStatistics(); break; case 5: manager.sortBySalary(); break; case 6: manager.saveToFile("employees.txt"); break; default: cout << "Invalid choice, please try again!" << endl; } } return 0; } ``` 这个程序使用了类和文件读写等 C++ 特性,实现了简单的职工工资管理系统。

相关推荐

最新推荐

recommend-type

按以下描述和要求建立两个类:基类 Rectangle(矩形类) 和派生类 Cube(正方体)

按以下描述和要求建立两个类:基类 Rectangle(矩形类) 和派生类 Cube(正方体) 1. Rectangle 私有成员:  double x1, y1; //左下角的坐标  double x2, y2; //右上角的坐标 公有成员:  带缺省值的构造...
recommend-type

C++图书馆管理程序.docx(带有完整源代码,类的声明和定义分开,非常清晰)

完整版的C++图书馆管理程序,包括对问题的分析,类的设计与声明,还画了类的层次图;运用了继承,和多态,使用了vector,以及文件读取;使用了人员继承和书本继承。
recommend-type

c++中map的基本用法和嵌套用法实例分析

主要介绍了c++中map的基本用法和嵌套用法,以实例形式分析了map容器的基本使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
recommend-type

26. 基于视觉的道路识别技术的智能小车导航源代码.zip

1.智能循迹寻光小车(原埋图+PCB+程序).zip 2.智能循迹小车程序.zip 3.智能寻迹小车c程序和驱动.zip 4. 智能小车寻迹(含霍尔测連)c程序,zip 5.智能小车完整控制程序,zip 6.智能小车黑线循迹、避障、遥控实验综合程序,zip 7.智能小车测速+12864显示 C程序,zip 8. 智能小车(循迹、避障、遥控、测距、电压检测)原理图及源代码,zip 9.智能灭火小车,zip 10,智能搬运机器人程序.zip 11.智能arduino小车源程序,z1p 12.-种基于STM32的语音蓝牙智能小车,zip 13.循迹小车决赛程序,zip 14.循迹小车51程序(超声波 颜色识别 舵机 步进电机 1602).zip 15.寻光小车,zip 16.小车测速程序,zip 17.五路循迹智能小车c源码.zip 18.无线小车原理图和程序,zip 19.四驱智能小车资料包(源程序+原理图+芯片手册+各模块产品手册).zip 20.4WD小车安装教程及程序,z1p 21.四路红外循迹小车决赛程序,zip 22,适合初学者借鉴的arduino智能小车代码集合,zip 23.脑电波控制小车,zip 24.蓝牙智能避障小车,zip 25.基于树莓派监控小车源码.zip 26.基于视觉的道路识别技术的智能小车导航源代码,zip 27.基于STM32F407的超声波智能跟随小车,zip 28.基于arduino的蓝牙智能小车,zip.zip 29.基于51的蓝牙智能小车,zip 30.基于51单片机的红外遥控控制小车程序,zip
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/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

hive中 的Metastore

Hive中的Metastore是一个关键的组件,它用于存储和管理Hive中的元数据。这些元数据包括表名、列名、表的数据类型、分区信息、表的存储位置等信息。Hive的查询和分析都需要Metastore来管理和访问这些元数据。 Metastore可以使用不同的后端存储来存储元数据,例如MySQL、PostgreSQL、Oracle等关系型数据库,或者Hadoop分布式文件系统中的HDFS。Metastore还提供了API,使得开发人员可以通过编程方式访问元数据。 Metastore的另一个重要功能是跟踪表的版本和历史。当用户对表进行更改时,Metastore会记录这些更改,并且可以让用户回滚到
recommend-type

JSBSim Reference Manual

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

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依