采用C++语言编写代码,数据结构用链表类完成,教职工信息管理系统,基本要求: 定义教职工(employee )类,其中至少包括姓名、性别、工号、联系电话、所在学院、系和学历。 功能要求: 1、设计菜单实现功能选择; 2、输入功能:输入职工信息,并保存到文件中; 3、查询功能: 1)能够根据工号精确查询职工信息; 2)能够根据姓名、学院、系、学历各项信息查询职工信息; 3)分系进行学历统计,计算各学历的人数; 4、根据教职工的学历排序输出; 5、根据工号修改职工信息; 6、根据工号删除职工信息; 7、所有的增加、修改、删除能同步到文件;也从文件读取数据到程序。
时间: 2023-07-16 09:13:09 浏览: 109
以下是使用链表类实现的教职工信息管理系统的代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 定义教职工类
class Employee {
private:
string name; // 姓名
string gender; // 性别
string id; // 工号
string phone; // 联系电话
string college; // 所在学院
string department; // 系
string education; // 学历
public:
// 构造函数
Employee(string _name = "", string _gender = "", string _id = "",
string _phone = "", string _college = "", string _department = "",
string _education = "") {
name = _name;
gender = _gender;
id = _id;
phone = _phone;
college = _college;
department = _department;
education = _education;
}
// 获取姓名
const string& getName() const {
return name;
}
// 获取工号
const string& getId() const {
return id;
}
// 获取学历
const string& getEducation() const {
return education;
}
// 打印教职工信息
void printInfo() const {
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "工号:" << id << endl;
cout << "联系电话:" << phone << endl;
cout << "所在学院:" << college << endl;
cout << "系:" << department << endl;
cout << "学历:" << education << endl;
}
};
// 定义链表节点类
class Node {
public:
Employee data; // 教职工信息
Node* next; // 指向下一个节点的指针
// 构造函数
Node(const Employee& _data = Employee(), Node* _next = nullptr) {
data = _data;
next = _next;
}
};
// 定义链表类
class LinkedList {
private:
Node* head; // 链表头指针
public:
// 构造函数
LinkedList() {
head = new Node();
}
// 析构函数
~LinkedList() {
Node* p = head;
while (p != nullptr) {
Node* q = p->next;
delete p;
p = q;
}
}
// 添加节点
void addNode(const Employee& data) {
Node* p = head;
while (p->next != nullptr) {
p = p->next;
}
p->next = new Node(data);
}
// 删除节点
void deleteNode(const string& id) {
Node* p = head;
while (p->next != nullptr && p->next->data.getId() != id) {
p = p->next;
}
if (p->next != nullptr) {
Node* q = p->next;
p->next = q->next;
delete q;
}
}
// 修改节点
void modifyNode(const string& id) {
Node* p = head->next;
while (p != nullptr && p->data.getId() != id) {
p = p->next;
}
if (p != nullptr) {
cout << "请输入新的教职工信息:" << endl;
string name, gender, phone, college, department, education;
cout << "姓名:";
cin >> name;
cout << "性别:";
cin >> gender;
cout << "联系电话:";
cin >> phone;
cout << "所在学院:";
cin >> college;
cout << "系:";
cin >> department;
cout << "学历:";
cin >> education;
p->data = Employee(name, gender, id, phone, college, department, education);
}
}
// 查找节点
void searchNode() {
int choice;
cout << "请选择查找方式:" << endl;
cout << "1. 工号" << endl;
cout << "2. 姓名" << endl;
cout << "3. 学院" << endl;
cout << "4. 系" << endl;
cout << "5. 学历" << endl;
cin >> choice;
Node* p = head->next;
int count = 0; // 统计符合条件的教职工数量
switch (choice) {
case 1: { // 根据工号查找
string id;
cout << "请输入工号:";
cin >> id;
while (p != nullptr) {
if (p->data.getId() == id) {
p->data.printInfo();
count++;
}
p = p->next;
}
break;
}
case 2: { // 根据姓名查找
string name;
cout << "请输入姓名:";
cin >> name;
while (p != nullptr) {
if (p->data.getName() == name) {
p->data.printInfo();
count++;
}
p = p->next;
}
break;
}
case 3: { // 根据学院查找
string college;
cout << "请输入学院:";
cin >> college;
while (p != nullptr) {
if (p->data.getCollege() == college) {
p->data.printInfo();
count++;
}
p = p->next;
}
break;
}
case 4: { // 根据系查找
string department;
cout << "请输入系:";
cin >> department;
while (p != nullptr) {
if (p->data.getDepartment() == department) {
p->data.printInfo();
count++;
}
p = p->next;
}
break;
}
case 5: { // 根据学历查找
string education;
cout << "请输入学历:";
cin >> education;
while (p != nullptr) {
if (p->data.getEducation() == education) {
p->data.printInfo();
count++;
}
p = p->next;
}
break;
}
default:
break;
}
if (count == 0) {
cout << "没有符合条件的教职工!" << endl;
}
}
// 统计学历人数
void countEducation() {
Node* p = head->next;
int bachelorCount = 0, masterCount = 0, doctoralCount = 0;
while (p != nullptr) {
if (p->data.getEducation() == "本科") {
bachelorCount++;
} else if (p->data.getEducation() == "硕士") {
masterCount++;
} else if (p->data.getEducation() == "博士") {
doctoralCount++;
}
p = p->next;
}
cout << "本科:" << bachelorCount << endl;
cout << "硕士:" << masterCount << endl;
cout << "博士:" << doctoralCount << endl;
}
// 根据学历排序输出
void sortByEducation() {
Node* p = head->next;
int length = 0;
while (p != nullptr) {
length++;
p = p->next;
}
Employee* arr = new Employee[length];
p = head->next;
for (int i = 0; i < length; i++) {
arr[i] = p->data;
p = p->next;
}
for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < length - i - 1; j++) {
if (arr[j].getEducation() > arr[j + 1].getEducation()) {
Employee temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < length; i++) {
arr[i].printInfo();
}
delete[] arr;
}
// 从文件中读取数据
void readFromFile() {
ifstream fin("employee.txt");
string name, gender, id, phone, college, department, education;
while (fin >> name >> gender >> id >> phone >> college >> department >> education) {
addNode(Employee(name, gender, id, phone, college, department, education));
}
fin.close();
}
// 将数据写入文件
void writeToFile() {
ofstream fout("employee.txt");
Node* p = head->next;
while (p != nullptr) {
fout << p->data.getName() << " " << p->data.getGender() << " " << p->data.getId() << " "
<< p->data.getPhone() << " " << p->data.getCollege() << " " << p->data.getDepartment() << " "
<< p->data.getEducation() << endl;
p = p->next;
}
fout.close();
}
};
// 显示菜单
void showMenu() {
cout << "欢迎使用教职工信息管理系统!" << endl;
cout << "1. 输入教职工信息" << endl;
cout << "2. 查询教职工信息" << endl;
cout << "3. 统计学历人数" << endl;
cout << "4. 根据学历排序输出" << endl;
cout << "5. 修改教职工信息" << endl;
cout << "6. 删除教职工信息" << endl;
cout << "0. 退出系统" << endl;
}
int main() {
LinkedList list;
list.readFromFile(); // 从文件中读取数据
int choice;
do {
showMenu();
cout << "请选择功能:";
cin >> choice;
switch (choice) {
case 1: { // 输入教职工信息
string name, gender, id, phone, college, department, education;
cout << "请输入教职工信息:" << endl;
cout << "姓名:";
cin >> name;
cout << "性别:";
cin >> gender;
cout << "工号:";
cin >> id;
cout << "联系电话:";
cin >> phone;
cout << "所在学院:";
cin >> college;
cout << "系:";
cin >> department;
cout << "学历:";
cin >> education;
list.addNode(Employee(name, gender, id, phone, college, department, education));
list.writeToFile(); // 将数据写入文件
break;
}
case 2: { // 查询教职工信息
list.searchNode();
break;
}
case 3: { // 统计学历人数
list.countEducation();
break;
}
case 4: { // 根据学历排序输出
list.sortByEducation();
break;
}
case 5: { // 修改教职工信息
string id;
cout << "请输入要修改的教职工工号:";
cin >> id;
list.modifyNode(id);
list.writeToFile(); // 将数据写入文件
break;
}
case 6: { // 删除教职工信息
string id;
cout << "请输入要删除的教职工工号:";
cin >> id;
list.deleteNode(id);
list.writeToFile(); // 将数据写入文件
break;
}
case 0:
cout << "退出系统!" << endl;
break;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
} while (choice != 0);
return 0;
}
```
注意:在使用链表类时,需要注意内存泄漏的问题,即在链表类的析构函数中释放链表节点的内存。同时,需要实现从文件中读取数据和将数据写入文件的功能,以便实现数据的持久化。
阅读全文