
```c
//Stack.h
#ifndef _STACK_H_
#define _STACK_H_
#include"ParkSystem.h"
typedef struct {
busType* top;
busType* base;
}Stack;
#define MAXSIZE 15
void InitStack(Stack* s);
void Pop(Stack* s, busType* e);
void Push(Stack* s, busType* e);
void GetTop(Stack* s,busType* e);
int EmptyStack(Stack s);
#endif
//Stack.c
#include<stdio.h>
#include<stdlib.h>
#include"Stack.h"
void InitStack(Stack* s) {
s->base = (busType*)malloc(MAXSIZE * sizeof(busType));
s->top = s->base;
}
void Push(Stack* s, busType* e) {
if ((s->top - s->base) == MAXSIZE) {
return;
}
*s->top = *e;
s->top++;
}
void Pop(Stack* s, busType* e) {
if (s->base == s->top) {
return;
}
s->top--;
*e = *(s->top);
}
void GetTop(Stack *s,busType *e) {
s->top--;
*e = *(s->top);
}
int EmptyStack(Stack s) {
if (s.base == s.top) {
return 0;
}
return 1;
}
//Queue.h
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include"ParkSystem.h"
typedef struct {
busType* base;
int front;
int rear;
}Queue;
#define MAXSIZE 15
void InitQueue(Queue* Q);
void EnQueue(Queue* Q, busType* e);
void DeQueue(Queue* Q, busType* e);
void Top(Queue* Q, busType* e);
int Empty(Queue Q);
#endif
//Queue.c
#include<stdio.h>
#include<stdlib.h>
#include"Queue.h"
void InitQueue(Queue* Q) {
Q->base = (busType*)malloc(MAXSIZE * sizeof(busType));
Q->front = 0;
Q->rear = 0;
}
void EnQueue(Queue* Q, busType *e) {
if ((Q->rear + 1) % MAXSIZE == Q->front) {
return;
}
Q->base[Q->rear] = *e;
Q->rear = (Q->rear + 1) % MAXSIZE;
}
void DeQueue(Queue* Q, busType* e) {
if (Q->front == Q->rear) {
return;
}
*e = Q->base[Q->front];
Q->front=(Q->front + 1) % MAXSIZE;
}
void Top(Queue* Q, busType* e) {
*e = Q->base[Q->front];
}
int Empty(Queue Q) {
if (Q.front == Q.rear) {
return 0;
}
return 1;
}
//ParkSystem.h
#ifndef _PARKSYSTEM_H_
#define _PARKSYSTEM_H_
#include"Stack.h"
#include"Queue.h"
typedef struct{
int nType;
int busNo;
int occurTime;
int status;
}busType;
void busArrive(Stack* s, Queue* q, busType* x);
void busLeave(Stack* s, Queue* q, busType x);
#endif
//ParkSystem.c
#include<stdio.h>
#include"ParkSystem.h"
void busArrive(Stack* s, Queue* q, busType* x) {
if ((s->top - s->base) == 7 && (q->rear - q->front + MAXSIZE) % MAXSIZE == 4) {
printf("停车场已满!\n");
return;
}
//两种情况,停车场里未满(便道可满可不满),停车场里已满便道未满
if (s->top - s->base < 7) {
x->status = 1;
Push(s, x);
}
else {
x->status = 2;
EnQueue(q, x);
}
}
void busLeave(Stack* s, Queue* q, busType x) {
Stack temp;
InitStack(&temp);
busType i, j;
if (x.status == 1) {
GetTop(s,&i);
while (i.busNo != x.busNo) {
Pop(s, &j);
Push(&temp, &j);
GetTop(s, &i);
}
Pop(s, &j);
printf("车号:%d#,到达时间:%d,离开时间:%d,普,收费:%d\n", x.busNo, j.occurTime, x.occurTime, (x.occurTime - j.occurTime) * 7);
while (EmptyStack(temp)) {
Pop(&temp, &j);
Push(s, &j);
}
}
else {
busType i, j;
Top(q, &i);
while (i.busNo != x.busNo) {
DeQueue(q, &j);
EnQueue(q, &j);
Top(q, &i);
}
DeQueue(q, &j);
printf("车号:%d#,到达时间:%d,离开时间:%d,普,收费:%d\n", x.busNo, j.occurTime, x.occurTime, (x.occurTime - j.occurTime) * 5);
}
}
//main.c
#include<stdio.h>
#include"ParkSystem.h"
int main()
{
Stack S;
InitStack(&S);
Queue Q;
InitQueue(&Q);
busType i;
scanf("%d %d %d ", &i.busNo, &i.nType, &i.occurTime);
busArrive(&S, &Q, &i);
scanf("%d %d %d ", &i.busNo, &i.nType, &i.occurTime);
busLeave(&S, &Q, i);
return 0;
}
