elevator.cpp

来自「关于一个五层楼的电梯模型,关于栈与队列的应用,内有实验报告.」· C++ 代码 · 共 58 行

CPP
58
字号
#define test 0	//test为1时对电梯效率进行测试,test为0时观察电梯运行情况。
#include "Building.h"

int main() {
	Elevator E;//电梯
	WQueue	w[Maxfloor+1][2];//每层都有两个等待队列:上楼队列和下楼队列
	//初始化
	InitEle(E);
	srand( (unsigned)time( NULL ));
	for(int i=0;i<=Maxfloor;i++) {
		InitQueue(w[i][Up]);
		InitQueue(w[i][Down]);
	}
#if test
	MaxTime=100000;//12099999
#else
	cout<<"请输入电梯的运行时间:";
	cin>>MaxTime;
#endif
	while(Time++<MaxTime) {
		//---------------乘客事件----------------
		//新乘客进入事件
		if(InterTime==0) NewClient(E,w);
		else InterTime--;
		//乘客放弃事件放弃
		for(int i=0;i<=Maxfloor;i++)
			for(int j=0;j<2;j++) {
				CGiveUp(w[i][j],EleFloor(E));
			}
		//乘客进出事件
		if(InOutCount==0) {
			if(EleStatus(E)==Opened) InOut(E,w);
		}
		else InOutCount--;
		//---------------电梯事件----------------
		if(CountOver(E)||EleStatus(E)==Closed||EleStatus(E)==Waiting) {
			Action a;
			a=ElevatorRun(E);
#if !test
			Print(E,a);
			if(EleStatus(E)==Decelerate) PrintStatus(E,w);
#endif
		}
#if !test
		Sleep(100);//延迟0.1s
#endif
	}
	cout<<"共"<<ID<<"人进入系统,";
	cout<<"共"<<GiveUpNumber<<"人放弃。"<<endl;
	cout<<"平均等待时间为:"<<TotalTime/(ID)<<endl;
	DestoryEle(E);
	for(i=0;i<=Maxfloor;i++) {
		DestroyQueue(w[i][Up]);
		DestroyQueue(w[i][Down]);
	}
	return 0;
}

⌨️ 快捷键说明

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