#include<iostream>
#include <algorithm>
#include<cmath>
#include <windows.h> // 包含 Sleep 函数
class Elevator
{
private:
const int MAX_passenger = 10; /*电梯最大承载人数*/
int people_num=0; /*电梯当前人数*/
const int elevator_speed = 5; /*电梯的速度,五秒一楼*/
float ele_direction=0;/*电梯当前方向,0为静止,1为上升,-1为下降*/
float floor_now=1; /*电梯当前楼层*/
float floor_end=1; /*电梯结束楼层*/
int state=0; /*电梯当前状态,0为静止,1为工作中*/
public:
Elevator();
~Elevator();
int time_now=0; /*电梯开始运作时间*/
int time_end=0; /*电梯停止运行的时间*/
float ele_middle_direct=0; /*电梯的过渡方向*/
void get_people(); /*进人*/
void out_people(); /*出人*/
float ask_floor(); /*询问当前楼层*/
int ask_state(); /*询问电梯状态*/
void state_change(); /*状态的变化*/
int ask_people(); /*询问当前人数*/
void ele_work(); /*电梯运作*/
float ask_ele_direction(); /*询问电梯的方向*/
void ele_direction_change(int n); /*更改电梯的方向*/
int ele_min_floor; /*除一楼所能到达的最底层*/
int ele_max_floor; /*所能到达的最高层*/
int ele_parity; /*电梯的奇偶,1奇数,0偶数,-1无所谓*/
int wait_time=0; /*上下车等待时间*/
int ele_work_time=0; /*电梯工作时间*/
int ele_free_time=0; /*电梯空闲时间*/
};
Elevator::Elevator()
{
}
Elevator::~Elevator()
{
}
void Elevator::get_people()
{
people_num += 1;
}
void Elevator::out_people()
{
people_num -= 1;
}
float Elevator::ask_floor()
{
return floor_now;
}
int Elevator::ask_state()
{
return state;
}
void Elevator::state_change()
{
if(state==0)
{
state = 1;
}
else if(state==1)
{
state = 0;
}
}
int Elevator::ask_people()
{
return people_num;
}
void Elevator::ele_work()
{
if(wait_time%3==0)
{
if(ele_middle_direct==0)
{
floor_now+=(0.2*ele_direction);
}
else
{
floor_now+=(0.2*ele_middle_direct);
}
}
else
{
wait_time++;
}
}
float Elevator::ask_ele_direction()
{
return ele_direction;
}
void Elevator::ele_direction_change(int n)
{
ele_direction = n;
}
class passengers
{
private:
int ID; /*乘客的ID*/
float pass_floor_start; /*乘客的初始楼层*/
float pass_floor_end; /*乘客的目的楼层*/
float pass_direct; /*乘客的方向*/
int pass_time; /*乘客到达时间*/
int pass_work_time; /*乘客工作时间*/
int pass_floor_end_2; /*乘客再次的目的楼层*/
public:
passengers();
~passengers();
void pass_sure_direct(); /*确定乘客的方向*/
int pass_ele_num=-1; /*乘客所乘坐的电梯编号*/
int pass_end_time=1000000; /*乘客结束乘梯的时间*/
void pass_id(int n); /*注册乘客ID*/
int ask_pass_id(); /*询问乘客ID*/
float ask_pass_floor_start(); /*询问乘客的初始楼层*/
float ask_pass_floor_end(); /*询问乘客的目的楼层*/
int ask_pass_time(); /*询问乘客到达时间*/
float ask_pass_direct(); /*询问乘客的方向*/
int pass_ele_if=0; /*是否坐上电梯,0未坐上,1已坐上,2已坐完*/
int pass_if_work=1; /*乘客已经完成几次坐电梯*/
int pass_L; /*乘客需要乘坐L+1次乘坐电梯*/
int pass_wait_time=0; /*乘客等待时间*/
};
passengers::passengers()
{
pass_time = (rand() % 10);
pass_floor_start = (rand() % 38) + 2;
pass_floor_end = (rand() % 38) + 2;
pass_L = pass_time = (rand() % 4)+1;
if(pass_floor_end > pass_floor_start)
{
pass_direct=1;
}
else if (pass_floor_end < pass_floor_start)
{
pass_direct=-1;
}
else
{
pass_direct=0;
}
}
passengers::~passengers()
{
}
void passengers::pass_sure_direct()
{
if(pass_if_work<=pass_L)
{
pass_work_time = (rand() % 29) +1;
pass_floor_end_2 = (rand() % 38) + 2;
pass_time = pass_end_time + pass_work_time;
pass_floor_start = pass_floor_end; /*更新乘客所在楼层和目的楼层*/
pass_floor_end = pass_floor_end_2;
pass_ele_if=0;
pass_ele_num=-1; /*乘客所乘坐的电梯编号*/
pass_end_time=1000000; /*乘客结束乘梯的时间*/
}
else if(pass_if_work==pass_L+1)
{
pass_work_time = (rand() % 29) +1;
pass_floor_end_2 = 1;
pass_time = pass_end_time + pass_work_time;
pass_floor_start = pass_floor_end; /*更新乘客所在楼层和目的楼层*/
pass_floor_end = pass_floor_end_2;
pass_ele_if=0;
pass_ele_num=-1; /*乘客所乘坐的电梯编号*/
pass_end_time=1000000; /*乘客结束乘梯的时间*/
}
if(pass_floor_end > pass_floor_start)
{
pass_direct=1;
}
else if (pass_floor_end < pass_floor_start)
{
pass_direct=-1;
}
else
{
pass_direct=0;
}
}
void passengers::pass_id(int n)
{
ID=n;
}
int passengers::ask_pass_id()
{
return ID;
}
float passengers::ask_pass_floor_start()
{
return pass_floor_start;
}
float passengers::ask_pass_floor_end()
{
return pass_floor_end;
}
int passengers::ask_pass_time()
{
return pass_time;
}
float passengers::ask_pass_direct()
{
return pass_direct;
}
int main()
{
Elevator ele[10];
int n,i,j,k,t,a,f,b,c;
int ele_ok; /*符合要求的电梯*/
std::cin>>n; /*n个人*/
passengers pass[n];
for(i=1;i<=n;i++)
{
pass[i-1].pass_id(i); /*给每位乘客赋予Id*/
std::cout<<"ID为"<<i<<"的乘客初始楼层和目的楼层分别为"<<pass[i-1].ask_pass_floor_start()<<","<<pass[i-1].ask_pass_floor_end()<<std::endl;
std::cout<<"到达的时间和坐电梯的次数为"<<pass[i-1].ask_pass_time()<<", "<<pass[i-1].pass_L<<std::endl;
}
for(i=0;i<10;i++) /*给电梯上限值*/
{
if(i==0||i==1)
{
ele[i].ele_min_floor=2;
ele[i].ele_max_floor=40;
ele[i].ele_parity=-1;
}
else if(i==2||i==3)
{
ele[i].ele_min_floor=25;
ele[i].ele_max_floor=40;
ele[i].ele_parity=-1;
}
else if(i==4||i==5)
{
ele[i].ele_min_floor=2;
ele[i].ele_max_floor=25;
ele[i].ele_parity=-1;
}
else if(i==6||i==7)
{
ele[i].ele_min_floor=2;
ele[i].ele_max_floor=40;
ele[i].ele_parity=0;
}
else if(i==8||i==9)
{
ele[i].ele_min_floor=3;
ele[i].ele_max_floor=39;
ele[i].ele_parity=1;
}
}
int pass_fin=0;
for(k=0;;k++) /*时间的流逝*/
{
for(i=0;i<n;i++)
{
if(pass[i].pass_if_work<=pass[i].pass_L + 1 && pass[i].pass_ele_if==2) /*该乘客已经完成一次坐电梯*/
{
pass[i].pass_sure_direct();
}
if(pass[i].ask_pass_time()>k) /*该乘客还未到达大楼*/
{
continue;
}
if(pass[i].pass_ele_if>0) /*去除坐上电梯的乘客*/
{
continue;
}
int ele_pass_diff=100;
ele_ok=-1;
for(j=0;j<10;j++)
{
if(pass[i].pass_ele_num!=-1) /*此人已有所需的电梯*/
{
ele_ok = pass[i].pass_ele_num;
break;
}
if(ele[j].ask_state()==0)
{
if(pass[i].ask_pass_floor_start()!=1)
{
if(pass[i].ask_pass_floor_start()<ele[j].ele_min_floor||pass[i].ask_pass_floor_start()>ele[j].ele_max_floor)
{
continue;
}
a=pass[i].ask_pass_floor_start();
if(ele[j].ele_parity!=-1 && a%2!=ele[j].ele_parity)
{
continue;
}
}
if(pass[i].ask_pass_floor_end()!=1)
{
if(pass[i].ask_pass_floor_end()<ele[j].ele_min_floor||pass[i].ask_pass_floor_end()>ele[j].ele_max_floor)
{
continue;
}
a=pass[i].ask_pass_floor_end();
if(ele[j].ele_parity!=-1 && a%2!=ele[j].ele_parity)
{
continue;
}
}
if(ele_pass_diff>abs(ele[j].ask_floor() - pass[i].ask_pass_floor_start()))
{
ele_pass_diff=abs(ele[j].ask_floor() - pass[i].ask_pass_floor_start());
ele_ok=j;
}
}
else if(ele[j].ask_state()==1 && ele[j].ask_people() < 10) /*正在运动的电梯*/
{
if(pass[i].ask_pass_floor_start()!=1)
{
if(pass[i].ask_pass_floor_start()<ele[j].ele_min_floor||pass[i].ask_pass_floor_start()>ele[j].ele_max_floor)
{
continue;
}
a=pass[i].ask_pass_floor_start();
if(ele[j].ele_parity!=-1 && a%2!=ele[j].ele_parity)
{
continue;
}
}
if(pass[i].ask_pass_floor_end()!=1)
{
if(pass[i].ask_pass_floor_end()<ele[j].ele_min_floor||pass[i].ask_pass_floor_end()>ele[j].ele_max_floor)
{
continue;
}
a=pass[i].ask_pass_floor_end();
if(ele[j].ele_parity!=-1 && a%2!=ele[j].ele_parity)
{
continue;
}
}
if(ele[j].ask_ele_direction() == pass[i].ask_pass_direct()) /*电梯方向与乘客方向相同*/
{
/*检验电梯是否已经经过乘客的初始楼层*/
if((pass[i].ask_pass_floor_start()-ele[j].ask_floor())/pass[i].ask_pass_direct() > 0)
{
/*检验是否为最近的电梯*/
if(ele_pass_diff>=abs(ele[j].ask_floor() - pass[i].ask_pass_floor_start()))
{
ele_pass_diff=abs(ele[j].ask_floor() - pass[i].ask_pass_floor_start());
ele_ok=j;
}
else
{
continue;
}
}
}
}
}
/*有此循环得到第ele_ok个电梯符合需求*/
if(ele_ok==-1)
{
continue;
}
pass[i].pass_ele_num = ele_ok;
if(ele[ele_ok].ask_state()==0)
{
ele[ele_ok].state_change(); /*改变电梯状态*/
ele[ele_ok].ele_direction_change(pass[i].ask_pass_direct()); /*改变电梯的最终运作方向*/
ele[ele_ok].time_now = k;
ele[ele_ok].ele_free_time += ele[ele_ok].time_now - ele[ele_ok].time_end;
}
if(fabs(ele[ele_ok].ask_floor()-pass[i].ask_pass_floor_start())<1e-2) /*电梯此时与乘客在同一楼层*/
{
/*一定要牢记浮点数里面,1.8 + 0.2 != 2 深刻的教训*/
pass[i].pass_end_time = k + abs(pass[i].ask_pass_floor_end()-pass[i].ask_pass_floor_start()) * 5;
ele[ele_ok].ele_middle_direct = 0;
ele[ele_ok].get_people();
pass[i].pass_wait_time += (k+1 - pass[i].ask_pass_time());
pass[i].pass_ele_if++; /*标记已坐上电梯*/
if(ele[ele_ok].wait_time%3==0)
{
for(f=0;f<n;f++)
{
if(pass[f].pass_ele_num==ele_ok && pass[f].pass_ele_if==1)
{
pass[f].pass_end_time += 3;
}
}
}
ele[ele_ok].wait_time = 1; /*标记以上车*/
std::cout<<"ID为"<<i+1<<"的乘客已上电梯"<<pass[i].pass_ele_num<<","<<"上电梯的时间为"<<k<<"秒"<<std::endl;
}
else /*若不在同一楼层*/
{
if(ele[ele_ok].ask_floor() > pass[i].ask_pass_floor_start())
{
ele[ele_ok].ele_middle_direct = -1;
}
else if(ele[ele_ok].ask_floor() < pass[i].ask_pass_floor_start())
{
ele[ele_ok].ele_middle_direct = 1;
}
}
}
std::cout<<"当前时间"<<k+1<<"秒"<<std::endl;
for(j=0;j<10;j++) /*每秒电梯进行工作*/
{
ele[j].ele_work();
}
t=0;
for(i=0;i<n;i++)
{
if(pass[i].pass_end_time==k+1) /*说明此时有乘客下电梯*/
{
ele[pass[i].pass_ele_num].out_people();
pass[i].pass_ele_if++; /*标记已坐完电梯*/
pass[i].pass_if_work++;
if(ele[pass[i].pass_ele_num].wait_time==0)
{
for(k=0;k<n;k++)
{
if(pass[f].pass_ele_if == 1 && pass[f].pass_ele_num == pass[i].pass_ele_num && pass[f].pass_end_time!=k+1)
{
pass[f].pass_end_time += 3;
}
}
}
ele[pass[i].pass_ele_num].wait_time=1;
std::cout<<"ID为"<<i+1<<"的乘客已下电梯"<<pass[i].pass_ele_num<<","<<"下电梯的时间为"<<pass[i].pass_end_time<<"秒"<<std::endl;
}
if(pass[i].pass_if_work>=pass[i].pass_L+1)
{
t++;
}
}
c=0;
for(j=0;j<10;j++) /*将完成工作的电梯状态归零*/
{
if(ele[j].ele_middle_direct==0 && ele[j].ask_people()==0)
{
ele[j].ele_direction_change(0);
}
if(ele[j].ele_middle_direct==0 && ele[j].ask_ele_direction()==0 && ele[j].ask_state()==1)
{
ele[j].state_change();
ele[j].time_end = k;
ele[j].ele_work_time += ele[j].time_end - ele[j].time_now;
}
if(ele[j].ask_state()==0)
{
c++;
}
}
for(j=0;j<10;j++)
{
int b = (int)(ele[j].ask_floor()+0.5);
std::cout<<"电梯编号"<<j<<"此时运行状态为"<<ele[j].ask_state()<<" (1为工作,2为休息)";
std::cout<<", 所在楼层为"<<b<<"楼";
std::cout<<", 此时过渡方向为"<<ele[j].ele_middle_direct;
std::cout<<", 此时最终方向为"<<ele[j].ask_ele_direction();
std::cout<<", 此时搭载乘客数量为"<<ele[j].ask_people()<<"名"<<std::endl;
}
if(t==n && c==10)
{
std::cout<<"所有人都做完电梯,任务结束"<<std::endl;
break;
}
Sleep(10); // 暂停一秒钟
system("cls"); // 清空屏幕
}
for(i=0;i<n;i++)
{
std::cout<<"ID为"<<i+1<<"的乘客等待总时间为"<<pass[i].pass_wait_time<<std::endl;
}
for(i=0;i<10;i++)
{
ele[i].ele_free_time += k-ele[i].time_end;
std::cout<<i<<"号电梯总工作时间为"<<ele[i].ele_work_time<<", 总空闲时间为"<<ele[i].ele_free_time<<std::endl;
}
return 0;
}