#include <iostream>
#include <iomanip>
#include <stdexcept>
using namespace std;
const int MAX_PROCESSES = 5;
struct CarNode {
int carID;
int currentProcess;
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;
}