数据结构大作业2-电梯

文件下载

点击下载

运行文件为解压后的Elevator.exe

如果您发现无法执行,程序显示无法找到V142的生成工具

请点击这里

源代码展示

dianTi.h

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
#pragma once
#include<stdio.h>
#include<Windows.h>
#include<conio.h>//控制台的一些文件
#include<time.h>
#include<math.h>

//颜色的定义
#define RED "\033[0;32;31m"
#define NONE "\033[m"
#define YELLOW "\033[44;34m"
#define BLUE "\033[43;34m"
#define GREEN "\033[40;32m"
#define PURPLE "\033[43;35m"
#define DGREEN "\033[40;36m"
#define WHITE "\033[46;37m"
#define OBLUE "\033[40;34m"
#define ORED "\033[40;31m"
#define OPURPLE "\033[40;35m"
#define OWHITE "\033[47;37m"

//基本架构函数
void GotoXY(int, int); //光标定位函数
void Hide();//隐藏光标函数
void printElevator();//打印电梯主函数
void clearFormerElevator();//将之前的电梯去干净
void printPlatform(); //打印平台函数
void kaimen(int); //开门函数
void guanmen(int); //关门函数
void chushihua(); //初始化相应的一些参数

//附加函数
int absto1(int); //正负转化+-1
int cengLocation(int); //层的定位函数
int haveshangXingButton(int, int); //判断上行按钮在两个整数中有无1的值
int havexiaXingButton(int, int); //判断下行按钮在两个整数中有无1的值
int haveTargetButton(int, int); //判断电梯两个整数间有无要到达的楼层
void chuLiButton(int, int, int); //将每个楼层按下的button进行可视化处理(方向1上-1下,楼层,按钮1上0下上下)
void createPeople(); //对人创建的处理
void movePeople(); //对人物移动的处理
int scanPeopleStruct();//扫描所有的人空间,寻找到最近的一个没有使用的人空间如果没有返回0
int isPaiDui(int); //判断前面是否有人在排队
int InOutElevator(); //出入电梯函数
void refreshZTL(); //电梯状态栏的刷新
int countPeopleInElevator(); //电梯内人数的计算
int countPeopleToFloorElevator(int); //电梯内前往每层楼人数的总和

//模块的各个函数
int Menu(); //主菜单函数

//下面是开始游戏函数部分
int Start(); //开始游戏

//下面是游戏中一些全局变量
int lowFloor; //定义楼层的个数(>=2)
int cengGao; //定义楼层高度
int diantiWidth; //定义电梯宽度
int platformWidth; //定义平台宽度
int nowceng; //定于电梯当前所在的楼层
int dianTizhuangTai; //定义当前天梯是上行1还是下行-1
int target[6]; //定义目标楼层 1表示该楼层电梯内有乘客需要抵达
int nowLocation; //定义当前图片所在位置即乘上cengGao的值
int shangXingButton[6];//上行电梯按钮状态1表示亮起
int xiaXingButton[6];//下行电梯按钮状态1表示亮起
int peopleNumMax; //定义人数的最大值

work.c

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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "dianTi.h"

lowFloor = 4; //暂定楼层为5层
cengGao = 10; //暂定层高10
diantiWidth = 15; //暂定电梯宽度为15
platformWidth = 25; //暂定平台宽度为25
nowceng = 1; //刚开始默认楼层为1
peopleNumMax = 10; //定义最多的人数为10个人

typedef struct PeopleStruct {
int isInUse; //定义该人的空间是否在使用中,如果在使用,则为1 否则为0
int weight;
int positionx; //轴所在的位置
int nowFloor; //当前楼层
int targetFloor; //目标楼层
int In_or_Out;//往电梯进入为1 出电梯为-1 不动为0
int isInElevator; //判断是否在电梯中
}People;

struct PeopleList{
People people[10];
}peoplelist;

