📄 park.cpp
字号:
#include "park.h" //引用顺序表类SeqListtemplate <class T>
#include <time.h>
#include <stdlib.h>
#include <iomanip>
//初始化栈
Park::Park( ) //构造一个空栈
{
top1=ParkSize/2;
top2=top1-1;
}
//销毁栈
Park::~Park( ) //释放栈空间
{
}
//元素进栈
void Park::Push(int i,Car x) //元素x进栈
{
if (top1==0 && top2==ParkSize-1) throw "停车场已满"; //如果栈满,抛出异常
if(top1==0 && top2<ParkSize-1)
i=2;
if(top2==ParkSize-1 && top1>0)
i=1;
if(i==1)
{
Data_Park[--top1]=x;
cout<<'\t'<<x.CarNum<<"号车从南门进入停车场,在第"<<top1+1<<"号位置上"<<endl;
}
if(i==2)
{
Data_Park[++top2]=x;
cout<<'\t'<<x.CarNum<<"号车从北门进入停车场,在第"<<top2+1<<"号位置上"<<endl;
}
}
//元素出栈
Car Park::Pop(int i) //栈顶元素出栈
{
if(i==1)
{
if (top1>top2) throw "停车场空";
return Data_Park[top1++];
}
if(i==2)
{
if(top1>top2) throw"停车场空";
return Data_Park[top2--];
}
}
void Park::Print()
{
if(top1>top2) throw "停车场空";
int t=top1;
cout<<endl;
cout<<'\t'<<"车牌号"<<'\t'<<'\t'<<"到达时间"<<'\t'<<'\t'<<"在停车场位置" <<endl;
while(t<=top2)
{
cout<<'\t'<<Data_Park[t].CarNum<<'\t'<<'\t'<<Data_Park[t].Hour<<':'<<Data_Park[t].Minute<<'\t'<<'\t'<<'\t'<<t+1<<endl;
t++;
}
}
bool Park:: CheckNum()
{
int t=top1;
while(t<=top2)
{
if(Data_Park[t].CarNum==a.CarNum)
return 1;
t++;
}
return 0;
}
bool Park:: CheckTime()
{
int t=top1;
while(t<=top2)
{
if((Data_Park[t].Hour+float(Data_Park[t].Minute)/60) > (a.Hour+ float(a.Minute)/60))
return 1;
t++;
}
return 0;
}
void Park::Input()
{
char t;
cout<<endl;
cout<<'\t'<<"请输入车的去向(A:到达,D:离去,E:退出)"<<endl;
cout<<'\t';
cin>>a.State;
while(!cin || a.State[1]!=NULL || (a.State[0]!='A' && a.State[0]!='D' && a.State[0]!='E'))//输入的是字符串时给出错误提示
{
cout<<'\t'<<"输入的汽车去向有错误,请重新输入"<<endl;
cin.clear();
cin.ignore();
cout<<'\t';
cin>>a.State;
}
if(a.State[0]=='E')
return;
cout<<'\t'<<"请输入车牌号"<<endl;
cout<<'\t';
cin>>a.CarNum;
while(CheckNum() && a.State[0]=='A')
{
cin.clear();
cin.ignore(128,'\n');
cout<<'\t'<<"车牌重复,请重新输入车牌号"<<endl;
cin>>a.CarNum;
}
cout<<'\t'<<"请输入时间(如 5:20)"<<endl;
cout<<'\t';
cin>>a.Hour>>t>>a.Minute;
while((!cin || t!=':' || (a.Hour<0 ||a.Hour>23 || a.Minute<0 || a.Minute>59) || CheckTime()) && a.State[0]=='A')
{
cout<<'\t'<<"输入错误,请重新输入"<<endl;
cin.clear();
cin.ignore(128,'\n');
cout<<'\t';
cin>>a.Hour>>t>>a.Minute;
}
cout<<endl;
}
int Park::Random()
{
srand(time(NULL)); //初始化种子
return rand()%2+1;
}
Car Park::Find()
{
Car x;
Car Data_temp[ParkSize];//退车时备用停车场
int top_temp=-1;
int t=top1;//把top1暂存t中
while(Data_Park[t].CarNum!=a.CarNum && t<=top2)//寻找输入的车的位置
t++;
if(t<=top2)
{
if(t<ParkSize/2)
{
while((top1)!=t)
Data_temp[++top_temp]=Data_Park[top1++];//退车让路
x=Pop(1);
flag=1;
while(top_temp!=-1)//让路的车再进去
Data_Park[--top1]=Data_temp[top_temp--];
}
else
{
while(top2!=t)//
Data_temp[++top_temp]=Data_Park[top2--];
x=Pop(2);
flag=2;
while(top_temp!=-1)
Data_Park[++top2]=Data_temp[top_temp--];
}
}
else if(top2<t)//当停车场为空时 ,t为top1,大于top2
{
throw"停车场查无该车";
}
return x;
}
void Park::Run(Shortcut &s)
{
Car point;
cout<<'\t'<<"请输入单位时间停车价格(元)"<<endl;
cout<<'\t';
cin>>Price;
while(!cin || getchar()!='\n')
{
cout<<'\t'<<"输入错误"<<endl;
cout<<'\t'<<"请重新输入单位时间停车价格(元)"<<endl<<'\t';
cin.clear();
cin.ignore(128,'\n');
cin>>Price;
}
Input();
while(a.State[0]!='E')//输入‘E’退出
{
if(a.State[0]=='A' && (top2-top1)!=ParkSize-1)//停车场未满
Push(Random(),a);
else if(a.State[0]=='A' && (top2-top1)==ParkSize-1)//停车场满
s.EnQueue(a);
if(a.State[0]=='D')
{
try
{
point=Find( );
float Charge=Price*((a.Hour-point.Hour)+float(a.Minute-point.Minute)/60);//计算车费
if(Charge<0)
{
char t;
cout<<'\t'<<"输入时间有误:车离开时间早于到达时间。请重新输入时间:"<<endl;
cout<<'\t';
cin>>a.Hour>>t>>a.Minute;
while(Charge<0 || !cin || t!=':' || (a.Hour<0 ||a.Hour>23 || a.Minute<0 || a.Minute>59))
{
cout<<'\t'<<"输入错误,请重新输入"<<endl;
cin.clear();
cin.ignore(128,'\n');
cout<<'\t';
cin>>a.Hour>>t>>a.Minute;
Charge=Price*((a.Hour-point.Hour)+float(a.Minute-point.Minute)/60);//计算车费
}
}
cout<<'\t'<<point.CarNum<<"车已离去,它的费用为"<<setiosflags(ios::fixed)<<setprecision(2)<<Charge<<"元"<<endl;
if (s.position!=0)//判断便道上是否有车
{
point=s.DeQueue();//便道上删除第一辆车
point.Hour=a.Hour;//便道上的车进站时赋予进站时间
point.Minute=a.Minute;
cout<<'\t'<<"停车场有空位";
Push(flag,point); //便道上的车进入停车场
}
}
catch(char str[])
{cout<<'\t'<<str<<endl;}
}
Input();
}
}
char Park ::Choice()
{
char q[10];
cin>>q;
while(1)
{
int len = strlen(q);
if (len == 1)
break;
else
{
cout<<'\t'<<"输入错误,请重新输入"<<endl;
cout<<'\t';
cin>>q;
}
}
return q[0];
}
//////////////////
//队列类的函数定义
//////////////////
Shortcut::Shortcut( )//初始化一个空队列
{
Node *s;
s=new Node;//申请一个空结点
s->next=NULL;//空结点的指针域置为空
front=rear=s;//队头指针和队尾指针都指向头结点
position=0;
}
Shortcut::~Shortcut( )//销毁一个队列,释放队列占用的存储空间。
{
while(front)//如果front不空,队列没有处理完,继续处理后面的数据元素
{
Node *p;
p=front->next; //p指向队头后面的结点。
delete front;//释放队头结点占用的空间
front=p;//队头指针指向新的结点
}
}
void Shortcut::EnQueue(Car x)//将元素x插入到队列的尾部
{
Node *s;
s=new Node; //申请一个结点s
s->Data_Shortcut=x; //将x值存入结点s的数据域
s->next=NULL;//s的指针域置空
rear->next=s; //将结点s插入到队尾
rear=s;//队尾指针指向新的队尾s
cout<<'\t'<<"停车场已满,自动进入便道第"<<++position<<"号位置"<<endl;
}
Car Shortcut::DeQueue()//删除队头的元素
{
Node *p;
Car x;
if (rear==front) throw "下溢";//如果队列为空,出错返回
p=front->next; //p指向队头的下一个元素
x=p->Data_Shortcut; //暂存队头元素
front->next=p->next; //将队头元素所在结点摘链
if (p->next==NULL) rear=front; //判断出队前队列长度是否为1
delete p;//删除队头元素
position--;
return x;//返回队头元素的值x
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -