我是
靠谱客的博主
无语眼睛,这篇文章主要介绍
一个简单的模拟车场的程序,现在分享给大家,希望可以做个参考。
一个简单的模拟车场的程序,用顺序栈 stack 模拟停车场,链队列 queue 模拟便道.若车场内已停满车,则后来的车要在便道上等候,当有车开走时,便道上的第一辆车即可以开入,当停车场内有车要离开时,在它这后进入的车辆必须退出车场为它让路,,特车辆离开后再按原次序返回车场。
#include
<
stdio.h
>
#include
<
stdlib.h
>
#include
<
malloc.h
>
#define
size 2

typedef
struct

...
{
int mark;
int intime;
int outtime;
}
cars;

typedef
struct
node

...
{
cars car;
struct node *next;
}
passage;

typedef
struct

...
{
passage *in;
passage *out;
}
queue;

typedef
struct

...
{
cars car[size];
int top;
}
stack;


void
InitStack(stack
*
s);
int
FullStack(stack
*
s);
void
PushStack(stack
*
s,cars
*
c);
void
PopStack(stack
*
s,cars
*
c);
void
SearchStack(stack
*
s,
int
*
space,
int
mark);
void
InitQueue(queue
*
q);
void
EnterQueue(queue
*
q,cars
*
c);
void
DeleteQueue(queue
*
q,cars
*
c);
int
EmptyQueue(queue
*
q);
void
InitPassage(passage
*
p);

int
main()

...
{
cars moto;
stack park, temporary;
queue dypass;
int choice, space, mark, time, i;

InitStack( &park );
InitStack( &temporary );
InitQueue( &dypass );

while(1)

...{
printf("******** ******* ");
printf("Choose in or out ? ");
printf("1:come in; 2:come out. ");
scanf("%d", &choice);

if(choice==1)

...{
printf(" Please input the car ' registration mark ");
scanf("%d", &moto.mark);
printf(" Please input the car pull in time ");
scanf("%d", &moto.intime);

moto.outtime = 0;
if(FullStack(&park))

...{
printf("the park is full,please stand in line ");
EnterQueue(&dypass, &moto);
}
else

...{
printf(" the parking space number is %d ",park.top+1);
PushStack(&park, &moto);
}
}

else
if(choice==2)

...{
printf("Which car want to pull out ? Input the mark ");
scanf("%d", &mark);

SearchStack(&park,&space,mark);

printf("input the pull out time ");
scanf("%d", &park.car[space].outtime);

time = park.car[space].outtime - park.car[space].intime;

printf("the fare you shuold hand over is %d ",5*time);

// for(i=park.top;i >space;i--)
// {
// PopStack(&park, &moto);
// PushStack(&temporary, &moto);
// }
while ( space )

...{
PopStack(&park, &moto);
PushStack(&temporary, &moto);
space --;
}
PopStack(&park,&moto);
// for(i = 1; i <= temporary.top; i ++)
// {
// PopStack(&temporary, &moto);
// PushStack(&park, &moto);
// }
while ( temporary.top > -1 )

...{
PopStack(&temporary, &moto);
PushStack(&park, &moto);
}
if( !EmptyQueue(&dypass) )

...{
printf("the mark %d can pull in park now ", dypass.out->car.mark);
DeleteQueue(&dypass, &moto);
PushStack(&park, &moto);
}
}

else
printf("What are you doing ? ");
}

system("PAUSE");
return 0;
}

void
InitPassage(passage
*
p)

...
{
p->next=NULL;
}

void
InitStack(stack
*
s)

...
{
s->top = -1;
}

int
FullStack(stack
*
s)

...
{
return ( s->top == (size-1) ? 1:0 );
}

void
PushStack(stack
*
s, cars
*
c)

...
{
s->top ++;
s->car[s->top] = *c;
}

void
PopStack(stack
*
s, cars
*
c)

...
{
if( s->top == 0 )
printf(" the cars is all pull out ");
else

...{
*c = s->car[s->top];
s->top --;
}
}

void
SearchStack(stack
*
s,
int
*
space,
int
mark)

...
{
int i, flag = 1;

for(i = 0; i < size; i ++)
if(s->car[i].mark == mark)

...{
*space = i;
flag = 0;
break;
}
if(flag == 1)
printf(" the car does not exit ");
}


void
InitQueue(queue
*
q)

...
{
q->in = ( passage* )malloc( sizeof(passage) );
q->out = q->in;
q->in = NULL;
}


void
EnterQueue(queue
*
q, cars
*
c)

...
{
passage *newnode;

newnode = ( passage* )malloc( sizeof(passage) );
newnode->car = *c;
newnode->next = NULL;

if ( q->in )
newnode->next = q->in->next;
else
q->out = newnode;
q->in = newnode;

}

void
DeleteQueue(queue
*
q,cars
*
c)

...
{
passage *p;
passage *ptmp;

if ( NULL == q->out )

...{
printf("There is no car waitting. ");
return ;
}

p = q->in;

if( q->out == q->in )

...{
printf("All the car are pull in park ");
q->in = q->out = NULL;
}
else

...{
while ( p->next != q->out )
p = p->next;

p->next = NULL;

ptmp = p;
p = q->out;
q->out = ptmp;
}

*c = p->car;
free( p );
}

int
EmptyQueue(queue
*
q)

...
{
if(q->in == q->out)

...{
printf("All the car are pull in park ");
return 0;
}
else
return 1;
}

最后
以上就是无语眼睛最近收集整理的关于一个简单的模拟车场的程序的全部内容,更多相关一个简单内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复