#include "elevator.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
node head; //头结点
node *p_head = &head; //头指针
node *p_tail = &head;//尾指针
int holdtime = 0, record = 0, near = 0;
elevator m;
elevator::elevator()
{
}
elevator::~elevator()
{
}
state elevator::getnowState()const {
return State;
}
void elevator::setnowState(state t) {
State = t;
}
int elevator::getfloor()const {
return floor;
}
void elevator::setfloor(int floor1) {
floor = floor1;
}
int elevator::getAll()const {
return All;
}
void elevator::setAll(int num) {
All += num;
}
int elevator::getDir()const {
return Dir;
}
void elevator::setDir(int num) {
Dir = num;
}
void elevator::addpassenger(passenger &x) {
m.append(x);
cout << " 第" << x.getID() << "名乘客进入等待队列 ";
//如果电梯本身在一楼则不需要进行操作
}
//判断是否有乘客请求 300t 电梯运行开始的点
bool elevator::NoPassenger() {
if (getnowState() == Waiting) { //如果电梯处于等待状态 判断是否达到300t
if (holdtime == 300 && floor != 1) {
//如果等够了300t并且不在1楼的话,开始回到一楼
setnowState(Down);
setDir(0);//往下
holdtime = 0;
MoveDown();
return true;
}
else if (holdtime == 300 && floor == 1) {
cout << "电梯空闲无人,正在一楼等待";
holdtime = 0;
return true ;
}
else if (All == 0 && m.empty() == true) {
cout << "电梯空闲无人,正在等待";
holdtime++;
return true;
}
else {
JudgeDirction();
return false;
}
}
else {
JudgeDirction();
return false;
}
}
//******判断电梯的走向********
void elevator::JudgeDirction() {
near = floor;//初始化near的值,为防止不满足条件的情况出现时依旧可以使用MoveDirction函数
int upoutfar = 0, downoutfar = 10;//定义2个记录上下出去最远请求的变量
int upinfar = 0, downinfar = 10;//定义2个记录上下进来最远请求的变量
if (State == Closed) {
if (getAll() == 0 && m.empty() == true) {//电梯里没人 也没有乘客在等待了
setnowState(Waiting);
return;
}
if (floor == 1) {
//设方向向上
setnowState(Up); setDir(1);
}
if (floor == 9) {
//向下
setnowState(Down); setDir(0);
}
if (getAll() != 0) {//电梯还有人
//此时需要区分电梯的运行方向,分两种情况来处理
for (int i = 1; i < 10; i++) {
if (DiantiLi[i] != 0) {
near = i;
if (i > floor) {
upinfar = i;
setnowState(Up);
setDir(1);
break;
}
else { //乘客是向下的
downinfar = i;
setnowState(Down);
setDir(0);
break;
}
}
}
}
if (m.empty() == false) {//电梯外等待的人不为空
node *ptemp = p_head->next; //遍历乘客
while (ptemp != NULL) {
if (getDir() == 1) {//只接上行乘客
if (ptemp->data.getnowfloor() > floor) {//乘客所处楼层在电梯楼层上,这样才有可能电梯前往接人
//开始检索在链表中的乘客,并且进行判断是否有要上行的
if (ptemp->data.getgofloor() > ptemp->data.getnowfloor()) {
//代表此人是上行,并且是在电梯运行方向的楼上,需要前往接人
if (ptemp->data.getgofloor() > upoutfar)upoutfar = ptemp->data.getgofloor(); //改变最远到达层数
}
if (ptemp->data.getgofloor() < ptemp->data.getnowfloor()) {
//乘客要下行 并且现在在电梯达到最上层 还往上 则电梯需要劳烦去接这位乘客 即修改最远到达位置
if (ptemp->data.getnowfloor() > upoutfar)upoutfar = ptemp->data.getnowfloor();
}
}
if (upinfar == 0 && upoutfar == 0) { setnowState(Down); setDir(0); }
if (upinfar <= upoutfar)near = upoutfar;
else near = upinfar;
}
else if (getDir() == 0) {//只接下行乘客
if (ptemp->data.getnowfloor() < floor) {//乘客所处楼层在电梯楼层下,这样才有可能电梯前往接人
//开始检索在链表中的乘客,并且进行判断是否有要下行的
if (ptemp->data.getgofloor() < ptemp->data.getnowfloor()) {
//代表此人是下行,并且是在电梯运行方向的楼下,需要前下接人
if (ptemp->data.getgofloor() < downoutfar)downoutfar = ptemp->data.getgofloor();
}
if (ptemp->data.getgofloor() > ptemp->data.getnowfloor()) {
//同上行最远一样 电梯往下送外完乘客后 还要劳烦去接他
if (ptemp->data.getnowfloor() < downoutfar)downoutfar = ptemp->data.getnowfloor();
}
}
//在第一层或者最高层
if (downinfar == 10 && downoutfar == 10) { setnowState(Up); setDir(1); }
if (downinfar <= downoutfar)near = downinfar;
else near = downoutfar;
}
ptemp = ptemp->next;
}
}
}
//电梯处于等待状态
if (State == Waiting) {
node *ptemp = p_head->next; //指针*ptemp访问乘客
int time_now = 0;
while (ptemp != NULL) {
//whenwait 就是乘客到达电梯处的时间 按键
int Time_now = ptemp->data.getwhenwait();//用于记录最先按的人
time_now = Time_now;
if (ptemp->data.getwhenwait()<time_now) {//挑选出最先按键的人
time_now = ptemp->data.getwhenwait();
}
ptemp = ptemp->next;
}
ptemp = p_head->next;
while (ptemp != NULL) {
int up(floor), down(floor);//当前
if (ptemp->data.getwhenwait() == time_now) {
int x = ptemp->data.getgofloor() - ptemp->data.getnowfloor(); //目的—当前
//此时会出现4种情况,并且只会有2种是电梯上行,2种是电梯下行
if ((x > 0 && (ptemp->data.getnowfloor()>floor)) || (x<0 && (ptemp->data.getnowfloor()>floor))) {
//乘客在电梯上面
setnowState(Up);
setDir(1);
upinfar = ptemp->data.getnowfloor(); //乘客当前位置
if (up < upinfar) {
up = upinfar; //更新上升到达位置
}
}
else if ((x > 0 && (ptemp->data.getnowfloor()<floor)) || (x<0 && (ptemp->data.getnowfloor()<floor))) {
//乘客在电梯当前位置下面
setnowState(Down);
setDir(0);
downinfar = ptemp->data.getnowfloor();
if (down > downinfar) {
down = downinfar;
}
}
if (down != floor&&up != floor) {//当上下同时有人请求时,满足先上的原则
setnowState(Up); near = upinfar; setDir(1);
}
else if (up != floor) {//只有上行的请求
setnowState(Up); near = upinfar; setDir(1);
}
else if (down != floor) {//只有下行的请求
setnowState(Down); near = downinfar; setDir(0);
}
if (floor == 1) { setnowState(Up); setDir(1); }
}
ptemp = ptemp->next;
}
if (near == floor) Open();
}
//电梯是上升状态
if (State == Up) {
if (getAll() != 0) {//电梯里有人
for (int i = 1; i < 10; i++) {
if (DiantiLi[i] != 0) {
if (i >= near) {
upinfar = i; //更新最远
}
}
}
}
if (m.empty() == false) { //外面等待的人不为空
node *ptemp = p_head->next;
while (ptemp != NULL) {
if (ptemp->data.getnowfloor() >= near) { //人在位置比电梯到达的位置还远
if (ptemp->data.getgofloor() >= upoutfar) {
upoutfar = ptemp->data.getgofloor();
}
if (ptemp->data.getnowfloor() >= upoutfar) {
upoutfar = ptemp->data.getnowfloor();
}
}
ptemp = ptemp->next;
}
}
if (upinfar >= upoutfar)near = upinfar;
else near = upoutfar;
}
//电梯下降状态
if (State == Down) {
if (getAll() != 0) {//电梯里有人
for (int i = 1; i < 10; i++) {
if (DiantiLi[i] != 0) {
if (i <= near)
{
downinfar = i; //更新电梯往下到达的最远
break;
}
}
}
}
if (m.empty() == false) { //电梯外面还有人
node *ptemp = p_head->next;
while (ptemp != NULL) {
if (ptemp->data.getnowfloor() <= near) {
if (ptemp->data.getnowfloor() <= downoutfar) {
//乘客在的位置比电梯到达的位置还远
downoutfar = ptemp->data.getnowfloor();
}
if (ptemp->data.getgofloor() <= downoutfar) {
downoutfar = ptemp->data.getgofloor();
}
}
ptemp = ptemp->next;
}
}
if (getAll() == 0 && m.empty() == true)near = 1; //没有乘客了 回一层待命
//还有乘客的话 比较电梯里外最远位置
else if (downinfar >= downoutfar) near = downoutfar;
else near = downinfar;
}
MoveDirction(near);
}
//判断此楼层是否有人需要