#include <stdlib.h>
#include <malloc.h>
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
typedef int SElemType
typedef int Status
typedef int CarStack
typedef int LinkQueue
typedef int InputData
// definition of array based stack
#define STACK_INIT_SIZE 100 //Initial size for memory allocation
#define STACKINCREMENT 10 //incremental size
typedef struct{
SElemType *base //base pointer
SElemType *top//top pointer
CarStack *garage
CarStack *Temp
LinkQueue *road
InputData *inputdata
intstacksize //current size
}SqStack
status PopStack(CarStack *garage, CarStack *Temp, LinkQueue *road, InputData *inputdata)//车出库
{
CarNode *Car = (CarNode *)malloc(sizeof(CarNode))
if (!Car)
{
cout <<谨孝仔 "内存分配失败!" <<endl
exit(0)
}
Car->num = inputdata->num
Car->leave = inputdata->t
int i = garage->top
while (i)
{
if (garage->stack[i -1 ]->num != Car->num)
{
garage->top--//进临时车站
Temp->stack[Temp->top] = garage->stack[garage->top]
Temp->top++
i--//没有找到,继续循环查找
if (i == 0)
{
cout <<"没有该祥汪车牌的车!" <<endl
}
}
else//获得车辆信息,并回归临时车栈的车辆
{
Car->reach = garage->stack[i -1 ]->reach
Print(Car)//打印车辆收费单
garage->top--
while (Temp->top>0)//回归临时车站的车辆
{
Temp->top--
garage->stack[garage->top] = Temp->stack[Temp->top]
garage->top++
}
if (road->head != road->rear)//从便道出来一辆车到停车场
{
garage->stack[garage->top] = road->head->next
cout <<慎袭 endl
cout <<"车场有空位,便道第一辆车可以进入!" <<endl
garage->top++
road->head = road->head->next
}
i = 0//已经找到,退出循环
}
}
}
void InfoStack(CarStack *garage)//车库车辆信息
{
if (garage->top ==0 )
cout <<"车库里没有车!" <<endl
else
{
cout <<" _________________________ " <<endl
cout <<"| |" <<endl
cout <<"|车 库|" <<endl
cout <<"|位置 车辆号码 到达时间|" <<endl
for (int i = 0i<garage->topi++)
{
cout <<"| " <<i +1 <<" " <<garage->stack[i]->num <<" " <<garage->stack[i]->reach <<" |" <<endl
}
cout <<"|_________________________|" <<endl
}
}
void InfoQueue(LinkQueue *road)//便道车辆信息
{
CarNode *p
p = road->head->next
int i = 0
if (road->head == road->rear)
cout <<"便道里没有车!" <<endl
else
{
cout <<" __________________ " <<endl
cout <<"| |" <<endl
cout <<"| 便道 |" <<endl
cout <<"| 位置车辆号码 |" <<endl
while (p != NULL)
{
cout <<"| " <<++i <<"" <<p->num <<" |" <<endl
p = p->next
}
cout <<"|__________________|" <<endl
}
free(p)
}
void Print(CarNode *Car)//打印出站车辆信息
{
int rh, lh
float t
rh = Car->reach
lh = Car->leave
t = (lh - rh) * 60 *0.01
cout <<" ---------------------------------------------------- " <<endl
cout <<"|车辆的号码进库时间 离开时间 车费(元) |" <<endl
cout <<"|" <<Car->num <<" " <<rh <<" " <<lh <<" " <<t <<" |" <<endl
cout <<"|____________________________________________________|" <<endl
}
void Input(InputData *inputdata)
{
int maxsize
cout <<"请输入停车场容量 Maxsize (最多为5) : "
do
{
cin >>maxsize
if (!(maxsize<5 &&maxsize>0))
{
cout <<"输入有误,请重新输入停车场容量:"
}
} while (!(maxsize<5 &&maxsize>0))
int i = 0, j =0 , k =1
char c
int n
CarStack garage, Temp
LinkQueue road
CarNode car
InitStack(&garage, maxsize)
InitStack(&Temp, maxsize)
InitQueue(&road)
while (k)
{
i = 0
while (!i)
{
cout <<" ____________________________________________________________" <<endl
cout <<"||" <<endl
cout <<"| 欢迎光临停车场,本停车场每分钟收费元 |" <<endl
cout <<"||" <<endl
cout <<"| 请输入车库命令,格式如下(命令,车牌号,入库时间) |" <<endl
cout <<"||" <<endl
cout <<"| A(a)-入库 D(d)-离开 P(p)-查看停车场车辆 P(p)-查看过道车辆 |" <<endl
cout <<"|____________________________________________________________|" <<endl
cin >>(inputdata->command) >>(inputdata->num) >>(inputdata->t)
c = inputdata->command
n = inputdata->num
if (c != 'A'&&c != 'a'&&c != 'D'&&c != 'd'&&c != 'W'&&c != 'w'&&c != 'P'&&c != 'p'&&c != 'E'&&c != 'e')
{
cout <<"命令不正确,请重新输入!" <<endl
i =0
}
else
i = 1
// if(SearchInGarage(garage,n,maxsize)&&SearchInRoad(road,n))
// {
// i =0
// cout<<"命令不正确,请重新输入!"<<endl
// }
}
switch (c)
{
case 'A':
case 'a':
GetCarNode(&garage, &road, inputdata, maxsize)//获取车辆信息
break
case 'D':
case 'd':
PopStack(&garage, &Temp, &road, inputdata)//车出库
break
case 'P':
case 'p'://查询停车场的信息
InfoStack(&garage)//车库车辆信息
break
case 'W':
case 'w'://查询侯车场的信息
InfoQueue(&road)//便道车辆信息
break
case 'E':
case 'e':
k =0
break
default:
break
}
}
cout <<endl
cin.clear()
}
bool SearchInGarage(CarStack garage, int n, int maxsize)//a表示要查找的车牌号,如果在停车场里面,就返回true
{
bool tag = false
for (int i =0 i<maxsize || (i != garage.top)i++)
{
if (n == garage.stack[i]->num)
{
tag = true
break
}
}
return tag
}
bool SearchInRoad(LinkQueue road, int n)//a表示要查找的车牌号,如果在通道里面,就返回true
{
bool tag = false
StackNode *p
p = (StackNode *)malloc(sizeof(StackNode))
if (road.head != road.rear)//如果队列非空
{
p = road.head
while (p != road.rear)
{
++p
if (p->num == n)
tag = true
}//退出此while循环时p指向最后一个元素
}
free(p)
return tag
}
2.3 停车场管理2.3.1 项目简介
设停车场是一个可以停放n辆汽车的南北方向的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在车场的最北端),若车场内已停满n辆车,那么后来的车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路滑州,待该辆车开出大门外,其它车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。要求程序输出每辆车到达后的停车位置(停车场或便道上),以及某辆车离开停车场时应缴纳的费用和它在停车场内停留的时间。
2.3.2 设计思路
停车场的管理流程如下:
①当车辆要进入停车场时,检查停车场是否已满,如果未满则车辆进入停车场;如果停车场已满,则车辆进入便道等候。
②当车辆要求出栈时,先让在它之后进入停车场的车辆退出停车场为它让路,再让该车退出停车场,让路的所有车辆再按其原来进入停车场的次序进入停车场。之后,再检查在便道上是否有车等候,有车则让最先等待的那辆车进入停车场。
2.3.3 数据结构
由于停车场只有一个大门,当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路,先进停车场的后退出,后进车场的先退出,符合栈的“后进先出,先进后出”的 *** 作特点,因此,可以用一个栈来模拟停车场。而当停车场满后,继续来到的其它车辆只能停在便道上,根据便道停车的特点,先排队的车辆先离开便道进入停车场,符合队列的“先进先出,后进后出”的 *** 作特点,因此,可以用一个队列来模拟便道。排在停车场中间的车辆可以提出离开停车场,并且停车场内在要离开的车辆之后到达的车辆都必须先离开停车场为它让路,然后这些车辆依原来到达停车场的次序进入停车场,因此在前面已设的一个栈和一个队列的基础上,还需要有一个地方保存为了让路离开停车场的车辆,由于先退出停车场的后进入停车场,所信穗蔽以很显然保存让路车辆的场地也应该用一个栈来模拟。因此,本题求解过程中需用到两个栈和一个队列。栈以顺序结构实现,队列以链表结构实现。
2.3.4 程序清单
程序提示:以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理。每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号码以及到达或离去的时刻。对每一组输入数据进行 *** 作后的输出信息为:若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在便道上停留的时间不收费)。
#include<stdio.h>
#include <stdlib.h>
#include<iostream.h>
#include<string.h>
#include<math.h>
#define size 1 //停车场位置数
//族迹模拟停车场的堆栈的性质;
typedef struct zanlind{
int number //汽车车号
int ar_time //汽车到达时间
}zanInode
typedef struct{
zanInode *base //停车场的堆栈底
zanInode *top //停车场的堆栈顶
int stacksize_curren
}stackhead
//堆栈的基本 *** 作;
void initstack(stackhead &L) //构造一个空栈
{
L.base=(zanInode*)malloc(size*sizeof(zanlind))
if(!L.base) exit(0)
L.top=L.base
L.stacksize_curren=0
}
void push(stackhead &L,zanInode e) //把元素e压入s栈
{
*L.top++=e
L.stacksize_curren++
}
void pop(stackhead &L,zanInode &e) //把元素ed出s栈
{
if(L.top==L.base)
{
cout<<"停车场为空 !!"
return
}
e=*--L.top
L.stacksize_curren--
}
//模拟便道的队列的性质;
typedef struct duilie{
int number //汽车车号
int ar_time //汽车到达时间
struct duilie *next
}*queueptr
typedef struct{
queueptr front //便道的队列的对头
queueptr rear //便道的队列的队尾
int length
}linkqueue
//队列的基本 *** 作;
void initqueue(linkqueue &q) //构造一个空队列
{
q.front=q.rear=(queueptr)malloc(sizeof(duilie))
if(!q.front||!q.rear)
exit(0)
q.front->next=NULL
q.length=0
}
void enqueue(linkqueue &q,int number,int ar_time) //把元素的插入队列(属性为number,ar_time)
{
queueptr p
p=(queueptr)malloc(sizeof(duilie))
if(!p) exit(0)
p->number=number
p->ar_time=ar_time
p->next=NULL
q.rear->next=p
q.rear=p
q.length++
}
void popqueue(linkqueue &q,queueptr &w) //把元素的插入队列(属性为number,ar_time)
{
queueptr p
if(q.front==q.rear)
{
cout<<"停车场的通道为空 ! !"<<endl
return
}
p=q.front->next
w=p
q.front->next=p->next
q.length--
if(q.rear==p) q.front=q.rear
}
void jinru(stackhead &st,linkqueue &q) //对进入停车场的汽车的处理;
{
int number,time_a
cout<<"车牌为:"
cin>>number
cout<<"进场的时刻:"
cin>>time_a
if(st.stacksize_curren<2)
{
zanInode e
e.number=number
e.ar_time=time_a
push(st,e)
cout<< " 该车已进入停车场在: "<<st.stacksize_curren<<" 号车道"<<endl<<endl
}
else
{
enqueue(q,number,time_a)
cout<<"停车场已满,该车先停在便道的第"<<q.length<<"个位置上"<<endl
}
}
void likai(stackhead &st,stackhead &sl,linkqueue &q) //对离开的汽车的处理;
{ //st堆栈为停车场,sl堆栈为倒车场
int number,time_d,flag=1,money,arrivaltime //q为便道队列
cout<<"车牌为:"
cin>>number
cout<<"出场的时刻:"
cin>>time_d
zanInode e,q_to_s
queueptr w
while(flag) //找到要开出的车,并d出停车场栈
{
pop(st,e)
push(sl,e)
if(e.number==number)
{
flag=0
money=(time_d-e.ar_time)*2
arrivaltime=e.ar_time
}
}
pop(sl,e) //把临时堆栈的第一辆车(要离开的)去掉;
while(sl.stacksize_curren) //把倒车场的车倒回停车场
{
pop(sl,e)
push(st,e)
}
if(st.stacksize_curren<2&&q.length!=0) //停车场有空位,便道上的车开进入停车场
{
popqueue(q,w)
q_to_s.ar_time=time_d
q_to_s.number=w->number
push(st,q_to_s)
cout<<"车牌为"<<q_to_s.number<<"的车已从通道进入停车场,所在的停车位为:"<<st.stacksize_curren<<endl<<endl
}
cout<<"\n 收 据"<<endl
cout<<" ====================== 车牌号: "<<number<<endl
cout<<"==================================================="<<endl
cout<<"|进车场时刻 | 出车场时刻 | 停留时间 | 应付(元)|"<<endl
cout<<"===================================================="<<endl
cout<<"| "<<arrivaltime<<" | "<<time_d<<" | "<<time_d-arrivaltime<<" | "<<money<<" |"<<endl
cout<<"-----------------------------------------------------"<<endl<<endl
}
void main()
{
int m=100
char flag //进入或离开的标识;
stackhead sting,slinshi //停车场和临时倒车场堆栈的定义;
linkqueue line //队列的定义;
initstack(sting) //构造停车场堆栈sting
initstack(slinshi) //构造倒车场堆栈slinshi
initqueue(line) //构造便道队列line
while(m)
{
cout<<"\n ** 停车场管理程序 **"<<endl
cout<<"==================================================="<<endl
cout<<"** **"<<endl
cout<<"** A --- 汽车 进 车场 D --- 汽车 出 车场 **"<<endl
cout<<"** **"<<endl
cout<<"** E --- 退出 程序 **"<<endl
cout<<"==================================================="<<endl
cout<<" 请选择 :(A,D,E): "
cin>>flag
switch(flag)
{
case 'A': jinru(sting,line)break //汽车进车场
case 'D': likai(sting,slinshi,line)break //汽车出车场
case 'E': exit(0)
}
m--
}
}
2.3.5 运行结果
液位仿真停车步骤有神键携:1、打开右转向灯,看后视镜,看右边有没有车超车你的车;游伏
2、减速就是踩刹车;
3、向右打方向,速度相当慢的时候挂空挡,然后回到正方向;
4、关闭发动机,踩下离合器,将档亮孝位换到1、2、3档。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)