当前位置:文档之家› 算法与数据结构课程设计停车场问题

算法与数据结构课程设计停车场问题

#include"time.h"
#include"stdio.h"
#include"stdlib.h"
#define n 6
#include"malloc.h"
typedef struct{//模拟输入信息
double time;
int number;
int state;
}information;
information temporary[6];//临时数组

typedef struct{
information data[n];
int top;
}SeqStack,*PSeqStack;

PSeqStack Init_SeqStack(void)
{
PSeqStack S;
S=(PSeqStack)malloc(sizeof(SeqStack));
if(S)
S->top=-1;
return S;
}

int Empty_SeqStack(PSeqStack S)//1表示为空
{
if(S->top==-1)
return 1;
else
return 0;
}
int Push_SeqStack(PSeqStack S,information x)//入栈
{
if(S->top==n-1)
return 0;
else
{
S->top++;
S->data[S->top]=x;
return 1;
}
}
information Pop_SeqStack(PSeqStack S,information *x)//出栈
{
if(Empty_SeqStack(S))
printf("栈是空的");
else
{
*x=S->data[S->top];
S->top--;
return *x;
}
}
int Search(PSeqStack S,int x)//查找车在停车场内的位置
{
int i;
for(i=0;i<6;i++)
if(x==(S->data[i]).number)
return i;



}




typedef struct node{//链式的队列
information data;
struct node *next;
}Qnode,*PQNode;//链队的结点问题

typedef struct queue{
PQNode front,rear;
}LinkQueue,*PLinkQueue;//将头尾指针封装在一起的链队

PLinkQueue Init_LinkQueue()
{
PLinkQueue Q;
Q=(PLinkQueue)malloc(sizeof(LinkQueue));
if(Q)
{
Q->front=NULL;
Q->rear=NULL;
}
return Q;
}//初始化一个空队列

int Empty_LinkQueue(PLinkQueue Q)
{
if(Q&&Q->front==NULL&&Q->rear==NULL)
return 1;
else
return 0;
}//判断栈空

int In_LinkQueue(PLinkQueue Q,information x)//进入便道,进入队列
{
PQNode p;
p=(PQNode)malloc(sizeof(Qnode));
if(!p)
{
printf("内存溢出");
return 0;
}
p->data=x;
p->next=NULL;
if(Empty_LinkQueue(Q))
Q->rear=Q->front=p;
else
{
Q->rear->next=p;
Q->rear=p;
}
return 1;
}//入队完成

int Out_LinkQueue(PLinkQueue Q,information *x)
{
PQNode p;
if(Empty_LinkQueue(Q))
{
printf("NULL");
return 0;
}
*x=Q->front->data;
p=Q->front;
Q->front=Q->front->next;
free(p);
if(!Q->front)
Q->rear=NULL;
return 1;
}

double time1()//获得系统时间
{
double s;
time_t timer;
struct tm *tblock;
timer=time(NULL);
tblock=localtime(&timer);
s=tblock->tm_hour*60+(tblock->tm_min);
return s;
}

int main()
{
PSeqStack S;
PLinkQueue Q;
int a=-1,b=0,c,j=0,i,e,k;
double time2;
information infor,x;

Q=Init_LinkQueue();
S=Init_SeqStack();
printf("请输入你要停驶车辆的辆数字\n");
scanf("%d",&e);
for(k=0;k{
printf("请输入要模拟的车辆信息,其中输入0表示到达,输入1表示离开\n");
scanf("%d%d",&infor.state,&infor.number);
if(infor.state==0)//车辆到达
{
if(S->top!=5)
{
infor.time=time1();
Push_SeqStack(S,infor);
a++;
printf("车停在停车场内的位

置是%d\n ",a);
}
else
{
In_LinkQueue(Q,infor);
b++;
printf("车在便道上的位置是%d \n",b);
}
}
else if(infor.state==1)//说明是离开
{
printf("车辆在停车场内");
c=Search(S,infor.number);//搜索该车位置
printf("%d %d\n",c,infor.number);
//说明车在停车场内
time2=time1()-S->data[c].time;
printf("车停留的时间%0.1f分钟,应收取的费用是%0.1f",time2,time2);

for(i=S->top;i>c+1;i--)//依次出栈
{
x=Pop_SeqStack(S,&x);//如果是*指针的话就会出错一般指针要赋值的
temporary[j++]=x; //记录出栈的车辆
}
Pop_SeqStack(S,&x);

while(j--&&j!=1)//顺次进栈
Push_SeqStack(S,temporary[j]);

while(!Empty_LinkQueue(Q)&&S->top!=5)//便道上的车进入停车场内
{
if(!Out_LinkQueue(Q,&x))
{
printf("便道上没有车辆");
break;
}
else
{
infor.time=time1(infor.time);
a=Push_SeqStack(S,infor);
printf("车停在停车场内的位置是%d\n ",a);
}
}
}
}
}

相关主题
文本预览
相关文档 最新文档