停车场的代码
我们做课程设计,题目是停车场,我写了代码,但总是出错,代码写的也不完整,就是进入的时候有时间,出来的时候没写时间,但这现在无所谓,问题是编译没问题,但运行的时候进入车站函数后总是死循环,求高手帮我一下,代码错的也帮我改一下,感激不尽#include <stdio.h>
typedef struct car
{
int hour;
int number;
int minite;
}Car;
typedef struct stack
{
int base;
int top;
Car *s[3];
}STack;
typedef struct QNode
{
Car *data;
struct QNode *next;
}QNode,*Queueptr;
typedef struct
{
Queueptr front;
Queueptr rear;
}LinkQueue;
initstack(STack *ET) /*初始化栈,包括临时栈*/
{
ET->base=ET->top=-1;
}
initQueue(LinkQueue *D) /*初始化队列*/
{
D->front=D->rear=(Queueptr)malloc(sizeof(QNode));
if(!D->front) exit (1);
D->front->next=NULL;
}
menu()
{
printf("Now please choice.1......................enter\n");
printf(" 2.......................leave\n");
printf(" 3.......................information\n");
}
push(STack *enter,LinkQueue *d) /*进入车站*/
{
Car *p;
LinkQueue *w;
printf("please enter the car information:number,time(xx,xx):"); /*按,逗号输出如111,11,11 车牌号是111,时间是11点11分*/
scanf("%d,%d,%d",&p->number,&p->hour,&p->minite);
if(enter->top>=2) /*为了简单,只能进入2辆车,当等于大于2时车站满,进入等候区,也就是进入队列*/
{
printf("the car park is full,you must wai:");
w=(Queueptr)malloc(sizeof(QNode)); /*分配空间*/
w->data=p;w->next=NULL;
d->rear->next=w;
d->rear=w;
return 1;
}
else
{
printf("you can enter the park:"); /*进入栈,也就是进入停车场*/
enter->top++; /*进入栈的,这么写可以吗,书上是把两句换了,我觉得这样也可以,所以这么写的*/
enter->s[enter->top]=p;
}
}
gettop(STack *enter,STack *temp,LinkQueue *d) /*出战,也是从停车场出来*/
{
int i,j;
Car *t;
if(enter->top==-1)
{
printf("there is no car in the park:");
return 1;
}
else
{
printf("please enter the leave location:"); /*选择出栈的位置*/
scanf("%d",&i);
while(enter->top!=i-1)
{
temp->top++; /*进入临时栈*/
temp->s[temp->top]=enter->s[enter->top];
enter->top--;
}
while(temp->top!=-1) /*进入临时栈的要进入主栈*/
{
enter->top++;
enter->s[enter->top]=temp->s[temp->top];
temp->top--;
}
if(d->front!=d->rear) /*等候区有车的话进入停车场*/
{
t=d->front->next;
d->front->next=t->next;
enter->top++;
enter->s[enter->top]=t->data;
if(d->rear==t) d->front=d->rear;
free(t);
}
}
}
park(STack *enter) /*停车场信息*/
{
int i;
Car *p;
printf("there is %d car in the park",enter->top+1);
for(i=0;i<=enter->top;i++)
printf("arrive number:%d,arrive time is %d:%d",p->number,p->hour,p->minite);
}
wait( LinkQueue *d) /*等车信息*/
{
if(d->front==d->rear)
printf("there is no car waiting");
else
printf("there is car in the waiting");
}
information(STack *enter,LinkQueue *d) /*车辆信息函数*/
{
int op;
printf("please choice:\n");
printf("1,car park information:\n");
printf("wait information:\n");
scanf("%d",&op);
switch(op)
{
case 1:park(enter); break;
case 2:wait(d); break;
}
}
main()
{
int i,j,op;
STack *enter,*temp;
LinkQueue *d;
initstack(enter);
initstack(temp);
initQueue(d);
menu();
printf("please choic:");
scanf("%d",&op);
do
{
switch(op)
{
case 1:push(enter,d); break; /*输入后总是死循环*/
case 2:gettop(enter,temp,d); break;
case 3:information(enter,d); break;
}
printf("please choice again:");
scanf("%d",&op);
}while(op);
}