课程设计
课 程:
题 目: 停车场管理系统
专 业:
班 级:
姓 名:
2010 年 9 月 6 日
《数据结构》课程设计
停车场管理系统
一、 课程设计目的
1、 通过课程设计,加深对《数据结构》这一课程所学内容的进一步理解与巩
固。
2、 通过课程设计,加深对结构化设计思想的理解,能对系统功能进行分析,
并设计合理的模块化结构。
3、 通过课程设计,提高程序开发功能,能运用合理的控制流程编写清晰高效
的程序。
4、 通过课程设计,训练C程序调试能力,能将一个中小型各级组织系统联调
通过。
5、 通过课程设计,开发一个中小型系统,掌握系统研发全过程。
6、 通话课程设计,培养分析问题、解决实际问题的能力。
二、课程设计内容
① 问题描述:
设有一个可以停放n辆汽车的狭长停车场,它只有一个大门可以供车辆进出。车辆按到达停车场时间的早晚依次从停车场最里面向大门口处停放(最先到达的第一辆车放在停车场的最里面)。如果停车场已放满n辆车,则后来的车辆只能在停车场大门外的便道上等待,一旦停车场内有车开走,则排以便道上的第一辆车就进入停车场。停车场内如有某辆车要开走,在它之后进入停车场的车都必须先退出停车场为它让路,待其开出停车场后,这些辆再依原来的次序进场。每辆车在离开停车场时,都应根据它在停车场内停留的时间长短交费。如果停留在便道上的车未进停车场时,允许其离去,不收停车费,并且仍然保持在便道上等待的车辆的次序。编制一程序模拟该停车场的管理。
② 基本要求:
要求程序输出每辆车到达后的停车位置(停车场或便道上),以及某辆车离开停车场应交纳的费用和它在停车场内停留的时间。
③ 实现提示:
汽车的模拟输入信息格式可以是:(到达/离去,汽车牌照号码,到达/离去的时刻)。例如,(‘A’,1,5)表示1号牌照车在5这个时刻到达,而(‘D’,5,
20)表示5号牌照车在20这个时刻离去。整个程序可以在输入信息为(‘E’,0,0)时结束。本题可用栈和队列来实现。
三、概要设计
1、设计思想
此停车场管理系统是在一个狭长的通道上的,而且只有一个大门可以供车辆进出,并且要实现停车场内某辆车要离开时,在它之后进入停车场的车都必须先退出停车场为它让路,待其开出停车场后,这些辆再依原来的次序进场的功能,就可以设计两个堆栈,其中一个堆栈用来模拟停车场,另一个堆栈用来模拟临时停车场,临时停车场用来存该放当有车辆离开时,原来停车场内为其让路的车辆。至于当停车场已满时,需要停放车辆的通道可以用一个链队列来实现。当停车场内开走一辆车时,通道上便有一辆车进入停车场,此时只需要改变通道上车辆结点的连接方式就可以了,使通道上第一辆车进入停车场这个堆栈,并且使通道上原来的第二辆车成为通道上的第一辆车,此时只需将模拟通道的链队列的头结点连到原来的第二辆车上就可以了。
2、实现方法
对于此停车场管理系统的实现,就是用两个堆栈来分别模拟停车场以及停车场内车辆为其它车辆让路时退出停车的临时停放地点。至于通道上车辆的停放则用一个链队列来实现,此时,通道上车辆的离开或者进入停车场只需改变
共 4 页 第 页 1
此链队列上的结点而已。对于要对停车场内的车辆根据其停放时间收取相应的停车费用,可以记录下车辆进入以及离开停车场的时间,再用时间差乘以相应的单价并且打印出最后的费用就可以实现了。
3、主要模块
①此停车场管理系统,主要分为以下若干模块:
首先定义用来模拟停车场的堆栈以及用来模拟通道的链队列为全局变量,然后编写主函数,在此主函数中实现对其它各个模块的调用。在主函数中首先调用option()函数,出现欢迎用户使用的主界面,然后提示用户进入此停车场管理系统后,再出现一个供用户选择的界面,在用户的选择过程中,程序又分别调用车辆的到达、车辆的离开、停车场内停放车辆的信息以及退出程序这四个函数模块。其中,在车辆的离开那个模块函数中又调用了打印离开车辆信息的函数,在停车场内停放车辆信息的那个模块函数中,又分别调用了显示停车场上车辆信息的函数以及显示便道上车辆信息的函数。最后,从调鼐的这四个函数中回到主函数结束整个程序的运行。
②在以上各个模块中,出现的调用的函数为:
void InitStack(SeqStackCar *s);初始化“停车位栈”
int InitQueue(LinkQueueCar *Q);初始化“辅助栈”
option();程序功能介绍和操作提示模块函数
int Arrival(SeqStackCar *Enter,LinkQueueCar *W);汽车插入停车位栈并修改该车状态
void Leave(SeqStackCar *Enter,SeqStackCar *Temp,LinkQueueCar *W);该车从停车位删除并修改停车状态
void PRINT(CarNode *p);汽车信息显示在屏幕上
共 4 页 第 页 2
void List(SeqStackCar S,LinkQueueCar W);从便道队列进入停车位栈 void List1(SeqStackCar *S);
; void List2(LinkQueueCar *W)
4、模块间关系
共 4 页 第 页 3
四、调试分析
(1) 调试过程中的主要问题
由于此停车场管理系统是分模块设计的,而且在程序的实现过程中又使用了清屏函数,所以,运行时用户选择任务并且执行完任务后,又会回到供用户选择功能的主界面,因此整个程序从整体上来讲结构清晰,使用方便。本程序的调试运行,总体上情况良好,但中间也出现了一些小问题。其中比较有代表性的主要问题有:
当停车场已经达到最大容量,但仍有车辆进入停车场的时候,运行界面上没有出现或者说出现了但又跳掉了“停车场已满,该车辆需在便道上等待!”的提示信息。我们小组成员经过反复商量讨论,并且在查阅了多种资料后,在那一个printf语句后加了一个getch(),此时,程序运行结果就符合要求了。
本程序中我使用的是VC6++进行的编译和运行!
共 4 页 第 页 5
(2) 测试结果的分析与讨论
①欢迎界面
共 4 页 第 页 6
②车辆到达
共 4 页 第 页 7
③车辆离开
共 4 页 第 页 8
共 4 页 第 页 9
④车辆信息(车场)
共 4 页 第 页 10
⑤车辆信息(便道)
共 4 页 第 页 11
五、用户手册
共 4 页 第 页 12
这个程序用来实现对停车场内车辆的管理,整个操作界面为中文,更加符合人性化的标准,使得用户使用方便,而且每一个操作都有提示,使得初次接触该程序的用户也能很快适应程序的操作。
这个程序操作简单,对于车牌号,只需输入车牌号上的数字就行,而且对于进出停车场的时间,也简化了操作,只需输入当时的时刻就行,没有具体到小时和分钟,但也许这也是该程序不足之处所在。而且该程序也给用户提供了选择的机会,当点击运行该程序后,如果想退出程序,可以输入n或N退出该运行程序。
至于使用过程中的更具体的问题,可以参照该报告书中调试分析那一项的屏幕截图,整个程序的运行界面大致就如上述屏幕截图的内容。
六、附录
1、源程序代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 2 /*车库容量*/
#define price 3 /*每车每时刻费用*/
typedef struct node
{ int num;
int reachtime;
int leavetime;
}CarNode; /*车辆信息结点*/
typedef struct NODE
{ CarNode *stack[MAX+1];
int top;
}SeqStackCar; /*模拟车站*/
typedef struct car
共 4 页 第 页 13
{ CarNode *data;
struct car *next;
}QueueNode;
typedef struct Node
{ QueueNode *head;
QueueNode *rear;
}LinkQueueCar; /*模拟通道*/
/*---------------------------------------------------------------------------*/ /*函数声明部分*/
void InitStack(SeqStackCar *); /*初始化栈*/
int InitQueue(LinkQueueCar *); /*初始化便道*/
int Arrival(SeqStackCar *,LinkQueueCar *); /*车辆到达*/
void Leave(SeqStackCar *,SeqStackCar *,LinkQueueCar *); /*车辆离开*/ void List(SeqStackCar,LinkQueueCar); /*显示存车信息*/
void PRINT(CarNode *p) ;
/*---------------------------------------------------------------------------*/ void option()
{ int i;
char choice;
gotoxy(0,0);
for(i=1;i<=240;i++)
system("graftabl 936");
printf("\004");
gotoxy(33,8);
printf("停车场管理系统");
gotoxy(25,12);
printf("小组成员: 周晓宇");
gotoxy(33,16);
printf("班级:ZK073032");
共 4 页 第 页 14
gotoxy(1,20);
printf("\n********************************************************************************");
printf("\t1.车辆到达--1 2.车辆离开--2 3.车辆信息--3 4.退出程序--4");
printf("\n********************************************************************************");
printf("\n\n\n\t 是否进入该系统(y/n)? "); choice=getchar();
if(choice=='N'||choice=='n')
exit(0);
}
void main()
{ SeqStackCar Enter,Temp;
LinkQueueCar Wait;
int ch;
system("graftabl 936");
option();
InitStack(&Enter); /*初始化车站*/
InitStack(&Temp); /*初始化让路的临时栈*/
InitQueue(&Wait); /*初始化通道*/
while(1)
{ clrscr();
printf("\n\n\t\t\t1. 车辆到达 请选择 1");
printf("\n\n\t\t\t2. 车辆离开 请选择 2");
printf("\n\n\t\t\t3. 车辆信息 请选择 3");
printf("\n\n\t\t\t4. 退出程序 请选择 4");
共 4 页 第 页 15
printf("\n\n\t\t\t现在请选择以上信息 : ");
while(1)
{ scanf("%d",&ch);
printf("\n");
if(ch>=1&&ch<=4)break;
else
printf("\n\t\t\t错误!请再次做出选择!\n\n\t\t\t"); }
switch(ch)
{ case 1:Arrival(&Enter,&Wait); break; /*车辆到达*/ case 2:Leave(&Enter,&Temp,&Wait); break; /*车辆离开*/
case 3:List(Enter,Wait); break; /*列表打印信息*/ case 4:exit(0); /*退出主程序*/
default: break; }
}
}
/*------------------------------------------------------------------------------*/
void InitStack(SeqStackCar *s) /*初始化栈*/
{ int i;
s->top=0;
for(i=0;i<=MAX;i++)
s->stack[s->top]=NULL;
}
int InitQueue(LinkQueueCar *Q) /*初始化便道*/
{ Q->head=(QueueNode *)malloc(sizeof(QueueNode));
if(Q->head!=NULL)
{ Q->head->next=NULL;
共 4 页 第 页 16
Q->rear=Q->head;
return(1);
}
else
return(-1);
}
void PRINT(CarNode *p) /*打印出站车的信息*/
{ int A1,A2;
printf("\n\t\t\t请输入离开时间: ");
scanf("%d",&(p->leavetime));
printf("\n\t\t\t离开车辆的车牌号: %d",p->num);
printf("\n\n\t\t\t离开车辆到达时间: %d ",p->reachtime); printf("\n\n\t\t\t离开车辆离开时间: %d",p->leavetime); A1=p->reachtime;
A2=p->leavetime;
printf("\n\n\t\t\t停车场管理费用: %d",(A2-A1)*price); free(p);
}
int Arrival(SeqStackCar *Enter,LinkQueueCar *W) /*车辆到达*/ { CarNode *p;
QueueNode *t;
p=(CarNode *)malloc(sizeof(CarNode));
flushall();
printf("\t\t\t请输入到达车辆车牌号: ");
scanf("%d",&(p->num));
if(Enter->top<MAX) /*车场未满,车进车场*/
{ Enter->top++;
printf("\n\t\t\t该车辆在停车场的位置是: %d\n",Enter->top);
共 4 页 第 页 17
printf("\n\t\t\t请输入该车辆到达的时间: ");
scanf("%d",&(p->reachtime));
Enter->stack[Enter->top]=p;
return(1);
}
else /*车场已满,车进便道*/
{ printf("\n\t\t\t停车场已满 该车辆需在便道上等待!"); getch();
t=(QueueNode *)malloc(sizeof(QueueNode));
t->data=p;
t->next=NULL;
W->rear->next=t;
W->rear=t;
return(1);
}
}
void Leave(SeqStackCar *Enter,SeqStackCar *Temp,LinkQueueCar *W) /*车辆离开*/
{ int i, room;
CarNode *p,*t;
QueueNode *q;
/*判断车场内是否有车*/
if(Enter->top>0) /*有车*/
{ while(1) /*输入离开车辆的信息*/
{ printf("\t\t\t停车场里停放的车辆总数: %d",Enter->top); printf("\n\n\t\t\t请输入要离开车辆的位置: "); scanf("%d",&room);
if(room>=1&&room<=Enter->top)
共 4 页 第 页 18
break;
}
while(Enter->top>room) /*车辆离开*/
{ Temp->top++;
Temp->stack[Temp->top]=Enter->stack[Enter->top];
Enter->stack[Enter->top]=NULL;
Enter->top--;
}
p=Enter->stack[Enter->top];
Enter->stack[Enter->top]=NULL;
Enter->top--;
while(Temp->top>=1)
{ Enter->top++;
Enter->stack[Enter->top]=Temp->stack[Temp->top]; Temp->stack[Temp->top]=NULL;
Temp->top--;
}
PRINT(p);
/*判断通道上是否有车及车站是否已满*/
if((W->head!=W->rear)&&Enter->top<MAX) /*便道的车辆进入车场*/ { q=W->head->next;
t=q->data;
Enter->top++;
printf("\n\n\t\t\t便道的%d号车进入车场第%d位置.",t->num,Enter->top);
printf("\n\n\t\t\t请输入现在的时间:");
scanf("%d",&(t->reachtime));
W->head->next=q->next;
共 4 页 第 页 19
if(q==W->rear)
W->rear=W->head;
Enter->stack[Enter->top]=t;
free(q);
}
else
printf("\n\n\t\t\t便道里没有车.\n"); }
else
printf("\n\n\t\t\t车场里没有车."); /*没车*/ }
void List1(SeqStackCar *S) /*列表显示车场信息*/
{ int i;
if(S->top>0) /*判断车站内是否有车*/
{ printf("\n\t\t\t车场:");
printf("\n\n\t\t\t位置 到达时间 车牌号\n"); for(i=1;i<=S->top;i++)
{ printf("%26d",i);
printf("%6d",S->stack[i]->reachtime); printf("%10d",S->stack[i]->num); printf("\n");
}
}
else
printf("\n\t\t\t车场里没有车");
}
void List2(LinkQueueCar *W) /*列表显示便道信息*/
{ QueueNode *p;
共 4 页 第 页 20
p=W->head->next;
if(W->head!=W->rear) /*判断通道上是否有车*/
{ printf("\n\t\t\t等待车辆的号码为: ");
while(p!=NULL)
{ printf("%-10d",p->data->num);
p=p->next;
}
printf("\n");
}
else
printf("\n\t\t\t便道里没有车.");
}
void List(SeqStackCar S,LinkQueueCar W)
{ int flag,tag;
flag=1;
while(flag)
{ printf("\n\t\t\t请选择 1|2|3:");
printf("\n\n\t\t\t1.车场\n\n\t\t\t2.便道\n\n\t\t\t3.返回\n\n\t\t\t");
while(1)
{ scanf("%d",&tag);
if(tag>=1||tag<=3) break;
else printf("\n\t\t\t请选择 1|2|3:"); }
switch(tag)
{ case 1:List1(&S);break; /*列表显示车场信息*/ case 2:List2(&W);break; /*列表显示便道信息*/ case 3:flag=0;break;
共 4 页 第 页 21
default: break;
}
}
}
2、设计体会
通过这一周的课程设计,加深了我对《数据结构》这门课程所学内容的进一步的理解与掌握;同时,通过对停车场管理系统的开发,使得我将计算机课程所学知识与实际问题很好地相联接在了一起。在这次课程设计中,不仅培养了我开发一个中小型程序的能力,而且也培养了我的团队合作能力。在这次对停车场管理系统的开发过程中,我们小组成员互相合作,互相帮助,其中有程序功能成功实现时的欣喜,也有遇到问题、解决问题时的执着以及迷茫。在这次课程设计中,使得我很好地了解了在开发程序过程中合作的重要性。
在这周课程设计中,我们小组所开发的停车场管理系统,基本上可以完成每一项功能。汽车进入停车场的信息、离开停车场的信息以及通道上的信息都可以在程序上一一实现。但是,该程序也有不足的地方。主要表现在车辆的车牌号上,现实中的车牌号是一串字符,可是,在这个程序中,为了简便起见,我们就车牌号定义为了整型,这个与现实是有些不符的。还有一个可以改进的地方就是记录车辆进入停车场以及离开停车场的时间,应该精确到小时以及分钟的,可是在程序中,为了简便起见,我们只是设置成了一个时刻,所以,在这方面还是有待改进的。改进的程序中,还应该增加时间的判断功能,即停车场内有可能有车辆停放的时间超过一天。
还有一个很重要的问题,对于停车场内可以停放的最多车辆数,为了测试数据的方便,我在程序中,定为了2,在实际使用中,可以改变程度开头的宏定义以增加停车场的容量。
总之,在这周的课程设计中,我以及我们这组的收获还是挺大的,不仅对于专
22 共 4 页 第 页
业课有了更好的认识,而且在合作的过程中更加了解了团队精神的重要性。
可供选择源程序代码:
一:
/*******************************停车场管理器*************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <conio.h>
/********************************************************************************/
#define MAXSTACKSIZE 2 /*车库容量*/
#define price 0.1 /*每车每分钟费用*/
共 4 页 第 页 23
typedef struct time{ int hour;
int min;
}Time; /*时间结点*/ typedef struct {
char num[10];
Time reach;
Time leave;
}CarNode; /*车辆信息结点*/ typedef struct {
CarNode *base;
CarNode *top;
int stacksize;
}SqStackCar; /*模拟车站*/ typedef struct car{ CarNode *data;
struct car *next;
}QueueNode;
typedef struct {
共 4 页 第 页 24
QueueNode *front;
QueueNode *rear;
}LinkQueueCar; /*模拟通道*/
int QueueEmpty(LinkQueueCar Q) /*便道判空函数*/ {
if(Q.front==Q.rear) return 1;
else return 0;
}
/********************************************************************************/
void InitStack(SqStackCar *s) /*初始化栈*/
{
s->base=(CarNode
*)malloc(MAXSTACKSIZE*sizeof(CarNode));
if(!s->base) exit(0);/*分配失败*/
s->top=s->base;
s->stacksize=MAXSTACKSIZE;
}
int Push(SqStackCar *s,CarNode *e) /*进站函数*/
共 4 页 第 页 25
{
if(s->top-s->base>=s->stacksize) return 0; else *s->top++=*e;
return 1;
}
int Pop(SqStackCar *s,CarNode *e) /*出站函数*/ {
if(s->top==s->base) return 0;
*e=*--s->top;
return 1;
}
int StackEmpty(SqStackCar s) /*判空函数*/ {
if(s.base==s.top) return 1;
else return 0;
}
int InitQueue(LinkQueueCar *Q) /*初始化便道*/ {
Q->front=Q->rear=(QueueNode
共 4 页 第 页 26
*)malloc(sizeof(QueueNode));
if(!Q->front) exit(0);
Q->front->next=NULL;
return 1;
}
/**************************************************************/
int EnQueue(LinkQueueCar *Q,CarNode *e) /*便道插入函数*/
{
QueueNode *p;
p=(QueueNode *)malloc(sizeof(QueueNode)); if(!p) exit(0);
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return 1;
}
共 4 页 第 页 27
int DeQueue(LinkQueueCar *Q,CarNode *e) /*便道删除函数*/
{
QueueNode *p;
if(Q->front==Q->rear) return 0;
p=Q->front->next;
e=p->data;
Q->front->next=p->next;
if(Q->rear==p) Q->rear=Q->front;
free(p);
return 1;
}
/********************************************************************************/
int Arrive(SqStackCar *In,LinkQueueCar *Wait) /*车辆到达函数*/
{
CarNode *i;
QueueNode *w;
共 4 页 第 页 28
i=(CarNode *)malloc(sizeof(CarNode));
flushall();
printf("Input the car number:");
gets(i->num);
if(In->top-In->base<MAXSTACKSIZE) /*车场未满,车辆进栈*/
{
printf("\nThe time the car arrive(00:00): ");
scanf("%d:%d",&i->reach.hour,&i->reach.min); Push(In,i);
printf("\nCar in success!!");
sleep(1);
return 1;
}
else /*停车场已满,车进便道*/
{
w=(QueueNode *)malloc(sizeof(QueueNode)); w->data=i;
w->next=NULL;
共 4 页 第 页 29
Wait->rear->next=w;
Wait->rear=w;
printf("The PART is full,car must wait in the road!"); sleep(1);
return 1;
}
return 0;
}
/********************************************************************************/
int Departure(SqStackCar *In,SqStackCar *temp,LinkQueueCar *Wait) /*车辆离开函数*/ {
int flag=0,a1,a2,b1,b2, money;
CarNode *p,*t;
QueueNode *q;
p=(CarNode *)malloc(sizeof(CarNode));
flushall();
printf("Input the out car number: ");
共 4 页 第 页 30
gets(p->num);
while(!StackEmpty(*In))
{
t=(CarNode *)malloc(sizeof(CarNode));
Pop(In,t);
if(strcmp(p->num,t->num)==0) /*比较车场中有无这辆车,有即出站*/
{
printf("Input the time the car out(00:00):");
scanf("%d:%d",&p->leave.hour,&p->leave.min);
printf("The ");
printf("%s",p->num);
printf(" Car out the part!");
a1= p->leave.hour;
a2= t->reach.hour;
b1= p->leave.min;
b2= t->reach.min;
money = ((a1-a2+24)%24*60+(b1-b2+60)%60)*price; /*计算车辆需要的费用*/
共 4 页 第 页 31
printf("\nThe time the arrive: %d:%d",t->reach.hour,t->reach.min); printf("\nThe time the leave: %d:%d",p->leave.hour,p->leave.min); printf("\nNeed: %d yuan",money); flag=1;
getch();
free(t);
break;
}
else
Push(temp,t);
} /*while*/
if(!flag)
{
printf("No this car!!");
getch();
}
共 4 页 第 页 car car 32
while(!StackEmpty(*temp))
{
Pop(temp,p);
Push(In,p);
}
free(p);
if(flag&&Wait->front!=Wait->rear) /*车站中有空位,便道有车,车入站*/
{
q=(QueueNode *)malloc(sizeof(QueueNode)); q=Wait->front->next;
t=q->data;
if(q!=NULL)
{
Push(In,t);
printf("\nThe ");
printf("%s",t->num);
printf(" car in part!");
printf("\nInput the time the car arrive(00:00): ");
共 4 页 第 页 33
scanf("%d:%d",&t->reach.hour,&t->leave.min); }
Wait->front->next=q->next;
if(q==Wait->rear) Wait->rear=Wait->front;
free(q);
}
return 1;
}
/********************************************************************************/
void Print(SqStackCar *In,SqStackCar *Temp,LinkQueueCar *Wait) /*打印函数*/
{
int c=0;
int count=1;
CarNode *p,*t;
QueueNode *q;
q=(QueueNode *)malloc(sizeof(QueueNode));
p=(CarNode *)malloc(sizeof(CarNode));
共 4 页 第 页 34
t=(CarNode *)malloc(sizeof(CarNode)); while(1&&c!='3')
{
clrscr();
gotoxy(1,10);
printf("1. Print the road!"); gotoxy(1,11);
printf("2. Print the part!"); gotoxy(1,12);
printf("3. return.");
do{
printf("\nInput your choice:"); c = getche();
printf("\n");
}while(c!='1'&&c!='2'&&c!='3'); if(c=='2') /*打印停车场*/ {
printf("The car in the part!\n"); count=1;
共 4 页 第 页 35
while(!StackEmpty(*In)) {
Pop(In,t);
Push(Temp,t);
}
while(!StackEmpty(*Temp)) {
Pop(Temp,t);
printf("The ");
printf("%d",count);
printf(" car number is: "); count++;
puts(t->num);
Push(In,t);
}
printf("Press any key to continue..."); getch();
}
if(c=='1') /*打印便道*/
共 4 页 第 页 36
{
printf("The car in the road!\n"); count=1;
q=Wait->front->next;
if(Wait->front!=Wait->rear) /**/ {
while(q!=NULL)
{
p=q->data;
printf("The ");
printf("%d",count);
printf(" Car number is: "); puts(p->num);
q=q->next;
count++;
}
}
else printf("\nNo car in the road."); printf("Press any key to continue...");
共 4 页 第 页 37
getch();
}
}
}
/***************************主程序***********************************************/ int Arrive(SqStackCar *In,LinkQueueCar *Wait);
int Departure(SqStackCar *In,SqStackCar *temp,LinkQueueCar *Wait);
void Print(SqStackCar *In,SqStackCar *temp,LinkQueueCar *Wait);
void initialization();
char readcommand();
void interpret(char cmd);
main()
{
char cmd;
SqStackCar part,temp;
LinkQueueCar road;
共 4 页 第 页 38
InitStack(&part);
InitStack(&temp);
InitQueue(&road);
printf("Press any key to continue......");
getch();
while(1)
{
initialization(); /*初始化界面*/
cmd = readcommand(); /*读取停车场状况*/
clrscr();
switch(cmd)
{
case 'a': Arrive(&part,&road); break;
case 'A': Arrive(&part,&road); break;
case 'd': Departure(&part,&temp,&road); break;
case 'D': Departure(&part,&temp,&road); break;
case 'p': Print(&part,&temp,&road); break;
case 'P': Print(&part,&temp,&road); break;
case 'e': printf("Press any to continue...");getch();exit(0);
共 4 页 第 页 39
break;
case 'E': printf("Press any to continue...");getch();exit(0); break;
default : printf("ERROR!"); break;
}
}
}
/********************************************************************************/
void initialization() /*初始函数*/
{
int i;
clrscr();
gotoxy(0,0);
for(i=1;i<=240;i++)
printf("\1");
gotoxy(15,8);
printf("THIS IS A CAR PART MANAGE SYSYTEM!");
共 4 页 第 页 40
gotoxy(15,12);
printf("NAME: GAOHE.");
gotoxy(15,13);
printf("NUM: 200812010205.");
gotoxy(15,14);
printf("GRADE: 2008.");
gotoxy(15,15);
printf("CLASS: INTERNET02");
gotoxy(1,20);
printf("\n********************************************************************************");
printf("1. Car Arrive--A 2. Car Departure--D 3. Print Car--P 4.Exit--E");
printf("\n********************************************************************************"); printf("Input C,D,P,E choose!!\n");
}
共 4 页 第 页 41
char readcommand() /*选择函数*/
{
char cmd;
do{
printf("Input your choice:");
cmd = getche();
printf("\n");
}while((cmd!='a')&&(cmd!='A')&&(cmd!='d')&&(cmd!='D')&&(cmd!='p')&&(cmd!='P')&&(cmd!='E')&&(cmd!='e'));
return cmd;
}
共 4 页 第 页 42
二
#include<iostream>
#include<fstream>
using namespace std;
#define MAX_SIZE 2//停车场能够容纳的车的数量 #define FARE 5//表示停车场的收费为每小时FARE元 int CountForStack=0;// 此全局变量用来计数堆栈现有的车数
int CountForQueue=0;// 此全局变量用来计数队列现有的车数
typedef struct//这个节点用来保存每辆车的信息
{ char Condition ;//用来表示“到达”或者“离开”的状态, int ArriveTime;//用来记录到达时间,默认为-1,说明还没有到达
int LeaveTime;// 用来记录离开时间,默认为-1,说明还没有离开
int License;// 记录车牌号
}CarNode;
typedef struct //栈的定义
共 4 页 第 页 43
{ CarNode *base;//栈底指针,指向0
CarNode *top;//栈顶指针,如果指向0,说明栈为空 int stacksize;//栈的容量大小
}CarStack;
typedef struct QNode//队列节点的定义
{ char Condition ;//用来表示“到达”或者“离开”的状态,
int ArriveTime;//用来记录到达时间,默认为-1,说明还没有到达
int LeaveTime;// 用来记录离开时间,默认为-1,说明还没有离开
int License;// 记录车牌号
QNode *next;//指向下一个节点的指针
}QNode;
typedef struct// 队列的定义
{ QNode *front;//队头指针
QNode *rear;//队尾指针
}Queue;
bool InitStack(CarStack &S)//此函数用来初始化栈
共 4 页 第 页 44
{ S.base =(CarNode *)malloc(MAX_SIZE*sizeof(CarNode));
if(!S.base )
{ cout<<"内存分配失败!"<<endl;
return false;//说明内存分配失败,返回false } S.top =S.base ;
S.stacksize =MAX_SIZE;
return true;}
bool InitQueue(Queue &Q)//此函数用来初始化队列 { Q.front =(QNode *)malloc(sizeof(QNode));
if(!Q.front )
{ cout<<"内存分配失败!"<<endl;
return false;}
Q.rear =Q.front ;
Q.front ->next =0;//下一个节点指空
return true;}
bool EnQueue(Queue &Q,QNode &qnode)//此函数用来入队一个节点
{ QNode *p=(QNode *)malloc(sizeof(QNode));
共 4 页 第 页 45
if(!p)
{ cout<<"内存分配失败!"<<endl;
return false;}
p->ArriveTime =qnode.ArriveTime ;
p->Condition =qnode.Condition ;
p->LeaveTime =qnode.LeaveTime ;
p->License =qnode.License ;
p->next =0;
Q.rear ->next =p; Q.rear =p;
return true;}
bool DeQueue(Queue &Q,QNode &t)//此函数用来出队{ if(Q.front ==Q.rear )
{ cout<<"队列为空!"<<endl;
return false;}
QNode *p=Q.front->next ;
t.ArriveTime =p->ArriveTime ;
t.Condition =p->Condition ;
t.LeaveTime =p->LeaveTime ;
t.License =p->License ;
共 4 页 第 页 46
Q.front ->next =p->next ;
if(Q.rear ==p)//如果P是指向最后一个出队的元素 Q.rear =Q.front ;
free(p);
return true;}
void InitCarNode(CarNode &C,char condition,int arrivetime,int leavetime,int license)//本函数用来初始化一个CarNode 节点
{ C.ArriveTime =arrivetime;
C.Condition =condition;
C.LeaveTime =leavetime;
C.License=license;}
bool Push(CarStack &S,CarNode &car)//此函数用来入栈一个CarNode 节点
{ if(S.top -S.base >=S.stacksize )
{ cout<<"此栈已满,不能压入新的信息"<<endl; return false;}
(*S.top ).ArriveTime =car.ArriveTime ;
(*S.top ).Condition =car.Condition ;
共 4 页 第 页 47
(*S.top ).LeaveTime =car.LeaveTime ;
(*S.top ).License =car.License ;
++S.top ;//栈顶指针上移
return true;}
bool Pop(CarStack &S,CarNode &t)//此函数用来弹出栈内元素
{ if(S.top ==S.base )
{ cout<<"栈空,不能执行出栈操作!"<<endl; return false;}
--S.top ;//栈顶指针下移
t.ArriveTime =(*S.top ).ArriveTime ;
t.Condition =(*S.top ).Condition ;
t.LeaveTime =(*S.top ).LeaveTime ;
t.License =(*S.top ).License ;
return true;}
bool IsStackFull(CarStack &S)//此函数用来判断堆栈是否已满
{ if(S.top -S.base >=S.stacksize )
return true;
共 4 页 第 页 48
else
return false;}
bool IsStackEmputy(CarStack &S)//此函数用来判断堆栈是否为空
{ if(S.top ==S.base )
return true;
else
return false;}
bool IsQueueEmputy(Queue &Q)//此函数用来判断队列是否为空
{ if(Q.front ==Q.rear )
return true;
else
return false;}
bool SearchInStack(CarStack&S,int a)
{ bool tag=false;
if(!IsStackEmputy(S))
{ CarNode *p=S.top-1 ;
while(p!=S.base )
共 4 页 第 页 49
{ if((*p).License ==a)
tag=true;
--p;}
if((*p).License ==a)
tag=true;}
return tag;}
bool SearchInQueue(Queue &Q,int a)//a表示要查找的车牌号,
{ bool tag=false;
if(!IsQueueEmputy(Q))//如果队列非空
{ QNode *p=Q.front->next ;
while(p!=Q.rear)
{ if((*p).License ==a) tag=true;
}//退出此while循环时p指向最后一个元素
if((*p).License ==a) tag=true;}
return tag;}
void InCar(CarStack &S,Queue &Q,int a1,int a2)//此函数用来表示进入车辆, 参数a1用来表示到达时间,参数a2表示车牌号码
共 4 页 第 页 50
{ if(SearchInStack(S,a2))
{ cout<<"车号"<<a2<<"已经存在于停车场内,输入有误"<<endl;
return ; }
if(SearchInQueue(Q,a2))
{ cout<<"车号"<<a2<<"已经存在于通道内,输入有误"<<endl;
return ; }
if(IsStackFull(S))//如果堆栈已满,说明停车场已满,需要停车在通道
{ QNode qnode;
qnode.ArriveTime =-1;//在通道时间不收费,所以不计时 qnode.Condition ='A';
qnode.LeaveTime =-1;//定义为-1,说明还没有开始离开 qnode.License =a2;
EnQueue(Q,qnode);//停在通道上
++CountForQueue;
cout<<"车号:"<<qnode.License <<"停在通道的第"<<CountForQueue<<"号位置"<<endl;}
共 4 页 第 页 51
else
{ CarNode carnode;
carnode.ArriveTime =a1;
carnode.Condition ='A';
carnode.LeaveTime =-1;
carnode.License =a2;
Push(S,carnode);
++CountForStack;
cout<<"车号:"<<carnode.License <<"到达时间 "<<carnode.ArriveTime <<"停在停车场的第"<<CountForStack<<"号位置"<<endl;} }
void OutCar(CarStack &S,Queue &Q,int a1,int a2)//此函数用来出车,参数a1用来表示离开时间,参数a2表示车牌号码
{ if(SearchInQueue(Q,a2))
{ cout<<"车号"<<a2<<"存在于通道内,还未进入停车场,不能离开"<<endl;
return ;}
if(!SearchInStack(S,a2))
共 4 页 第 页 52
{ cout<<"车号"<<a2<<"不在停车场内,输入有误"<<endl; return ; }
CarStack tempstack;
InitStack(tempstack);//建立并且初始化用于暂存出车时让车的堆栈
bool tag1=false;//标志这个停车场出车以前是否已满,默认为没有满
tag1=IsStackFull(S);
bool tag2=true;//标志通道是否有汽车在等待, 默认为通道为空
tag2=IsQueueEmputy(Q);
CarNode temp;//用来保存暂时取出的汽车
bool tag3=false;//用来标志是否是离开时间小于到达时间而导致离开失败,true表示离开失败
while(1)//让车离开
{ Pop(S,temp);
if(temp.License ==a2)
{ if(a1<temp.ArriveTime )
{ cout<<"输入有误,离开时间不能小于到达时间,离开
共 4 页 第 页 53
失败"<<endl;
tag3=true;
Push(tempstack,temp);}
else
cout<<"车号"<<a2<<"现在离开停车场,所用的时间为"<<a1-temp.ArriveTime <<"收费为"<<(a1-temp.ArriveTime )*FARE<<endl;
break; }
else
Push(tempstack ,temp);//进入暂存栈}
while(!IsStackEmputy(tempstack))//倒出的车再次进入停车场
{ Pop(tempstack,temp);
Push(S,temp);}
QNode tempqnode;//用来暂时保存从通道出来的汽车 if(tag1==true&&tag2==false&&tag3==false)
{ DeQueue(Q,tempqnode);
--CountForQueue;
temp.ArriveTime =a1 ;
共 4 页 第 页 54
temp.Condition =tempqnode.Condition ;
temp.LeaveTime =tempqnode.LeaveTime ;
temp.License =tempqnode.License ;
Push(S,temp); }
if(tag2==true&&tag3==false)// 如果停车场没有满,并且离开成功
--CountForStack; }
void showmenu(CarStack &S,Queue &Q)
{ cout<<"******************************选择菜单*******************************"<<endl;
cout<<" 1: 停车"<<endl;
cout<<" 2: 离开"<<endl;
cout<<" 3: 退出"<<endl;
cout<<"*****************************请按键选择******************************"<<endl;
int tag; cin>>tag;
while(tag!=1&&tag!=2&&tag!=3) cin>>tag;
int a1;
unsigned int a2;
共 4 页 第 页 55
switch(tag)
{ case 1:
cout<<"请输入到达的车号"<<endl; cin>>a1; cout<<"请输入到达时间"<<endl; cin>>a2; InCar(S,Q,a2,a1); break;
case 2:
cout<<"请输入离开的车号"<<endl; cin>>a1; cout<<"请输入离开的时间"<<endl; cin>>a2; OutCar(S,Q,a2,a1); break;
case 3:
return ; break; }
char ch;
cout<<"*******************按E/e退出,按任意键返回菜单**************************"<<endl;
cin>>ch;
if(ch!='E'&&ch!='e')
showmenu(S,Q); }
int main()
{ CarStack carstack;
共 4 页 第 页 56
InitStack(carstack);// 建立并且初始化用于停车场的堆栈 Queue carqueue;
InitQueue(carqueue);//建立并且初始化用于通道的队列 showmenu(carstack,carqueue);}
三:
#include<iostream>
#include<string>
using namespace std;
共 4 页 第 页 57
#define MAX 2 //停车场车位数
#define price 0.05 //每分钟收取的费用
typedef struct time
{
int hour;
int min;
}Time;//定义时间结点
typedef struct node
{
string num;
Time reach;
Time leave;
}CarNode;//定义每辆车的牌号,进入时刻,开出时刻typedef struct NODE
{
CarNode *stack[MAX+1];
int top;
}SeqStackCar;//用栈定义,构造停车场
typedef struct car
共 4 页 第 页 58
{
CarNode *data; struct car *next;
}QueueNode;//用队列结点定义,构造停车场外的单个等候车辆
typedef struct Node
{
QueueNode *head; QueueNode *rear;
}LinkQueueCar;//用队列定义,构造停车场外的等候便道
void InitStack(SeqStackCar *); //初始化堆栈函数声明
int InitQueue(LinkQueueCar *); //初始化队列头结点函数声明
int Arrival(SeqStackCar *,LinkQueueCar *); //车辆进入时登记函数声明
void Leave(SeqStackCar *,SeqStackCar *,LinkQueueCar *);//车辆离开时函数声明
void List(SeqStackCar,LinkQueueCar); /*查看停车场内车辆
共 4 页 第 页 59
情况及
便道上车辆情况的函数声明*/
void main( )
{
SeqStackCar Enter,Temp;//初始栈,中转栈 LinkQueueCar Wait;//便道队列 int a; InitStack(&Enter); InitStack(&Temp); InitQueue(&Wait);//初始化
while(1)
{
cout<<"********************************************************************"<<endl;
cout<<" 欢迎光临停车场!"; cout<<"(*^__^*) !"; cout<<endl<<"车辆到达登记->请按 1"; cout<<" 车辆离开登记->请按 2"<<endl; cout<<"车辆停靠查询->请按 3";
共 4 页 第 页 60
置 cout<<" 退出系统->请按 4"<<endl;//系统选项设
while(1)
{
cin>>a;
if(a>=1&&a<=4) break;
else cout<<endl<<"请选择: 1~4.";
}
switch(a)
{
case 1:Arrival(&Enter,&Wait);break;//调用入停车场函数
case 2:Leave(&Enter,&Temp,&Wait);break;//调用出停车场函数
case 3:List(Enter,Wait);break;//调用查看函数 case 4:exit(0); default: break;
}
}
共 4 页 第 页 61
}
void InitStack(SeqStackCar *s) //堆栈初始化
{
s->top=0;
s->stack[s->top]=NULL;
}
int InitQueue(LinkQueueCar *Q)//队列初始化
{
Q->head=new QueueNode ;
if(Q->head!=NULL)
{
Q->head->next=NULL;
Q->rear=Q->head;
return 1;
}
else return -1;
}
void PRINT(CarNode *p,int room) //输出离开停车场的车辆情况
共 4 页 第 页 62
{
int A,B,C,D; cout<<"\n车辆离开的时间:"; cin>>p->leave.hour>>p->leave.min; cout<<"离开车辆的车牌号为:"; cout<<p->num; cout<<endl<<"其到达时间为: "<<p->reach.hour<<":"<<p->reach.min;
cout<<"离开时间为: "<<p->leave.hour<<":"<<p->leave.min;
A=p->reach.hour; B=p->reach.min; C=p->leave.hour; D=p->leave.min; cout<<endl<<"应交费用为: "<<((C-A)*60+(D-B))*price<<"元"<<endl;
cout<<"车辆离开登记完毕!"<<endl; cout<<"*********************************************************************"<<endl;
共 4 页 第 页 63
} delete p;
int Arrival(SeqStackCar *Enter,LinkQueueCar *W) {
CarNode *p; QueueNode *t; p=new CarNode; cout<<"********************************************************************"<<endl;
cout<<"车辆到达登记开始:"<<endl; cout<<endl<<"请输入车牌号:"; cin>>p->num; if(Enter->top<MAX)//如果车位未满则进停车场内 {
Enter->top++;
cout<<endl<<"车辆在车场第"<<Enter->top<<"位置."; cout<<endl<<"车辆到达时间:";
cin>>p->reach.hour>>p->reach.min;
cout<<endl<<"车辆到达登记完毕!"<<endl;
共 4 页 第 页 64
cout<<"*********************************************************************"<<endl;
Enter->stack[Enter->top]=p;
return 1;
}
else //如果车位已满,则停靠在便道上
{
cout<<"*********************************************************************"<<endl;
cout<<endl<<"该车须在便道等待!有车位时进入车场"<<endl;
t=new QueueNode ;
t->data=p;
t->next=NULL;
W->rear->next=t;
W->rear=t;
return 1;
}
}
共 4 页 第 页 65
void Leave(SeqStackCar *Enter,SeqStackCar *Temp,LinkQueueCar *W)
{
if(Enter->top>0)
{
while(1)
{
cout<<"****************************************int room; CarNode *p,*t; QueueNode *q; *****************************"<<endl;
cout<<"车辆离开登记开始:"<<endl;
cout<<endl<<"请输入车在车场的位置/1--"<<Enter->top<<"/:";
cin>>room;
}
共 4 页 第 页 66 if(room>=1&&room<=Enter->top) break;
while(Enter->top>room)//从停车场堆栈向中转堆栈移动车辆,直到要离开车辆的位置停止
{
Temp->top++;
Temp->stack[Temp->top]=Enter->stack[Enter->top]; Enter->stack[Enter->top]=NULL;
Enter->top--;
}
p=Enter->stack[Enter->top];
Enter->stack[Enter->top]=NULL;
Enter->top--;
while(Temp->top>=1)//将中转堆栈中的车辆移回停车场堆栈
{
Enter->top++;
Enter->stack[Enter->top]=Temp->stack[Temp->top]; Temp->stack[Temp->top]=NULL;
Temp->top--;
}
共 4 页 第 页 67
cout<<"*********************************************************************"<<endl;
cout<<"车辆离开登记结算:"<<endl;
PRINT(p,room);
if((W->head!=W->rear)&&Enter->top<MAX)
{
q=W->head->next;
t=q->data;
Enter->top++;
cout<<endl<<"便道的"<<t->num<<"号车进入车场第"; cout<<Enter->top<<"位置."<<endl;
cout<<"请输入"<<t->num<<"号车进入车场的时间:"; cin>>t->reach.hour>>t->reach.min;
W->head->next=q->next;
if(q==W->rear) W->rear=W->head;
Enter->stack[Enter->top]=t;
delete q;
}
else cout<<endl<<"便道里没有车"<<endl;
共 4 页 第 页 68
}
else cout<<endl<<"车场里没有车."<<endl;
}
void List1(SeqStackCar *S) //查看停车场内车辆情况的函数定义
{
int i;
if(S->top>0)
{
cout<<"********************************************************************"<<endl;
{
cout<<i; cout<<" cout<<endl<<"车场内部车辆停靠情况:"<<endl; cout<<endl<<"位置 到达时间 车牌号"<<endl; for(i=1;i<=S->top;i++)
"<<S->stack[i]->reach.hour<<":"<<S->stack[i]->reach.min; cout<<" "<<S->stack[i]->num<<endl;
共 4 页 第 页 69
}
}
else cout<<endl<<"车场里没有车"<<endl;
cout<<"********************************************************************"<<endl;
}
void List2(LinkQueueCar *W) //查看便道上停靠车辆情况的函数定义
{
{
cout<<"****************************************QueueNode *p; p=W->head->next; if(W->head!=W->rear) ****************************"<<endl;
cout<<endl<<"便道停靠车辆情况:"<<endl;
while(p!=NULL)
{
共 4 页 第 页 70
}
}
cout<<endl<<"车辆牌号:"; cout<<p->data->num<<endl; p=p->next; else cout<<endl<<"便道里没有车."<<endl; cout<<"********************************************************************"<<endl;
}
void List(SeqStackCar S,LinkQueueCar W) //车辆列表显示函数
{
int flag,tag; flag=1;
while(flag)
{
cout<<"********************************************************************"<<endl;
cout<<"车辆停靠查询开始:"<<endl;
共 4 页 第 页 71
cout<<endl<<"请选择 1|2|3:"<<endl; cout<<"1.车场列表"<<endl<<"2.便道列表"<<endl<<"3.返回主菜单"<<endl;
while(1)
{
cin>>tag;
if(tag>=1||tag<=3) break;
else cout<<endl;
cout<<"请选择 1~3:";
}
switch(tag)
{
case 1:List1(&S);
cout<<"车辆停靠查询结束!"<<endl;break; case 2:List2(&W);
cout<<"车辆停靠查询结束!"<<endl;break; case 3:flag=0;break;
default: break;
}
共 4 页 第 页 72
}
cout<<"********************************************************************"<<endl;
}
四:
void InitStack(SqStackCar *s) /*初始化栈*/
{
s->base=(CarNode
*)malloc(MAXSTACKSIZE*sizeof(CarNode));
if(!s->base) exit(0);/*分配失败*/
s->top=s->base;
s->stacksize=MAXSTACKSIZE;
}
int Push(SqStackCar *s,CarNode *e) /*进站函数*/
共 4 页 第 页 73
{
if(s->top-s->base>=s->stacksize) return 0; else *s->top++=*e;
return 1;
}
int Pop(SqStackCar *s,CarNode *e) /*出站函数*/ {
if(s->top==s->base) return 0;
*e=*--s->top;
return 1;
}
int StackEmpty(SqStackCar s) /*判空函数*/ {
if(s.base==s.top) return 1;
else return 0;
}
int InitQueue(LinkQueueCar *Q) /*初始化便道*/ {
Q->front=Q->rear=(QueueNode
共 4 页 第 页 74
*)malloc(sizeof(QueueNode));
if(!Q->front) exit(0);
Q->front->next=NULL;
return 1;
}
/**************************************************************/
int EnQueue(LinkQueueCar *Q,CarNode *e) /*便道插入函数*/
{
QueueNode *p;
p=(QueueNode *)malloc(sizeof(QueueNode)); if(!p) exit(0);
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return 1;
}
共 4 页 第 页 75
int DeQueue(LinkQueueCar *Q,CarNode *e) /*便道删除函数*/
{
QueueNode *p;
if(Q->front==Q->rear) return 0;
p=Q->front->next;
e=p->data;
Q->front->next=p->next;
if(Q->rear==p) Q->rear=Q->front;
free(p);
return 1;
}
/********************************************************************************/
int Arrive(SqStackCar *In,LinkQueueCar *Wait) /*车辆到达函数*/
{
CarNode *i;
QueueNode *w;
共 4 页 第 页 76
i=(CarNode *)malloc(sizeof(CarNode));
flushall();
printf("Input the car number:");
gets(i->num);
六、源代码
/*******************************停车场管理器*************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <conio.h>
/********************************************************************************/
#define MAXSTACKSIZE 2 /*车库容量*/
#define price 0.1 /*每车每分钟费用*/
typedef struct time{
int hour;
共 4 页 第 页 77
int min;
}Time; /*时间结点*/ typedef struct {
char num[10];
Time reach;
Time leave;
}CarNode; /*车辆信息结点*/ typedef struct {
CarNode *base;
CarNode *top;
int stacksize;
}SqStackCar; /*模拟车站*/ typedef struct car{ CarNode *data;
struct car *next;
}QueueNode;
typedef struct {
QueueNode *front; QueueNode *rear;
共 4 页 第 页 78
}LinkQueueCar; /*模拟通道*/
int QueueEmpty(LinkQueueCar Q) /*便道判空函数*/ {
if(Q.front==Q.rear) return 1;
else return 0;
}
/********************************************************************************/
void InitStack(SqStackCar *s) /*初始化栈*/
{
s->base=(CarNode
*)malloc(MAXSTACKSIZE*sizeof(CarNode));
if(!s->base) exit(0);/*分配失败*/
s->top=s->base;
s->stacksize=MAXSTACKSIZE;
}
int Push(SqStackCar *s,CarNode *e) /*进站函数*/ {
if(s->top-s->base>=s->stacksize) return 0;
共 4 页 第 页 79
else *s->top++=*e;
return 1;
}
int Pop(SqStackCar *s,CarNode *e) /*出站函数*/ {
if(s->top==s->base) return 0;
*e=*--s->top;
return 1;
}
int StackEmpty(SqStackCar s) /*判空函数*/ {
if(s.base==s.top) return 1;
else return 0;
}
int InitQueue(LinkQueueCar *Q) /*初始化便道*/ {
Q->front=Q->rear=(QueueNode
*)malloc(sizeof(QueueNode));
if(!Q->front) exit(0);
共 4 页 第 页 80
Q->front->next=NULL;
return 1;
}
/**************************************************************/
int EnQueue(LinkQueueCar *Q,CarNode *e) /*便道插入函数*/
{
QueueNode *p;
p=(QueueNode *)malloc(sizeof(QueueNode)); if(!p) exit(0);
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return 1;
}
int DeQueue(LinkQueueCar *Q,CarNode *e) /*便道删除函数*/
共 4 页 第 页 81
{
QueueNode *p;
if(Q->front==Q->rear) return 0;
p=Q->front->next;
e=p->data;
Q->front->next=p->next;
if(Q->rear==p) Q->rear=Q->front;
free(p);
return 1;
}
/********************************************************************************/
int Arrive(SqStackCar *In,LinkQueueCar *Wait) /*车辆到达函数*/
{
CarNode *i;
QueueNode *w;
i=(CarNode *)malloc(sizeof(CarNode));
flushall();
共 4 页 第 页 82
printf("Input the car number:");
gets(i->num);
if(In->top-In->base<MAXSTACKSIZE) /*车场未满,车辆进栈*/
{
printf("\nThe time the car arrive(00:00): ");
scanf("%d:%d",&i->reach.hour,&i->reach.min); Push(In,i);
printf("\nCar in success!!");
sleep(1);
return 1;
}
else /*停车场已满,车进便道*/
{
w=(QueueNode *)malloc(sizeof(QueueNode)); w->data=i;
w->next=NULL;
Wait->rear->next=w;
Wait->rear=w;
共 4 页 第 页 83
printf("The PART is full,car must wait in the road!"); sleep(1);
return 1;
}
return 0;
}
/********************************************************************************/
int Departure(SqStackCar *In,SqStackCar *temp,LinkQueueCar *Wait) /*车辆离开函数*/ {
int flag=0,a1,a2,b1,b2, money;
CarNode *p,*t;
QueueNode *q;
p=(CarNode *)malloc(sizeof(CarNode));
flushall();
printf("Input the out car number: ");
gets(p->num);
while(!StackEmpty(*In))
共 4 页 第 页 84
{
t=(CarNode *)malloc(sizeof(CarNode));
Pop(In,t);
if(strcmp(p->num,t->num)==0) /*比较车场中有无这辆车,有即出站*/
{
printf("Input the time the car out(00:00):");
scanf("%d:%d",&p->leave.hour,&p->leave.min);
printf("The ");
printf("%s",p->num);
printf(" Car out the part!");
a1= p->leave.hour;
a2= t->reach.hour;
b1= p->leave.min;
b2= t->reach.min;
money = ((a1-a2+24)%24*60+(b1-b2+60)%60)*price; /*计算车辆需要的费用*/
printf("\nThe time the car arrive: %d:%d",t->reach.hour,t->reach.min);
共 4 页 第 页 85
printf("\nThe time the leave: %d:%d",p->leave.hour,p->leave.min); printf("\nNeed: %d yuan",money); flag=1;
getch();
free(t);
break;
}
else
Push(temp,t);
} /*while*/
if(!flag)
{
printf("No this car!!");
getch();
}
while(!StackEmpty(*temp))
{
共 4 页 第 页 car 86
Pop(temp,p);
Push(In,p);
}
free(p);
if(flag&&Wait->front!=Wait->rear) /*车站中有空位,便道有车,车入站*/
{
q=(QueueNode *)malloc(sizeof(QueueNode)); q=Wait->front->next;
t=q->data;
if(q!=NULL)
{
Push(In,t);
printf("\nThe ");
printf("%s",t->num);
printf(" car in part!");
printf("\nInput the time the car arrive(00:00): "); scanf("%d:%d",&t->reach.hour,&t->leave.min); }
共 4 页 第 页 87
Wait->front->next=q->next;
if(q==Wait->rear) Wait->rear=Wait->front;
free(q);
}
return 1;
}
/********************************************************************************/
void Print(SqStackCar *In,SqStackCar *Temp,LinkQueueCar *Wait) /*打印函数*/
{
int c=0;
int count=1;
CarNode *p,*t;
QueueNode *q;
q=(QueueNode *)malloc(sizeof(QueueNode));
p=(CarNode *)malloc(sizeof(CarNode));
t=(CarNode *)malloc(sizeof(CarNode));
while(1&&c!='3')
共 4 页 第 页 88
{
clrscr();
gotoxy(1,10);
printf("1. Print the road!"); gotoxy(1,11);
printf("2. Print the part!"); gotoxy(1,12);
printf("3. return.");
do{
printf("\nInput your choice:"); c = getche();
printf("\n");
}while(c!='1'&&c!='2'&&c!='3'); if(c=='2') /*打印停车场*/ {
printf("The car in the part!\n"); count=1;
while(!StackEmpty(*In)) {
共 4 页 第 页 89
Pop(In,t);
Push(Temp,t);
}
while(!StackEmpty(*Temp)) {
Pop(Temp,t);
printf("The ");
printf("%d",count);
printf(" car number is: "); count++;
puts(t->num);
Push(In,t);
}
printf("Press any key to continue..."); getch();
}
if(c=='1') /*打印便道*/
{
printf("The car in the road!\n");
共 4 页 第 页 90
count=1;
q=Wait->front->next;
if(Wait->front!=Wait->rear) /**/ {
while(q!=NULL)
{
p=q->data;
printf("The ");
printf("%d",count);
printf(" Car number is: "); puts(p->num);
q=q->next;
count++;
}
}
else printf("\nNo car in the road."); printf("Press any key to continue..."); getch();
}
共 4 页 第 页 91
}
}
/***************************主程序***********************************************/ int Arrive(SqStackCar *In,LinkQueueCar *Wait);
int Departure(SqStackCar *In,SqStackCar *temp,LinkQueueCar *Wait);
void Print(SqStackCar *In,SqStackCar *temp,LinkQueueCar *Wait);
void initialization();
char readcommand();
void interpret(char cmd);
main()
{
char cmd;
SqStackCar part,temp;
LinkQueueCar road;
InitStack(&part);
InitStack(&temp);
共 4 页 第 页 92
InitQueue(&road);
printf("Press any key to continue......");
getch();
while(1)
{
initialization(); /*初始化界面*/
cmd = readcommand(); /*读取停车场状况*/
clrscr();
switch(cmd)
{
case 'a': Arrive(&part,&road); break;
case 'A': Arrive(&part,&road); break;
case 'd': Departure(&part,&temp,&road); break;
case 'D': Departure(&part,&temp,&road); break;
case 'p': Print(&part,&temp,&road); break;
case 'P': Print(&part,&temp,&road); break;
case 'e': printf("Press any to continue...");getch();exit(0); break;
case 'E': printf("Press any to continue...");getch();exit(0);
共 4 页 第 页 93
break;
default : printf("ERROR!"); break; }
}
}
/********************************************************************************/
void initialization() /*初始函数*/
{
int i;
clrscr();
gotoxy(0,0);
for(i=1;i<=240;i++)
printf("\1");
gotoxy(15,8);
printf("THIS IS A CAR PART MANAGE SYSYTEM!");
gotoxy(15,12);
printf("NAME: LIYONGJUN.");
共 4 页 第 页 94
gotoxy(15,13);
printf("NUM: 3104006893.");
gotoxy(15,14);
printf("GRADE: 2004.");
gotoxy(15,15);
printf("CLASS: COMPUTER SCIENCE AND TECHNOLOGY 10");
gotoxy(1,20);
printf("\n********************************************************************************");
printf("1. Car Arrive--A 2. Car Departure--D 3. Print Car--P 4.Exit--E");
printf("\n********************************************************************************"); printf("Input C,D,P,E choose!!\n");
}
char readcommand() /*选择函数*/
{ char cmd;
共 4 页 第 页 95
do{
printf("Input your choice:");
cmd = getche();
printf("\n");
}while((cmd!='a')&&(cmd!='A')&&(cmd!='d')&&(cmd!='D')&&(cmd!='p')&&(cmd!='P')&&(cmd!='E')&&(cmd!='e'));
return cmd;
}
五:
#include<stdio.h>
#define Size 3 /*车库容量*/
共 4 页 第 页 96
#define Price 5 /*单位价格*/
#define Null 0
typedef struct time
{
int hour; /*时间结点*/
}Time;
typedef struct
{
int car_number;
Time arrivetime,leavetime; /*车信息结点*/ int fee;
}car_info;
typedef struct
{
car_info *north;
car_info *south; /*停车场信息*/ int number;
}car_park;
共 4 页 第 页 97
typedef struct
{
car_info *west;
car_info *east; /*倒车场信息*/ int number;
}car_park_back;
Typedef struct car
{
car_info *data;
struct car *next; /*结点信息*/ }carnode;
typedef struct node
{
carnode *head;
carnode *rear; /*便道信息*/
共 4 页 第 页 98
int number;
}car_park_temp;
void init_car_park(car_park *cp)
{
cp->north=(car_info *)malloc(Size * sizeof(car_info)); /*初始化停车场*/
if(!cp->north) printf("error\n");
cp->south=cp->north; /*令栈顶等于栈底*/ cp->number=0;
}
void enter_car_park(car_park *cp,car_info *car)
{
*cp->south++=*car; /*车辆进入停车场*/ cp->number++;
}
int notfull_car_park(car_park *cp)
{
int e;
if(cp->south-cp->north>=Size) /*判断常常是否已满
共 4 页 第 页 99
*/
e=0;
else
e=1;
return(e);
}
int notempty_car_park_back(car_park_back *cpb) {
int e;
if(cpb->east==cpb->west)
e=0; /*判断倒车场是否不空*/
else
e=1;
return(e);
}
void back_car_park(car_park *cp,car_info *car)
{
*car=*cp->south; /*进行倒车操作*/
共 4 页 第 页 100
cp->number--;
}
void init_car_park_back(car_park_back *cpb)
{
cpb->west=(car_info *)malloc(Size *sizeof(car_info)); if(!cpb->west) printf("error\n"); /*倒车场初始化*/ cpb->east=cpb->west;
cpb->number=0;
}
void enter_car_park_back(car_park_back *cpb,car_info *car) {
*cpb->east++=*car;
cpb->number++; /*进入倒车场操作*/ }
void leave_car_park_back(car_park_back *cpb,car_info *car) {
*car=*--cpb->east;
cpb->number--; /*离开倒车场操作*/
共 4 页 第 页 101
}
void init_car_park_temp(car_park_temp *cpt)
{
cpt->head=cpt->rear=(carnode
/*初始化便道*/
cpt->head->next=Null;
cpt->number=0;
}
void enter_car_park_temp(car_park_temp *cpt,car_info *car) {
carnode *p;
p=(carnode *)malloc(sizeof(carnode)); /*进入便道操作*/
p->data=car;
p->next=Null;
cpt->rear->next=p;
cpt->rear=p;
cpt->number++;
}
共 4 页 第 页 102 *)malloc(sizeof(carnode));
void leave_car_park_temp(car_park_temp *cpt,car_info *car,car_park *cp)
{
carnode *p;
p=cpt->head->next;
car=p->data; /*离开便道操作*/ cpt->head->next=p->next;
enter_car_park(cp, car); /*进入停车场操作*/ cpt->number--;
}
int notempty_car_park_temp(car_park_temp *cpt) {
int e;
if(cpt->head==cpt->rear) /*判断便道是否为空*/ e=0;
else
e=1;
return(e);
共 4 页 第 页 103
}
void leave_car_park(car_park *cp,car_info *car,car_park_back *cpb)
{
int e, a1,b1,t; /*定义时间变量*/
car_info *car1,*car2;
car1=(car_info *)malloc(sizeof(car_info)); /*车辆实体化*/
car2=(car_info *)malloc(sizeof(car_info));
while((--cp->south)->car_number!=car->car_number) /*判断车号是否为要出去的车号*/
{
back_car_park(cp,car1); /*进行倒车操作*/ enter_car_park_back(cpb,car1); /*进入倒车场*/
}
car->arrivetime.hour=cp->south->arrivetime.hour; a1=car->arrivetime.hour;
b1=car->leavetime.hour;
共 4 页 第 页 104
t=(b1-a1);
car->fee=t*Price; /*计算价格*/ printf("the time of the car is %3d hour\n",t); printf("the money is %3d yuan\n",car->fee); e=notempty_car_park_back(cpb); 是否为空*/
while(e==1)
{
leave_car_park_back(cpb,car2);
enter_car_park(cp,car2);
e=notempty_car_park_back(cpb); 空*/
}
cp->number--;
}
void main() /*主函数*/
{
共 4 页 第 页 /*判断倒车场/*离开倒车场 进入停车场 判断倒车场是否为105
char ch; /*定义字符和
int e,n,i; 整形变量*/
car_park_back *cpb; /* 定义停车场,倒车场以及便道的变量*/
car_park *cp;
car_park_temp *cpt,*cpt2;
car_info *car;
cp=(car_park *)malloc(sizeof(car_park)); /*实体化变量*/
cpb=(car_park_back *)malloc(sizeof(car_park));
cpt=(car_park_temp *)malloc(sizeof(car_park_temp));
init_car_park(cp); /*实体化停车场,倒车场,便道*/
init_car_park_back(cpb);
init_car_park_temp(cpt);
do
{
car=(car_info *)malloc(sizeof(car_info));
共 4 页 第 页 106
printf("\ninput the 'A' or 'L' or 'X' ,end with '0' :");/*输入待操作的命令*/
scanf("%s",&ch);
e=notfull_car_park(cp); /*判断车场是否为空*/
switch(ch) /*判断要输入的命令*/
{
case 'A':if(e==1) /*车场不满,进入车辆*/ {
printf("input the car_number:"); /*输入车辆的基本信息*/
scanf("%d",&car->car_number); printf("input the arrivetime:"); scanf("%d",&(*car).arrivetime.hour);
enter_car_park(cp,car); /*进入车场*/
printf("the car is in the car_park,the place is %d\n",cp->number);
}
共 4 页 第 页 107
else /*若车场满,进入便道*/
{
enter_car_park_temp(cpt,car);
printf("input the car_number:");
scanf("%d",&car->car_number);
printf("the car is in the car_park_temp,the place of temp is %d\n",cpt->number);
}
break;
case 'L': /*离开停车场*/
printf("input the car_number:"); /*输入要离开车辆的号码以及离开时间*/
scanf("%d",&car->car_number);
printf("input the leavetime of the car:");
scanf("%d",&(*car).leavetime.hour);
leave_car_park(cp,car,cpb); /*执行离开车场*/
i=(*car).leavetime.hour; /*令便道里车辆进入车场的时间和车场里面的
车
共 4 页 第 页 108
离开的时间相等*/
n=notempty_car_park_temp(cpt); /*判断便道是否不空*/ if(n==1)
printf("The
car %d",cpt->head->next->data->car_number); /*记住便道里第一辆车的号码*/
leave_car_park_temp(cpt,car,cp); /*离开便道,进入停车场*/
printf(" is enter the car_park,arrivetime is %d ,the place of carpark is %d\n",i,cp->number);
break;
case 'X': /*查看车场以及便道里的车辆信息*/ printf("\nThere are %d cars in car park!\nFolowing is the carnumber in the car park:\n ",cp->number);
for(i=1;i<=cp->number;i++) /*做循环,依次输出停车场里车辆的信息*/
printf("%d,",(cp->north++)->car_number); cp->north=cp->north-cp->number;
cpt2->head=cpt->head; /*设一个虚拟指针使其指向
共 4 页 第 页 109
头结点的下一个位置*/
if(cpt->number==0) /*便道里没有车的情况*/ printf("\nThere is no cars in temp!") ;
else
{ printf("\nThere are %d cars in car temp!\nFolowing is the carnumber in the car temp:\n ",cpt->number);
for(i=1;i<=cpt->number;i++) /*做一个循环,输出便道里车辆的信息*/
{ printf("%d ",cpt2->head->next->data->car_number); /* 输出车辆的号码*/
cpt2->head=cpt2->head->next;
}
break;
default:break; /*退出循环*/
}
}while(ch!='0'); /*退出操作*/
共 4 页 第 页 110 }
}
《数据结构》课程设计报告
设计题目 停车场管理系统 专 业 计算机科学与技术 班 级 08级 姓 名 刘亚兰 学 号 081114028 完成日期 2101-06-02
共 4 页 第 页 111
共 4 页 第 页 112
目 录
1. 问题描述??????????????????2页
2. 系统设计??????????????????2页
3. 数据结构与算法描述?????????????4页
4. 测试结果与分析???????????????5页
5. 总 结???????????????????5页
6. 参考文献??????????????????5页
附录 程序源代码????????????????6页
共 4 页 第 页 113
停车场管理系统
1. 问题描述
设有一个可以停放n辆汽车的狭长停车场,它只有一个大门可以供车辆进出。车辆按到达停车场时间的早晚依次从停车场最里面向大门口处停放(最先到达的第一辆车放在停车场的最里面)。如果停车场已放满n辆车,则后来的车辆只能在停车场大门外的便道上等待,一旦停车场内有车开走,则排在便道上的第一辆车就进入停车场。停车场内如有某辆车要开走,在它之后进入停车场的车都必须先退出停车场为它让路,待其开出停车场后,这些车辆再依原来的次序进场。每辆车在离开停车场时,都应根据它在停车场内停留的时间长短交费。如果停留在便道上的车未进停车场就要离去,允许其离去,不收停车费,并且仍然保持在便道上等待的车辆的次序。编制一程序模拟该停车场的管理。
2. 系统设计
2.1 设计目标
要求程序输出每辆车到达后的停车位置(停车场或便道上),以及某辆车离开停车场时应交纳的费用和它在停车场内停留的时间。
2.2 设计思想
由于停车场是一个狭窄通道,而且只有一个大门可供汽车进出,问题要求汽车停车场内按车辆到达时间的先后顺序,依次由北向南排列。由此很容易联想到数据结构中的堆栈模型,因此可首先设计一个堆栈,以堆栈来模拟停车场,又每个汽车的车牌号都不一样,这样一来可以根据车牌号准确找到汽车位置,所以堆栈里的数据元素我设计成汽车的车牌号。当停车场内某辆车要离开时,在他之后进入的车辆必须先退出车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入停车场。
共 4 页 第 页 114
这是个一退一进的过程,而且让道的汽车必须保持原有的先后顺序,因此可再设计一个堆栈,以之来暂时存放为出站汽车暂时让道的汽车车牌号。当停车场满后,继续进来的汽车需要停放在停车场旁边的便道上等候,若停车场有汽车开走,则按排队的先后顺序依次进站,最先进入便道的汽车将会最先进入停车场,这完全是一个先进先出模型,因此可设计一个队列来模拟便道,队列中的数据元素仍然设计成汽车的车牌号。另外,停车场根据汽车在停车场内停放的总时长来收费的,在便道上的时间不计费,因此必须记录车辆进入停车场时的时间,车辆离开停车场时的时间不需要记录,当从终端输入时可直接使用。由于时间不象汽车一样需要让道,我设计了一个顺序表来存放时间。又用顺序表用派生法设计了一个堆栈,恰好满足上面模拟停车场的需要。
2.3 系统模块划分
3. 数据结构与算法描述
(1)要实现停车场管理的功能,首先要先建立两个栈s0,s1和一个队列Q,其中栈s1模拟停车场,另一个栈s0临时存放给要离开的汽车让路而从停车场退出的汽车,队列Q用来模拟停车便道。
共 4 页 第 页 115
(2)定义栈和队列的结构体,实现队列和栈的初始化以及栈,压车元素进栈,取车元素出栈,车元素入列,队头元素出列和取出队列中车元素操作。
(3)当车辆到达时,首先检查停车场是否是满的,若不满则放入停车场内,若停车场是满的,则放在便道上。即若栈是不满的就入栈,否则就入队列。
(4)当车辆离开时,首先要寻找到要离开车辆的车牌号,若车是从停车场离开,则在它之后进入的车辆必须先退出为它让路,待该辆车开出大门外,其它车辆再按原次序进入停车场,并将停放在便道上第一位置的车开进停车场,离开的车辆按其在停车场内停留的时间交费。即当元素在栈s1中时,就将在它之后的元素出栈并依序放入栈s0中,此元素出栈,随后将栈s0中的元素放入s0中,最后将队列Q的首元素放入栈s1中。
(5)若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车辆离去,从停车场离去,则输出汽车应交的费用,否则停车便道直接离去,此时排在它之前的汽车要先开走让路,然后依次排到队尾。
4. 测试结果与分析
共 4 页 第 页 116
5. 总 结
(1)编程时要认真,出现的错误要及时找出并改正,遇到问题要去查相关的资料,反复的调试程序,把各个要注意的问题要想到:同时要形成自己的调试程序的风格,从每个细节出发,不放过每个知识点。另外,要注意符号的使用。
(2)通过此次课程设计我学到了好多的东西,以前会的知识更加熟悉了,而且有了更深的认识;不太清楚的知识点也有了新的理解。
6. 参考文献 (包括书籍、论文、网络资料等)
[1] 苏仕华等.《数据结构课程设计》.北京:机械工业出版社,2005.5
[2] 钱能等. 《C++程序设计教程》. 北京:清华大学出版社 2009.10 附录 程序源代码
#include<iostream>
#include<fstream>
using namespace std;
#define MAX_SIZE 2//停车场能够容纳的车的数量
#define FARE 5//表示停车场的收费为每小时FARE元
int CountForStack=0;// 此全局变量用来计数堆栈现有的车数
int CountForQueue=0;// 此全局变量用来计数队列现有的车数
typedef struct//这个节点用来保存每辆车的信息
{ char Condition ;//用来表示“到达”或者“离开”的状态,
int ArriveTime;//用来记录到达时间,默认为-1,说明还没有到达 int LeaveTime;// 用来记录离开时间,默认为-1,说明还没有离开 int License;// 记录车牌号
}CarNode;
typedef struct //栈的定义
{ CarNode *base;//栈底指针,指向0
CarNode *top;//栈顶指针,如果指向0,说明栈为空
int stacksize;//栈的容量大小
}CarStack;
typedef struct QNode//队列节点的定义
{ char Condition ;//用来表示“到达”或者“离开”的状态,
int ArriveTime;//用来记录到达时间,默认为-1,说明还没有到达
共 4 页 第 页 117
int LeaveTime;// 用来记录离开时间,默认为-1,说明还没有离开 int License;// 记录车牌号
QNode *next;//指向下一个节点的指针
}QNode;
typedef struct// 队列的定义
{ QNode *front;//队头指针
QNode *rear;//队尾指针
}Queue;
bool InitStack(CarStack &S)//此函数用来初始化栈
{ S.base =(CarNode *)malloc(MAX_SIZE*sizeof(CarNode)); if(!S.base )
{ cout<<"内存分配失败!"<<endl;
return false;//说明内存分配失败,返回false }
S.top =S.base ;
S.stacksize =MAX_SIZE;
return true;}
bool InitQueue(Queue &Q)//此函数用来初始化队列
{ Q.front =(QNode *)malloc(sizeof(QNode));
if(!Q.front )
{ cout<<"内存分配失败!"<<endl;
return false;}
Q.rear =Q.front ;
Q.front ->next =0;//下一个节点指空
return true;}
bool EnQueue(Queue &Q,QNode &qnode)//此函数用来入队一个节点 { QNode *p=(QNode *)malloc(sizeof(QNode));
if(!p)
{ cout<<"内存分配失败!"<<endl;
return false;}
p->ArriveTime =qnode.ArriveTime ;
p->Condition =qnode.Condition ;
p->LeaveTime =qnode.LeaveTime ;
p->License =qnode.License ;
p->next =0;
Q.rear ->next =p; Q.rear =p;
return true;}
bool DeQueue(Queue &Q,QNode &t)//此函数用来出队
{ if(Q.front ==Q.rear )
{ cout<<"队列为空!"<<endl;
共 4 页 第 页 118
return false;}
QNode *p=Q.front->next ;
t.ArriveTime =p->ArriveTime ;
t.Condition =p->Condition ;
t.LeaveTime =p->LeaveTime ;
t.License =p->License ;
Q.front ->next =p->next ;
if(Q.rear ==p)//如果P是指向最后一个出队的元素
Q.rear =Q.front ;
free(p);
return true;}
void InitCarNode(CarNode &C,char condition,int arrivetime,int leavetime,int license)//本函数用来初始化一个CarNode 节点
{ C.ArriveTime =arrivetime;
C.Condition =condition;
C.LeaveTime =leavetime;
C.License=license;}
bool Push(CarStack &S,CarNode &car)//此函数用来入栈一个CarNode 节点 { if(S.top -S.base >=S.stacksize )
{ cout<<"此栈已满,不能压入新的信息"<<endl;
return false;}
(*S.top ).ArriveTime =car.ArriveTime ;
(*S.top ).Condition =car.Condition ;
(*S.top ).LeaveTime =car.LeaveTime ;
(*S.top ).License =car.License ;
++S.top ;//栈顶指针上移
return true;}
bool Pop(CarStack &S,CarNode &t)//此函数用来弹出栈内元素
{ if(S.top ==S.base )
{ cout<<"栈空,不能执行出栈操作!"<<endl;
return false;}
--S.top ;//栈顶指针下移
t.ArriveTime =(*S.top ).ArriveTime ;
t.Condition =(*S.top ).Condition ;
t.LeaveTime =(*S.top ).LeaveTime ;
t.License =(*S.top ).License ;
return true;}
bool IsStackFull(CarStack &S)//此函数用来判断堆栈是否已满
{ if(S.top -S.base >=S.stacksize )
共 4 页 第 页 119
return true;
else
return false;}
bool IsStackEmputy(CarStack &S)//此函数用来判断堆栈是否为空
{ if(S.top ==S.base )
return true;
else
return false;}
bool IsQueueEmputy(Queue &Q)//此函数用来判断队列是否为空
{ if(Q.front ==Q.rear )
return true;
else
return false;}
bool SearchInStack(CarStack&S,int a)
{ bool tag=false;
if(!IsStackEmputy(S))
{ CarNode *p=S.top-1 ;
while(p!=S.base )
{ if((*p).License ==a)
tag=true;
--p;}
if((*p).License ==a)
tag=true;}
return tag;}
bool SearchInQueue(Queue &Q,int a)//a表示要查找的车牌号,
{ bool tag=false;
if(!IsQueueEmputy(Q))//如果队列非空
{ QNode *p=Q.front->next ;
while(p!=Q.rear)
{ if((*p).License ==a) tag=true;
}//退出此while循环时p指向最后一个元素
if((*p).License ==a) tag=true;}
return tag;}
void InCar(CarStack &S,Queue &Q,int a1,int a2)//此函数用来表示进入车辆, 参数a1用来表示到达时间,参数a2表示车牌号码
{ if(SearchInStack(S,a2))
{ cout<<"车号"<<a2<<"已经存在于停车场内,输入有误"<<endl;
return ; }
if(SearchInQueue(Q,a2))
共 4 页 第 页 120
{ cout<<"车号"<<a2<<"已经存在于通道内,输入有误"<<endl;
return ; }
if(IsStackFull(S))//如果堆栈已满,说明停车场已满,需要停车在通道 { QNode qnode;
qnode.ArriveTime =-1;//在通道时间不收费,所以不计时
qnode.Condition ='A';
qnode.LeaveTime =-1;//定义为-1,说明还没有开始离开
qnode.License =a2;
EnQueue(Q,qnode);//停在通道上
++CountForQueue;
cout<<"车号:"<<qnode.License <<"停在通道的第"<<CountForQueue<<"号位置"<<endl;}
else
{ CarNode carnode;
carnode.ArriveTime =a1;
carnode.Condition ='A';
carnode.LeaveTime =-1;
carnode.License =a2;
Push(S,carnode);
++CountForStack;
cout<<"车号:"<<carnode.License <<"到达时间 "<<carnode.ArriveTime <<"停在停车场的第"<<CountForStack<<"号位置"<<endl;} }
void OutCar(CarStack &S,Queue &Q,int a1,int a2)//此函数用来出车,参数a1用来表示离开时间,参数a2表示车牌号码
{ if(SearchInQueue(Q,a2))
{ cout<<"车号"<<a2<<"存在于通道内,还未进入停车场,不能离开"<<endl; return ;}
if(!SearchInStack(S,a2))
{ cout<<"车号"<<a2<<"不在停车场内,输入有误"<<endl;
return ; }
CarStack tempstack;
InitStack(tempstack);//建立并且初始化用于暂存出车时让车的堆栈 bool tag1=false;//标志这个停车场出车以前是否已满,默认为没有满 tag1=IsStackFull(S);
bool tag2=true;//标志通道是否有汽车在等待, 默认为通道为空
tag2=IsQueueEmputy(Q);
CarNode temp;//用来保存暂时取出的汽车
bool tag3=false;//用来标志是否是离开时间小于到达时间而导致离开失败,true表示离开失败
共 4 页 第 页 121
while(1)//让车离开
{ Pop(S,temp);
if(temp.License ==a2)
{ if(a1<temp.ArriveTime )
{ cout<<"输入有误,离开时间不能小于到达时间,离开失败"<<endl; tag3=true;
Push(tempstack,temp);}
else
cout<<"车号"<<a2<<"现在离开停车场,所用的时间为"<<a1-temp.ArriveTime <<"收费为"<<(a1-temp.ArriveTime )*FARE<<endl;
break; }
else
Push(tempstack ,temp);//进入暂存栈}
while(!IsStackEmputy(tempstack))//倒出的车再次进入停车场
{ Pop(tempstack,temp);
Push(S,temp);}
QNode tempqnode;//用来暂时保存从通道出来的汽车
if(tag1==true&&tag2==false&&tag3==false)
{ DeQueue(Q,tempqnode);
--CountForQueue;
temp.ArriveTime =a1 ;
temp.Condition =tempqnode.Condition ;
temp.LeaveTime =tempqnode.LeaveTime ;
temp.License =tempqnode.License ;
Push(S,temp); }
if(tag2==true&&tag3==false)// 如果停车场没有满,并且离开成功
--CountForStack; }
void showmenu(CarStack &S,Queue &Q)
{ cout<<"******************************选择菜单*******************************"<<endl;
cout<<" 1: 停车"<<endl;
cout<<" 2: 离开"<<endl;
cout<<" 3: 退出"<<endl;
cout<<"*****************************请按键选择******************************"<<endl;
int tag; cin>>tag;
while(tag!=1&&tag!=2&&tag!=3) cin>>tag;
int a1;
unsigned int a2;
共 4 页 第 页 122
switch(tag)
{ case 1:
cout<<"请输入到达的车号"<<endl; cin>>a1;
cout<<"请输入到达时间"<<endl; cin>>a2;
InCar(S,Q,a2,a1); break;
case 2:
cout<<"请输入离开的车号"<<endl; cin>>a1;
cout<<"请输入离开的时间"<<endl; cin>>a2;
OutCar(S,Q,a2,a1); break;
case 3:
return ; break; }
char ch;
cout<<"*******************按E/e退出,按任意键返回菜单**************************"<<endl;
cin>>ch;
if(ch!='E'&&ch!='e')
showmenu(S,Q); }
int main()
{ CarStack carstack;
InitStack(carstack);// 建立并且初始化用于停车场的堆栈
Queue carqueue;
InitQueue(carqueue);//建立并且初始化用于通道的队列
showmenu(carstack,carqueue);}
共 4 页 第 页 123
共 4 页 第 页 124