链表综合算法设计(c++数据结构)

 

课程设计题目1--链表综合算法设计

一、设计内容
已知简单的人事信息系统中职工记录包含职工编号(no)、职工姓名(name)、部门名称(depname)、职称(title)和工资数(salary)等信息(可以增加其他信息),设计并完成一个简单的人事信息管理系统,要求完成但不限于以下功能:
(1)    增加一个职工信息;
(2)    显示所有职工信息;
(3)    按部门名称分类显示该部门所有职工信息;
(4)    按部门显示各部门职工工资总额;
(5)    删除职工信息(可以删除符合条件的一批记录)
(6)    按职称调整工资;
(7)    可自行增加功能(例如按职工编号排序等其他功能)
二、设计要求
(1)以菜单形式显示,主菜单包括(可以扩展)(1:增加职工信息 2:删除员工信息 3:查找职工信息 4:工资调整 5:统计分析  0:退出)相应的主菜单可以设计子菜单。(根据系统功能的多少评定成绩)
(2)提交设计报告,报告包括代码、设计思路(流程图)和实现界面截图等。

代码实现

#include <iostream>
#include <string>
using namespace std;
// 职工信息结构体
struct Employee {
    int no;
    string name;
    string depname;
    string title;
    double salary;
    Employee* next;
};
// 全局变量,链表头指针
Employee* head = nullptr;
// 操作函数声明
void addEmployee();
void displayAllEmployees();
void displayEmployeesByDepartment();
void displayDepartmentTotalSalary();
void deleteEmployee();
void adjustSalaryByTitle();
void menu();
// 增加一个职工信息
void addEmployee() {
    Employee* newEmployee = new Employee;
    cout << "请输入职工编号:";
    cin >> newEmployee->no;
    cout << "请输入职工姓名:";
    cin >> newEmployee->name;
    cout << "请输入部门名称:";
    cin >> newEmployee->depname;
    cout << "请输入职称:";
    cin >> newEmployee->title;
    cout << "请输入工资数:";
    cin >> newEmployee->salary;
    newEmployee->next = nullptr;
    if (head == nullptr) {
        head = newEmployee;
    }
    else {
        Employee* temp = head;
        while (temp->next != nullptr) {
            temp = temp->next;
        }
        temp->next = newEmployee;
    }
    cout << "职工信息添加成功!" << endl;
}
// 显示所有职工信息
void displayAllEmployees() {
    if (head == nullptr) {
        cout << "暂无职工信息!" << endl;
        return;
    }
    Employee* temp = head;
    cout << "所有职工信息:" << endl;
    while (temp != nullptr) {
        cout << "编号:" << temp->no << endl;
        cout << "姓名:" << temp->name << endl;
        cout << "部门:" << temp->depname << endl;
        cout << "职称:" << temp->title << endl;
        cout << "工资:" << temp->salary << endl;
        cout << "-------------------------" << endl;
        temp = temp->next;
    }
}
// 按部门名称分类显示该部门所有职工信息
void displayEmployeesByDepartment() {
    if (head == nullptr) {
        cout << "暂无职工信息!" << endl;
        return;
    }
    string department;
    cout << "请输入部门名称:";
    cin >> department;
    Employee* temp = head;
    bool found = false;
    cout << "部门" << department << "下的职工信息:" << endl;
    while (temp != nullptr) {
        if (temp->depname == department) {
            cout << "编号:" << temp->no << endl;
            cout << "姓名:" << temp->name << endl;
            cout << "职称:" << temp->title << endl;
            cout << "工资:" << temp->salary << endl;
            cout << "-------------------------" << endl;
            found = true;
        }
        temp = temp->next;
    }
    if (!found) {
        cout << "未找到部门" << department << "下的职工信息!" << endl;
    }
}
// 按部门显示各部门职工工资总额
void displayDepartmentTotalSalary() {
    if (head == nullptr) {
        cout << "暂无职工信息!" << endl;
        return;
    }
    // 使用一个数组来存储每个部门的工资总额
    const int MAX_DEPARTMENTS = 100;
    double departmentTotalSalary[MAX_DEPARTMENTS] = { 0.0 };
    Employee* temp = head;
    while (temp != nullptr) {
        departmentTotalSalary[temp->depname.length()] += temp->salary;
        temp = temp->next;
    }
    cout << "各部门职工工资总额:" << endl;
    for (int i = 0; i < MAX_DEPARTMENTS; i++) {
        if (departmentTotalSalary[i] > 0) {
            cout << "部门:" << i << " 工资总额:" << departmentTotalSalary[i] << endl;
        }
    }
}
// 删除职工信息
void deleteEmployee() {
    if (head == nullptr) {
        cout << "暂无职工信息!" << endl;
        return;
    }
    int employeeNo;
    cout << "请输入要删除的职工编号:";
    cin >> employeeNo;
    Employee* current = head;
    Employee* previous = nullptr;
    while (current != nullptr) {
        if (current->no == employeeNo) {
            if (previous == nullptr) {
                // 删除头节点
                head = current->next;
            }
            else {
                previous->next = current->next;
            }
            delete current;
            cout << "职工信息删除成功!" << endl;
            return;
        }
        previous = current;
        current = current->next;
    }
    cout << "未找到职工编号为" << employeeNo << "的记录!" << endl;
}
// 按职称调整工资
void adjustSalaryByTitle() {
    if (head == nullptr) {
        cout << "暂无职工信息!" << endl;
        return;
    }
    string jobTitle;
    cout << "请输入职称:";
    cin >> jobTitle;
    double salaryAdjustment;
    cout << "请输入工资调整金额:";
    cin >> salaryAdjustment;
    Employee* temp = head;
    bool found = false;
    while (temp != nullptr) {
        if (temp->title == jobTitle) {
            temp->salary += salaryAdjustment;
            found = true;
        }
        temp = temp->next;
    }
    if (found) {
        cout << "职称为" << jobTitle << "的职工工资调整成功!" << endl;
    }
    else {
        cout << "未找到职称为" << jobTitle << "的职工!" << endl;
    }
}
// 菜单
void menu() {
    int choice;
    do {
        cout << "-------------------------" << endl;
        cout << "1. 增加职工信息" << endl;
        cout << "2. 显示所有职工信息" << endl;
        cout << "3. 按部门分类显示职工信息" << endl;
        cout << "4. 按部门显示各部门职工工资总额" << endl;
        cout << "5. 删除职工信息" << endl;
        cout << "6. 按职称调整工资" << endl;
        cout << "0. 退出" << endl;
        cout << "请输入您的选择:";
        cin >> choice;
        switch (choice) {
        case 1:
            addEmployee();
            break;
        case 2:
            displayAllEmployees();
            break;
        case 3:
            displayEmployeesByDepartment();
            break;
        case 4:
            displayDepartmentTotalSalary();
            break;
        case 5:
            deleteEmployee();
            break;
        case 6:
            adjustSalaryByTitle();
            break;
        case 0:
            cout << "感谢使用人事信息管理系统!" << endl;
            break;
        default:
            cout << "无效的选择!" << endl;
            break;
        }
    } while (choice != 0);
}
int main() {
    menu();
    return 0;
}
 


