c++ 管理系统/包含增删改查排序保存加载文件功能(结构体和链表两种方式)

员工管理系统。它提供了以下功能:

添加员工:用户可以输入员工的姓名、年龄和职位,系统将把员工添加到员工列表中。

删除员工:用户可以输入要删除的员工的姓名,系统将根据姓名查找并从员工列表中删除该员工的记录。

更新员工信息:用户可以输入要更新的员工的姓名,然后输入新的年龄和职位,系统将根据姓名找到该员工并更新其年龄和职位。

显示员工信息:系统将列出所有员工的姓名、年龄和职位。

搜索员工:用户可以输入要搜索的员工的姓名,系统将根据姓名查找并显示该员工的详细信息。

排序员工:系统将按照职位对员工列表进行排序,并显示排序后的员工信息。

保存到文件:系统将把当前的员工列表保存到一个文本文件中。

从文件加载数据:系统将从之前保存的文件中加载员工数据,并更新当前的员工列表。

退出:用户选择退出程序,程序将结束运行。

每个功能选项都有相应的处理函数来执行相应的操作。用户选择一个功能后,系统将调用相应的函数来处理用户的请求,并在操作完成后显示菜单供用户继续选择其他功能。

这个员工管理系统可以帮助管理员轻松管理员工信息,包括添加、删除、更新、搜索和排序员工。此外,它还提供了保存和加载数据的功能,以便在不同的运行会话之间保留员工信息。

使用结构体的方式存储;

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>

struct Employee {
    std::string name;
    int age;
    std::string position;
};

void addEmployee(std::vector<Employee>& employees) {
    std::string name, position;
    int age;

    std::cout << "Enter name: ";
    std::cin >> name;
    std::cout << "Enter age: ";
    std::cin >> age;
    std::cout << "Enter position: ";
    std::cin >> position;

    employees.push_back({ name, age, position });
    std::cout << "Employee added successfully." << std::endl;
}

void deleteEmployee(std::vector<Employee>& employees) {
    std::string name;
    std::cout << "Enter name of employee to delete: ";
    std::cin >> name;

    auto it = std::find_if(employees.begin(), employees.end(), [name](const Employee& emp) {
        return emp.name == name;
        });

    if (it != employees.end()) {
        employees.erase(it);
        std::cout << "Employee deleted successfully." << std::endl;
    }
    else {
        std::cout << "Employee not found." << std::endl;
    }
}

void updateEmployee(std::vector<Employee>& employees) {
    std::string name;
    std::cout << "Enter name of employee to update: ";
    std::cin >> name;

    auto it = std::find_if(employees.begin(), employees.end(), [name](const Employee& emp) {
        return emp.name == name;
        });

    if (it != employees.end()) {
        std::string position;
        int age;

        std::cout << "Enter new age: ";
        std::cin >> age;
        std::cout << "Enter new position: ";
        std::cin >> position;

        it->age = age;
        it->position = position;
        std::cout << "Employee updated successfully." << std::endl;
    }
    else {
        std::cout << "Employee not found." << std::endl;
    }
}

void displayEmployees(const std::vector<Employee>& employees) {
    if (employees.empty()) {
        std::cout << "No employees to display." << std::endl;
    }
    else {
        std::cout << "Employees:" << std::endl;
        for (const auto& employee : employees) {
            std::cout << "Name: " << employee.name << ", Age: " << employee.age << ", Position: " << employee.position << std::endl;
        }
    }
}

void searchEmployee(const std::vector<Employee>& employees) {
    std::string name;
    std::cout << "Enter name of employee to search: ";
    std::cin >> name;

    auto it = std::find_if(employees.begin(), employees.end(), [name](const Employee& emp) {
        return emp.name == name;
        });

    if (it != employees.end()) {
        std::cout << "Employee found:" << std::endl;
        std::cout << "Name: " << it->name << ", Age: " << it->age << ", Position: " << it->position << std::endl;
    }
    else {
        std::cout << "Employee not found." << std::endl;
    }
}

bool compareByPosition(const Employee& emp1, const Employee& emp2) {
    return emp1.position < emp2.position;
}

void sortEmployees(std::vector<Employee>& employees) {
    std::sort(employees.begin(), employees.end(), compareByPosition);
    std::cout << "Employees sorted by position." << std::endl;
}

void saveToFile(const std::vector<Employee>& employees, const std::string& filename) {
    std::ofstream file(filename);
    if (file.is_open()) {
        for (const auto& employee : employees) {
            file << employee.name << "," << employee.age << "," << employee.position << std::endl;
        }
        file.close();
        std::cout << "Data saved to file successfully." << std::endl;
    }
    else {
        std::cout << "Unable to open file." << std::endl;
    }
}

void loadFromFile(std::vector<Employee>& employees, const std::string& filename) {
    std::ifstream file(filename);
    if (file.is_open()) {
        employees.clear();
        std::string line;
        while (std::getline(file, line)) {
            std::istringstream iss(line);
            std::string name, position;
            int age;
            if (std::getline(iss, name, ',') && iss >> age && std::getline(iss >> std::ws, position)) {
                employees.push_back({ name, age, position });
            }
        }
        file.close();
        std::cout << "Data loaded from file successfully." << std::endl;
    }
    else {
        std::cout << "Unable to open file." << std::endl;
    }
}

int main() {
    std::vector<Employee> employees;
    std::string filename = "employees.txt";

    while (true) {
        std::cout << "Menu:" << std::endl;
        std::cout << "1. Add Employee" << std::endl;
        std::cout << "2. Delete Employee" << std::endl;
        std::cout << "3. Update Employee" << std::endl;
        std::cout << "4. Display Employees" << std::endl;
        std::cout << "5. Search Employee" << std::endl;
        std::cout << "6. Sort Employees" << std::endl;
        std::cout << "7. Save to File" << std::endl;
        std::cout << "8. Load from File" << std::endl;
        std::cout << "9. Exit" << std::endl;
        std::cout << "Enter your choice: ";

        int choice;
        std::cin >> choice;

        switch (choice) {
        case 1:
            addEmployee(employees);
            break;
        case 2:
            deleteEmployee(employees);
            break;
        case 3:
            updateEmployee(employees);
            break;
        case 4:
            displayEmployees(employees);
            break;
        case 5:
            searchEmployee(employees);
            break;
        case 6:
            sortEmployees(employees);
            break;
        case 7:
            saveToFile(employees, filename);
            break;
        case 8:
            loadFromFile(employees, filename);
            break;
        case 9:
            std::cout << "Exiting program." << std::endl;
            return 0;
        default:
            std::cout << "Invalid choice. Please try again." << std::endl;
            break;
        }
        std::cout << std::endl;
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值