//光标定位函数
void GotoXY(int x, int y)
{
HANDLE hout;
COORD cor;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
cor.X = x;
cor.Y = y;
SetConsoleCursorPosition(hout, cor);
}

//隐藏光标
void Hide()
{
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cor_info = { 1, 0 };
SetConsoleCursorInfo(hout, &cor_info);//隐藏光标

}

//菜单栏目
int Menu() {
Hide();
GotoXY(36, 12); //定位光标的位置
printf(ORED"欢迎来到电梯小游戏");
GotoXY(43, 14);
printf("1. 开始游戏");
GotoXY(43, 16);
printf("2. 设置");
GotoXY(43, 18);
printf("退出游戏请输入 0"NONE);
Hide(); //隐藏光标
char ch;
int result = 1;
ch = _getch();
switch (ch)
{
case '1': result = 1; break;
case '2': result = 2; break;
case '0': result = 0; break;
default: result = 5; break;
}
system("cls"); //清空当前页面
return result;
}

// 初始化一些参数,包括电梯的状态以及电梯运行的状况
void chushihua() {
for (int i = 0; i <= lowFloor; i++) {
shangXingButton[i] = 0;
xiaXingButton[i] = 0;
target[i] = 0;
}

for (int i = 0; i < 10; i++) {
peoplelist.people[i].isInUse = 0;
}
nowLocation = cengLocation(nowceng);
//zhuangTai = 1; //定义刚开始的状态是上行
refreshZTL();
}

//对人创建的处理
void createPeople() {
int suijiNum = (int)rand() % 100;
if (suijiNum <= 2) {
int position = scanPeopleStruct();
if (position)
{
int nowFloor = (int)rand() % (lowFloor) + 1;
int targetFloor = (int)rand() % (lowFloor) + 1;
while (targetFloor == nowFloor) {
targetFloor = (int)rand() % (lowFloor)+1;
}
int weight = (int)rand()%40 + 40;

//下面就开始对数据进行赋值
peoplelist.people[position - 1].isInUse = 1;
peoplelist.people[position - 1].nowFloor = nowFloor;
peoplelist.people[position - 1].targetFloor = targetFloor;
peoplelist.people[position - 1].In_or_Out = 1;
peoplelist.people[position - 1].positionx = diantiWidth * 3 + 14;
peoplelist.people[position - 1].isInElevator = 0;
}else {
return;
}
}
}

void movePeople() {
int positiony;
for (int i = 0; i < 10; i++)
{
//将每个需要移动的人移动一下
if (peoplelist.people[i].In_or_Out == 0) {
continue;
}
if (isPaiDui(i)) {
peoplelist.people[i].In_or_Out = 0;
//人到电梯门前的按按钮处理
chuLiButton(absto1(peoplelist.people[i].targetFloor - peoplelist.people[i].nowFloor), peoplelist.people[i].nowFloor, 1);
refreshZTL();
}else if (peoplelist.people[i].isInUse) {
positiony = (lowFloor - peoplelist.people[i].nowFloor + 1) * cengGao - 1;
GotoXY(peoplelist.people[i].positionx, positiony);
printf(" ");
GotoXY(peoplelist.people[i].positionx, positiony-1);
printf(" ");
peoplelist.people[i].positionx = peoplelist.people[i].positionx - peoplelist.people[i].In_or_Out;
GotoXY(peoplelist.people[i].positionx, positiony);
printf("!");
GotoXY(peoplelist.people[i].positionx, positiony-1);
printf("%d", peoplelist.people[i].targetFloor);
}
//人到达电梯后离开的处理操作
if (peoplelist.people[i].positionx == diantiWidth * 3 + 16) {
peoplelist.people[i].isInUse = 0;
positiony = (lowFloor - peoplelist.people[i].nowFloor + 1) * cengGao - 1;
GotoXY(peoplelist.people[i].positionx, positiony);
printf(" ");
GotoXY(peoplelist.people[i].positionx, positiony-1);
printf(" ");
}
}
}

