lift.cpp

来自「非常完整的电梯模拟系统 欢迎大家提出宝贵建议」· C++ 代码 · 共 105 行

CPP
105
字号
#include "Lift.h"

Lift::Lift()
{
	state=LEISURE;
	weight=0;
	count=0;
	Building B;
}

void Lift::EnterLift(int floor,int state)
{
	assert(floor>=0);
	Person p;
	if(state==UP)
	{
		while(!B.Up[floor].IsEmpty())
		{
			cout<<endl;
			p=B.Up[floor].DeQueue();
			if(weight<MAXW&&count<=MAXN)
			{
				lift.EnQueue1(p,p.to);
			    weight+=p.weight;
			    count++;
			    cout<<p;
			    cout<<"\t该人进入了电梯"<<endl;
			}
			else
				break;
		}
	}
	if(state==DOWN)
	{
		while(!B.Down[floor].IsEmpty())
		{
			cout<<endl;
			p=B.Down[floor].DeQueue();
			if(weight<MAXW&&count<=MAXN)
			{
				lift.EnQueue2(p,p.to);
			    weight+=p.weight;
				count++;
				cout<<p;
				cout<<"\t该人进入了电梯"<<endl;
			}
			else
				break;
		}
	}
}

void Lift::OutLift(int floor,int state)
{
	assert(floor>=0);
	Person p;
	if(state==UP||state==DOWN)
	{
		while(!lift.IsEmpty())
		{
			cout<<endl;
			p=lift.Get();
			if(p.to==floor+1)
			{
				lift.DeQueue();
				weight-=p.weight;
				count--;
				cout<<p;
				cout<<"\t该人走出了电梯"<<endl;
			}
			else
				break;
		}
	}
}

void Lift::Run()
{
	while(1)
	{
		Lift l;
		for(int i=0;i<TOTAL_FLOOR;i++)
		{
			for(long j=0;j<500000000;j++);
		    cout<<"\t***************************"<<endl;
		    cout<<"\t第"<<i+1<<"层"<<endl;
		    l.EnterLift(i,1);
		    l.OutLift(i,1);
		    cout<<"\t电梯现有人数"<<l.GetCount()<<endl;
		    cout<<"\t电梯现在载重"<<l.GetWeight()<<endl;
			cout<<"\t***************************"<<endl;
		}
		for(i=9;i>=0;i--)
		{
			for(long j=0;j<500000000;j++);
			cout<<"\t***************************"<<endl;
			cout<<"\t第"<<i+1<<"层"<<endl;
			l.EnterLift(i,2);
			l.OutLift(i,2);
			cout<<"\t电梯现有人数"<<l.GetCount()<<endl;
			cout<<"\t电梯现在载重"<<l.GetWeight()<<endl;
			cout<<"\t***************************"<<endl;
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?