编辑代码

#include <iostream>
#include <iomanip>
#include <stdexcept>

using namespace std;

// 最大工序数定义
const int MAX_PROCESSES = 5;

// 车辆节点结构定义
struct CarNode {
    int carID;            // 车辆唯一标识
    int currentProcess;   // 当前工序(1-5)
    CarNode* next;        // 链表指针

    // 构造函数
    CarNode(int id) : carID(id), currentProcess(1), next(nullptr) {}
};

// 生产线管理类
class ProductionLine {
private:
    CarNode* head;        // 链表头指针
public:
    // 构造函数
    ProductionLine() : head(nullptr) {}

    // 析构函数(内存释放)
    ~ProductionLine() {
        CarNode* current = head;
        while (current) {
            CarNode* temp = current;
            current = current->next;
            delete temp;
        }
    }

    // 添加新车到生产线末尾
    void addCar(int id) {
        CarNode* newNode = new CarNode(id);
        if (!newNode) {
            throw runtime_error("内存分配失败!");
        }

        if (!head) {
            head = newNode;
        } else {
            CarNode* temp = head;
            while (temp->next) {
                temp = temp->next;
            }
            temp->next = newNode;
        }
        cout << "[+] 车辆 " << id << " 已加入生产线(工序" 
             << newNode->currentProcess << ")" << endl;
    }

    // 显示全部车辆状态
    void displayStatus() const {
        if (!head) {
            cout << "[-] 生产线暂无车辆!" << endl;
            return;
        }

        cout << "当前生产线状态:" << endl;
        cout << "---------------------" << endl;
        cout << left << setw(8) << "车辆ID"
             << setw(8) << "当前工序"
             << setw(8) << "状态" << endl;
        cout << "---------------------" << endl;

        CarNode* temp = head;
        int position = 1;
        while (temp) {
            string status = (temp->currentProcess > MAX_PROCESSES) ? "已完成" : "进行中";
            cout << left << setw(8) << temp->carID
                 << setw(8) << temp->currentProcess
                 << setw(8) << status << endl;
            temp = temp->next;
        }
        cout << "---------------------" << endl;
    }

    // 更新指定车辆工序
    void updateCar(int id) {
        CarNode* current = head;
        CarNode* prev = nullptr;

        // 查找目标车辆
        while (current && current->carID != id) {
            prev = current;
            current = current->next;
        }

        if (!current) {
            cout << "[-] 错误:未找到车辆 " << id << "!" << endl;
            return;
        }

        // 更新工序状态
        current->currentProcess++;
        cout << "[*] 车辆 " << id << " 已完成工序" 
             << current->currentProcess - 1 << " -> " 
             << current->currentProcess << endl;

        // 检查是否完成所有工序
        if (current->currentProcess > MAX_PROCESSES) {
            cout << "[!] 车辆 " << id << " 已完成全部工序,正在移除..." << endl;
            
            if (!prev) { // 头节点处理
                head = current->next;
            } else {
                prev->next = current->next;
            }
            
            delete current;  // 释放内存
        }
    }
};

// 主函数
int main() {
    ProductionLine line;
    bool running = true;

    // 主菜单循环
    while (running) {
        cout << "=== 汽车生产线管理系统 ===" << endl;
        cout << "1. 添加新车" << endl;
        cout << "2. 显示状态" << endl;
        cout << "3. 更新工序" << endl;
        cout << "4. 退出系统" << endl;
        cout << "请选择操作:";

        int choice;
        cin >> choice;

        try {
            switch (choice) {
                case 1: {
                    int id;
                    cout << "请输入车辆ID:";
                    cin >> id;
                    line.addCar(id);
                    break;
                }
                case 2:1
                    line.displayStatus();
                    break;
                case 3: {
                    int id;
                    cout << "请输入要更新的车辆ID:";
                    cin >> id;
                    line.updateCar(id);
                    break;
                }
                case 4:
                    running = false;
                    cout << "退出系统..." << endl;
                    break;
                default:
                    cout << "无效的选择!" << endl;
            }
        } catch (const exception& e) {
            cerr << "错误:" << e.what() << endl;
        }
    }

    return 0;
}