vs代码实现

#include <iostream>
#include <string>
using namespace std;

// 职工信息结构体
struct Employee {
	int no;
	string name;
	string depname;
	string title;
	double salary;
	Employee* next;
};

// 全局变量,链表头指针
Employee* head = nullptr;

// 操作函数声明
void addEmployee();
void displayAllEmployees();
void displayEmployeesByDepartment();
void displayDepartmentTotalSalary();
void deleteEmployee();
void adjustSalaryByTitle();
void menu();

// 增加一个职工信息
void addEmployee() {
	Employee* newEmployee = new Employee;
	cout << "请输入职工编号:";
	cin >> newEmployee->no;
	cout << "请输入职工姓名:";
	cin >> newEmployee->name;
	cout << "请输入部门名称:";
	cin >> newEmployee->depname;
	cout << "请输入职称:";
	cin >> newEmployee->title;
	cout << "请输入工资数:";
	cin >> newEmployee->salary;

	newEmployee->next = nullptr;

	if (head == nullptr) {
		head = newEmployee;
	}
	else {
		Employee* temp = head;
		while (temp->next != nullptr) {
			temp = temp->next;
		}
		temp->next = newEmployee;
	}

	cout << "职工信息添加成功!" << endl;
}

// 显示所有职工信息
void displayAllEmployees() {
	if (head == nullptr) {
		cout << "暂无职工信息!" << endl;
		return;
	}

	Employee* temp = head;
	cout << "所有职工信息:" << endl;
	while (temp != nullptr) {
		cout << "编号:" << temp->no << endl;
		cout << "姓名:" << temp->name << endl;
		cout << "部门:" << temp->depname << endl;
		cout << "职称:" << temp->title << endl;
		cout << "工资:" << temp->salary << endl;
		cout << "-------------------------" << endl;
		temp = temp->next;
	}
}

// 按部门名称分类显示该部门所有职工信息
void displayEmployeesByDepartment() {
	if (head == nullptr) {
		cout << "暂无职工信息!" << endl;
		return;
	}

	string department;
	cout << "请输入部门名称:";
	cin >> department;

	Employee* temp = head;
	bool found = false;
	cout << "部门" << department << "下的职工信息:" << endl;
	while (temp != nullptr) {
		if (temp->depname == department) {
			cout << "编号:" << temp->no << endl;
			cout << "姓名:" << temp->name << endl;
			cout << "职称:" << temp->title << endl;
			cout << "工资:" << temp->salary << endl;
			cout << "-------------------------" << endl;
			found = true;
		}
		temp = temp->next;
	}

	if (!found) {
		cout << "未找到部门" << department << "下的职工信息!" << endl;
	}
}

