停车场管理

#include<iostream>
using namespace std;
#define MAXSIZE 2
typedef struct{
    int id;//汽车编号
    string type;//汽车到达或者离开
    string number;//车牌号
    string arrivetime;//汽车到达的时间
    string entertime;//汽车进入停车场的时间
    string leavetime;//汽车离开停车场的时间
}ElemType;
typedef struct SNode
{
    ElemType data[MAXSIZE];
    int top;

}SNode,*Stack;
typedef struct QNode
{
    ElemType data;
    struct QNode *next;
}QNode;
typedef struct{
    QNode *front;
    QNode *real;
}Queue;

void InitStack(Stack &s)
{
    s=new SNode;
    s->top=-1;
}
bool EmptyStack(Stack s)
{
    return s->top==-1;
}
bool FullStack(Stack s)
{
    return s->top==MAXSIZE-1;
}
bool Top(Stack s,ElemType &e)
{
	if(EmptyStack(s))
	{
		return false;
	}
    e=s->data[s->top];
    return true;
}
bool Pop(Stack &s,ElemType &e)
{
	if(EmptyStack(s))
	{
		return false;
	}
    e=s->data[s->top--];
    return true;
}
bool Push(Stack &s,ElemType e)
{
	if(FullStack(s))
	{
		return false;
	}
    s->data[++s->top]=e;
    return true;
}
void InitQueue(Queue *&q)
{
    q=new Queue;
    q->front=q->real=NULL;
}
bool EmptyQueue(Queue *q)
{
    return q->real==NULL;
}
void EnQueue(Queue *&q,ElemType e)
{
    QNode *p;
    p=new QNode;
    p->data=e;
	p->next=NULL;
    if (q->real==NULL)
    {
        q->real=q->front=p;
    } else{
        q->real->next=p;
        q->real=p;
    }
}
bool DeQueue(Queue *&q,ElemType &e)
{
    QNode *t;
    if (q->real==NULL)//当队列为空时
    {
        return false;
    }
    t=q->front;
    if (q->front==q->real)//当队列中只有一个元素时
    {
        q->front=q->real=NULL;
    } else{
        q->front=q->front->next;
    }

    e=t->data;
    delete(t);
    return true;
}

int zhuanhuan(string a)
{
    int ans=0;
    for (int i = 0; i < a.length(); ++i) {
        ans=ans*10+a[i]-'0';
    }
    return ans;
}
int time(string a,string b)
{
    int ans=0;
    int pos1=a.find(':');
    int pos2=b.find(':');
    string c=a.substr(pos1+1);
    string d=b.substr(pos2+1);
    string e=a.substr(0,pos1);
    string f=b.substr(0,pos2);
    int fen1=zhuanhuan(c);
    int fen2=zhuanhuan(d);
    int h1=zhuanhuan(e);
    int h2=zhuanhuan(f);
    ans=(h1-h2)*60+fen1-fen2;

    return ans;
}

int main()
{
    ElemType  e;
    Stack s;
    Queue *q;
    InitStack(s);
    InitQueue(q);
    int pos=0;
    while(true)
    {
        cout<<"输入信息类型:"<<endl;
        cin>>e.type;
        if (e.type=="arrive")
        {
            cout<<"汽车编号:";
            cin>>e.id;
            cout<<"汽车车牌号:";
            cin>>e.number;
            cout<<"汽车到达时间:";
            cin>>e.arrivetime;
            if (!FullStack(s))
            {
                e.entertime=e.arrivetime;
                Push(s,e);
                cout<<e.id<<"号汽车在停车场的第"<<s->top+1<<"个位置"<<endl;
            }

            else
            {

                pos++;
                EnQueue(q,e);
                cout<<e.id<<"号汽车在便道的第"<<pos<<"个位置"<<endl;

            }


        } else if (e.type=="leave"){
            if (EmptyStack(s))
                cout<<"停车场中还没有车"<<endl;
            else
            {
            	cout<<"请输入离开的汽车编号:";
            	int id;
            	cin>>id;
            	int i=0;
	            ElemType es[2];
            	while(true)
	            {

		            Pop(s,e);
		            if(e.id==id)
		            {
		            	break;
		            }
		            es[i++]=e;
	            }
				for(int j=0;j<i;j++)
				{
					Push(s,es[j]);
				}
                cout<<"汽车的离开时间:";
                cin>>e.leavetime;
                cout<<"离开的是"<<e.id<<"号汽车"<<endl;
                cout<<"它停留的时间为"<<time(e.leavetime,e.entertime)<<"分钟"<<endl;
                cout<<"费用为"<<3*time(e.leavetime,e.entertime)<<endl;
                if (EmptyQueue(q))
                {
                    cout<<"便道中无车,所以没有车进入停车场"<<endl;
                } else
                {
                    QNode *t=q->front;
                    t->data.entertime=e.leavetime;
                    Push(s,t->data);
                    DeQueue(q,e);
                    pos--;
                    cout<<e.id<<"号汽车进入停车场"<<endl;
                }

            }

        } else{
            break;
        }
    }
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值