//判断前面是否有人在排队:0 没有人排队:1
int isPaiDui(int peopleNum) {
int nowFloor = peoplelist.people[peopleNum].nowFloor;
int positionxNext = peoplelist.people[peopleNum].positionx - peoplelist.people[peopleNum].In_or_Out;
int positionxi;
int iFloor;
//处理到达墙但是门没开的情况
if (peoplelist.people[peopleNum].positionx == 2 * diantiWidth + 6) {
return 1;
}
for (int i = 0; i < 10; i++)
{
positionxi = peoplelist.people[i].positionx;
iFloor = peoplelist.people[i].nowFloor;
if (peoplelist.people[i].isInUse && (nowFloor == iFloor) && (peoplelist.people[i].In_or_Out == 0)) {
if (positionxi == positionxNext) {
return 1;
}
}

}
return 0;
}

// 扫描所有的人空间,寻找到最近的一个没有使用的人空间如果没有返回0
int scanPeopleStruct() {
for (int i = 0; i < 10; i++)
{
if (peoplelist.people[i].isInUse == 0) {
return i+1;
}
}
return 0;
}

//电梯内人数计算
int countPeopleInElevator() {
int number = 0;
for (int i = 0; i < peopleNumMax; i++) {
if (peoplelist.people[i].isInUse && peoplelist.people[i].isInElevator) {
number++;
}
}
return number;
}

//电梯内前往每层楼人数的总和
int countPeopleToFloorElevator(targetFloor) {
int number = 0;
for (int i = 0; i < peopleNumMax; i++) {
if (peoplelist.people[i].isInUse && peoplelist.people[i].isInElevator) {
if (peoplelist.people[i].targetFloor == targetFloor) {
number++;
}
}
}
return number;
}

//电梯状态栏的刷新
void refreshZTL() {
GotoXY(diantiWidth * 3 + 24, 0);
printf("电梯内状态:");
if (dianTizhuangTai == 0) {
printf("停止运行");
}
else if (dianTizhuangTai == 1) {
printf("电梯上行");
}
else if (dianTizhuangTai == -1) {
printf("电梯下行");
}
GotoXY(diantiWidth * 3 + 24, 1);
printf("电梯内按钮状态为:");
GotoXY(diantiWidth * 3 + 28, 2);
for (int i = 1; i <= lowFloor; i++) {
if (target[i] == 0) {
printf("%d ", i);
}
else {
printf(BLUE"%d"NONE, i);
printf(" ");
}

}
GotoXY(diantiWidth * 3 + 24, 4);
printf("电梯内人数为:%d", countPeopleInElevator());

GotoXY(diantiWidth * 3 + 24, 6);
printf("电梯内前往每层楼的人数:");
for (int i = 1; i <= lowFloor; i++) {
GotoXY(diantiWidth * 3 + 26, 6 + i);
printf("前往%d楼的人数为:%d", i, countPeopleToFloorElevator(i));
}


}

