编写一个人事管理系统代码,运用链表,要求具有以下功能,员工信息(姓名,性别,工号,职务,工资)的录入,修改,添加,删除,查询(按名字查询和按职工号查询),且有输入文件和输出文件
时间: 2024-09-12 12:05:46 浏览: 34
1_lower7y7_定义一个包含学生信息(学号_成绩)的顺序表和链表(二选一)_姓名_使其具有如下功能_
5星 · 资源好评率100%
为了创建一个人事管理系统的链表实现,我们首先需要定义一个员工节点结构体,然后实现链表的基本操作以及输入输出文件的功能。这里是一个简化的例子,使用C++实现:
```cpp
#include <iostream>
#include <fstream>
// 员工节点结构体
struct Employee {
std::string name;
char gender;
int id;
std::string position;
double salary;
// 构造函数和默认析构函数
Employee(const std::string& n, char g, int i, const std::string& p, double s) :
name(n), gender(g), id(i), position(p), salary(s) {}
~Employee() {} // 虚析构函数 (不需要在这里显示声明为virtual,因为这是默认行为)
// 显示员工信息
void display() {
std::cout << "Name: " << name << ", Gender: " << gender << ", ID: " << id
<< ", Position: " << position << ", Salary: " << salary << std::endl;
}
};
// 链表节点
class Node {
public:
Employee* data;
Node* next;
Node(Employee* emp) : data(emp), next(nullptr) {}
};
// 链表头指针
Node* head = nullptr;
// 添加员工到链表
void addEmployee(Node*& head, Employee emp) {
Node* newNode = new Node(&emp);
if (!head) {
head = newNode;
} else {
Node* current = head;
while (current->next) {
current = current->next;
}
current->next = newNode;
}
}
// 删除员工(按ID)
void deleteEmployeeByID(int id) {
if (!head) return;
if (head->data->id == id) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* current = head;
while (current->next && current->next->data->id != id) {
current = current->next;
}
if (current->next) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
}
}
// 按照名称查找员工
Employee* searchByName(const std::string& name) {
Node* current = head;
while (current) {
if (current->data->name == name) {
return current->data;
}
current = current->next;
}
return nullptr;
}
// 导入数据从文件
void importFromFile(const std::string& filename) {
std::ifstream file(filename);
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string name;
char gender;
int id;
std::string position;
double salary;
if (!(iss >> name >> gender >> id >> position >> salary)) { break; } // 如果读取失败,停止处理
Employee emp(name, gender, id, position, salary);
addEmployee(head, emp);
}
file.close();
} else {
std::cerr << "Could not open the file." << std::endl;
}
}
// 导出数据到文件
void exportToFile(const std::string& filename) {
std::ofstream file(filename);
if (file.is_open()) {
for (Node* node = head; node; node = node->next) {
node->data->display();
file << std::endl;
}
file.close();
} else {
std::cerr << "Could not open the file for writing." << std::endl;
}
}
int main() {
// 初始化并导入数据
importFromFile("employees.txt");
// 执行其他操作,如添加、删除、查询等...
// 导出数据到文件
exportToFile("output.txt");
return 0;
}
```
阅读全文