-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function.cpp
75 lines (75 loc) · 2.44 KB
/
Function.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
#include"Function.h"
void getParameter(int& e_num, int& f_num, int& p_num)
{
cout << "_________________________________________________________\n\n现在开始模拟:" << endl;
cout << "请输入电梯数: ";
cin >> e_num;
cout << "请输入楼层数: ";
cin >> f_num;
while (f_num <= 1) {
cout << "数据有误,最少两层。\n";
cin >> f_num;
}
cout << "请输入乘客数量: ";
cin >> p_num;
while (p_num <= 0) {
cout << "数据有误,最少一个人。\n";
cin >> p_num;
}
cout << "\n**********************************************************\n正在模拟中... \n";
}
void PrintPeople(vector<vector<People>> the_floors, int &max_floor)
{
for (int k = 0; k < the_floors.size(); k++) {
while (the_floors[k].empty() == false) {
cout << " 乘客 " << the_floors[k].back().getId() << ": \t所在楼层 " << the_floors[k].back().getCurrentFloor() << " \t目标楼层:" << the_floors[k].back().getDestination() << endl;
if (max_floor < the_floors[k].back().getCurrentFloor())
max_floor = the_floors[k].back().getCurrentFloor();
the_floors[k].pop_back();
}
}
}
void run(bool& f)
{
cout << "1.开始模拟\n" << "2.退出" << endl;
srand(time(0));
int choice;
cin >> choice;
//开始模拟
if (choice == 1) {
int e_num = -1; //电梯数
int f_num = -1; //层数
int p_num; //人数
int max_floor = 1;//最高层数
getParameter(e_num, f_num, p_num);
Building My_Building(e_num, f_num); //创楼
int count = 1; //次数
int People_ID = 0; //人的编号
int num_exited = 0; //出电梯的人数
queue <People> Exit_People; //出电梯的人的队列
for (int i = 0; i < p_num; i++) { //初始化
People the_People(count, People_ID, f_num);
People_ID++;
My_Building.add_People_Floor(the_People);
}
PrintPeople(My_Building.get_FloorVec(), max_floor);
//cout << "***" << max_floor << " ** " << endl;
while (num_exited < p_num) {
cout << "\n··········第 " << count << " 次···········\n\n";
num_exited += My_Building.Function(count, Exit_People, max_floor);
count++;
}
cout << "\n模拟结束\n******************************************************\n\n总人数: " << p_num << endl;
double sum = 0;
while (!Exit_People.empty()) {
People current = Exit_People.front();
sum += current.getWaitTime();
Exit_People.pop();
}
cout << "平均等待时间: " << sum / p_num << "\n__________________________________________________________";
cout << endl << endl;
}
else if (choice == 2) { //结束
f = false;
}
}