//开始游戏
int Start()
{
chushihua();
printPlatform();
printElevator();
dianTizhuangTai = 0;
int timeCount = 1; //时间单元,1~100用于分配速度
while (1) {
timeCount++;

if (timeCount % 4 == 0)
{
nowLocation = nowLocation - dianTizhuangTai;
if (nowLocation % cengGao == 0) {
nowceng = lowFloor - (nowLocation / cengGao);
}
if (dianTizhuangTai != 0) {
printElevator();
}
}

if (dianTizhuangTai == 1) {
if (haveshangXingButton(nowceng, lowFloor) || havexiaXingButton(nowceng + 1, lowFloor) || (haveTargetButton(nowceng, lowFloor) && nowceng != lowFloor)) {
//状态保持不变
}
else if (haveshangXingButton(1, nowceng) || havexiaXingButton(1, nowceng) || haveTargetButton(1, nowceng)) {
//状态发生改变
dianTizhuangTai = -1;
}
else {
dianTizhuangTai = 0;
}
}
else if (dianTizhuangTai == -1) {
if (haveshangXingButton(1, nowceng - 1) || havexiaXingButton(1, nowceng) || (haveTargetButton(1, nowceng) && nowceng != 1)) {
//状态保持不变
}
else if (haveshangXingButton(nowceng, lowFloor) || havexiaXingButton(nowceng + 1, lowFloor) || haveTargetButton(nowceng, lowFloor)) {
//状态发生改变
dianTizhuangTai = 1;
}
else {
dianTizhuangTai = 0;
}
}
if (dianTizhuangTai == 0) {
if (haveshangXingButton(nowceng, lowFloor) || havexiaXingButton(nowceng + 1, lowFloor) || haveTargetButton(nowceng, lowFloor)) {
dianTizhuangTai = 1;
}
else if (haveshangXingButton(1, nowceng) || havexiaXingButton(1, nowceng) || haveTargetButton(1, nowceng)) {
dianTizhuangTai = -1;
}
}

if (nowLocation % cengGao == 0) {
if (shangXingButton[nowceng] == 1 || xiaXingButton[nowceng] == 1 || target[nowceng] == 1) {
if (target[nowceng] == 1) {
target[nowceng] = 0;
chuLiButton(dianTizhuangTai, nowceng, 0);
kaimen(nowceng);
InOutElevator();//人进出电梯
refreshZTL();
guanmen(nowceng);
}else if (dianTizhuangTai == 1 && shangXingButton[nowceng] == 1) {
chuLiButton(dianTizhuangTai, nowceng, 0);
kaimen(nowceng);
InOutElevator();//人进出电梯
refreshZTL();
guanmen(nowceng);
}else if (dianTizhuangTai == -1 && xiaXingButton[nowceng] == 1) {
chuLiButton(dianTizhuangTai, nowceng, 0);
kaimen(nowceng);
InOutElevator();//人进出电梯
refreshZTL();
guanmen(nowceng);
}
}
}

createPeople(); //定义人的创建过程
movePeople(); //对人物移动的处理


if (timeCount == 100)
{
timeCount = 0;
}
Sleep(50);

}
}

//出入电梯函数
int InOutElevator() {
for (int i = 0; i < 10; i++) {
if (peoplelist.people[i].In_or_Out == 0 && peoplelist.people[i].nowFloor == nowceng && peoplelist.people[i].isInElevator == 0) {
if (dianTizhuangTai == absto1(peoplelist.people[i].targetFloor - peoplelist.people[i].nowFloor)) {
int positiony = (lowFloor - peoplelist.people[i].nowFloor + 1) * cengGao - 1;
GotoXY(peoplelist.people[i].positionx, positiony);
printf(" ");
GotoXY(peoplelist.people[i].positionx, positiony - 1);
printf(" ");
peoplelist.people[i].positionx = 6;
target[peoplelist.people[i].targetFloor] = 1;
peoplelist.people[i].isInElevator = 1;
}
}
}

for (int i = 0; i < 10; i++) {
if (peoplelist.people[i].isInElevator == 1 && peoplelist.people[i].targetFloor == nowceng) {
int positiony = (lowFloor - peoplelist.people[i].nowFloor + 1) * cengGao - 1;
peoplelist.people[i].positionx = diantiWidth * 3 + 6;
peoplelist.people[i].In_or_Out = -1;
peoplelist.people[i].nowFloor = nowceng;
peoplelist.people[i].isInElevator = 0;
}
}
}

//打印电梯主函数
void printElevator()
{
Hide();
clearFormerElevator();
GotoXY(0, nowLocation); //电梯上框
for (int i = 0; i <= diantiWidth; i++) {
printf(DGREEN"██"NONE);
}
GotoXY(0, nowLocation + 10);//电梯下框
for (int i = 0; i <= diantiWidth; i++) {
printf(DGREEN"██"NONE);
}
for (int i = 0; i <= 10; i++) {//电梯门
GotoXY(diantiWidth * 2, i + nowLocation);
printf(DGREEN"██"NONE);
}
//内部人员;
}

