C++数据结构学习:实现电梯动画

这个是一年前帮同学做的课设,但是放了一年才来放,可能有些效果没有很好实现。
效果:有电梯人数限制,等待的人随机概率决定是否继续等待
通过ctrl+c结束否则一直运行
在这里插入图片描述
在这里插入图片描述

1、Client.h

#pragma once
#ifndef CLIENT_H_
#define CLIENT_H_
#define MAX_LINEUP_SIZE 5
#define MAX_LIFT_SIZE 10 
#include<iostream>
#include<time.h>
using namespace std;
typedef enum { Up, Down, idle }Ele_Dir;
typedef enum { In, Wait, Leave }Per_Stage;
//定义乘客类型
typedef struct ClientType {
	int inFloor;
	int toFloor;
	int des_dir;//0下,1上,-1是出电梯或弃权走人
	int WaitingTime = 0;
	int GivenTime;
	Per_Stage CStage;//In,Wait,Leave
};
#endif

2、ClientList.h

#pragma once
#ifndef CLIENTLIST_H_
#define CLIENTLIST_H_
#include"Client.h"
typedef ClientType ElemType;
typedef struct QNode {
	ElemType client;
	struct QNode* next = NULL;
}QNode, * QueuePtr;
typedef struct {
	QueuePtr front;//队头
	QueuePtr rear;//队尾
}ClientLink;
//1.初始化
bool InitStack(ClientLink& S) {
	S.front = S.rear = new QNode;
	S.front->next = NULL;
	return true;
}
//2.入队
bool Push(ClientLink& L, ElemType e) {//front是无用的
	QNode* p = new QNode;
	p->client = e;
	p->next = NULL;
	L.rear->next = p;
	L.rear = p;
	return true;
}
//3.头出队
bool Pop(ClientLink& Q, ElemType& e) {
	if (Q.rear == Q.front) return false;
	QNode* p = Q.front->next;
	e = p->client;
	Q.front->next = p->next;
	//特殊注意:当最后一个出队时,要把尾指针指向头结点
	if (Q.rear == p) {
		//Q.front->next = NULL;
		Q.rear = Q.front;
	}
	delete p;
	return true;
}
bool isEmpty(ClientLink Q) {
	if (Q.front != Q.rear) {//判空
		return false;
	}
	else
		return true;
}
void show(ClientLink Q) {
	ElemType e;
	while (!isEmpty(Q)) {
		Pop(Q, e);
		cout << e.toFloor << " ";
	}
}
//特值出队
bool Pop2(ClientLink& Q, ElemType& e) {
	if (Q.rear == Q.front) return false;
	QNode* P = Q.front->next;
	QNode* T = Q.front;
	while (P->client.toFloor != e.toFloor && P != NULL) {//用2个属性来代表一个人
		T = P;
		P = P->next;
	}
	if (!P) return false;
	else //找到P
	{

		e = P->client;
		T->next = P->next;
		//特殊注意:当最后一个出队时,要把尾指针指向头结点
		if (Q.rear == P)
		{
			Q.rear = T;
		}
		delete P;
		return true;


	}
}



/*int inFloor;
	int toFloor;
	int des_dir;//0下,1上
	int WaitingTime;
	int GivenTime;
	Per_Stage CStage;//In,Wait,Leave*/
int InFloor(ClientLink S, int i) {
	QNode* p = S.front;
	int time = i + 1;
	if (!isEmpty(S)) {
		while (p && time) {
			p = p->next;
			time -= 1;
		}
		if (p) {
			return p->client.inFloor;
		}
	}
}
int ToFloor(ClientLink S, int i) {

	QNode* p = S.front;
	int time = i + 1;
	if (!isEmpty(S)) {
		while (p && time) {
			p = p->next;
			time -= 1;
		}
		if (p) {
			return p->client.toFloor;
		}
	}
	else
	{
		return -1;
	}
}
int Des_Dir(ClientLink S, int i) {
	if (!isEmpty(S)) {
		QNode* p = S.front;
		int time = i + 1;
		while (p && time) {
			p = p->next;
			time -= 1;
		}
		if (p)  return p->client.des_dir;
	}
}
int  Waiting_Time(ClientLink S, int i) {
	if (!isEmpty(S)) {
		QNode* p = S.front;
		int time = i + 1;
		while (p && time) {
			p = p->next;
			time -= 1;
		}
		if (p)  return p->client.WaitingTime;
	}
}
int   Given_Time(ClientLink S, int i) {
	if (!isEmpty(S)) {
		QNode* p = S.front;
		int time = i + 1;
		while (p && time) {
			p = p->next;
			time -= 1;
		}
		if (p)  return p->client.GivenTime;
	}
}
Per_Stage  C_Stage(ClientLink S, int i) {
	if (!isEmpty(S)) {
		QNode* p = S.front;
		int time = i + 1;
		while (p && time) {
			p = p->next;
			time -= 1;
		}
		if (p)  return p->client.CStage;
	}
}


