/* File name: exp3-7.cpp */
# Include <stdio. h>
# Include <malloc. h>
# Define N 3/* Maximum number of parking lots */
# Define M 4/* Maximum number of parking in the waiting yard */
# Define price 2/* parking fee per unit */
Typedef struct
{
Int carno [N];/* license plate number */
Int cartime [N];/* arrival time */
Int top;/* Stack pointer */
} Sqstack;/* define the sequence stack type */
Typedef struct
{
Int carno [m];/* license plate number */
Int front, rear;/* head and tail pointer */
} Sqqueue;/* define the cyclic queue type */
/* The following is the basic algorithm of the sequence stack */
Void initstack (sqstack * & S)
{
S = (sqstack *) malloc (sizeof (sqstack ));
S-> Top =-1;
}
Int stackempty (sqstack * s)
{
Return (S-> Top =-1 );
}
Int stackfull (sqstack * s)
{
Return (S-> Top = N-1 );
}
Int push (sqstack * & S, int E1, int E2)
{
If (S-> Top = N-1)
Return 0;
S-> top ++;
S-> carno [S-> top] = e1;
S-> cartime [S-> top] = e2;
Return 1;
}
Int POP (sqstack * & S, Int & E1, Int & E2)
{
If (S-> Top =-1)
Return 0;
E1 = s-> carno [S-> top];
E2 = s-> cartime [S-> top];
S-> top --;
Return 1;
}
Void dispstack (sqstack * s)
{
Int I;
For (I = s-> top; I> = 0; I --)
Printf ("% d", S-> carno [I]);
Printf ("\ n ");
}
/* The following is the basic algorithm for cyclic queue */
Void initqueue (sqqueue * & Q)
{
Q = (sqqueue *) malloc (sizeof (sqqueue ));
Q-> front = Q-> rear = 0;
}
Int queueempty (sqqueue * q)
{
Return (Q-> front = Q-> rear );
}
Int queuefull (sqqueue * q)/* determines if the team is full */
{
Return (Q-> rear + 1) % m = Q-> front );
}
Int enqueue (sqqueue * & Q, int e)/* enter */
{
If (Q-> rear + 1) % m = Q-> front)/* full team */
Return 0;
Q-> rear = (Q-> rear + 1) % m;
Q-> carno [q-> rear] = E;
Return 1;
}
Int dequeue (sqqueue * & Q, Int & E)/* team out */
{
If (Q-> front = Q-> rear)/* empty Team */
Return 0;
Q-> front = (Q-> front + 1) % m;
E = Q-> carno [q-> front];
Return 1;
}
Void dispqueue (sqqueue * q)/* elements in the output queue */
{
Int I;
I = (Q-> front + 1) % m;
Printf ("% d", Q-> carno [I]);
While (Q-> rear-I + M) % m> 0)
{
I = (I + 1) % m;
Printf ("% d", Q-> carno [I]);
}
Printf ("\ n ");
}
Void main ()
{
Int comm;
Int No, E1, time, E2;
Int I, J;
Sqstack * St, * ST1;
Sqqueue * qu;
Initstack (ST );
Initstack (ST1 );
Initqueue (qu );
Do
{
Printf ("Enter the command (1: Arrival 2: Drive 3: Parking Lot 4: Waiting field 0: Exit ):");
Scanf ("% d", & comm, & No, & time );
Switch (Comm)
{
Case 1:/* car arrival */
If (! Stackfull (ST)/* parking lot not satisfied */
{
Push (St, no, time );
Printf ("> parking lot location: % d \ n", St-> top + 1 );
}
Else/* the parking lot is full */
{
If (! Queuefull (qu)/* out of stock */
{
Enqueue (qu, no );
Printf ("> waiting location: % d \ n", qu-> rear );
}
Else
Printf ("> the waiting yard is full, and parking cannot be stopped \ n ");
}
Break;
Case 2:/* automobile fault */
For (I = 0; I <= ST-> top & St-> carno [I]! = No; I ++ );
If (I> St-> top)
Printf ("> the specified car \ n is not found ");
Else
{
For (j = I; j <= ST-> top; j ++)
{
Pop (St, E1, E2 );
Push (ST1, E1, E2);/* reverse to ST1 of the temporary stack */
}
Pop (St, E1, E2);/* The car leaves */
Printf ("> % d car parking fees: % d \ n", no, (time-e2) * price );
While (! Stackempty (ST1)/* re-return the temporary stack ST1 to the st */
{
Pop (ST1, E1, E2 );
Push (St, E1, E2 );
}
If (! When queueempty (qu)/* the team is not empty, head the team into the stack st */
{
Dequeue (qu, E1 );
Push (St, E1, time);/* start billing at the current time */
}
}
Break;
Case 3:/* display the parking lot status */
If (! Stackempty (ST ))
{
Printf ("> vehicles in the parking lot:");/* output vehicles in the parking lot */
Dispstack (ST );
}
Else
Printf ("> no vehicles in the parking lot \ n ");
Break;
Case 4:/* display the waiting position */
If (! Queueempty (qu ))
{
Printf ("> vehicles in the waiting yard:");/* output vehicles in the waiting yard */
Dispqueue (qu );
}
Else
Printf ("> no vehicles in the waiting yard \ n ");
Break;
Case 0:/* end */
If (! Stackempty (ST ))
{
Printf ("> vehicles in the parking lot:");/* output vehicles in the parking lot */
Dispstack (ST );
}
If (! Queueempty (qu ))
{
Printf ("> vehicles in the waiting yard:");/* output vehicles in the waiting yard */
Dispqueue (qu );
}
Break;
Default:/* Other Situations */
Printf ("> input command error \ n ");
Break;
}
} While (Comm! = 0 );
}