// 按部门显示各部门职工工资总额
void displayDepartmentTotalSalary() {
	if (head == nullptr) {
		cout << "暂无职工信息!" << endl;
		return;
	}

	// 使用一个数组来存储每个部门的工资总额
	const int MAX_DEPARTMENTS = 100;
	double departmentTotalSalary[MAX_DEPARTMENTS] = { 0.0 };

	Employee* temp = head;
	while (temp != nullptr) {
		departmentTotalSalary[temp->depname.length()] += temp->salary;
		temp = temp->next;
	}

	cout << "各部门职工工资总额:" << endl;
	for (int i = 0; i < MAX_DEPARTMENTS; i++) {
		if (departmentTotalSalary[i] > 0) {
			cout << "部门:" << i << " 工资总额:" << departmentTotalSalary[i] << endl;
		}
	}
}

// 删除职工信息
void deleteEmployee() {
	if (head == nullptr) {
		cout << "暂无职工信息!" << endl;
		return;
	}

	int employeeNo;
	cout << "请输入要删除的职工编号:";
	cin >> employeeNo;

	Employee* current = head;
	Employee* previous = nullptr;

	while (current != nullptr) {
		if (current->no == employeeNo) {
			if (previous == nullptr) {
				// 删除头节点
				head = current->next;
			}
			else {
				previous->next = current->next;
			}

			delete current;
			cout << "职工信息删除成功!" << endl;
			return;
		}

		previous = current;
		current = current->next;
	}

	cout << "未找到职工编号为" << employeeNo << "的记录!" << endl;
}

// 按职称调整工资
void adjustSalaryByTitle() {
	if (head == nullptr) {
		cout << "暂无职工信息!" << endl;
		return;
	}

	string jobTitle;
	cout << "请输入职称:";
	cin >> jobTitle;

	double salaryAdjustment;
	cout << "请输入工资调整金额:";
	cin >> salaryAdjustment;

	Employee* temp = head;
	bool found = false;
	while (temp != nullptr) {
		if (temp->title == jobTitle) {
			temp->salary += salaryAdjustment;
			found = true;
		}
		temp = temp->next;
	}

	if (found) {
		cout << "职称为" << jobTitle << "的职工工资调整成功!" << endl;
	}
	else {
		cout << "未找到职称为" << jobTitle << "的职工!" << endl;
	}
}

// 菜单
void menu() {
	int choice;
	do {
		cout << "-------------------------" << endl;
		cout << "1. 增加职工信息" << endl;
		cout << "2. 显示所有职工信息" << endl;
		cout << "3. 按部门分类显示职工信息" << endl;
		cout << "4. 按部门显示各部门职工工资总额" << endl;
		cout << "5. 删除职工信息" << endl;
		cout << "6. 按职称调整工资" << endl;
		cout << "0. 退出" << endl;
		cout << "请输入您的选择:";
		cin >> choice;

		switch (choice) {
		case 1:
			addEmployee();
			break;
		case 2:
			displayAllEmployees();
			break;
		case 3:
			displayEmployeesByDepartment();
			break;
		case 4:
			displayDepartmentTotalSalary();
			break;
		case 5:
			deleteEmployee();
			break;
		case 6:
			adjustSalaryByTitle();
			break;
		case 0:
			cout << "感谢使用人事信息管理系统!" << endl;
			break;
		default:
			cout << "无效的选择!" << endl;
			break;
		}
	} while (choice != 0);
}

int main() {
	menu();
	return 0;
}


  • 12
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
单向链表是一种常见的数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。单向链表的实现可以使用C++语言来完成。以下是一个简单的单向链表的代码示例: ```c++ #include <iostream> using namespace std; // 定义链表节点结构体 struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // 定义链表类 class LinkedList { public: LinkedList() { head = NULL; } // 在链表头部插入节点 void insertAtHead(int val) { ListNode* newNode = new ListNode(val); newNode->next = head; head = newNode; } // 在链表尾部插入节点 void insertAtTail(int val) { ListNode* newNode = new ListNode(val); if (head == NULL) { head = newNode; } else { ListNode* cur = head; while (cur->next != NULL) { cur = cur->next; } cur->next = newNode; } } // 删除链表中第一个值为val的节点 void deleteNode(int val) { if (head == NULL) { return; } if (head->val == val) { ListNode* temp = head; head = head->next; delete temp; return; } ListNode* cur = head; while (cur->next != NULL && cur->next->val != val) { cur = cur->next; } if (cur->next != NULL) { ListNode* temp = cur->next; cur->next = cur->next->next; delete temp; } } // 遍历链表并输出每个节点的值 void printList() { ListNode* cur = head; while (cur != NULL) { cout << cur->val << " "; cur = cur->next; } cout << endl; } private: ListNode* head; }; // 测试代码 int main() { LinkedList list; list.insertAtHead(1); list.insertAtHead(2); list.insertAtTail(3); list.insertAtTail(4); list.printList(); list.deleteNode(2); list.printList(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值