1. Experimental topic
There is only one narrow passage for n cars in the parking lot, and only one gate for cars to enter and exit. The cars in the parking lot are arranged from north to south according to the sequence of vehicle arrival time (the gate is at the southernmost end, and the first car arriving first is parked at the northernmost end of the parking lot). If the parking lot is full of N cars, the later cars can only be parked outside the door Wait on the road. Once a car leaves, the first car on the sidewalk can drive in; When a vehicle in the parking lot wants to leave, the vehicle entering after it must first exit the parking lot to make way for it. When the vehicle leaves the gate, other vehicles will enter the parking lot in the original order. Each vehicle parked in the parking lot must pay the fee according to the length of its stay when it leaves the parking lot. Try to prepare a simulation program for the parking lot to manage according to the above requirements.
2. Demand analysis
This program is written in C language in VS2010
1. The parking lot adopts trestle structure, and the access road outside the parking lot adopts queue structure
2 when the vehicle wants to enter the parking lot, check whether the parking lot is full. If it is not full, enter the stack. If it is full, enter the sidewalk, i.e. queue.
3 when the vehicle requests to leave the stack, the vehicles at the top of the stack will be ejected first. After the vehicle leaves the stack, check whether there are vehicles in the waiting queue (sidewalk). If there are vehicles, take out a vehicle from the head of the queue and press it into the stack.
4 an auxiliary stack can be added to save information when the stack is out
3. Outline design
The parking lot is simulated by stack and the access road outside the parking lot is simulated by queue. The parking on the access road is not included in the parking time. The first car leaves, the last car stops on the sidewalk, and then enters the station.
4. Detailed design
#include<bits/stdc++.h> #include<iostream> using namespace std; int n; typedef int Status; typedef struct Car1 { int num; int time1; } CarNode; typedef struct { CarNode* base; CarNode* top; int stacksize; }Park; typedef struct Car2 { int num; int time1; struct Car2* next; } *CarPtr; typedef struct { CarPtr front; CarPtr rear; int length; }Shortcut; Status InitStack(Park& P) { P.base = (CarNode*)malloc(n * sizeof(Car1)); if (!P.base) exit(0); P.top = P.base; P.stacksize = 0; return true; } Status Push(Park& P, CarNode e) { *P.top++ = e; ++P.stacksize; return true; } Status Pop(Park& P, CarNode& e) { if (P.top == P.base) cout << "Parking lot is empty" << endl; else { e = *--P.top; --P.stacksize; } return 1; } Status InitQuene(Shortcut& S) { S.front = S.rear = (CarPtr)malloc(sizeof(Car2)); if (!S.front || !S.rear) exit(0); S.front->next = NULL; S.length = 0; return 1; } Status EnQuene(Shortcut& S, int number, int time) { CarPtr p; p = (CarPtr)malloc(sizeof(Car2)); if (!p) exit(0); p->num = number; p->time1 = time; p->next = NULL; S.rear->next = p; S.rear = p; ++S.length; return 1; } Status Arrival(Park& P, Shortcut& S) { int number, time; cout << "Please enter the car number and parking time:" << endl; cin >> number >> time; if (P.stacksize < n) { CarNode c; c.num = number; c.time1 = time; Push(P, c); cout << "The car should be parked at No" << P.stacksize << "Lane No" << endl; } else { EnQuene(S, number, time); cout << "The parking lot is full, please stop at the No" << S.length << "Location" << endl; } return 1; } Status Leave(Park& P, Park& P1, Shortcut& S) {//Handling of departing vehicles int number, le_time, flag = 1, money, ar_time; cout << "Please enter the license plate number and exit time:" << endl; cin >> number >> le_time; CarNode e, m; CarPtr w; while (P.stacksize) { Pop(P, e); if (e.num == number) { flag = 0; money = (le_time - e.time1) * 2; ar_time = e.time1; break; } Push(P1, e); } while (P1.stacksize) { Pop(P1, e); Push(P, e); } // The car came out of the parking lot if (flag == 0) { if (S.length != 0) { //DeQueue(S,w); m.time1 = le_time; m.num = w->num; Push(P, m); free(w); cout << "The license plate number is" << m.num << "Your car has entered the parking lot by the sidewalk" << endl; } cout << "The parking fee is" << money << endl << "The number of occupied parking spaces is" << P.stacksize << endl; } else cout << "The parking lot does not exist, and the brand is" << number << "My car" << endl; return 1; } int main() { int m = 1; cout << "Please enter the parking capacity:" << endl; cin >> n; getchar(); char flag;//option Park P, Q; Shortcut S; InitStack(P); InitStack(Q); InitQuene(S); while (m) { cout << "************Parking lot management procedure ***********" << endl; cout << "Please select an action: A.parking D.leave E.Exit the system"; scanf("%c", &flag); switch (flag) { case 'A': case 'a': Arrival(P, S); break; //The car enters the parking lot case 'D': case 'd': Leave(P, Q, S); break; //The car leaves the parking lot case 'E': case 'e': m = 0; break; default: cout << "Please re-enter:"; break; } getchar(); } return 0; }
5. Commissioning analysis
1 stack is characterized by first in and last out. If the advanced vehicles want to go, another stack is needed to store the later vehicles.
2 time calculation, the time staying on the sidewalk should be subtracted
3 there are still some mistakes in the program, a small number of errors in the code, and the problem of parking time is not handled perfectly.
6. Instructions for use
1. Select parking, vehicle number and parking time.
2. Drive out of the garage. Select the car number to leave the garage.
3. Check the vehicle condition in the garage.
4. Exit the parking management procedure.