题目要求
设停车场(如下图1所示)内只有一个可停放几量汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在车场的最北端),若车场内已经停满几量汽车,则后来的汽车只能在门外的便道上等候,一旦停车场内有车开走,则排在便道上的第一辆汽车即可开入;当停车场内某车辆要离开时,由于停车场是狭长的通道,在它之后开入车场的车辆必须先退出车场为它让路,待该车辆开出大门外后,为它让路的车辆再按原次序进入车场。在这里假设汽车不能从便道上开走。试设计一个停车场管理程序(这里只是一个假想的停车场管理,并不代表实际的停车场管理)。

分析
分析:汽车在停车场内进出是按照栈的运算方式来实现的,先到的先进停车场;停车场的汽车离开停车场时,汽车场内其它汽车为该辆汽车让路,也是按栈的方式进行;汽车在便道上等候是按队列的方式进行的。因此,将停车场设计成一个栈,汽车让路也需要另一个栈来协助完成,汽车进出便道用队列来实现。
本设计,栈采用顺序栈结构,队列用链式存储结构。
存储结构定义如下:
#define stacksize 10
typedef struct sqstack
{
int data[stacksize];
int top;
} SqStackTp;
typedef struct linked_queue
{
int data;
struct linked_queue * next;
}LqueueTp;
typedef struct
{
LqueueTp *front , *rear ;
} QueptrTp;
停车场管理的算法描述如下:
1)接受命令和车号,若是汽车要进停车场,先判断停车场栈是否满,若不满,则汽车入栈,否则汽车进入便道队列等候。
2)若是汽车要离开停车场,为给汽车让路,将停车场栈上若干辆汽车入临时栈,等这辆车出停车场后,临时栈中的汽车出栈,在回到停车场栈,然后看便道队列是否为空,若不空则说明有汽车等候,从队头取出汽车号,让该车进入停车场栈。
3)重复1),2)直到为退出命令(车号为0或负数)。
代码实现
主函数 main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "car.h"
int main()
{
int caozuo,car;
P.top=0;
LP.top=0;
H.next=NULL;
Hhead.front=Hhead.rear=&H;
introduce();
while(scanf("%d",&caozuo)!=EOF)
{
switch(caozuo)
{
case 1:jin();break;//出停车场
case 2:chu();break;//进停车场
case 3:printP();break;//打印停车场内的车辆
case 4:printH();break;//打印候车区的车辆
case 5:system("cls");break;
default :printf("您输入的操作不正确,请重新输入!\n");break;
}
introduce();
}
return 0;
}
头文件 car.h
#ifndef CAR_H_INCLUDED
#define CAR_H_INCLUDED
#define stacksize 10
typedef struct sqstack
{
int data[stacksize];
int top;
} SqStackTp;
typedef struct linked_queue
{
int data;
struct linked_queue * next;
}LqueueTp;
typedef struct//存储链队列的头指针和尾指针
{
LqueueTp *front , *rear ;
} QueptrTp;
void jin();
void chu();
void printP();
void printH();
void introduce();
SqStackTp P;//停车场
SqStackTp LP;//临时停车场
LqueueTp H;//候车场
QueptrTp Hhead;
#endif // CAR_H_INCLUDED
介绍函数 introduce.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "car.h"
void introduce()
{
printf("****************1表示汽车进场****************\n");
printf("****************2表示汽车出场****************\n");
printf("****************3表示输出停车场全部车辆******\n");
printf("****************4表示输出候车区的全部车辆****\n");
printf("****************5表示清屏********************\n");
printf("**********请输入您要进行的操作及车辆的车号***\n");
}
进停车场 jin.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "car.h"
void jin()
{
int car;
scanf("%d",&car);
if(car<=0)exit(1);//车号为0或为负数直接退出系统
if(P.top==stacksize)//说明停车场已经满,则将车进入队列
{
(Hhead.rear)->next=(LqueueTp *)malloc(sizeof(LqueueTp));
Hhead.rear=(Hhead.rear)->next;
(Hhead.rear)->data=car;
(Hhead.rear)->next=NULL;
return;
}
else//否则直接将该车进入停车场
{
P.data[P.top++]=car;
return;
}
}
出停车场 chu.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "car.h"
void chu()
{
int car;
scanf("%d",&car);
if(car<=0)exit(1);
while(P.data[--P.top]!=car)//将car之后的车辆进入临时链表LP
{
LP.data[LP.top++]=P.data[P.top];
}
while(LP.top!=0)//再将临时链表中的车辆写入停车场链表P
{
P.data[P.top++]=LP.data[--LP.top];
}
if(Hhead.rear!=Hhead.front)//判断候车队列H是否有车辆,有的话就出来一个进入停车场
{
P.data[P.top++]=(Hhead.front)->next->data;
(Hhead.front)->next=(Hhead.front)->next->next;
if((Hhead.front)->next==Hhead.rear)
{
Hhead.rear=Hhead.front;
}
}
}
printP.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "car.h"
void printP()
{
int flag=0;
if(P.top==0)
{
printf("停车场已空\n");
return;
}
printf("由北向南依次为:\n");
while(flag!=P.top)
{
printf("%d ",P.data[flag++]);
}
printf("\n");
}
printH.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "car.h"
void printH()
{
LqueueTp *p;
if(Hhead.front==Hhead.rear)
{
printf("候车区没有车辆\n");
return;
}
p=(Hhead.front)->next;
printf("由等候时间由长到短为:\n");
while(p!=Hhead.rear)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d\n",p->data);
}
本文介绍了如何使用C语言设计一个停车场管理系统,利用栈来模拟车辆进出的顺序,队列则用于管理等待的车辆。系统包含主函数、头文件、介绍函数、进出场函数及打印功能。车辆进入时,若停车场未满则直接进入,否则进入等待队列;车辆离开时,其他车辆需退出为它让路,然后按原次序返回。
1687

被折叠的 条评论
为什么被折叠?



