📄 elevator.cpp
字号:
int out_people;
char ustring1[BUF_LENGTH];
char ustring2[BUF_LENGTH];
set_cursor_pos(1,17); clear_line();
set_cursor_pos(1,17);
cout << "电梯 " << (car_number+1)
<< " 已经停在第 " << (current_floor+1)<<"层"
<< "\n请输入你要到达的楼层号(按0关门)";
for(int j=1; j<NUMBER_FLOOR; j++){
set_cursor_pos(1, 19);
cout << "第" << j << "个目的: ";
cin.get(ustring1, BUF_LENGTH);
cin.ignore(10, '\n');
dest_floor = atoi(ustring1);
if(dest_floor!=0){
cout<<"在这个目的层要多少人要出去电梯:";
cin.get(ustring2, BUF_LENGTH);
cin.ignore(10, '\n');
out_people = atoi(ustring2);
}
set_cursor_pos(1,19); clear_line();
set_cursor_pos(1,20); clear_line();
if(dest_floor==0){
set_cursor_pos(1,17); clear_line();
set_cursor_pos(1,18); clear_line();
set_cursor_pos(1,19); clear_line();
set_cursor_pos(1,20); clear_line();
return;
}
--dest_floor;
if(dest_floor==current_floor)
{ --j; continue; }
if(j==1 && current_dir==STOP)
current_dir = (dest_floor < current_floor) ? DN : UP;
destination[dest_floor] =out_people;
destination_display();
}
}
//--------------------------------------------------------------
void elevator::dicide_next(){ //decide what to do
int j;
//flags indicate if destinations or requests above/below us
bool destins_above, destins_below; //destinations
bool requests_above, requests_below; //requests
//floor number of closest request above us and below us
int nearest_higher_req = 0;
int nearest_lower_req = 0;
//flags indicate if there is another car, going in the same
//direction, between us and the nearest floor request (FR)
bool car_between_up, car_between_dn;
//flags indicate if there is another car, going in the
//opposite direction, on the opposite side of the nearest FR
bool car_opposite_up, car_opposite_dn;
//floor and direction of other car (not us)
int ofloor; //floor
direction odir; //direction
//ensure we don't go too high or too low
if( (current_floor==NUMBER_FLOOR-1 && current_dir==UP)
|| (current_floor==0 && current_dir==DN) )
current_dir = STOP;
//if there's a destination on this floor, unload passengers
if(get_destination_num(current_dir)){
set_destination_num(current_dir); //erase destination
current_dir = STOP;
}
//if there's an UP floor request on this floor,
//and if we're going up or stopped, load passengers
if( (ptrBuilding->ptrFloor[current_floor]->get_floor_in_num(UP))&&
(current_dir != DN) ){
current_dir = UP; //(in case it was STOP)
return;
}
//if there's a down floor request on this floor,
//and if we're going down or stopped, load passengers
if( (ptrBuilding->ptrFloor[current_floor]->get_floor_in_num(DN))&&
(current_dir != UP) ){
current_dir = DN; //(in case it was STOP)
return;
}
//check if there are other destinations or requests
//record distance to nearest request
destins_above = destins_below = false;
requests_above = requests_below = false;
for(j=current_floor+1; j<NUMBER_FLOOR; j++)
{ //check floors above
if(destination[j]) //if destinations
destins_above = true; //set flag
if(ptrBuilding->ptrFloor[j]->get_floor_in_num(UP) ||
ptrBuilding->ptrFloor[j]->get_floor_in_num(DN))
{ //if requests
requests_above = true; //set flag
if( !nearest_higher_req ) //if not set before
nearest_higher_req = j; // set nearest req
}
}
for(j=current_floor-1; j>=0; j--) //check floors below
{
if(destination[j] ) //if destinations
destins_below = true; //set flag
if( ptrBuilding->ptrFloor[j]->get_floor_in_num(UP)||
ptrBuilding->ptrFloor[j]->get_floor_in_num(DN))
{ //if requests
requests_below = true; //set flag
if( !nearest_lower_req ) //if not set before
nearest_lower_req = j; // set nearest req
}
}
//if no requests or destinations above or below, stop
if( !destins_above && !requests_above &&
!destins_below && !requests_below)
{
current_dir = STOP;
return;
}
//if destinations and we're stopped, or already going the
//right way, go toward destinations
if( destins_above && (current_dir==STOP || current_dir==UP) )
{
current_dir = UP;
return;
}
if( destins_below && (current_dir==STOP || current_dir==DN) )
{
current_dir = DN;
return;
}
//find out if there are other cars, (a) going in the same
//direction, between us and the nearest floor request;
//or (b) going in the opposite direction, on the other
//side of the floor request
car_between_up = car_between_dn = false;
car_opposite_up = car_opposite_dn = false;
for(j=0; j<NUMBER_CAR; j++) //check each car
{
if(j != car_number) //if it's not us
{ //get its floor
ofloor = ptrBuilding->get_cars_floor(j); //and
odir = ptrBuilding->get_cars_dir(j); //direction
//if it's going up and there are requests above us
if( (odir==UP || odir==STOP) && requests_above )
//if it's above us and below the nearest request
if( (ofloor > current_floor
&& ofloor <= nearest_higher_req)
//or on same floor as us but is lower car number
|| (ofloor==current_floor && j < car_number) )
car_between_up = true;
//if it's going down and there are requests below us
if( (odir==DN || odir==STOP) && requests_below )
//if it's below us and above the nearest request
if( (ofloor < current_floor
&& ofloor >= nearest_lower_req)
//or on same floor as us but is lower car number
|| (ofloor==current_floor && j < car_number) )
car_between_dn = true;
//if it's going up and there are requests below us
if( (odir==UP || odir==STOP) && requests_below )
//it's below request and closer to it than we are
if(nearest_lower_req >= ofloor
&& nearest_lower_req - ofloor
< current_floor - nearest_lower_req)
car_opposite_up = true;
//if it's going down and there are requests above us
if( (odir==DN || odir==STOP) && requests_above )
//it's above request and closer to it than we are
if(ofloor >= nearest_higher_req
&& ofloor - nearest_higher_req
< nearest_higher_req - current_floor)
car_opposite_dn = true;
} //end if(not us)
} //end for(each car)
//if we're going up or stopped, and there is an FR above us,
//and there are no other cars going up between us and the FR,
//or above the FR going down and closer than we are,
//then go up
if( (current_dir==UP || current_dir==STOP)
&& requests_above && !car_between_up && !car_opposite_dn )
{
current_dir = UP;
return;
}
//if we're going down or stopped, and there is an FR below
//us, and there are no other cars going down between us and
//the FR, or below the FR going up and closer than we are,
//then go down
if( (current_dir==DN || current_dir==STOP)
&& requests_below && !car_between_dn && !car_opposite_up )
{
current_dir = DN;
return;
}
//if nothing else happening, stop
current_dir = STOP;
} //end decide(), finally
//--------------------------------------------------------------
void elevator::move() {
if(current_dir==UP) //if going up, go up
current_floor++;
else if(current_dir==DN) //if going down, go down
current_floor--;
} //end move()
//--------------------------------------------------------------
int main(){
/*set_cursor_pos(1,22); clear_line();
set_cursor_pos(1,22);
cout<<"please input the numbers of elevator(1-8):";
cin>>NUMBER_CAR;
set_cursor_pos(1,22); clear_line();
set_cursor_pos(1,22);
cout<<"please input the numbers of floor(2-25):";
cin>>NUMBER_FLOOR;*/
building theBuilding;
while(true){
theBuilding.master_tick();//send time tick to all cars
wait(1000); //pause
theBuilding.record_each_floor_rep();//get floor requests from user
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -