设备信息管理系统(链表结构)

该代码实现了一个设备信息管理系统,包括设备的增删改查功能,以及按设备种类和所属部门进行分类。系统使用链表存储设备信息,通过哈希表进行快速查找和分类。此外,还支持设备信息的文件存取操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述


#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
using namespace std;
// 设备信息结构体
struct Device {
    int deviceNumber;//设备号
    string deviceName;//设备名称
    string recipient;//领用人
    string department;//所属部门
    int quantity;//数量
    string purchaseDate;//购买时间
    double price;//价格
    Device* next;
};
// 设备管理类
class DeviceManager {
private:
    Device* head;
public:
    // 构造函数
    DeviceManager() {
        head = nullptr;
    }
    // 析构函数
    ~DeviceManager() {
        clear();
    }
    // 添加设备信息
    void addDevice(int deviceNumber, const string& deviceName, const string& recipient, const string& department,
        int quantity, const string& purchaseDate, double price) {
        Device* newDevice = new Device;
        newDevice->deviceNumber = deviceNumber;
        newDevice->deviceName = deviceName;
        newDevice->recipient = recipient;
        newDevice->department = department;
        newDevice->quantity = quantity;
        newDevice->purchaseDate = purchaseDate;
        newDevice->price = price;
        newDevice->next = nullptr;
        if (head == nullptr) {
            head = newDevice;
        }
        else {
            Device* current = head;
            while (current->next != nullptr) {
                current = current->next;
            }
            current->next = newDevice;
        }
    }
    // 显示设备信息
    void displayDevices() {
        if (head == nullptr) {
            cout << "设备信息为空" << endl;
            return;
        }
        Device* current = head;
        while (current != nullptr) {
            cout << "设备号: " << current->deviceNumber << endl;
            cout << "设备名称: " << current->deviceName << endl;
            cout << "领用人: " << current->recipient << endl;
            cout << "所属部门: " << current->department << endl;
            cout << "数量: " << current->quantity << endl;
            cout << "购买时间: " << current->purchaseDate << endl;
            cout << "价格: " << current->price << endl;
            cout << "-------------------------------------" << endl;
            current = current->next;
        }
    }
    // 修改设备信息
    void modifyDevice(int deviceNumber, const string& deviceName, const string& recipient, const string& department,
        int quantity, const string& purchaseDate, double price) {
        if (head == nullptr) {
            cout << "设备信息为空" << endl;
            return;
        }
        Device* current = head;
        while (current != nullptr) {
            if (current->deviceNumber == deviceNumber) {
                current->deviceName = deviceName;
                current->recipient = recipient;
                current->department = department;
                current->quantity = quantity;
                current->purchaseDate = purchaseDate;
                current->price = price;
                cout << "设备信息修改成功" << endl;
                return;
            }
            current = current->next;
        }
        cout << "找不到指定设备号的设备" << endl;
    }
    // 删除设备信息
    void deleteDevice(int deviceNumber) {
        if (head == nullptr) {
            cout << "设备信息为空" << endl;
            return;
        }
        if (head->deviceNumber == deviceNumber) {
            Device* temp = head;
            head = head->next;
            delete temp;
            cout << "设备信息删除成功" << endl;
            return;
        }
        Device* current = head;
        while (current->next != nullptr) {
            if (current->next->deviceNumber == deviceNumber) {
                Device* temp = current->next;
                current->next = current->next->next;
                delete temp;
                cout << "设备信息删除成功" << endl;
                return;
            }
            current = current->next;
        }
        cout << "找不到指定设备号的设备" << endl;
    }
    // 计算设备总金额
    double calculateTotalValue() {
        double totalValue = 0.0;
        Device* current = head;
        while (current != nullptr) {
            totalValue += current->price * current->quantity;
            current = current->next;
        }
        return totalValue;
    }
    // 按设备种类进行分类
    void classifyByDeviceType() {
        // 实现设备按种类分类的代码
        cout << "按设备种类进行分类" << endl;
        unordered_map<string, Device*> deviceNameMap;  // 哈希表,用于存储每个部门对应的设备信息

    // 遍历链表,将设备按照所属部门分类存储到哈希表中
        Device* current = head;
        while (current != NULL) {
            string deviceName = current->deviceName;
            if (deviceNameMap.find(deviceName) == deviceNameMap.end()) {
                // 如果当前部门不在哈希表中,新建一个链表节点,并将当前设备添加到链表中
                Device* newNode = new Device();
                *newNode = *current;
                newNode->next = NULL;
                deviceNameMap[deviceName] = newNode;
            }
            else {
                // 如果当前部门已经在哈希表中,将当前设备添加到该部门对应的链表末尾
                Device* lastNode = deviceNameMap[deviceName];
                while (lastNode->next != NULL) {
                    lastNode = lastNode->next;
                }
                Device* newNode = new Device();
                *newNode = *current;
                newNode->next = NULL;
                lastNode->next = newNode;
            }

            current = current->next;
        }

        // 遍历哈希表,打印输出分类后的设备信息
        for (auto it = deviceNameMap.begin(); it != deviceNameMap.end(); ++it) {
            string deviceName = it->first;
            Device* deviceList = it->second;

            cout << "部门:" << deviceName << endl;
            Device* currentDevice = deviceList;
            while (currentDevice != NULL) {
                cout << "设备号:" << currentDevice->deviceNumber << endl;
                cout << "设备名称:" << currentDevice->deviceName << endl;
                cout << "领用人:" << currentDevice->recipient << endl;
                cout << "数量:" << currentDevice->quantity << endl;
                cout << "购买时间:" << currentDevice->purchaseDate << endl;
                cout << "价格:" << currentDevice->price << endl;
                cout << endl;

                currentDevice = currentDevice->next;
            }

            // 释放每个部门链表的内存
            while (deviceList != NULL) {
                Device* nextDevice = deviceList->next;
                delete deviceList;
                deviceList = nextDevice;
            }
        }
    }
    // 按所属部门进行分类
    void classifyByDepartment() {
        // 实现设备按所属部门分类的代码
        cout << "按所属部门进行分类" << endl;
        unordered_map<string, Device*> departmentMap;  // 哈希表,用于存储每个部门对应的设备信息

    // 遍历链表,将设备按照所属部门分类存储到哈希表中
        Device* current = head;
        while (current != NULL) {
            string department = current->department;
            if (departmentMap.find(department) == departmentMap.end()) {
                // 如果当前部门不在哈希表中,新建一个链表节点,并将当前设备添加到链表中
                Device* newNode = new Device();
                *newNode = *current;
                newNode->next = NULL;
                departmentMap[department] = newNode;
            }
            else {
                // 如果当前部门已经在哈希表中,将当前设备添加到该部门对应的链表末尾
                Device* lastNode = departmentMap[department];
                while (lastNode->next != NULL) {
                    lastNode = lastNode->next;
                }
                Device* newNode = new Device();
                *newNode = *current;
                newNode->next = NULL;
                lastNode->next = newNode;
            }

            current = current->next;
        }

        // 遍历哈希表,打印输出分类后的设备信息
        for (auto it = departmentMap.begin(); it != departmentMap.end(); ++it) {
            string department = it->first;
            Device* deviceList = it->second;

            cout << "部门:" << department << endl;
            Device* currentDevice = deviceList;
            while (currentDevice != NULL) {
                cout << "设备号:" << currentDevice->deviceNumber << endl;
                cout << "设备名称:" << currentDevice->deviceName << endl;
                cout << "领用人:" << currentDevice->recipient << endl;
                cout << "数量:" << currentDevice->quantity << endl;
                cout << "购买时间:" << currentDevice->purchaseDate << endl;
                cout << "价格:" << currentDevice->price << endl;
                cout << endl;

                currentDevice = currentDevice->next;
            }

            // 释放每个部门链表的内存
            while (deviceList != NULL) {
                Device* nextDevice = deviceList->next;
                delete deviceList;
                deviceList = nextDevice;
            }
        }
    }
    // 保存设备信息到文件
    void saveToFile(const string& filename) {
        ofstream file(filename);
        if (!file) {
            cout << "无法打开文件" << endl;
            return;
        }
        Device* current = head;
        while (current != nullptr) {
            file << current->deviceNumber << "," << current->deviceName << "," << current->recipient << ","
                << current->department << "," << current->quantity << "," << current->purchaseDate << ","
                << current->price << endl;
            current = current->next;
        }
        file.close();
        cout << "设备信息已保存到文件" << endl;
    }
    // 从文件中加载设备信息
    void loadFromFile(const string& filename) {
        clear();
        ifstream file(filename);
        if (!file) {
            cout << "无法打开文件" << endl;
            return;
        }
        int deviceNumber, quantity;
        string deviceName, recipient, department, purchaseDate;
        double price;
        char comma;
        while (file >> deviceNumber >> comma && getline(file, deviceName, ',') && getline(file, recipient, ',')
            && getline(file, department, ',') && file >> quantity >> comma && getline(file, purchaseDate, ',')
            && file >> price) {
            addDevice(deviceNumber, deviceName, recipient, department, quantity, purchaseDate, price);
        }
        file.close();
        cout << "设备信息已从文件加载" << endl;
    }
    // 清空设备信息
    void clear() {
        Device* current = head;
        while (current != nullptr) {
            Device* temp = current;
            current = current->next;
            delete temp;
        }
        head = nullptr;
    }
};
// 主菜单界面
void showMainMenu() {
    cout << "设备信息管理系统" << endl;
    cout << "1.添加设备信息" << endl;
    cout << "2.显示设备信息" << endl;
    cout << "3.修改设备信息" << endl;
    cout << "4.删除设备信息" << endl;
    cout << "5.计算设备总金额" << endl;
    cout << "6.按照设备种类进行分类" << endl;
    cout << "7.按照设备所属部门进行分类" << endl;
    cout << "8.保存设备信息到文件" << endl;
    cout << "9.从文件加载设备信息" << endl;
    cout << "0.退出系统" << endl;
    cout << "请选择操作: ";
}
int main() {
    DeviceManager manager;
    int choice;
    do {
        showMainMenu();
        cin >> choice;
        switch (choice) {
        case 1: {
            int deviceNumber, quantity;
            string deviceName, recipient, department, purchaseDate;
            double price;
            cout << "请输入设备号: ";
            cin >> deviceNumber;
            cout << "请输入设备名称: ";
            cin.ignore();
            getline(cin, deviceName);
            cout << "请输入领用人: ";
            getline(cin, recipient);
            cout << "请输入所属部门: ";
            getline(cin, department);
            cout << "请输入数量: ";
            cin >> quantity;
            cout << "请输入购买时间: ";
            cin.ignore();
            getline(cin, purchaseDate);
            cout << "请输入价格: ";
            cin >> price;
            manager.addDevice(deviceNumber, deviceName, recipient, department, quantity, purchaseDate, price);
            cout << "设备信息添加成功" << endl;
            break;
        }
        case 2:
            manager.displayDevices();
            break;
        case 3: {
            int deviceNumber, quantity;
            string deviceName, recipient, department, purchaseDate;
            double price;
            cout << "请输入要修改的设备号: ";
            cin >> deviceNumber;
            cout << "请输入设备名称: ";
            cin.ignore();
            getline(cin, deviceName);
            cout << "请输入领用人: ";
            getline(cin, recipient);
            cout << "请输入所属部门: ";
            getline(cin, department);
            cout << "请输入数量: ";
            cin >> quantity;
            cout << "请输入购买时间: ";
            cin.ignore();
            getline(cin, purchaseDate);
            cout << "请输入价格: ";
            cin >> price;
            manager.modifyDevice(deviceNumber, deviceName, recipient, department, quantity, purchaseDate, price);
            break;
        }
        case 4: {
            int deviceNumber;
            cout << "请输入要删除的设备号: ";
            cin >> deviceNumber;
            manager.deleteDevice(deviceNumber);
            break;
        }
        case 5: {
            double totalValue = manager.calculateTotalValue();
            cout << "设备总金额: " << totalValue << endl;
            break;
        }
        case 6:
            manager.classifyByDeviceType();
            break;
        case 7:
            manager.classifyByDepartment();
            break;
        case 8: {
            string filename;
            cout << "请输入要保存的文件名: ";
            cin >> filename;
            manager.saveToFile(filename);
            break;
        }
        case 9: {
            string filename;
            cout << "请输入要加载的文件名: ";
            cin >> filename;
            manager.loadFromFile(filename);
            break;
        }
        case 0:
            cout << "感谢使用设备信息管理系统,再见!" << endl;
            break;
        default:
            cout << "无效的选择" << endl;
            break;
        }
        cout << endl;
    } while (choice != 0);
    return 0;
}

