-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystem.cpp
332 lines (332 loc) · 9.05 KB
/
System.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "System.h"
#include <Windows.h>
#include <iostream>
#include <conio.h>
using namespace std;
time_t StartTime;//定义系统开始时间
time_t Now;//系统当前时间
int Wait = 0;//电梯处于等待状态的时间
void System::Initialize() {//初始化函数
time(&StartTime);
srand((unsigned)time(NULL));//随机种子
time(&StartTime);
}
bool System::StandBy() {//等候命令的函数
if (_kbhit()) {
char a=getchar();
getchar();
int n = int(a - '0');
cout << "生成了"<<n<<"个人" << endl;
static int No = 1;
for (int i = 0; i < n; i++, No++) {
int startFloor = rand() % 9 + 1;
int endFloor = rand() % 9 + 1;
int waitTime = rand() % 60 + 30;
time(&Now);
Person per(No, startFloor, endFloor, waitTime, Now);
OutList.PushBack(per);
}
OutList.Display();
}
BeyondToleration();//每秒钟检查是否有人等不及了
Sleep(1000);//设置每一秒钟检查请求一次
if (ele.getCondition() == Waiting) { Wait++; }
if (ele.getCondition() == Waiting&&OutList.IsEmpty() == true && InList.IsEmpty() == true) {
if (Wait > 30)
{
cout << "处于等待阶段时间大于30秒回到一楼" << endl;
//SetDirection(1);//因为电梯里外都没人函数没有停止条件所以不能调用
while (ele.getfloor() != 1) {
ele.setCondition(Down);
ele.GoDown();
// cout << difftime(Now, StartTime) << endl;
cout << "电梯正在下行..." << endl;
Sleep(GoDownTime);
}
cout << "电梯到达本垒层Over" << endl;
return true;
}
else StandBy();
}
else {
Wait = 0;
Run();
StandBy();
}
return false;
}
void System::SetDirection(int floor) {
BeyondToleration();
if (floor > ele.getfloor()) {
ele.setCondition(Up);
time(&Now);
cout << "第" << difftime(Now, StartTime) << "秒电梯开始向上加速" << endl;
while (ele.getfloor() != floor) {
ele.GoUp();
Sleep(GoUpTime);
time(&Now);
cout << "第" << difftime(Now, StartTime) << "秒电梯正在上行..." << endl;
}
Arrive();
Sleep(Accelerate);
return;
}
else if (floor < ele.getfloor()) {
ele.setCondition(Down);
time(&Now);
cout << "第" << difftime(Now, StartTime) << "秒电梯开始向下加速" << endl;
while (ele.getfloor() != floor) {
ele.GoDown();
Sleep(GoDownTime);
time(&Now);
cout << "第" << difftime(Now, StartTime) << "秒电梯正在下行..." << endl;
}
Arrive();
Sleep(Accelerate);
return;
}
else if (floor == ele.getfloor()) {//缺了arrive()
ele.setCondition(Waiting);
if (GoIn() == true && ele.IsFull() == false)
In();
if (GoOut() == true)
Out();
if (ele.IsFull() == true) {
cout << "电梯人满了" << endl;
}
return;
}
}
bool System::GoIn() {
if (ele.IsFull() == false) {
if (OutList.IsEmpty() != true) {
time(&Now);
for (int i = 0; i < OutList.Size(); i++) {
if (OutList.GetData(i + 1).getStartTime() > Now) continue;
else if (OutList.GetData(i + 1).getStartFloor() == ele.getfloor())
return true;
}
}
}
return false;
}
bool System::GoOut() {//仅判断是个有人在当前楼层出去
if (InList.IsEmpty() == false)
for (int i = 0; i < InList.Size(); i++) {
if (InList.GetData(i + 1).getEndFloor() == ele.getfloor())
return true;
}
return false;
}
void System::Out() {//真的将能出去的人在链表中删除
int count = 0;
condition temp = ele.getCondition();
for (int i = 0; i < InList.Size(); i++) {
if (InList.GetData(i + 1).getEndFloor() == ele.getfloor()) {
InList.Erase(i + 1);
i--;
count++;
}
}
ele.setCondition(Opening);
ele.setCondition(Opened);
Sleep(InorOutTime*count);
time(&Now);
if (count != 0) cout << "第" << difftime(Now, StartTime) << "秒" << count << "名乘客正在出电梯门...." << endl;
ele.GoOut(count);
ele.setCondition(Closing);
ele.setCondition(Closed);
ele.setCondition(temp);
}
void System::In() {//真的将能进去的人在链表中删除
if (ele.IsFull() == true) return;
int count = 0;
condition temp = ele.getCondition();
for (int i = 0; i < OutList.Size(); i++) {
if (OutList.GetData(i + 1).getStartTime() > Now) continue;
else if (OutList.GetData(i + 1).getStartFloor() == ele.getfloor()) {
InList.PushBack(OutList.GetData((i + 1)));
OutList.Erase(i + 1);
i--;
count++;
if (count == 13) {
cout << "电梯超载" << endl;
break;
}
}
}
ele.setCondition(Opening);
ele.setCondition(Opened);
Sleep(InorOutTime*count);
time(&Now);
ele.setCondition(Closing);
ele.setCondition(Closed);
ele.setCondition(temp);
ele.GoIn(count);
if (count != 0) cout << "第" << difftime(Now, StartTime) << "秒" << count << "名乘客正在进入电梯门...." << endl;
}
bool System::Arrive() {
BeyondToleration();
if (GoIn() == true || GoOut() == true) {
condition temp = ele.getCondition();
if (ele.getCondition() == Up) {
Sleep(UpDecelerate);
time(&Now);
cout << "第" << difftime(Now, StartTime) << "秒电梯正在向上减速" << endl;
}
if (ele.getCondition() == Down) {
Sleep(DownDecelerate);
time(&Now);
cout << "第" << difftime(Now, StartTime) << "秒电梯正在向下减速" << endl;
}
cout << "电梯到达目标楼层:" << ele.getfloor() << endl;
ele.setCondition(Opening);
cout << "正在开门..." << endl;
ele.setCondition(Opened);
if (GoIn() == true && ele.IsFull() == false)
In();
if (GoOut() == true)
Out();
ele.setCondition(Closing);
ele.setCondition(Closed);
Sleep(closeDoor);
ele.setCondition(temp);
time(&Now);
cout << "第" << difftime(Now, StartTime) << "秒正在关门..." << endl;
return true;
}
else return false;
}
void System::Run() {//电梯调度运行函数
int near0;
int near1;
int near2;
if (ele.IsFull() == true) {
int flag;
int min = 9;
int NearIn = 12;
if (InList.IsEmpty() != true) {
for (int i = 0; i < InList.Size(); i++) {
flag = abs(InList.GetData(i + 1).getEndFloor() - ele.getfloor());
if (flag <= min) {
min = flag;
NearIn = InList.GetData(i + 1).getEndFloor();
}
}
}
SetDirection(NearIn);
}
if (ele.getCondition() == Closed) {
if (OutList.IsEmpty() == true && InList.IsEmpty() == true)
ele.setCondition(Waiting);
return;
}
if (ele.getCondition() == Waiting) {
int flag;
int min = 9;
int NearIn = 12;
int NearOut = 12;
if (InList.IsEmpty() != true) {
for (int i = 0; i < InList.Size(); i++) {
flag = abs(InList.GetData(i + 1).getEndFloor() - ele.getfloor());
if (flag <= min) {
min = flag;
NearIn = InList.GetData(i + 1).getEndFloor();
}
}
}
if (OutList.IsEmpty() != true) {
for (int i = 0; i < OutList.Size(); i++) {
if (OutList.GetData(i + 1).getStartTime() > Now) continue;
flag = abs(OutList.GetData(i + 1).getStartFloor() - ele.getfloor());
if (flag <= min) {
min = flag;
NearOut = OutList.GetData(i + 1).getStartFloor();
}
}
}
near0 = NearOut > NearIn ? NearIn : NearOut;
if (near0 == 12) {
ele.setCondition(Waiting);
near0 = ele.getfloor();
}
SetDirection(near0);
}
if (ele.getCondition() == Up) {
int flag;
int min = 9;
int UpNearIn = 12;
int UpNearOut = 12;
if (InList.IsEmpty() != true) {
for (int i = 0; i < InList.Size(); i++) {
flag = InList.GetData(i + 1).getEndFloor() - ele.getfloor();
if (flag <= min&&flag > 0) {
min = flag;
UpNearIn = InList.GetData(i + 1).getEndFloor();
}
}
}
if (OutList.IsEmpty() != true) {
for (int i = 0; i < OutList.Size(); i++) {
if (OutList.GetData(i + 1).getStartTime() > Now) continue;
flag = OutList.GetData(i + 1).getStartFloor() - ele.getfloor();
if (flag <= min&&flag >= 0) {
min = flag;
UpNearOut = OutList.GetData(i + 1).getStartFloor();
}
}
}
near1 = UpNearOut > UpNearIn ? UpNearIn : UpNearOut;
if (near1 == 12) {
ele.setCondition(Down);
near1 = ele.getfloor();
}
SetDirection(near1);
}
else if (ele.getCondition() == Down) {
int flag;
int min = 9;
int DownNearIn = 12;
int DownNearOut = 12;
if (InList.IsEmpty() != true) {
for (int i = 0; i < InList.Size(); i++) {
flag = ele.getfloor() - InList.GetData(i + 1).getEndFloor();
if (flag <= min&&flag >= 0) {
min = flag;
DownNearIn = InList.GetData(i + 1).getEndFloor();
}
}
}
if (OutList.IsEmpty() != true) {
for (int i = 0; i < OutList.Size(); i++) {
if (OutList.GetData(i + 1).getStartTime() > Now) continue;
flag = ele.getfloor() - OutList.GetData(i + 1).getStartFloor();
if (flag <= min&&flag >= 0) {
min = flag;
DownNearOut = OutList.GetData(i + 1).getStartFloor();
}
}
}
near2 = DownNearOut > DownNearIn ? DownNearIn : DownNearOut;
if (near2 == 12) {
ele.setCondition(Up);
near1 = ele.getfloor();
}
SetDirection(near2);
}
}
void System::BeyondToleration() {
time(&Now);
double flag = difftime(Now, StartTime);
if (OutList.IsEmpty() != true) {
for (int i = 0; i < OutList.Size(); i++) {
if (OutList.GetData(i + 1).getStartTime() > Now) continue;
else if (OutList.GetData(i + 1).getMaxTime() < flag - difftime(OutList.GetData(i + 1).getStartTime(), StartTime))
{
cout << "第" << OutList.GetData(i + 1).getId() << "人等不及离开了" << endl;
OutList.Erase(i + 1);
i--;
}
}
}
}