Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of http://github.com/Th0ar/Elevator-Emulator

  • Loading branch information...
commit fad6e9b62eee410b3a9f8d1f9a2769e509ffa3a0 2 parents 8fe345a + 41c4413
Kaikai Zhu authored
View
2  Headers/elevator.h
@@ -61,6 +61,8 @@ class Elevator
void Move(); /* 电梯依据指令栈顶移动,否则按照随即下客顺序移动 */
+ void AsciiMove();
+
void PushInst(Instruction*); /* 添加指令 */
std::list<Instruction*> *Get_inst(); /* 得到指令栈的指针 */
View
42 Headers/floor.h
@@ -10,37 +10,43 @@ class Floor
friend class Passenger;
friend class Emulator;
private:
- list<Passenger*> passengers;
- list<Passenger*> inPassengers;
- list<Passenger*> outPassengers;
- Order* upOrder;
- Order* downOrder;
+ list<Passenger*> passengers; /* 楼层乘客 */
+
+ list<Passenger*> inPassengers; /* 将来还未到的乘客 */
+
+ list<Passenger*> outPassengers; /* 离开楼层的乘客 */
+
+ Order* upOrder; /* 楼层向上的请求,相当于按下的向上按钮 */
+
+ Order* downOrder; /* 楼层的向下请求,相当于按下的向下按钮 */
public:
- Floor(); /* Construct funtion */
+ Floor(); /* Construct funtion */
+
+ void PushPassenger(Passenger *p); /* Let a new passenger p comes in */
- void PushPassenger(Passenger *p); /* Let a new passenger p comes in */
+ void AsciiPushPassenger(Passenger* pass); /* Push passenger function for ascii */
- int Get_nPassenger(); /* Get totalnum of passengers on a floor */
+ int Get_nPassenger(); /* Get totalnum of passengers on a floor */
- Passenger *PopPassenger(); /* Pop and return the frontest passenger from queue */
+ Passenger *PopPassenger(); /* Pop and return the frontmost passenger from queue */
- Passenger *FrontPassenger(); /* Get passenger at the front of the queue(list) */
+ Passenger *FrontPassenger(); /* Get passenger at the front of the queue(list) */
- void LetThemWait(); /* Let passengers wait for one unit time */
+ void LetThemWait(); /* Let passengers wait for one unit time */
- bool Ordered(); /* Return true if floor has order an elevator */
+ bool Ordered(); /* Return true if floor has order an elevator */
- void Set_downOrder(Order* order); /* 设置下楼的请求 */
+ void Set_downOrder(Order* order); /* 设置下楼的请求 */
- void Set_upOrder(Order* order); /* 设置向上的请求 */
+ void Set_upOrder(Order* order); /* 设置向上的请求 */
- bool UpOrdered(); /* 返回向上按钮是否被按(是否有向上请求) */
+ bool UpOrdered(); /* 返回向上按钮是否被按(是否有向上请求) */
- bool DownOrdered(); /* 返回向下按钮是否被按(是否有向下请求) */
+ bool DownOrdered(); /* 返回向下按钮是否被按(是否有向下请求) */
- void CancelOrder(char dir); /* 取消 dir 方向的请求 */
+ void CancelOrder(char dir); /* 取消 dir 方向的请求 */
- void CheckOrder(); /* 楼层检查是否有乘客要去某方向,但是这个方向的按钮没有被按,如果有这种情况,就让该乘客请求一部电梯 */
+ void CheckOrder(); /* 楼层检查是否有乘客要去某方向,但是这个方向的按钮没有被按,如果有这种情况,就让该乘客请求一部电梯 */
void PushOutPassengers(Passenger* pass);
};
View
1  Headers/instruction.h
@@ -7,6 +7,7 @@ class Instruction
public:
Instruction(int dest_, int dirCode_) : dest(dest_), dirCode(dirCode_){};
int dest; /* 指令目的地 */
+
int dirCode; /* 指令的载人方向
* 1: 中途开门接受上、下两个方向乘客
* 2: 中途开门只接受向上的乘客
View
47 Headers/passenger.h
@@ -7,22 +7,37 @@
class Passenger
{
private:
- int timeWait;
- int timeMove;
- int from;
- int to;
- static int total;
- static int maxWaitTime;
- static int minWaitTime;
- static int totalWaitTime;
- static int maxDuration;
- static int minDuration;
- static int totalDuration;
- static int nCompleteDuration;
-
- double fpos;
- int status;
+ int timeWait; /* 每个乘客等待时间 */
+ int timeMove; /* 在电梯中运动时间 */
+
+ int from; /* 出发地 */
+
+ int to; /* 目的地 */
+
+ double fpos; /* 浮点数位置 */
+
+ int status; /* 标示乘客的状态
+ * 1. 还未到楼层
+ * 2. 在楼层等待,或在电梯
+ * 3. 正在离开楼层
+ */
+
+ static int total; /* 总乘客数 */
+
+ static int maxWaitTime; /* 最大等待时间 */
+
+ static int minWaitTime; /* 最小等待时间 */
+
+ static int totalWaitTime; /* 总等待时间 */
+
+ static int maxDuration; /* 最大过程时间 */
+
+ static int minDuration; /* 最小过程时间 */
+
+ static int totalDuration; /* 总过程时间 */
+
+ static int nCompleteDuration; /* 完成的载客过程的数目 */
public:
Passenger(int from_, int to_); /* Construct a passenfer from `from_` to `to_` */
@@ -54,6 +69,6 @@ class Passenger
void MakeAnOrder(); /* 乘客按下电梯按钮,每层楼有两个按钮,乘客将根据目的地选择,以按下了乘客所需
* 按钮时乘客不会再按 */
- double Get_fpos();
+ double Get_fpos(); /* 获得乘客的浮点数位置,用作图形界面中来客和离客的过程 */
};
#endif
View
2  Makefile
@@ -12,4 +12,4 @@ ascii: Sources/ascii.cpp Sources/elevator.cpp Sources/emulator.cpp Sources/fcfs.
$(CC) $(CFLAGS) $(LIBS) -o ascii Sources/ascii.cpp Sources/elevator.cpp Sources/emulator.cpp Sources/fcfs.cpp Sources/passenger.cpp \
Sources/floor.cpp Sources/PoissRand.cpp Sources/pickup.cpp Sources/order.cpp Sources/Print.cpp Sources/costflow.cpp \
- Sources/Scan.cpp
+ Sources/Scan.cpp
View
13 Sources/ascii.cpp
@@ -36,7 +36,8 @@ void Born() {
int nPassenger = PoissRand(Emulator::lambda);
for(int j = 0; j < nPassenger; j++) {
Passenger *pass = new Passenger(i, floor(Rand(0, Emulator::nFloor)));
- Emulator::floors[i].PushPassenger(pass);
+
+ Emulator::floors[i].AsciiPushPassenger(pass);
pass->MakeAnOrder();
}
@@ -44,16 +45,16 @@ void Born() {
}
void Control() {
- if (Emulator::elevatorType == "PFCFS") {
+ if (Emulator::elevatorType == "fcfs") {
Controller::Fcfs();
} else if (Emulator::elevatorType == "costflow") {
Controller::CostFLow();
} else if (Emulator::elevatorType == "pickup") {
- Controller::PickUp();
+ Controller::PickUp();
} else if (Emulator::elevatorType == "scan") {
- Controller::Scan();
+ Controller::Scan();
}
- /* else if (Emulator::elevatorType == "costflow") {
+ /* else if (Emulator::elevatorType == "xxx") {
* Controller::costflow();
*
* } */
@@ -62,7 +63,7 @@ void Control() {
void Move() {
EleIter iter;
for(iter = Emulator::elevators.begin(); iter != Emulator::elevators.end(); iter++) {
- (*iter)->Move();
+ (*iter)->AsciiMove();
}
}
View
37 Sources/elevator.cpp
@@ -34,6 +34,7 @@ void Elevator::CloseDoor() {
if (toggleDoorCounter == 0)
doorStatus = 0;
}
+
void Elevator::Move() {
int inst = 0;
if (instructions.empty()) {
@@ -73,6 +74,41 @@ void Elevator::Move() {
}
}
+void Elevator::AsciiMove() {
+ int inst = 0;
+ if (instructions.empty()) {
+ if (!passengers.empty()) {
+ inst = passengers.front()->Get_to();
+ } else return;
+ } else {
+ inst = instructions.back()->dest;
+ }
+ if (pos == inst) {
+ dir = '-';
+ if (doorStatus == 0) doorStatus = 1;
+ else {
+ int dirCode = defaultDir;
+ if (!instructions.empty()) {
+ dirCode = instructions.back()->dirCode;
+ instructions.pop_back();
+ }
+ Letout();
+ Letin(dirCode);
+ }
+ } else {
+ if (doorStatus == 1) doorStatus = 0;
+ else {
+ if (pos > inst) {
+ pos--;
+ dir = 'v';
+ } else {
+ pos++;
+ dir = '^';
+ }
+ }
+ }
+}
+
/*
* Function template, provides assertion to passgers->remove_if() function.
* Return:
@@ -102,7 +138,6 @@ void Elevator::Letout() {
void Elevator::Letin(int dirCode) {
Floor* floor = &Emulator::floors[pos];
-
while (floor->Get_nPassenger() && passengers.size() < unsigned(capacity)) {
Passenger *pass = floor->PopPassenger();
if (pass) passengers.push_back(pass);
View
9 Sources/fcfs.cpp
@@ -1,6 +1,7 @@
#include "../Headers/models.h"
#include "../Headers/emulator.h"
+/* 找寻最空闲的电梯,空闲电梯是指停留着的没有指令的没有载客的电梯,如果没有找到这样的电梯返回 NULL */
Elevator* Idlest() {
EleIter iter;
@@ -10,13 +11,21 @@ Elevator* Idlest() {
return NULL;
}
+/* 先来先服务算法 */
void Controller::Fcfs() {
+ /* 设置电梯默认运行方向为上下两个方向 */
Elevator::defaultDir = 1;
+
while(Idlest() != NULL) {
+ /* 如果没有请求,不做调度 */
if (Emulator::orders.empty()) return;
+
Elevator* idle = Idlest();
+
+ /* 否则取出最早的请求,交给空闲电梯 */
Order* order = Emulator::orders.front();
Emulator::orders.pop_front();
+
idle->PushInst(new Instruction(order->from, 1));
}
}
View
4 Sources/floor.cpp
@@ -12,6 +12,10 @@ void Floor::PushPassenger(Passenger *p) {
inPassengers.push_back(p);
}
+void Floor::AsciiPushPassenger(Passenger *p) {
+ passengers.push_back(p);
+}
+
Passenger *Floor::PopPassenger() {
Passenger *p = NULL;
if(!passengers.empty()) {
View
4 Sources/passenger.cpp
@@ -91,12 +91,14 @@ void Passenger::MakeAnOrder() {
if(to > from) {
if (floor->UpOrdered()) return;
Order *order = new Order(this);
- Emulator::orders.push_back(order);
floor->Set_upOrder(order);
+
+ Emulator::orders.push_back(order);
} else {
if (floor->DownOrdered()) return;
Order *order = new Order(this);
floor->Set_downOrder(order);
+
Emulator::orders.push_back(order);
}
}
View
46 Sources/qt/emulator.cpp
@@ -131,6 +131,20 @@ void Emulator::paintEvent(QPaintEvent *event)
painter.setPen(QColor(Qt::black));
}
+ /* 绘制乘客 */
+ for (i = 0; i < nFloor; i++) {
+ list<Passenger*>::iterator iter;
+ for (iter = floors[i].inPassengers.begin(); iter != floors[i].inPassengers.end(); iter++) {
+ painter.setBrush(QColor(Qt::gray));
+ painter.drawRect(bias - 100 + 13*(*iter)->Get_fpos(), height() - (120 + i * 50), 30, 30);
+ }
+ for (iter = floors[i].outPassengers.begin(); iter != floors[i].outPassengers.end(); iter++) {
+ painter.setBrush(QColor(Qt::gray));
+ painter.drawRect(bias + 550 + 13*(*iter)->Get_fpos(), height() - (120 + i * 50), 30, 30);
+ }
+ }
+
+ painter.setBrush(QBrush("#c56c00"));
/* 绘制楼层 */
for (i = 0; i < nFloor; i++) {
painter.drawRect(bias + 60, height() - (84 + i * 50), 110, 4);
@@ -138,9 +152,18 @@ void Emulator::paintEvent(QPaintEvent *event)
painter.drawText(bias + 105, height() - (90 + i * 50), QString::number(floors[i].Get_nPassenger()));
painter.setPen(QColor(Qt::red));
if (floors[i].UpOrdered())
- painter.drawText(bias + 145, height() - (90 + i * 50), "^");
+ painter.drawText(bias + 145, height() - (90 + i * 50) - 10, "*");
+ else {
+ painter.setPen(Qt::gray);
+ painter.drawText(bias + 145, height() - (90 + i * 50) - 10, "*");
+ painter.setPen(Qt::red);
+ }
if (floors[i].DownOrdered()) {
- painter.drawText(bias + 145, height() - (90 + i * 50) + 5, "v");
+ painter.drawText(bias + 145, height() - (90 + i * 50) + 10, "*");
+ } else {
+ painter.setPen(Qt::gray);
+ painter.drawText(bias + 145, height() - (90 + i * 50) + 10, "*");
+ painter.setPen(Qt::red);
}
painter.setPen(QColor(Qt::black));
}
@@ -153,20 +176,6 @@ void Emulator::paintEvent(QPaintEvent *event)
painter.drawLine(bias + 180, 67 + i * 50, 700 + bias, 67 + i * 50);
}
- /* 绘制乘客 */
- for (i = 0; i < nFloor; i++) {
- list<Passenger*>::iterator iter;
- for (iter = floors[i].inPassengers.begin(); iter != floors[i].inPassengers.end(); iter++) {
- painter.setBrush(QColor(Qt::gray));
- painter.drawRect(bias - 100 + 13*(*iter)->Get_fpos(), height() - (120 + i * 50), 30, 30);
- }
- for (iter = floors[i].outPassengers.begin(); iter != floors[i].outPassengers.end(); iter++) {
- painter.setBrush(QColor(Qt::gray));
- painter.drawRect(bias + 550 + 13*(*iter)->Get_fpos(), height() - (120 + i * 50), 30, 30);
- }
-
- }
-
/* 打印统计数据 */
nEle->setText (QString::number(nElevator));
nPas->setText (QString::number(Passenger::Total()));
@@ -227,15 +236,18 @@ struct fpos_lt
return p->Get_fpos() >= pos;
}
};
-/* 电梯依据指令或默认逻辑移动 */
+/* 电梯依据指令或默认逻辑移动,乘客移动 */
void Emulator::Move() {
EleIter iter;
list<Passenger*>::iterator pass_iter;
list<Passenger*> toDelete;
+ /* 电梯移动 */
for (iter = elevators.begin(); iter != elevators.end(); iter++) {
(*iter)->Move();
}
+
+ /* 乘客移动 */
for (int i = 0; i < nFloor; i++) {
for (pass_iter = floors[i].inPassengers.begin(); pass_iter != floors[i].inPassengers.end(); pass_iter++) {
if ((*pass_iter)->Move()) {
Please sign in to comment.
Something went wrong with that request. Please try again.