职工信息的综合运用 目的:深入掌握单链表应用的算法设计。 内容:设计一个职工文件emp.dat,每个职工记录包含职工号(no)、名字(name)、部门号(depno)和工资(salary)信息。设计一个程序exp2-11.cpp完成以下功能: (1)从emp.dat文件中读出职工记录,并建立一个带头结点的单链表L. (2)输入一个职工记录。 (3)显示所有职工记录。 (4)按职工号no对所有职工记录进行递增排序。 (5) 按部门号depno对所有职工记录进行递增排序。 (6)按工资salary对所有职工记录进行递增排序。 (7)删除指定职工号的职工记录。 (8)删除职工文件中的全部记录。 (9)将单链表L中所有职工记录存储到职工文件emp.dat中。
时间: 2024-03-10 14:49:13 浏览: 204
单链表算法功能的实现
这是一个比较完整的题目,需要涉及到文件的读写、单链表的操作以及排序算法等知识点。下面是一个简单的C++程序,实现了上述要求:
```cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 定义职工信息结构体
struct Employee {
int no;
string name;
int depno;
double salary;
Employee *next;
};
// 读取职工文件并创建职工链表
void createList(Employee *&head) {
ifstream fin("emp.dat");
if (!fin) {
cout << "Failed to open file!" << endl;
return;
}
Employee *p = head;
while (!fin.eof()) {
Employee *emp = new Employee;
fin >> emp->no >> emp->name >> emp->depno >> emp->salary;
emp->next = nullptr;
p->next = emp;
p = emp;
}
fin.close();
}
// 在职工链表中插入一个职工记录
void insertRecord(Employee *&head) {
Employee *p = head;
while (p->next != nullptr) {
p = p->next;
}
Employee *emp = new Employee;
cout << "Input employee's no, name, department number and salary: ";
cin >> emp->no >> emp->name >> emp->depno >> emp->salary;
emp->next = nullptr;
p->next = emp;
}
// 显示所有职工记录
void displayList(Employee *head) {
Employee *p = head->next;
while (p != nullptr) {
cout << p->no << "\t" << p->name << "\t" << p->depno << "\t" << p->salary << endl;
p = p->next;
}
}
// 按职工号递增排序
void sortByNo(Employee *&head) {
Employee *p, *q, *pre, *tmp;
pre = head;
while (pre->next != nullptr) {
p = pre->next;
q = p->next;
if (q == nullptr) {
break;
}
if (p->no > q->no) {
pre->next = q;
p->next = q->next;
q->next = p;
tmp = p;
p = q;
q = tmp;
}
while (q->next != nullptr && p->no > q->next->no) {
pre = pre->next;
p = pre->next;
q = p->next;
}
}
}
// 按部门号递增排序
void sortByDepno(Employee *&head) {
Employee *p, *q, *pre, *tmp;
pre = head;
while (pre->next != nullptr) {
p = pre->next;
q = p->next;
if (q == nullptr) {
break;
}
if (p->depno > q->depno) {
pre->next = q;
p->next = q->next;
q->next = p;
tmp = p;
p = q;
q = tmp;
}
while (q->next != nullptr && p->depno > q->next->depno) {
pre = pre->next;
p = pre->next;
q = p->next;
}
}
}
// 按工资递增排序
void sortBySalary(Employee *&head) {
Employee *p, *q, *pre, *tmp;
pre = head;
while (pre->next != nullptr) {
p = pre->next;
q = p->next;
if (q == nullptr) {
break;
}
if (p->salary > q->salary) {
pre->next = q;
p->next = q->next;
q->next = p;
tmp = p;
p = q;
q = tmp;
}
while (q->next != nullptr && p->salary > q->next->salary) {
pre = pre->next;
p = pre->next;
q = p->next;
}
}
}
// 删除指定职工号的职工记录
void deleteRecord(Employee *&head, int no) {
Employee *p = head, *q;
while (p->next != nullptr) {
q = p->next;
if (q->no == no) {
p->next = q->next;
delete q;
cout << "Record deleted." << endl;
return;
}
p = p->next;
}
cout << "Record not found." << endl;
}
// 删除职工文件中的全部记录
void deleteAllRecords() {
ofstream fout("emp.dat");
fout.close();
cout << "All records deleted." << endl;
}
// 将职工链表中所有职工记录存储到职工文件中
void saveToFile(Employee *head) {
ofstream fout("emp.dat");
if (!fout) {
cout << "Failed to open file!" << endl;
return;
}
Employee *p = head->next;
while (p != nullptr) {
fout << p->no << "\t" << p->name << "\t" << p->depno << "\t" << p->salary << endl;
p = p->next;
}
fout.close();
cout << "All records saved." << endl;
}
int main() {
Employee *head = new Employee;
head->next = nullptr;
createList(head);
int choice;
while (true) {
cout << "1. Insert a record" << endl;
cout << "2. Display all records" << endl;
cout << "3. Sort by no" << endl;
cout << "4. Sort by department number" << endl;
cout << "5. Sort by salary" << endl;
cout << "6. Delete a record by no" << endl;
cout << "7. Delete all records" << endl;
cout << "8. Save all records to file" << endl;
cout << "0. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 0:
return 0;
case 1:
insertRecord(head);
break;
case 2:
displayList(head);
break;
case 3:
sortByNo(head);
break;
case 4:
sortByDepno(head);
break;
case 5:
sortBySalary(head);
break;
case 6:
int no;
cout << "Enter employee's no: ";
cin >> no;
deleteRecord(head, no);
break;
case 7:
deleteAllRecords();
break;
case 8:
saveToFile(head);
break;
default:
cout << "Invalid choice." << endl;
break;
}
}
return 0;
}
```
需要注意的是,该程序中的排序算法都是基于插入排序实现的。在实际应用中,可以使用更高效的排序算法,例如快速排序、归并排序等。同时,该程序中的文件读写操作也比较简单,没有进行异常处理等细节处理。实际应用中,需要根据具体情况进行修改和完善。
阅读全文