#endif

3、ElevatorQueue.h

#pragma once
#ifndef ELEVATORQUEUE_H_
#define ELEVATORQUEUE_H_
#include"Floor.h"
#define MaxSize 10//循环队列最大长度电梯人数限制9
using namespace std;
typedef  ClientType  QElemType;//数据类型
typedef struct {
	QElemType* base;
	int front;//头指针
	int rear;//尾指针
	int inFloor;
	int clientNumber;
	Ele_Dir EStage;//Up,Down,idle
	int toFloor;
	int row;
}ElevatorQueue;//设置标志位(
bool InitQueue(ElevatorQueue& Q) {
	//空队列
	Q.base = new QElemType[MaxSize];
	for (int i = 0; i < MaxSize; i++) {
		Q.base[i].toFloor = -1;
	}
	Q.inFloor = 0;
	Q.clientNumber = 0;
	Q.row = 15;
	Q.EStage = idle;
	if (!Q.base)
		return false;
	Q.front = Q.rear = 0; return true;
}
//2.入列
bool EnQueue(ElevatorQueue& Q, QElemType e) {
	if ((Q.rear + 1) % MaxSize == Q.front)
		return false;//循环队满
	Q.base[Q.rear] = e;
	Q.rear = (Q.rear + 1) % MaxSize;//队尾加1
	return true;
}
//3.队长度
int QueueLength(ElevatorQueue Q) {
	return (Q.rear + MaxSize - Q.front) % MaxSize;
}
//4.出队列,删除头元素
bool DeQueue(ElevatorQueue& Q, QElemType& e) {
	if (Q.front == Q.rear)	return false;//判空
	e = Q.base[Q.front];
	Q.front = (Q.front + 1) % MaxSize;
	return true;
}
//5.取头元素
QElemType GetTop(ElevatorQueue Q) {
	if (Q.rear != Q.front)
		return Q.base[Q.front];
}
bool isEmpty(ElevatorQueue Q) {
	if (Q.rear == Q.front)
		return true;
	else return false;
}
//6.遍历
void ShowQueue(ElevatorQueue Q) {
	if (Q.front == Q.rear)
		cout << "队列空" << endl;
	while (Q.front != Q.rear) {
		cout << Q.base[Q.front].toFloor << " ";
		Q.front = (Q.front + 1) % MaxSize;
	}
}
int InFloor(ElevatorQueue Q) {
	return Q.inFloor;
}
int ClientNumber(ElevatorQueue Q) {
	return Q.clientNumber;
}
Ele_Dir E_Stage(ElevatorQueue Q) {//Up,Down,idle
	return Q.EStage;
}
int Row(ElevatorQueue Q) {
	return Q.row;
}
bool isFull(ElevatorQueue& Q) {
	if ((Q.rear + 1) % MaxSize == Q.front)
		return true;//循环队满
	else
		return false;
}
//===================生成楼层操作===================
void createFloor(ElevatorQueue lift, FloorType* lineup) {
	int EleLayer = lift.inFloor;
	int i;
	srand(unsigned(time(0)));
	for (i = 0; i <= 4; i++) {
		lineup[i].WaitNumber = rand() % MAX_LINEUP_SIZE;//至多4人
		lineup[i].EleAchieved = false;
		lineup[i].ffloor = i;
		lineup[i].downNumber = 0;
		lineup[i].upNumber = 0;
		lineup[i].passenger_leave.toFloor = -1;
		InitStack(lineup[i].passenger_wait);
		InitStack(lineup[i].passenger_waitDown);
		InitStack(lineup[i].passenger_waitUp);
	}
	QElemType client;
	//第零层
	lineup[0].WaitNumber = 3;
	for (i = 0; i < lineup[0].WaitNumber; i++) {
		client.inFloor = 0;
		client.toFloor = rand() % 4 + 1;//1-4
		client.des_dir = 1;
		client.GivenTime = rand() % 100 + 100;
		if (lineup[0].ffloor != lift.inFloor)
		{
			client.CStage = Wait;
			lineup[0].EleAchieved = false;
		}
		else {
			client.CStage = In;
			lineup[0].EleAchieved = true;
		}
		Push(lineup[0].passenger_wait, client);
		Push(lineup[0].passenger_waitUp, client);
		lineup[0].upNumber++; lineup[0].downNumber = 0;
	}
	//第四层
	for (i = 0; i < lineup[4].WaitNumber; i++) {
		client.inFloor = 4;
		client.toFloor = rand() % 4;//0-3
		client.des_dir = 0;
		client.GivenTime = rand() % 100 + 100;
		if (lineup[4].ffloor != lift.inFloor)
		{
			client.CStage = Wait;
			lineup[4].EleAchieved = false;
		}
		else
		{
			client.CStage = In;
			lineup[4].EleAchieved = true;
		}
		Push(lineup[4].passenger_wait, client);
		Push(lineup[4].passenger_waitDown, client);
		lineup[4].downNumber++;
		lineup[4].upNumber = 0;
	}
	//1-3层
	for (int j = 1; j <= 3; j++) {
		for (i = 0; i < lineup[j].WaitNumber; i++)
		{
			client.inFloor = j;
			client.des_dir = 1;
			if (client.des_dir == 0) {//下
				client.toFloor = 4;
				lineup[j].downNumber++;
				Push(lineup[j].passenger_wait, client);
				Push(lineup[j].passenger_waitDown, client);
			}

			else if (client.des_dir == 1) {//上

				client.toFloor = (j + 1) + rand() % (4 - j);

				lineup[j].upNumber++;
				Push(lineup[j].passenger_wait, client);
				Push(lineup[j].passenger_waitUp, client);
			}
			client.GivenTime = rand() % 100 + 100;
			if (lineup[j].ffloor != lift.inFloor)
			{
				client.CStage = Wait;
				lineup[j].EleAchieved = false;
			}
			else if (lineup[j].ffloor == lift.inFloor)//假定不限制时间
			{
				client.CStage = In;
				lineup[j].EleAchieved = true;
			}
		}

	}
}
int  ToMaxFloor(ElevatorQueue& lift) {
	if (!isEmpty(lift)) {
		int maxfloor = lift.front, minfloor = lift.front;
		int current = (lift.front + 1) % MaxSize;
		while (current != lift.rear) {
			if (lift.base[current].toFloor > lift.base[maxfloor].toFloor) maxfloor = current;
			if (lift.base[current].toFloor < lift.base[minfloor].toFloor) minfloor = current;
			current = (current + 1) % MaxSize;
		}
		if (lift.EStage == Up) return lift.base[maxfloor].toFloor;
		else if (lift.EStage == Down) return lift.base[minfloor].toFloor;
	}
	else {//初始化
		if (lift.inFloor == 0)
			return 4;
		else if (lift.inFloor == 4)
			return 0;
	}
}
bool isCallUp(FloorType* lineup, ElevatorQueue& lift) {
	int layer = lift.inFloor;
	if (lift.EStage == Up) {
		for (int i = ToMaxFloor(lift); i <= 3; i++) {//电梯里乘客到的最高层开始向上,判断再上有没有想上去的
			if (isGoUp(lineup, i))
				return true;
		}
		return false;
	}
	else if (lift.EStage == idle) {
		if (lift.inFloor == 0) {
			for (int i = 0; i <= 3; i++) {//电梯里乘客到的最高层开始向上,判断再上有没有想上去的
				if (isGoUp(lineup, i))
					return true;
			}
			return false;
		}
		else if (lift.inFloor == 4) {
			return false;
		}
	}
}
bool isCallDown(FloorType* lineup, ElevatorQueue& lift) {
	int layer = lift.inFloor;
	if (lift.EStage == Down) {
		for (int i = ToMaxFloor(lift); i >= 1; i--) {
			if (isGoDown(lineup, i))
				return true;
		}
		return false;
	}
	else if (lift.EStage == idle) {
		if (lift.inFloor == 4) {
			for (int i = layer; i >= 1; i--) {
				if (isGoDown(lineup, i))
					return true;
			}
			return false;
		}
		else if (lift.inFloor == 0) {
			return false;
		}
	}
}
bool isPushLineToList(FloorType* lineup, ElevatorQueue& lift) {
	int layer = lift.inFloor;
	QElemType e;
	if (lift.EStage == Down) {
		int waitPersonNumber = lineup[lift.inFloor].downNumber;
		if (!isFull(lift) && !isEmpty(lineup[lift.inFloor].passenger_waitDown)) {
			return true;
		}
		return false;
	}
	else if (lift.EStage == Up) {
		int waitPersonNumber = lineup[layer].upNumber;
		if (!isFull(lift) && !isEmpty(lineup[layer].passenger_waitUp)) {
			return true;
		}
		else
			return false;
	}
}
bool PushLineToList(FloorType* lineup, ElevatorQueue& lift) {
	int layer = lift.inFloor;
	QElemType e;
	if (lift.EStage == Down) {
		int waitPersonNumber = lineup[lift.inFloor].downNumber;
		if (!isFull(lift) && !isEmpty(lineup[lift.inFloor].passenger_waitDown)) {
			//	while (!isFull(lift) && !isEmpty(lineup[lift.inFloor].passenger_waitDown)) {
			if (!isFull(lift) && !isEmpty(lineup[lift.inFloor].passenger_waitDown)) {
				Pop(lineup[lift.inFloor].passenger_waitDown, e);
				Pop2(lineup[lift.inFloor].passenger_wait, e);
				lineup[lift.inFloor].downNumber--;
				lineup[lift.inFloor].WaitNumber--;
				EnQueue(lift, e); lift.clientNumber++;
			}
			return true;
		}
		return false;
	}
	else if (lift.EStage == Up) {
		int waitPersonNumber = lineup[layer].upNumber;
		if (!isFull(lift) && !isEmpty(lineup[layer].passenger_waitUp)) {
			//while (!isFull(lift) && !isEmpty(lineup[layer].passenger_waitUp)) {
			if (!isFull(lift) && !isEmpty(lineup[layer].passenger_waitUp)) {
				Pop(lineup[layer].passenger_waitUp, e);
				Pop2(lineup[layer].passenger_wait, e);
				lineup[layer].upNumber--; lineup[layer].WaitNumber--;
				EnQueue(lift, e); lift.clientNumber++;
			}
			return true;
		}
		else
			return false;
	}
}
int LiftToFloor(ElevatorQueue lift, int i) {//返回第几个人去的楼层

	if (lift.front != lift.rear) {//队列非空
		int time = i;
		int p = lift.front;
		while (time != 0 && p != (lift.rear) % MaxSize) {
			p = (p + 1) % MaxSize;
			time--;
		}
		if (p != (lift.rear) % MaxSize) {
			return lift.base[p].toFloor;
		}
	}

}
void LiftClientStageFollow(ElevatorQueue& lift) {
	int layer = lift.inFloor;
	int t = lift.front;
	QElemType temp;
	if (!isEmpty(lift)) {
		while (t != lift.rear) {
			lift.base[t].inFloor = layer;
			lift.base[t].CStage = In;
			t = (t + 1) % MaxSize;
		}
	}

}
void recreateFloor(ElevatorQueue lift, FloorType* lineup) {//补1或0人
	int EleLayer = lift.inFloor;
	int i;
	srand(unsigned(time(0)));
	for (i = 0; i <= 4; i++) {
		lineup[i].addWaitNumber = rand() % 2;
		lineup[i].WaitNumber += lineup[i].addWaitNumber;
	}
	QElemType client;
	//第零层
	for (i = 0; i < lineup[0].addWaitNumber; i++) {
		client.inFloor = 0;
		client.toFloor = 1;
		client.des_dir = 1;
		client.GivenTime = rand() % 100 + 100;
		if (lineup[0].ffloor != lift.inFloor)
		{
			client.CStage = Wait;
			lineup[0].EleAchieved = false;
		}
		else {
			client.CStage = In;
			lineup[0].EleAchieved = true;
		}
		Push(lineup[0].passenger_wait, client);
		Push(lineup[0].passenger_waitUp, client);
		lineup[0].upNumber++;
		lineup[0].downNumber = 0;
	}
	//第四层
	for (i = 0; i < lineup[4].addWaitNumber; i++) {
		client.inFloor = 4;
		client.toFloor = rand() % 4;//0-3
		client.des_dir = 0;
		client.GivenTime = rand() % 100 + 100;
		if (lineup[4].ffloor != lift.inFloor)
		{
			client.CStage = Wait;
			lineup[4].EleAchieved = false;
		}
		else
		{
			client.CStage = In;
			lineup[4].EleAchieved = true;
		}
		Push(lineup[4].passenger_wait, client);
		Push(lineup[4].passenger_waitDown, client);
		lineup[4].downNumber++;
		lineup[4].upNumber = 0;
	}
	//1-3层
	for (int j = 1; j <= 3; j++) {
		for (i = 0; i < lineup[j].addWaitNumber; i++)
		{
			client.inFloor = j;
			client.des_dir = 1;

			if (client.des_dir == 0) {//下
				client.toFloor = 4;
				lineup[j].downNumber++;
				Push(lineup[j].passenger_wait, client);
				Push(lineup[j].passenger_waitDown, client);
			}
			else if (client.des_dir == 1) {//上
				client.toFloor = (j + 1) + rand() % (4 - j);
				lineup[j].upNumber++;
				Push(lineup[j].passenger_wait, client);
				Push(lineup[j].passenger_waitUp, client);
			}
			client.GivenTime = rand() % 100 + 100;
			if (lineup[j].ffloor != lift.inFloor)
			{
				client.CStage = Wait;
				lineup[j].EleAchieved = false;
			}
			else if (lineup[j].ffloor == lift.inFloor)//假定不限制时间
			{
				client.CStage = In;
				lineup[j].EleAchieved = true;
			}
		}

	}
}
#endif

