Simulate a parking lot.
Problem description
A parking lot is a parking lot.NThe long and narrow passage of a car, and there is only one door for the car to enter and exit. In the parking lot, cars are arranged from north to south in sequence according to the vehicle arrival time (the gate is at the southernmost point, and the first car arrived at the southernmost point of the parking lot ), if the parking lot is fullNA car, then the car can only wait outside the door, once there is a car to drive away, the first car on the road to open; when a car in the parking lot to leave, the vehicle that enters after it must exit the vehicle yard to make way for it. When the vehicle leaves the gate, other vehicles will enter the vehicle yard in the original order, when a car parked in a parking lot leaves the parking lot, it must pay the fee based on the length of its stay. Prepare a simulation program for the management of the parking lot according to the above requirements.
Basic Requirements
Simulate the parking lot with stacks, simulate the sidewalk on the off-site with queues, and perform simulated management based on the input data sequence read from the terminal. Each group of input data includes three data items: Vehicle arrival or departure information, vehicle license plate number, and arrival or departure time. The output information of each group of input data is as follows: if the vehicle arrives, the parking position of the vehicle in the parking lot or on the sidewalk is output; if the vehicle leaves, then, the time for the car to stay in the parking lot and the payable fees are output (the time for the car to stay in the driveway is not charged ).
Test Cases (example)
SetN =2. The input data is: ('A',), ('A',), ('D',), ('A ), ('A',), ('A',), ('D',), ('E ). 'A' indicates that the vehicle has arrived, 'D' indicates that the vehicle has left, and 'E' indicates that the input has ended.
Stack and queue are required. According to the question, when a car in the parking lot is about to leave, all the cars entering the parking lot must exit the parking lot to make way for it, after the parking lot is opened, the functions of these vehicles entering the site in the original order can be designed with two stacks, one of which is used to simulate the parking lot, and the other is used to simulate the temporary parking lot. There is a car in the parking lot, and the give way car enters the simulated parking lot, and then the simulated parking lot enters the parking lot. When the capacity of the parking lot is exceeded, the bus stops at the entrance and uses the queue for simulation.
The stack and queue of this Code are both in a sequential structure.
1 # include <iostream> 2 using namespace std; 3 4 class node {// node class 5 public: 6 int num; 7 int time; 8}; 9 class AStack: public node {// Stack class 10 private: 11 int size; 12 int top; 13 node * listArray; 14 public: 15 AStack (int sz) {16 size = sz; top = 0; listArray = new node [sz]; 17} 18 ~ AStack () {delete [] listArray;}; 19 bool push (node a) {20 if (top = size) return false; 21 listArray [top ++] =; 22 return true; 23} 24 bool pop (node & a) {25 if (top = 0) return false; 26 else {27 a = listArray [-- top]; return true; 28} 29} 30 int length () {31 return top; 32} 33 bool topValue (node & a) {34 if (top = 0) return false; 35 a = listArray [top-1]; 36 return true; 37} 38}; 39 clas S AQueue: public node {// queue class 40 private: 41 int size; 42 int front; 43 int rear; 44 node * listArray; 45 public: 46 AQueue (int sz) {47 size = sz + 1; 48 rear = 0; front = 1; 49 listArray = new node [size]; 50} 51 ~ AQueue () {delete [] listArray;}; 52 bool enqueue (node it) {53 if (rear + 2) % size) = front) return false; 54 rear = (rear + 1) % size; 55 listArray [rear] = it; 56 return true; 57} 58 bool dequeue (node & it) {59 if (length () = 0) return false; 60 it = listArray [front]; 61 front = (front + 1) % size; 62 return true; 63} 64 int length () {65 return (rear + size)-front + 1) % size; 66} 67}; 68 void input (AStack & parking1, AQueue & wait, node s) {// enter the parking lot 69 if (parking1.push (s )) {70 cout <"in the parking lot" <parking1.length () <"bit" <endl; 71 return; 72} 73 else {74 cout <"in the remarks section" <wait. length () + 1 <"bit" <endl; 75 wait. enqueue (s); 76} 77} 78 int output (AStack & parking1, AStack & parking2, AQueue & wait, node s) {// node temp of 79 out-of-parking lots; int time; 80 while (parking1.topValue (temp) & (temp. nu M! = S. num) {81 parking1.pop (temp); 82 parking2.push (temp); 83} 84 if (parking1.topValue (temp )) {// determine whether the car exists in the parking lot. 85 parking1.pop (temp); 86 time = s. time-temp. time; 87} 88 else time =-1; 89 while (parking2.pop (temp) 90 parking1.push (temp); 91 if (time) {92 if (wait. length () {93 wait. dequeue (temp); 94 temp. time = s. time; 95 parking1.push (temp); 96} 97} 98 return time; 99} 100 int main () {10 1 cout <"Enter the parking lot capacity and parking fee per minute:" <endl; 102 int maxSize, money, parkTime; 103 cin> maxSize> money; 104 AStack parking1 (maxSize), parking2 (maxSize); 105 AQueue wait (100); 106 char a; node s; 107 while (1) {108 cout <"input command: "<endl; 109 cin> a> s. num> s. time; 110 while (! (A = 'A' | A = 'D' | a = 'E') & s. num> = 0 & s. time >=0) {111 cout <"input error, enter again:" <endl; 112 cin> a> s. num> s. time; 113}; 114 if (a = 'E') break; 115 else if (a = 'A') {116 input (parking1, wait, s ); 117 continue; 118} 119 else {120 parkTime = output (parking1, parking2, wait, s); 121 if (parkTime =-1) 122 cout <"the car is not in this parking lot. Please enter it again! "<Endl; 123 else 124 cout <" parking time: "<parkTime <ends <" the payable fee is: "<parkTime * money <endl; 125} 126} 127 system (" pause "); 128 return 0; 129}View Code