这个是一年前帮同学做的课设,但是放了一年才来放,可能有些效果没有很好实现。
效果:有电梯人数限制,等待的人随机概率决定是否继续等待
通过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;
}