天天看点

停车场管理 (数据结构)

#include<iostream>

using namespace std;

#define null 0

int getn()

{

int n;

cout<<"本停车场每小时10元."<<endl;

cout<<"请输入停车场可容纳的车:";

cin>>n;

return n;

}

int n=getn();

struct car{

int num;

int time;

car *next;

};

struct stack{

int top;

int base0,base1,base2;

car *stack0;

};

void initstack(stack &s){

s.stack0=new car[n];

s.top=s.base0=s.base1=-1;

s.base2=0;

}

void push(stack &s,car p){

s.top++;

s.stack0[s.top]=p;

}

void pop(stack &s,car &p){

if(s.top==-1)cout<<"no car!!";

else {p=s.stack0[s.top];s.top--;}

}

struct Queue{

car *front,*front1;

car *rear;

int k;

};

void initQueue(Queue &Q){

Q.rear=Q.front=Q.front1=new car;

Q.front->next=null;

Q.k =0;

}

void EnQueue(Queue &Q,car *p){

car *pt;

pt=new car;

pt=p;

pt->next=null;

Q.rear->next=pt;

Q.rear=pt;

Q.k++;

}

void DeQueue(Queue &Q,car &p){

car *ps;

if(Q.rear==Q.front)cout<<"便道上的车为空!!"<<endl;

else{

   Q.k--;

   ps=Q.front->next;

   p=*ps;

   Q.front->next=ps->next;

   if(Q.rear ==ps)Q.rear =Q.front ;

   delete ps;

}

}

int travel(stack &s,Queue &Q,car &pt){

int k;

k=1;

while(s.base2<=s.top){

   if(pt.num==s.stack0[s.base2].num||pt.time<=s.stack0[s.base2].time){

    k=0;break;

   }

   s.base2++;

}

while(Q.front1->next!=null){

   if(Q.front1->next->num==pt.num||pt.time<=Q.front1->next->time){

    k=0;break;

   }

   Q.front1=Q.front1->next;

}

s.base2=0;

Q.front1=Q.front;

return k;

}

int main(){

char c;

stack s,s1;

Queue Q;

initstack(s);

initstack(s1);

initQueue(Q);

car *ps;

car p,pt,p1,p2;

   while(cin>>c){

    ps=new car;

    cin>>ps->num>>ps->time;

    pt.num=ps->num;pt.time=ps->time;

    if(c=='e')break;

    if(c=='a'){

     travel(s,Q,pt);

     if(travel(s,Q,pt)==0){cout<<"输入车辆错误!"<<endl;continue;}

     else{

     if(s.top<=n-2){

      push(s,pt);s.base0=s.top;cout<<"车牌号码为"<<pt.num<<"的车,停车位置为停车场"<<s.top<<"号!"<<endl;

     }

       else {

     EnQueue(Q,ps);cout<<"车牌号码为"<<pt.num<<"的车,停车位置为便道"<<Q.k<<"号!"<<endl;

     }

     }

    }

    else if(c=='d'){

       while(s.stack0[s.top].num!=ps->num&&s.base1!=s.top){

        pop(s,p);push(s1,p);

       }

       if(s.base1==s.top){cout<<"没有对应汽车"<<endl;s.top=s.base0;s1.top=-1;continue;}

       else{

        pop(s,p);p2.time=pt.time;

        if(ps->time<p.time){cout<<"输入时间错误!"<<endl;push(s,p);

        while(s.top<=n-2&&s1.top>=0){

        pop(s1,p);push(s,p);s.base0=s.top;

        }

        continue;

        }

        else{cout<<"车号为"<<p.num<<"的车停车时间为:"<<ps->time-p.time<<"小时"<<endl;

        cout<<"车号为"<<p.num<<"的车该负现金为:"<<(ps->time-p.time)*10<<"元!"<<endl;

        }

       while(s.top<=n-2&&s1.top>=0){

          pop(s1,p);push(s,p);s.base0=s.top;

       }

       }

       while(s.top<=n-2){

        if(Q.rear==Q.front){cout<<"便道上的车为空!!"<<endl;break;}

        else{DeQueue(Q,p1); p1.time=p2.time;push(s,p1);}

       }

    }

    else{

     cout<<"输入格式错误"<<endl;break;

    }

    }

return 0;

}

继续阅读