问题描述
设停车场是一个可停放n辆车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列,(大门在最南端,最先到达的第一辆车放在车场的最北端)。若停车场内已经停满n辆车,那么后来的车只能在门外的便道上等候。一旦有车开走,则排在便道上的第一辆车即可开入。当停车场内某车要离开时,在它之后进入的车辆必须先退出车场为它让路,待该车开出大门外,其他车辆再按原次序进入车场,每辆停放在车场的车在它离开时必须按它停留的时间长短缴纳费用。试为停车场编制按上述要求进行管理的模拟程序。
要求
根据各节点的信息,调用相应的函数或者语句,将节点入栈入队,出栈或者出对。
代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<queue>
#include<stack>
#include<time.h>
#include <windows.h>
using namespace std;
int n = 0;
int money = 0;
time_t Time = 0;
struct Vehicle_Information//车辆信息
{
char LicensePlateNumber[8];
time_t ArriveTime;
time_t LeaveTime;
time_t Time;
int Cost;
Vehicle_Information()
:ArriveTime(0)
,LeaveTime(0)
,Time(0)
,Cost(0)
{
*LicensePlateNumber = NULL;
}
};
void delay()//延迟函数
{
int n = 6;
cout<<"*请等待";
while(n)
{
Sleep(200);
cout<<".";
n--;
}
cout<<endl;
}
typedef struct Vehicle_Information Vehicle;
class Parking
{
public:
bool IsParked()//判断停车场是否已停满车
{
if(Park.size() == n)
return true;
return false;
}
bool Find0(char* num)//在停车场中查找
{
int count = 0;
while(!Park.empty())
{
if(strcmp(Park.top().LicensePlateNumber,num) == 0)
{
count = 1;
break;
}
GiveWay.push(Park.top());
Park.pop();
}
if(!GiveWay.empty())
{
while(!GiveWay.empty())
{
Park.push(GiveWay.top());
GiveWay.pop();
}
}
if(count == 1)
return true;
return false;
}
bool Find1(char *num)
{
queue<Vehicle> V;
int count = 0;
while(!Shortcut.empty())
{
if(strcmp(Shortcut.front().LicensePlateNumber,num) == 0)
{
count = 1;
break;
}
V.push(Shortcut.front());
Shortcut.pop();
}
if(!V.empty())
{
while(!V.empty())
{
Shortcut.push(V.front());
V.pop();
}
}
if(count == 1)
return true;
return false;
}
void Input()//输入车辆信息,并将车辆停入停车场
{
cout<<"*请输入所停车辆的信息:"<<endl;
cout<<"*车牌号:";
cin>>Information.LicensePlateNumber;
delay();
if(Find0(Information.LicensePlateNumber))
{
cout<<"*请注意!您输入的号码为伪造号码!"<<endl;
return;
}
if(Find1(Information.LicensePlateNumber))
{
cout<<"*请注意!您输入的号码为伪造号码!"<<endl;
return;
}
if(!IsParked())
{
Information.ArriveTime = time(&Time);
Park.push(Information);
Sleep(100);
cout<<"*车辆已停好!"<<endl;
}
else
{
Shortcut.push(Information);
cout<<"*停车场已满!车辆已停入便道!"<<endl;
}
}
void Output()//车辆离开停车场
{
if(Park.empty())
{
cout<<"*停车场已空!"<<endl;
}
cout<<"*请输入要离开车辆的车牌号:";
char num[8] = {0};
cin>>num;
delay();
int n = Park.size();
while(!Park.empty())
{
if(strcmp(Park.top().LicensePlateNumber,num) == 0)
{
Park.top().LeaveTime = time(&Time);
Park.top().Time = (int)(Park.top().LeaveTime - Park.top().ArriveTime );
Park.top().Cost =(int)( Park.top().Time * money);
CurrentVehicleInformation(Park.top());
Park.pop();
Sleep(100);
cout<<"*车辆已驶离!"<<endl;
break;
}
GiveWay.push(Park.top());
Park.pop();
}
if(GiveWay.size() == n)
{
cout<<"*无此车牌号!"<<endl;
return;
}
if(!GiveWay.empty())
{
while(!GiveWay.empty())
{
Park.push(GiveWay.top());
GiveWay.pop();
}
}
}
void CurrentVehicleInformation(Vehicle V)//当前所操作车辆信息
{
printf("车牌号\t驶入时间\t当前时间\t停车时间\t当前费用\n");
printf("%s\t",V.LicensePlateNumber);
TimeTransform(&(V.ArriveTime));
TimeTransform(&(V.LeaveTime));
printf("%ds\t\t",V.Time);
printf("%d元\n",V.Cost);
}
void ShortcutIn()//将便道上的车停入停车场
{
if(Shortcut.empty())
{
cout<<"*便道未停靠车辆!"<<endl;
return;
}
else
{
if(Park.size() == n)
{
cout<<"*停车位已满!"<<endl;
return;
}
else
{
Shortcut.front().ArriveTime = time(&Time);
Park.push(Shortcut.front());
Shortcut.pop();
delay();
cout<<"*车辆已停入车位!"<<endl;
}
}
}
void TimeMoney()//赋予车辆当前时间,用于计算当前费用
{
time_t t = time(&Time);
while(!Park.empty())
{
Park.top().LeaveTime = t;
Park.top().Time = (int)(Park.top().LeaveTime - Park.top().ArriveTime );
Park.top().Cost =(int)( Park.top().Time * money);
GiveWay.push(Park.top());
Park.pop();
}
while(!GiveWay.empty())
{
Park.push(GiveWay.top());
GiveWay.pop();
}
}
void ParkInformation()//停车场信息
{
cout<<"*当前停车场共有 "<<Park.size()<<" 辆车!"<<endl;
cout<<"*剩余车位为:"<<n-Park.size()<<endl;
cout<<"*便道目前有 "<<Shortcut.size()<<" 辆车在等待停车"<<endl;
if(!Park.empty())
{
TimeMoney();
cout<<"*停车场所停车辆信息:"<<endl;
printf("车牌号\t驶入时间\t当前时间\t停车时间\t当前费用\n");
while(!Park.empty())
{
GiveWay.push(Park.top());
Park.pop();
}
while(!GiveWay.empty())
{
printf("%s\t",GiveWay.top().LicensePlateNumber);
TimeTransform(&(GiveWay.top().ArriveTime));
TimeTransform(&(GiveWay.top().LeaveTime));
printf("%ds\t\t",GiveWay.top().Time);
printf("%d元\n",GiveWay.top().Cost);
Park.push(GiveWay.top());
GiveWay.pop();
}
}
}
void TimeTransform(time_t const *time_value)//时间转换
{
struct tm* p;
p = localtime(time_value);
printf("%d:%d:%d \t",p->tm_hour,p->tm_min,p->tm_sec);
}
protected:
Vehicle Information;
stack<Vehicle> Park;//停车场
stack<Vehicle> GiveWay;//让路
queue<Vehicle> Shortcut;//便道
};
void Menu()
{
cout<<endl;
cout<<"*************************************************************"<<endl;
Sleep(200);
cout<<" 菜单 "<<endl;
Sleep(200);
cout<<"*************************************************************"<<endl;
Sleep(200);
cout<<"************* 1.车辆入库 2.车辆驶离 ******************"<<endl;
Sleep(200);
cout<<"************* 3.便道车辆驶入 4.停车场内所有车辆信息 ******"<<endl;
Sleep(200);
cout<<"************* 5.退出 ****************************************"<<endl;
}
int main()
{
Parking P;
cout<<"*************************************************************"<<endl;
cout<<" 停车场管理系统 "<<endl;
cout<<"*************************************************************"<<endl;
cout<<"*请输入停车场的车位数:"<<endl;
cin>>n;
cout<<"*请输入停车场每秒的停车费用:"<<endl;
cin>>money;
if(n < 0)
{
n = 0;
}
while(n)
{
Menu();
int count = 0;
Sleep(500);
cout<<"*请选择当前要进行的操作:"<<endl;
cin>>count;
switch(count)
{
case 1:
P.Input();
break;
case 2:
P.Output();
break;
case 3:
P.ShortcutIn();
break;
case 4:
P.ParkInformation();
break;
case 5:
cout<<"*已退出!"<<endl;
exit(0);
break;
default:
cout<<"*输入有误!"<<endl;
break;
}
}
if(n == 0)
{
cout<<"*您的停车场正在被强拆!";
delay();
cout<<"*停车场已拆除完毕!请输入大于0的车位进行重建!"<<endl;
}
return 0;
}