数据表结构说明 4 1、基础字典 4 1.1设备分类字典sb_zd_class 4 1.2折旧方法字典sb_zd_depreciation(暂时没用到) 4 1.3折旧率字典sb_zd_depreciation_rate 4 1.4折旧类型字典sb_zd_depreciation_type 5 1.5设备名称字典sb_zd_equipname 5 1.6设备入出库类型字典sb_zd_in_out_type 5 1.7 设备库帐号字典sb_zd_kzh 6 1.8设备维修单位字典sb_zd_maintenance_unit 6 1.9设备制造厂商字典sb_zd_manufacture 6 1.10设备计量单位类型字典 sb_zd_measure_type 7 1.11设备计量单位字典 sb_zd_measurer 7 1.12设备调配原因字典sb_zd_move_cause 8 1.13设备状态字典sb_zd_state 8 1.14设备供应商字典 sb_zd_supplyer 8 1.15设备单位字典sb_zd_unit 9 1.16设备用途字典sb_zd_usage 9 1.17设备维修类型sb_zd_maintenance_kind 9 1.18设备内部帐号字典sb_zd_inner_acct_no 9 2、业务数据表 10 2.1设备现有附件表sb_appendix 10 2.2设备附件使用表sb_appendix_use 11 2.3设备成本效益信息表sb_cost_benefit 11 2.4设备折旧变更记录表sb_depreciation_alter_record 12 2.5设备折旧记录表sb_depreciation_record 12 2.6设备进口说明表sb_import_comment 12 2.7设备贷款记录表sb_in_credit(暂时没用到) 13 2.8设备购进明细表sb_in_detl 13 2.9主设备表sb_main_equipment 14 2.10设备维修计划单sb_maintenance_plan(暂时没用到) 15 2.11设备维修记录sb_maintenance_record 15 2.12设备计量记录sb_measure_record 16 2.13设备调配明细sb_move_detl 16 2.14设备付款明细sb_pay_detl 17 2.15设备服务计划sb_service_plan(暂时没用到) 17 2.16设备服务记录sb_service_record(暂时没用到) 18 2.17设备增值表sb_value_increment 18 2.18设备销减表sb_waste 19 2.19设备月结信息sb_report 19 2.20设备配置表sb_config 20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Minuw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值