堆栈链表复习--停车场
#include<iostream>#include<string>#include<iomanip>#include <limits> // 引入 numeric_limits 用于清除缓冲区 #include <vector> // 用于队列中存储多个元素 using namespace std; // 定义存储时间的结构体struct dateTime{ int minute;}; struct Car{ string chepai; dateTime stayTime;}; // 定义停车场栈struct Node{ Car* car; Node* next;}; class carParks{ private: Node* top; int num; int Max; public: carParks(int max){ this->top = NULL; this->num = 0; this->Max = max; } // 入栈 void push(Car* car){ Node* newNode = new Node(); if(newNode == NULL) { cout<<"内存开辟失败"<<endl; return; } newNode->car = car; newNode->next = this->top; this->top = newNode; this->num++; } // 判断栈空 bool isEmpty(){ return this->top == NULL; } // 出栈 Car* pop(){ if(this->isEmpty()){ cout<<"栈为空,请检查程序"<<endl; return NULL; } Node* popNode = this->top; this->top = this->top->next; Car* popCar = popNode->car; delete popNode; this->num--; return popCar; } // 查看栈顶元素 Car* peek(){ if(this->isEmpty()) return NULL; return this->top->car; } // 判断栈满 bool isFull(){ return this->num == this->Max; } // 查看最大数量 int maxNum(){ return this->Max; } // 查看当前车辆数 int Num(){ return this->num; } }; // 定义人行道队列class Queue{private: int front; int rear; int maxNum; vector<Car*> cars; // 用 vector 来动态存储 Car 对象 int size;public: Queue(int maxNum){ this->maxNum = maxNum; this->front = 0; this->rear = -1; this->size = 0; cars.resize(maxNum); // 初始化容量 } // 判断队满 bool isFull(){ return this->size == this->maxNum; } // 判断队空 bool isEmpty(){ return size == 0; } // 入队 void enqueue(Car* car){ if(this->isFull()){ return; } // 环形队列 this->rear = (this->rear + 1) % this->maxNum; this->cars[rear] = car; this->size ++; } Car* dequeue(){ if(isEmpty()){ return NULL; } Car *decar = this->cars[this->front]; this->front = (this->front + 1) % this->maxNum; this->size--; return decar; } // 查看队头元素 Car* peek() { if (this->isEmpty()) { cout << "队列为空" << endl; return NULL; } return this->cars[front]; } // 查看大小 int Num(){ return this->size; } }; // 入车void pushCar(carParks &park, Queue &renxingdao){ cout<<">>>>>>有一辆车开到了<<<<<<"<<endl; Car* newCar = new Car(); // 输入属性 cout<<"请输入车牌>>"; cin>>newCar->chepai; if(park.isFull()){ cout<<"停车场车辆已满,放在了人行道"<<endl; renxingdao.enqueue(newCar); cout<<"当前人行道等候数:"<<renxingdao.Num()<<endl; return; } // 车辆入栈 park.push(newCar); cout<<"\n车牌号为<"<<park.peek()->chepai<<">的车已进入停车场!"<<endl;} // 出车void popCar(carParks &park, int hourCost, Queue &renxingdao){ cout<<"请输入出库的车牌>>"; string name; cin>>name; bool find = false; int minute = 0; // 维护一个辅助栈 carParks cp(park.maxNum()); while(!park.isEmpty()){ if(park.peek()->chepai == name){ cout<<"请输入当前车的分钟数>>"; cin>>park.peek()->stayTime.minute; minute = park.peek()->stayTime.minute; park.pop(); find = true; break; } cp.push(park.pop()); } // 将辅助栈的车辆撤回 while(!cp.isEmpty()){ park.push(cp.pop()); } if(find == false){ cout<<"停车场未找到该车辆!"<<endl; return; } cout<<"车辆已出库! 共收到停车费 "<<hourCost * (minute % 60)<< "元"; // 将人行道的车移入车库 if(!renxingdao.isEmpty()){ cout << "开始将人行道上的车辆移入车库..." << endl; Car* tempCar = renxingdao.dequeue(); // 取出车 park.push(tempCar); cout<<"车牌号<"<<park.peek()->chepai<<">已移入车库"<<endl; } } // 查看车void lookPark(carParks &park){ // 维护一个辅助栈 carParks cp(park.maxNum()); cout<<"当前停车场车辆的车牌号分别为:"<<endl; while(!park.isEmpty()){ cout<<">>"<<park.peek()->chepai<<endl; cp.push(park.pop()); } // 将辅助栈的车辆撤回 while(!cp.isEmpty()){ park.push(cp.pop()); } cout<<"共有车辆 "<<park.Num()<<endl;} void lookRenxingdao(Queue &R) { cout << "当前人行道车辆的车牌号分别为:" << endl; // 创建一个辅助队列 Queue tempQueue(R.Num()); // 遍历原队列并查看车牌号 while (!R.isEmpty()) { cout << ">>" << R.peek()->chepai << endl; // 将队列中的元素放入辅助队列中 tempQueue.enqueue(R.dequeue()); } // 将所有元素重新放回原队列 while (!tempQueue.isEmpty()) { R.enqueue(tempQueue.dequeue()); } // 输出队列中的车辆数量 cout << "共有车辆 " << R.Num() << endl;} // 菜单void shouMenu() { int choice; // 在此维护两个栈,停车场和人行道 int a; cout<<"请输入你的停车场最大容车辆>>"; cin>>a; carParks park(a); Queue renxingdao(10000); // 直接给人行道一个很大的常量 cout<<"请输入停车场的费用>>"; int hourCost; cin>>hourCost; while (true) { // 打印菜单标题 cout << "==============================\n"; cout << " 欢迎使用停车管理系统 \n"; cout << "==============================\n"; // 显示菜单选项 cout << "\n请选择操作:\n"; cout << " [1] 入车\n"; cout << " [2] 出车\n"; cout << " [3] 查看停车场\n"; cout << " [4] 查看人行横道\n"; cout << " [5] 退出\n"; cout << "\n请输入您的选择 (1-5): "; cin >> choice; // 根据选择执行相应操作 switch (choice) { case 1: pushCar(park, renxingdao); break; case 2: system("cls"); popCar(park, hourCost, renxingdao); break; case 3: system("cls"); lookPark(park); break; case 4: system("cls"); lookRenxingdao(renxingdao); break; case 5: cout << "\n感谢使用停车管理系统!再见!\n"; return; // 退出菜单 default: cout << "\n无效的输入!请输入 1-5 之间的数字。\n"; // 清空缓冲区的所有字符,直到换行符为止 cin.clear(); cin.ignore(std::numeric_limits< streamsize >::max(), '\n'); } system("pause"); system("cls"); } } int main(){ shouMenu(); return 0;}