#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
/*#define NULL 0*/
#define ERROR 0
#define OK 1
#define OVERFLOW 0
#define STACK_INIT_SIZE 2/*车库容量*/
/*----------------------------------------------------------------------*/
typedef struct time{
int hour;
int min;
}Time; /*时间结点*/
/*----------------------------------------------------------------------*/
typedef struct/*车信息*/
{
char label;
float time;
}Car,Car2;
typedef struct/*车库信息*/
{
Car *top;
Car *base;
int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
S->base=(Car *)malloc(STACK_INIT_SIZE*sizeof(Car));
if(!(S->base)) return ERROR;
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
int StackEmpty(SqStack S)
{
if(S.top==S.base)
return OK;
else
return ERROR;
}
int StackFull(SqStack S)
{
if(S.top-S.base>=STACK_INIT_SIZE)
return OK;
else
return ERROR;
}
int Push(SqStack *S,Car e)
{
if(S->top-S->base>=STACK_INIT_SIZE)
return OVERFLOW;
else
{
*(S->top++)=e;
return OK;
}
}
int Pop(SqStack *S,Car *e)
{
if(S->top==S->base)
return ERROR;
*e=*(--(S->top));
}
/*----------------------------------------------------------------*/
typedef struct/*备用车道*/
{
Car2 *top2;
Car2 *base2;
int stacksize2;
}SqStack2;
int InitStack2(SqStack2 *S2)
{
S2->base2=(Car2 *)malloc(STACK_INIT_SIZE*sizeof(Car2));
if(!(S2->top2)) return ERROR;
S2->top2=S2->base2;
S2->stacksize2=STACK_INIT_SIZE;
return OK;
}
int Push2(SqStack2 *S2,Car2 e2)
{
if(S2->top2-S2->base2>=STACK_INIT_SIZE)
return OVERFLOW;
*(S2->top2++)=e2;
return OK;
}
int Pop2(SqStack2 *S2,Car2 *e2)
{
if(S2->top2==S2->base2)
exit(OVERFLOW);
*e2=*(--(S2->top2));
return OK;
}
int StackEmpty2(SqStack2 S2)
{
if(S2.top2==S2.base2)
return OK;
else
return ERROR;
}
/*--------------------------------------------------------------------*/
typedef struct QNode/*车道信息*/
{
Car data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
int InitQueue(LinkQueue *Q)
{
Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
if(!(Q->front)) return ERROR;
Q->front->next=NULL;
return OK;
}
int EnQueue(LinkQueue *Q,Car e)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) return ERROR;
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return OK;
}
int QueueEmpty(LinkQueue Q)
{
if(Q.front==Q.rear)
return OK;
else
return ERROR;
}
int DeQueue(LinkQueue *Q,Car *e)
{
QueuePtr p;
if(Q->front==Q->rear) return ERROR;
p=Q->front->next;
*e=p->data;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
free(p);
return OK;
}
/*-----------------------------------------------------------------------*/
/*主函数*/
main()
{
int i,position_s=1,position_q=1;
int ch=1,status;
float time,money;
LinkQueue Q;
Car car_I,car_D,car_M;
SqStack S;
SqStack2 S2;
clrscr();
InitStack(&S);
InitStack2(&S2);
InitQueue(&Q);/*初始化*/
/*--------------------------------------------------------------------------*/
while(ch==1)
{
clrscr();
for(i=0;i<80;i++)printf("*");
printf("\n");
for(i=0;i<24;i++)
printf("-");
printf("\YAOTIANSHUNRI CAR POSITION\t");
for(i=0;i<24;i++)
printf("-");
printf("\n\t\t\t");
for(i=0;i<10;i++)
printf("-");
printf("Information:");
for(i=0;i<10;i++)
printf("-");
printf("\n\n");
do
{
printf("\n\t\t\t1-arrival 2-departure 0-exit 2/1/0 ?\b");
scanf("%d",&status);
}while(status!=1&&status!=2&&status!=0);
if(status==1)/*当车到达*/
{
printf("\n\t\t\tCar Number :");
scanf("%d",&car_I.label);
printf("\n\t\t\tTime : ?\b");
scanf("%f",&(car_I.time));
if(!StackFull(S))
{
Push(&S,car_I);
printf("\n\n");
for(i=0;i<80;i++)
printf("-");
printf("\n");
printf("\tCAR NUMBER :");
printf(" %d",car_I.label);
printf("\tARRIVE TIME :");
printf(" %5.2f",car_I.time);
printf("\n\n");
for(i=0;i<80;i++)
printf("-");
printf("\n\n");
printf("\nWelcome to our YAOTIANSHUNRI CAR POSITION !\
\n\nThe position of your car is %d",position_s);
position_s++;
printf("\n");
printf("Do you want to continue 1-continue/0-quit ?\b");
scanf("%d",&ch);
}
else
{
EnQueue(&Q,car_I);
printf("Welcome to our YAOTIANSHUNRI CAR POSITION ,We are sorry that\
\n\nOur position is full,but you are free to place your car\
\n\non our road.The position of your car is %d",position_q);
position_q++;
printf("\nDo you want to continue 1-continue/0-quit ?\b");
scanf("%d",&ch);
}
}
else if(status==2)/*当车离开*/
{
clrscr();
for(i=0;i<80;i++)printf("*");
printf("\n");
for(i=0;i<24;i++)
printf("-");
printf("\tYAOTIANSHUNRI CAR POSITION\t");
for(i=0;i<24;i++)
printf("-");
printf("\n\t\t\t");
for(i=0;i<10;i++)
printf("-");
printf("Information");
for(i=0;i<10;i++)
printf("-");
printf("\n\n");
printf("\tYour are going to drive your car away ,\n\t\tPlease fill of the form !\n");
printf("\n\t\tYour car Number :");
scanf("%d",&car_D.label);
printf("\n\t\t\tTime : ?\b");
scanf("%f",&(car_D.time));
do
{
Pop(&S,&car_M);
if(car_D.label!=car_M.label)
Push2(&S2,car_M);
else
car_I.time=car_M.time;
}while(car_D.label!=car_M.label);
while(!StackEmpty2(S2))
{
Pop2(&S2,&car_M);
Push(&S,car_M);
}
while(!QueueEmpty(Q)&&!StackFull(S))
{
if(!StackFull(S))
{
DeQueue(&Q,&car_M);
Push(&S,car_M);
printf("The car %d just drived away ,\n\n\tthe car%d has entered the\
YAOTIANSHUNRI CAR POSITION .\n",car_D.label,car_M.label);
}
}
time=car_D.time-car_I.time;
if(time<0.0000)
printf("Sorry!You input a wrong time !Please check and type again.\n");
if(time>0.00001&&time<3.0)
money=time*2.0;
else
money=time*3.0;
printf("\n\n");
for(i=0;i<80;i++)
printf("-");
printf("\n\t");
printf("Your car number %d :\tThe fee is :%5.2f",car_D.label,money);
printf("\n\n");
for(i=0;i<80;i++)
printf("-");
printf("\n\n");
printf("Welcome to back ! Do you want to continue 1-contine/0-quit ?\b");
scanf("%d",&ch);
}
else exit(0);/*退出程序*/
}
}