```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;
}
出现这种问题的原因是因为头文件重复包含,即头文件A包含了B,头文件B又包含了A,导致编译器在加载头文件时陷入死循环。
解决办法:修改头文件包含,如果两个文件代码确实紧密关联,建议写到一个文件中。
修改步骤:
(1)新建一个def.h文件,把ParkSystem.h文件中的busType结构体定义移动到def.h文件中,如下:
#ifndef _DEF_H_
#define _DEF_H_
typedef struct {
int nType;
int busNo;
int occurTime;
int status;
}busType;
#endif // !_DEF_H_
(2)删除ParkSystem.h文件中 busType结构体的定义,修改如下:
#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
(3)将Queue.h和Stack.h中的 #include "ParkSystem.h" 改成
#include "def.h"
OK,编译运行就可以了。
既然是用的vs那就在头文件的第一行加上这段代码
#pragma once