停车管理系统

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define N 2
struct car
{
	int ID;//汽车牌照号
	int Time; // 汽车到达或离去时间
	char Su;//汽车到达还是离去 
	int position;//汽车在停车场或过道的位置 
};
typedef struct car Car;
struct SNode//两个栈共享空间 ,模拟停车场让车情景 
{
	Car Data[MAXSIZE];
	int Top1; 
	int Top2;
};
typedef struct SNode Stack;
typedef struct QNode PtrToSNode;
struct QNode
{
	Car Data;
	PtrToSNode *Next;
};
struct queue
{
	PtrToSNode *front,*rear;
	int size;
};
typedef struct queue Queue;

Stack CreateStack()//建立空栈 
{
	Stack S;
	S.Top1=-1;
	S.Top2=MAXSIZE;
	return S;
}
Queue *CreateQueue()//建立空队列 
{
	Queue *Q;
	Q=(Queue *)malloc(sizeof(Queue));
	Q->front=(PtrToSNode *)malloc(sizeof(PtrToSNode));
	
	Q->rear=(PtrToSNode *)malloc(sizeof(PtrToSNode));
	Q->rear->Next=NULL;
	Q->size=0;
	Q->front->Next=Q->rear;
	return Q;
}
bool IsEmpty(Queue *Q) 
{
	if(Q->size==0)
	return true;
	return false;
}
     
void InsertQueue(Queue *Q,Car C) //队首插入元素 
{
	if(IsEmpty(Q))
	{
		Q->rear->Data=C;				
	}
	else
	{
		PtrToSNode *P;
        P=(PtrToSNode *)malloc(sizeof(PtrToSNode));
        P->Data=C;
	    P->Next=NULL;
		Q->rear->Next=P;
		Q->rear=P;	    
	}
    Q->size++;
    return ;
}
Car DeleteQueue(Queue *Q) //队尾删除元素,返回Car类型 
{
	Car A;
	PtrToSNode *P;
	P=Q->front->Next;
	Q->front->Next=P->Next;
	A=P->Data;
	free(P);
	Q->size--;
	return A;
}
bool PushStack1(Stack &S,Car C) 
{
	if(S.Top2-S.Top1==1)
	return false;
	S.Top1++;
	S.Data[S.Top1]=C;
	return true;
}
bool PushStack2(Stack &S,Car C) 
{
	if(S.Top2-S.Top1==1)
	return false;
	S.Top2--;
	S.Data[S.Top2]=C;
	return true;
}
Car DeleteStack1(Stack &S)
{
	Car C;
	C=S.Data[S.Top1];
	S.Top1--;
	return C;
}
Car DeleteStack2(Stack &S)
{
	Car C;
	C=S.Data[S.Top2];
	S.Top2++;
	return C;
}
Car Change(Stack &S,Car C)
//遇到要出停车场的车时,当停车场内某辆车要离开时,
//在它之后进入的车辆必须先退出车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场 
{
	Car A,B;
	int s=0;
	while(S.Data[S.Top1].ID!=C.ID)
	{
		A=DeleteStack1(S);
		PushStack2(S,A);
		s++;
	}
	A=S.Data[S.Top1];
	DeleteStack1(S);
	while(s--)
	{
		B=DeleteStack2(S);
		B.position--;
		PushStack1(S,B);
	}
	return A;
}
void CountTimeAndCost(Car C,Car A)//计算出停车场的车停车时间和费用 ,并输出结果 
{
	int t,cost;
	t=C.Time-A.Time;
	cost=t*10;
	printf("停车时间:%d 累计费用:%d
",t,cost);
}
int main() 
{
	Car A,B,C;
	Stack S;
	Queue *Q;
	Q=CreateQueue();
	S=CreateStack();
	printf("	欢迎使用停车管理系统
");
	while(1)
	{
		printf("-------------A、停车---------------
");
		printf("-------------D、取车---------------
");
		printf("-------------E、退出---------------
");
		scanf("%c",&C.Su); 
		printf("依次输入你的车牌号,当前时间
");
		scanf("%d%d",&C.ID,&C.Time);
		getchar();//注意这里要吸收回车,否则会被下一轮输入的char吸收 
		if(C.Su=='E')
		break;
		if(C.Su=='A')
		{
			if(S.Top1==N-1)
			{
			    C.position=Q->size+1;
				InsertQueue(Q,C);
				printf("停车位置:便道 %d
",C.position);			
			}
			else
			{
				C.position=S.Top1+2;
				PushStack1(S,C);
				printf("停车位置:停车场 %d
",C.position);
			}
			
		}
		else
		{
			A=Change(S,C);
			CountTimeAndCost(C,A);
			if(!IsEmpty(Q))
			{
				B=DeleteQueue(Q); 
				B.position=S.Top1+2;
				PushStack1(S,B);
			}
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/clanguageweaver/p/6682065.html