#include<iostream>
#include<time.h>
using namespace std;
#define Maxsize 10//栈最大队列数
#define price 0.01//每秒计费
class information {
public:
string plate_number;
float time;//入栈时间戳
information(string r, float i) { plate_number = r; time = i; }
information() { plate_number = "0"; time = 0; }
};
//停车场模拟栈定义
typedef class {
public:
int top;//栈顶指针
information vehicle[Maxsize];
}sqstack;
//栈初始化
void initstack(sqstack& S) {
S.top = -1;
}
//栈判空,栈空返回true,否则返回false
bool stackempty(sqstack s) {
if (s.top == -1)return true;
else return false;
}
//进栈(栈块,要压入的数据),栈满返回false
bool push(sqstack& S, information a)
{
if (S.top == Maxsize - 1)return false;
S.top++;
S.vehicle[S.top] = a;
return true;
}
//出栈(栈块,接收数据的变量),栈空返回false
bool pop(sqstack& S, information& a)
{
if (S.top == -1)return false;
a=S.vehicle[S.top];
S.top--;
return true; }
//将a中information全部pop,push到z,z爆栈会返回false
bool ALL(sqstack &a,sqstack &z)
{
sqstack d;
information w;
while (a.top != -1) {
if (z.top == Maxsize - 1)return false;
pop(a, w);
push(z, w);
}
return true;
}
//指定车牌号出栈(栈块,车牌号),返回应支付的金额
bool delete_car(sqstack& S, string st1,float &w)
{
if (S.top == -1)return false;
information a;
sqstack s1;
initstack(s1);
while (S.top != -1)
{
pop(S, a);
if (a.plate_number != st1)//这次弹出的不对就压倒s1中
{push(s1, a);}
else { ALL(s1, S);w=price*(clock() - a.time)/ CLOCKS_PER_SEC; return true; }//找到了
}//如果循环结束了,说明S栈空了都没找到st1
ALL(s1, S);
return false;//没找到
}
//(车牌号,时间戳)
information input(string st1,float a)
{
information as;
as.plate_number = st1;
as.time = a;
return(as);
}
//栈显示
void display(sqstack a) {
int s =a.top;
cout << "车牌号" << " " << "系统时间" << endl;
while(s>-1)
{
cout << (a.vehicle[s]).plate_number<< " " << (a.vehicle[s]).time/ CLOCKS_PER_SEC << endl;
s--;
}
}
void menu()
{
cout << "-------------------------------------------------------------------------" << endl;
cout << "请操作:" << endl;
cout << "1:栈直观展示" << endl;
cout << "2:停车场进车(入栈)" << endl;
cout << "3:指定车牌号离开停车场(指定出栈)" << endl;
cout << "-------------------------------------------------------------------------" << endl;
}
void menu1()
{
cout << "-------------------------------------------------------------------------" << endl;
cout << "请输入车牌号" << endl;
cout << "-------------------------------------------------------------------------" << endl;
}
int main()
{
sqstack a;
initstack(a);//栈初始化
int i = 0;
while (i == 0)
{
int a1, a2;
menu();
cin >> a1;
switch (a1)
{
case 1:display(a); break;
case 2:
{
menu1(); string a3; cin >> a3;
if (push(a, input(a3, clock())))cout << "入栈成功" << endl;
else cout << "爆栈" << endl;
}break;
case 3:
{cout << "请输入要离开的车的车牌号" << endl; string sd; cin >> sd; float rt=0;
if (delete_car(a, sd, rt))
cout << "已删除车牌号为" << sd << "的信息" << endl << "应收费" << rt << "单位金额" << endl;
else cout << "未找到" << endl;
}
}
}
return 0;
}