4、Floor.h

#pragma once
#include"ClientList.h"
#ifndef FLOOR_H_
#define FLOOR_H_
typedef struct FloorType {
	int WaitNumber;
	int ffloor;//楼层
	int upNumber;
	int downNumber;
	int addWaitNumber = 0;
	bool EleAchieved;
	ClientLink passenger_wait;
	ClientLink passenger_waitUp;
	ClientLink passenger_waitDown;
	ClientType passenger_leave;
};
bool isGoUp(FloorType* lineup, int i) {
	if (lineup[i].upNumber != 0)
		return true;
	else return false;
}
bool isGoDown(FloorType* lineup, int i) {
	if (lineup[i].downNumber != 0)
		return true;
	else return false;
}
#endif

5、ConsoleApplication1.cpp

#include"ElevatorQueue.h"
#include<windows.h>
#include<ctime>
#include<stdio.h>
typedef struct BuildingType			//存储电梯系统的结构体数组 
{
	int dt[100][100];
	int length;
	int width;
}buildingtype;
//判断有人要下
bool isPopListToLine(FloorType* lineup, ElevatorQueue& lift) {
	int currentFloor = lineup[lift.inFloor].ffloor;

	if (lift.front != lift.rear) {//队列非空		
		int p = lift.front;
		QElemType e;
		while (p != (lift.rear) % MaxSize) {
			if (lift.base[p].toFloor == currentFloor)
			{
				return true;
			}
			else
				p = (p + 1) % MaxSize;
		}
	}
	else
		return false;
}
//实现下电梯
bool PopListToLine(buildingtype* building, FloorType* lineup, ElevatorQueue& lift, QElemType& e) {
	int currentFloor = lineup[lift.inFloor].ffloor;
	if (lift.front != lift.rear) {//队列非空		

		if (isPopListToLine(lineup, lift)) {

			int p = lift.front;
			while (lift.base[p].toFloor != currentFloor && p != (lift.rear) % MaxSize) {
				p = (p + 1) % MaxSize;
			}
			if (p != (lift.rear) % MaxSize) {
				QElemType  temp = lift.base[p]; lift.base[p] = lift.base[lift.front];  lift.base[lift.front] = temp;

				DeQueue(lift, e);

				lift.clientNumber--;
				return true;
			}



		}


	}
	else
		return false;
}
//判断是否开门
bool isCallOpen(FloorType* lineup, ElevatorQueue& lift) {
	int layer = lift.inFloor;
	if ((lift.EStage == Up && isGoUp(lineup, layer) == true) || (lift.EStage == Down && isGoDown(lineup, layer) == true)) //到某一层刚好停下有同趋势
	{
		return true;
	}
	else if ((lift.EStage == Up && lift.inFloor == 4) || (lift.EStage == Down && lift.inFloor == 0))//达到最终层时
	{
		return true;
	}
	else if (isPopListToLine(lineup, lift))
		return true;
	else
		return false;
}
void print(buildingtype* building, FloorType* lineup, ElevatorQueue lift)打印出电梯系统的图形
{
	system("cls");
	int i, j;
	int m = building->width, n = building->length;
	for (i = 0; i < m; i++)
	{
		for (j = 0; j < n; j++)
		{
			switch (building->dt[i][j])
			{
			case 0:
				printf("  ");//0代表空 
				break;
			case 1:
				printf("█");//1代表墙 
				break;
			case 2:
				printf(" #");//2代表人
				break;
			case 3:
				printf("‖");//3代表电梯门
				break;
			case 4:
				printf("==");//4代表电梯一部分
				break;
			case 5:
				if (j - 14 < lineup[4 - i / 3].WaitNumber)
					printf("F%d", ToFloor(lineup[4 - i / 3].passenger_wait, j - 14)); //5代表 某一层的人头顶上的数字
				break;
			case 6:
				if ((j - 1) < lift.clientNumber && (j - 1) >= 0) {
					printf("F%d", lift.base[(j - 1 + lift.front) % MaxSize].toFloor);//6代表 电梯层的人头顶上的数字
				}
				break;
			default:
				printf("F%d", building->dt[i][j] - 7);
				break;
			}
		}
		printf("\n");
	}
	printf("\n\n");
	printf("\n\n");
	return;
}
void makelift(buildingtype* building, FloorType* lineup, ElevatorQueue lift)//初始化电梯系统的坐标 
{
	building->width = 17;
	building->length = 33;
	int i, j, m = building->width, n = building->length;

	for (i = 0; i < m; i++)
		for (j = 0; j < n; j++)
			building->dt[i][j] = 0;

	for (i = 0; i < m; i++)
	{
		for (j = 0; j < n; j++)
		{
			if (i == 0 || j == 0 || (i % 3 == 0 && j >= 13) || i == m - 1 || j == n - 1)//用来造:最外围的墙和5个楼层 
			{
				building->dt[i][j] = 1;
			}
			if (i % 3 == 2 && j > 13 && lineup[4 - i / 3].WaitNumber != 0 && (j - 13) <= lineup[4 - i / 3].WaitNumber)//用来造:每一层的人 
			{
				building->dt[i][j] = 2;
			}
			if (j == 13 && (i % 3 != 0) && building->dt[i][j] != 1)//用来造:电梯门
			{
				building->dt[i][j] = 3;
			}
			if (i == lift.row && j >= 1 && j <= 12)//用来造:电梯底座 
			{
				building->dt[i][j] = 4;
				//电梯对应门
				if (lift.row % 3 == 0 && isCallOpen(lineup, lift)) {
					building->dt[i - 1][13] = 0;
					building->dt[i - 2][13] = 0;
				}
				if (lift.clientNumber != 0 && j >= 1 && j <= (lift.clientNumber))//用来造:电梯每一层的人 
				{
					building->dt[i - 1][j] = 2;
					building->dt[i - 2][j] = 6;
				}
			}
			if (i % 3 == 1 && j > 13 && lineup[4 - i / 3].WaitNumber != 0 && (j - 13) <= lineup[4 - i / 3].WaitNumber)//用来造:每一层的人头顶上的数字 
			{
				building->dt[i][j] = 5;
			}


		}
	}
}
void repaint(buildingtype* building, FloorType* lineup, ElevatorQueue& lift) {
	if (lift.EStage == Up)
	{
		while (lift.row != 2)
		{
			if (lift.row % 3 == 0) {//到达5个楼层 	
				int currentFloor = lift.inFloor;
				lift.inFloor = (15 - lift.row) / 3;// lift.inFloor = 4 - (lift.row - 3) / 3;
				int i = 2;
				int j = 3;
				if (isCallOpen(lineup, lift))
				{
					while (isPopListToLine(lineup, lift)) {
						QElemType* e = &lineup[lift.inFloor].passenger_leave;
						PopListToLine(building, lineup, lift, lineup[lift.inFloor].passenger_leave);
						int flag = e->toFloor;
						makelift(building, lineup, lift);
						building->dt[12 - 3 * currentFloor - i][building->length - j] = 7 + flag;
						building->dt[13 - 3 * currentFloor - i][building->length - j] = 2;
						print(building, lineup, lift);
						cout << "开门下电梯" << endl;
						Sleep(500);
					}
					while (isPushLineToList(lineup, lift)) {
						PushLineToList(lineup, lift);
						makelift(building, lineup, lift);
						Sleep(1000);
						building->dt[lift.row - 1][13] = 0;
						building->dt[lift.row - 2][13] = 0;
						print(building, lineup, lift);
						cout << "开门上电梯" << endl;
						Sleep(500);
					}
					if (lift.row == 3 && isCallDown(lineup, lift)) {
						building->dt[lift.row - 1][13] = 0;
						building->dt[lift.row - 2][13] = 0;
					}
					else {
						building->dt[lift.row - 1][13] = 3;
						building->dt[lift.row - 2][13] = 3;
					}
					print(building, lineup, lift);
				}
				int layer = lift.inFloor;
				int t = lift.front;
				检查用
				//QElemType temp;
				//if (!isEmpty(lift)) {
				//	while (t != lift.rear) {
				//		cout << lift.base[t].toFloor << " ";
				//		t = (t + 1) % MaxSize;
				//	}
				//}
				//Sleep(1000);
			}
			else {
				Sleep(100);
				makelift(building, lineup, lift);
				print(building, lineup, lift);
				Sleep(500);
				recreateFloor(lift, lineup);
				makelift(building, lineup, lift);
				print(building, lineup, lift);
				cout << "楼层可能进入新的等待群体" << endl;
				Sleep(500);
			}
			lift.row -= 1;
			LiftClientStageFollow(lift);

		}
		lift.row += 1;
	}
	else if (lift.EStage == Down)
	{
		while (lift.row != 16)
		{
			if (lift.row % 3 == 0) {//到达5个楼层 	
				lift.inFloor = (15 - lift.row) / 3;
				int currentFloor = lift.inFloor - 1;
				int i = 2;
				int j = 3;
				if (isCallOpen(lineup, lift))
				{
					while (isPopListToLine(lineup, lift)) {
						QElemType* e = &lineup[lift.inFloor].passenger_leave;
						PopListToLine(building, lineup, lift, lineup[lift.inFloor].passenger_leave);
						int flag = e->toFloor;
						makelift(building, lineup, lift);
						building->dt[12 - 3 * currentFloor - i][building->length - j] = 7 + flag;
						building->dt[13 - 3 * currentFloor - i][building->length - j] = 2;
						building->dt[lift.row - 1][13] = 0;
						building->dt[lift.row - 2][13] = 0;
						print(building, lineup, lift);
						cout << "开门下楼" << endl;
						Sleep(500);
					}
					while (isPushLineToList(lineup, lift)) {
						PushLineToList(lineup, lift);
						makelift(building, lineup, lift);
						Sleep(500);
						building->dt[lift.row - 1][13] = 0;
						building->dt[lift.row - 2][13] = 0;
						print(building, lineup, lift);
						cout << "开门上电梯" << endl;
						Sleep(500);
					}
					if (lift.row == 15 && isCallUp(lineup, lift)) {
						building->dt[lift.row - 1][13] = 0;
						building->dt[lift.row - 2][13] = 0;
					}
					else {
						building->dt[lift.row - 1][13] = 3;
						building->dt[lift.row - 2][13] = 3;
					}
					print(building, lineup, lift);
				}
				检查用
				//int layer = lift.inFloor;
				//int t = lift.front;
				//QElemType temp;
				//if (!isEmpty(lift)) {
				//	while (t != lift.rear) {
				//		cout << lift.base[t].toFloor << " ";
				//		t = (t + 1) % MaxSize;
				//	}
				//}
				//Sleep(1000);
			}
			else {
				makelift(building, lineup, lift);
				print(building, lineup, lift);
				Sleep(500);
				recreateFloor(lift, lineup);
				makelift(building, lineup, lift);
				print(building, lineup, lift);
				cout << "楼层可能进入新的等待群体" << endl;
				Sleep(500);
			}
			lift.row += 1;
			LiftClientStageFollow(lift);
		}
		lift.row -= 1;
	}
	lift.EStage = idle;

}
int main()
{
	buildingtype* building = (buildingtype*)malloc(sizeof(buildingtype));
	ElevatorQueue  lift;
	//电梯的初始化
	InitQueue(lift);
	FloorType  lineup[5]; //lineup结构体数组:所有5层楼的排队的人 	
	createFloor(lift, lineup);
	makelift(building, lineup, lift);
	print(building, lineup, lift);
	while (true) {
		if (isCallUp(lineup, lift) == true) {
			lift.EStage = Up;
		}
		else if (isCallDown(lineup, lift) == true) {
			lift.EStage = Down;
		}
		else
			lift.EStage = idle;
		repaint(building, lineup, lift);
	}
	return 0;
}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
设计一个电梯模拟系统。这是一个离散的模拟程序,由随机事件驱动,以模拟时钟决定乘客或电梯的动作发生的时刻和顺序,系统在某个模拟瞬间处理有待完成的各种事情,然后把模拟时钟推进到某个动作预定要发生的下一时刻。 要求: (1)模拟某校九层教学楼的电梯系统。该楼有一个自动电梯,能在每层停留,其中第一层是大楼的进出层,即是电梯的“本垒层”,电梯“空闲”时,将来到该层候命。 电梯一共有八个状态,即正在开门(Opening)、已开门(Opened)、正在关门(Closing)、已关门(Closed)、等待(Waiting)、移动(Moving)、加速(Accelerate)、减速(Decelerate)。 (2)乘客可随机地进出于任何层。对每个人来说,他有一个能容忍的最长等待时间,一旦等候电梯时间过长,他将放弃。最后一个人放弃能不能取消按键? (3)模拟时钟从0开始,时间单位为0.1秒。人和电梯的各种动作均要消耗一定的时间单位(简记为t),比如: 有人进出时,电梯每隔40t测试一次,若无人进出,则关门; 关门和开门各需要20t; 每个人进出电梯均需要25t; 电梯加速需要15t; 下行时要不要加速? 上升时,每一层需要51t,减速需要14t;每一层和减速? 下降时,每一层需要61t,减速需要23t; 如果电梯在某层静止时间超过300t,则驶回1层候命。驶回本垒层间接到消息? (4)电梯调度规则如下: ①就近原则:电梯的主要调度策略是首先响应沿当前行进方向上最近端的请求直到满足最远端请求。若该方向上无请求时,就改变移动方向; ②在就近原则无法满足的情况下,首先满足更高层的请求; ③电梯的最大承载人数为13人,电梯人数达到13人后,在有人出电梯之前,不接受进入电梯的请求; ④乘客上下电梯时先出后进。进电梯时乘客是按发出乘坐请求的顺序依次进入,每次只能进入一人且每个人花费的时间都为25t; ⑤电梯在关门期间(电梯离开之前)所在层提出请求的乘客同样允许进入。 (5)按时序显示系统状态的变化过程,即发生的全部人和电梯的动作序列。 扩展要求: 实现电梯模拟的可视化界面。用动画显示电梯的升降,人进出电梯。设计有下列对象:电梯、人、电梯控制板及其上各种按钮、模拟时钟等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

广大菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值