# Include <iostream>
# Include <fstream>
Using namespace std;
# Define MAX_SIZE 2 // number of vehicles that can be accommodated in the parking lot
# Define FARE 5 // indicates that the parking lot is charged at fare RMB per hour.
Int CountForStack = 0; // This global variable is used to count the number of existing vehicles in the stack.
Int CountForQueue = 0; // This global variable is used to count the number of existing vehicles in the queue.
Typedef struct // This node is used to save the information of each vehicle
{
Char Condition; // used to indicate the status of "arriving" or "leaving". "" indicates that it has not arrived yet and has not left.
Int ArriveTime; // used to record the arrival time. The default value is-1.
Int LeaveTime; // used to record the exit time. The default value is-1, indicating that the exit is not completed.
Int License; // record the License plate number
} CarNode;
Typedef struct // stack Definition
{
CarNode * base; // stack bottom pointer, pointing to 0
CarNode * top; // stack top pointer. If it points to 0, the stack is empty.
Int stacksize; // stack size
} CarStack;
Typedef struct QNode // queue node Definition
{
Char Condition; // used to indicate the status of "arriving" or "leaving". "" indicates that it has not arrived yet and has not left.
Int ArriveTime; // used to record the arrival time. The default value is-1.
Int LeaveTime; // used to record the exit time. The default value is-1, indicating that the exit is not completed.
Int License; // record the License plate number
QNode * next; // pointer to the next node
} QNode;
Typedef struct // queue Definition
{
QNode * front; // head pointer
QNode * rear; // team end pointer
} Queue;
Bool InitStack (CarStack & S) // This function is used to initialize the stack
{
S. base = (CarNode *) malloc (MAX_SIZE * sizeof (CarNode ));
If (! S. base)
{
Cout <"memory allocation failed! "<Endl;
Return false; // indicates that the memory allocation fails and false is returned.
}
S. top = S. base;
S. stacksize = MAX_SIZE;
Return true;
}
Bool InitQueue (Queue & Q) // This function is used to initialize the Queue
{
Q. front = (QNode *) malloc (sizeof (QNode ));
If (! Q. front)
{
Cout <"memory allocation failed! "<Endl;
Return false;
}
Q. rear = Q. front;
Q. front-> next = 0; // The next node is empty.
Return true;
}
Bool EnQueue (Queue & Q, QNode & qnode) // This function is used to join a node.
{
QNode * p = (QNode *) malloc (sizeof (QNode ));
If (! P)
{
Cout <"memory allocation failed! "<Endl;
Return false;
}
P-> ArriveTime = qnode. ArriveTime;
P-> Condition = qnode. Condition;
P-> LeaveTime = qnode. LeaveTime;
P-> License = qnode. License;
P-> next = 0;
Q. rear-> next = p;
Q. rear = p;
Return true;
}
Bool DeQueue (Queue & Q, QNode & t) // This function is used to team out
{
If (Q. front = Q. rear)
{
Cout <"the queue is empty! "<Endl;
Return false;
}
QNode * p = Q. front-> next;
T. ArriveTime = p-> ArriveTime;
T. Condition = p-> Condition;
T. LeaveTime = p-> LeaveTime;
T. License = p-> License;
Q. front-> next = p-> next;
If (Q. rear = p) // if P is the element that points to the last team
Q. rear = Q. front;
Free (p );
Return true;
}
Void InitCarNode (CarNode & C, char condition, int arrivetime, int leavetime, int license) // This function is used to initialize a CarNode node.
{
C. ArriveTime = arrivetime;
C. Condition = condition;
C. LeaveTime = leavetime;
C. License = license;
}
Bool Push (CarStack & S, CarNode & car) // This function is used to import a CarNode to the stack.
{
If (S. top-S. base> = S. stacksize)
{
Cout <"this stack is full and cannot be pushed into new information" <endl;
Return false;
}
(* S. top). ArriveTime = car. ArriveTime;
(* S. top). Condition = car. Condition;
(* S. top). LeaveTime = car. LeaveTime;
(* S. top). License = car. License;
++ S. top; // stack top pointer move up
Return true;
}
Bool Pop (CarStack & S, CarNode & t) // This function is used to Pop up the elements in the stack.
{
If (S. top = S. base)
{
Cout <"the stack is empty. The stack output operation cannot be performed! "<Endl;
Return false;
}
-- S. top; // move down the stack top pointer
T. ArriveTime = (* S. top). ArriveTime;
T. Condition = (* S. top). Condition;
T. LeaveTime = (* S. top). LeaveTime;
T. License = (* S. top). License;
Return true;
}
Bool IsStackFull (CarStack & S) // This function is used to determine whether the stack is full.
{
If (S. top-S. base> = S. stacksize)
Return true;
Else
Return false;
}
Bool IsStackEmputy (CarStack & S) // This function is used to determine whether the stack is empty.
{
If (S. top = S. base)
Return true;
Else
Return false;
}
Bool IsQueueEmputy (Queue & Q) // This function is used to determine whether the Queue is empty.
{
If (Q. front = Q. rear)
Return true;
Else
Return false;
}
Bool SearchInStack (CarStack & S, int a) // a indicates the license plate number to be searched. If it is in the parking lot, true is returned.
{
Bool tag = false;
If (! IsStackEmputy (S ))
{
CarNode * p = S. top-1;
While (p! = S. base)
{
If (* p). License =)
Tag = true;
-- P;
}
If (* P). License =)
Tag = true;
}
Return tag;
}
Bool searchinqueue (queue & Q, int A) // A indicates the license plate number to be searched. If it is in the channel, true is returned.
{
Bool tag = false;
If (! Isqueueemputy (q) // If the queue is not empty
{
Qnode * P = Q. Front-> next;
While (P! = Q. rear)
{
If (* P). License =)
Tag = true;
} // Exit this while loop when P points to the last element
If (* P). License =)
Tag = true;
}
Return tag;
}
Void InCar (CarStack & S, Queue & Q, int a1, int a2) // This function is used to indicate entering the vehicle. Parameter a1 is used to indicate the arrival time, and parameter a2 indicates the license plate number.
{
If (SearchInStack (S, a2 ))
{
Cout <"Car Number" <a2 <"already exists in the parking lot. incorrect input" <endl;
Return;
}
If (SearchInQueue (Q, a2 ))
{
Cout <"vehicle No." <a2 <"already exists in the channel. incorrect input" <endl;
Return;
}
If (IsStackFull (S) // if the stack is full, the parking lot is full and you need to stop at the Channel.
{
QNode qnode;
Qnode. ArriveTime =-1; // There is no charge for the channel time, so no timing
Qnode. Condition = 'a ';
Qnode. LeaveTime =-1; // It is defined as-1, which indicates that it has not started to leave.
Qnode. License = a2;
Enqueue (Q, qnode); // stop at the Channel
++ Countforqueue;
Cout <"car No.:" <qnode. License <"the number of stops at the Channel" <countforqueue <"no." <Endl;
}
Else
{
Carnode;
Carnode. arrivetime = A1;
Carnode. condition = 'a ';
Carnode. leavetime =-1;
Carnode. License = a2;
Push (S, carnode );
++ Countforstack;
Cout <"vehicle ID:" <carnode. license <"Arrival Time" <carnode. arrivetime <"parking in the parking lot" <countforstack <"location" <Endl;
}
}
Void OutCar (CarStack & S, Queue & Q, int a1, int a2) // this function is used for departure. Parameter a1 indicates departure time, and parameter a2 indicates the license plate number.
{
If (SearchInQueue (Q, a2 ))
{
Cout <"car No." <a2 <"exists in the channel and has not entered the parking lot. You cannot leave" <endl;
Return;
}
If (! SearchInStack (S, a2 ))
{
Cout <"Car Number" <a2 <"not in the parking lot, incorrect input" <endl;
Return;
}
CarStack tempstack;
InitStack (tempstack); // creates and initializes the stack used to temporarily store the vehicle when it leaves the vehicle.
Bool tag1 = false; // indicates whether the parking lot is full before departure. The default value is not full.
Tag1 = IsStackFull (S );
Bool tag2 = true; // indicates whether a car is waiting in the channel. The channel is empty by default.
Tag2 = isqueueemputy (Q );
Carnode temp; // used to save the car temporarily removed
Bool tag3 = false; // used to indicate whether the departure time is earlier than the arrival time, leading to the departure failure. True indicates that the departure failed.
While (1) // let the car go
{
Pop (S, temp );
If (temp. License = a2)
{
If (A1 <temp. arrivetime)
{
Cout <"incorrect input. The departure time cannot be earlier than the arrival time. Exit failed" <Endl;
Tag3 = true;
Push (tempstack, temp );
}
Else
Cout <"Car Number" <A2 <"now leaves the parking lot, the time spent is" <a1-temp.ArriveTime <"charged as" <(a1-temp.ArriveTime) * fare <Endl;
Break;
}
Else
Push (tempstack, temp); // enter the temporary Stack
}
While (! IsStackEmputy (tempstack) // The inverted car enters the parking lot again
{
Pop (tempstack, temp );
Push (S, temp );
}
QNode tempqnode; // used to temporarily Save the car from the Channel
If (tag1 = true & tag2 = false & tag3 = false) // if the parking lot is full before departure and the channel is not empty, the exit fails.
{
DeQueue (Q, tempqnode );
-- CountForQueue;
Temp. ArriveTime = a1;
Temp. Condition = tempqnode. Condition;
Temp. LeaveTime = tempqnode. LeaveTime;
Temp. License = tempqnode. License;
Push (S, temp );
}
If (tag2 = true & tag3 = false) // if the parking lot is not full and the exit is successful
-- CountForStack;
}
Void showmenu (CarStack & S, Queue & Q)
{
Cout <"******************************* Select menu **** * ************************** "<endl;
Cout <"1: Parking" <endl;
Cout <"2: Leave" <endl;
Cout <"3: Exit" <endl;
Cout <"****************************** please press the key to select **** * ************************* "<Endl;
Int tag;
Cin> tag;
While (tag! = 1 & tag! = 2 & tag! = 3)
Cin> tag;
Int A1;
Unsigned int A2;
Switch (TAG)
{
Case 1:
Cout <"Enter the vehicle number to arrive" <Endl;
Cin> A1;
Cout <"Enter the arrival time" <Endl;
Cin> A2;
Incar (S, Q, A2, A1 );
Break;
Case 2:
Cout <"Enter the departure vehicle ID" <Endl;
Cin> A1;
Cout <"Enter the departure time" <Endl;
Cin> A2;
Outcar (S, Q, A2, A1 );
Break;
Case 3:
Return;
Break;
}
Char ch;
Cout <"******************** exit by E/E, press any key to return to the menu *********************** "<Endl;
Cin> CH;
If (Ch! = 'E' & Ch! = 'E ')
Showmenu (S, q );
}
Void main ()
{
Carstack;
Initstack (carstack); // creates and initializes a stack for parking lots.
Queue carqueue;
Initqueue (carqueue); // creates and initializes a queue for the Channel
Showmenu (carstack, carqueue );
}