汽车在停车场内按车辆到达时间的先后顺序依次由南向北排列(大门在最北端,最先到达的第一辆车停放在停车场的最南端),若停车场内已停满n辆车,则后来的汽车只能在门外的便道(即候车场)等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出停车场为它让路,待该车辆开出大门外,其他车辆再按原次序进入停车场,每辆停放在停车场的车在它离开停车场时必须按停留的时间长短交纳费用。
#include#include using namespace std; #define Maxsize 3//栈最大队列数 #define Maxsize1 4//环形队,最大队列为Maxsize1-1 #define price 0.01//每秒计费 //-------------------------------------------------------以下为候车场相关(环形队)--------------------------------------------------------------------- typedef class { public: string plate_number[Maxsize1]; int front, rear; }SqQueue; //候车场计数 int countsq(SqQueue a) { int s = 0; while (a.front != a.rear) { s++; a.front++; } return s; } //队列初始化 void initsqueue(SqQueue &Q) { Q.front = Q.rear = 0; } //入队,爆队返回false bool enqueue(SqQueue& Q, string a) { if ((Q.rear + 1) %Maxsize1==Q.front)return false; Q.plate_number[Q.rear] = a; Q.rear = (Q.rear+1) % Maxsize1; return true; } //出队,栈空的返回false bool dequeue(SqQueue& Q,string a) { if (Q.front == Q.rear)return false; a = Q.plate_number[Q.front]; Q.front = (Q.front + 1) % Maxsize; return true; } //获得队头元素 bool queue(SqQueue Q, string a) { if (Q.front == Q.rear)return false; a = Q.plate_number[Q.front]; return true; } void show2(SqQueue a) { cout << "-------------------------------------------------------------------------" << endl; cout << "从队头到队尾显示:" << endl; while (a.front != a.rear) { cout << a.plate_number[a.front] << endl; a.front=(a.front+1)%Maxsize1; } } //---------------------------------------------以下为停车场相关---------------------------------------------------------------------------- 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 << "4:候车场队列展示" << endl; cout << "5:候车场队首进入停车场" << endl; cout << "6:候车场还有几辆车?" << endl; cout << "7:关闭系统" << endl; cout << "-------------------------------------------------------------------------" << endl; } void menu1() { cout << "-------------------------------------------------------------------------" << endl; cout << "请输入车牌号" << endl; cout << "-------------------------------------------------------------------------" << endl; } //--------------------------------------------------------以下为动感地带------------------------------------------------------------------- //单个放行,爆栈或空队返回false bool Single_release(sqstack& sta, SqQueue& que) { string w; if (((sta.top +1) == Maxsize)||(que.front == que.rear))return false; dequeue(que, w); push(sta, input(w, clock())); return true; } //---------------------------------------------------------以下为主函数-------------------------------------------------------------------- int main() { sqstack a; SqQueue i; initsqueue(i);//队初始化 initstack(a);//栈初始化 int ier = 0; while (ier== 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; if (enqueue(i, a3)) cout << "已将信息存储到队尾" << endl; else cout << "候车场已满!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1" << 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; }break; case 4: {show2(i); }break; case 5: {if (Single_release(a, i)) cout << "候车场的队首车辆已进入停车场" << endl; else cout << "操作失败,其原因为爆栈或者空队"<



