2025年4月18日 星期五 乙巳(蛇)年 正月十九 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > VC/VC++

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

时间:09-16来源:作者:点击数:36
CDSY,CDSY.XYZ

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

在这里插入图片描述
  • #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;
  • }
CDSY,CDSY.XYZ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