//清理之前的电梯
void clearFormerElevator() {
for (int i = 0; i <= lowFloor * cengGao; i++) {
for (int j = 0; j <= diantiWidth*2; j++) {
GotoXY(j, i);
printf(" ");
}
}
}

//打印平台函数
void printPlatform() {
//平台打印
for (int i = 0; i <= cengGao * lowFloor; i = i + cengGao) {
GotoXY(diantiWidth * 2 + 4, i); //平台上框
for (int i = 0; i <= 16; i++) {
printf(PURPLE"██"NONE);
}
}

//造平台门
for (int i = 0; i <= cengGao * lowFloor; i++) {
GotoXY(diantiWidth * 2 + 4, i);
printf(YELLOW"██"NONE);
}
}

void kaimen(int ceng) {
int dingweiY = (lowFloor - ceng) * cengGao;//定义楼层上框
//平台门开启
Sleep(200);
for (int i = 9; i > 2; i--) {
//平台门开启
GotoXY(diantiWidth * 2 + 4, i + dingweiY);
printf(" ");
//电梯门开启
GotoXY(diantiWidth * 2, i + dingweiY);
printf(" ");
}
GotoXY(diantiWidth * 2 + 2, 10 + dingweiY);
printf(PURPLE"██"NONE);


}

void guanmen(int ceng) {
int dingweiY = (lowFloor - ceng) * cengGao;//定义楼层上框

for (int i = 1; i < 10; i++) {
//平台门关闭
GotoXY(diantiWidth * 2 + 4, i + dingweiY);
printf(YELLOW"██"NONE);
//电梯门关闭
GotoXY(diantiWidth * 2, i + dingweiY);
printf(DGREEN"██"NONE);
Sleep(200);
GotoXY(diantiWidth * 2 + 2, 10 + dingweiY);
printf(" ");
}
}

elevator_main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "dianTi.h"

int main()
{
system("title 电梯");
int flag = 1;
while (flag) {
system("cls");
switch (Menu())
{
case 1:
Start();
break;
case 0:
flag = 0;
break;
default:
break;
}
}
}

addition.c

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
#include "dianTi.h"

//正负转化+-1
int absto1(int i) {
if (i > 0) {
return 1;
}
else if(i < 0){
return -1;
}
return 0;
}

int cengLocation(int i) {
return (lowFloor - i) * cengGao;
}

//判断上行按钮在两个整数中有无1的值
int haveshangXingButton(int min, int max) {
for (int i = min; i <= max; i++) {
if (shangXingButton[i]) {
return 1;
}
}
return 0;
}

//判断下行按钮在两个整数中有无1的值
int havexiaXingButton(int min, int max) {
for (int i = min; i <= max; i++) {
if (xiaXingButton[i]) {
return 1;
}
}
return 0;
}

//判断电梯两个整数间有无要到达的楼层
int haveTargetButton(int min, int max) {
for (int i = min; i <= max; i++) {
if (target[i]) {
return 1;
}
}
return 0;
}

//将每个楼层按下的button进行可视化处理
void chuLiButton(int direction, int ceng, int data) {
if (direction == 1) {
if (shangXingButton[ceng] == data) {
return;
}
shangXingButton[ceng] = data;
Hide();
int y = cengLocation(ceng) + 2;
int x = diantiWidth * 2 + 7;
GotoXY(x, y);

if (data == 1) {
printf("▲");
}
else if (data == 0) {
printf(" ");
}
}
else if (direction == -1) {
if (xiaXingButton[ceng] == data) {
return;
}
xiaXingButton[ceng] = data;
Hide();

int y = cengLocation(ceng) + 5;
int x = diantiWidth * 2 + 7;
GotoXY(x, y);

if (data == 1) {
printf("▼");
}
else if (data == 0) {
printf(" ");
}
}
}

如果对您有帮助,欢迎投币哦~