⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elev.cpp

📁 一个用C++实现的模拟电梯运行的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
   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==NUM_FLOORS-1 && current_dir==UP)
      || (current_floor==0 && current_dir==DN) )
      current_dir = STOP;

   //if there's a destination on this floor, unload passengers
   if( destination[current_floor]==true )
      {
      destination[current_floor] = false;  //erase destination
      if( !unloading_timer)                //unload
         unloading_timer = LOAD_TIME;
      return;
      }
   //if there's an UP floor request on this floor,
   //and if we're going up or stopped, load passengers
   if( (ptrBuilding->get_floor_req(UP, current_floor) && 
        current_dir != DN) )
      {
      current_dir = UP;  //(in case it was STOP)
      //remove floor request for direction we're going
      ptrBuilding->set_floor_req(current_dir, 
                                 current_floor, false);
      if( !loading_timer)                  //load
         loading_timer = LOAD_TIME;
      return;
      }
   //if there's a down floor request on this floor,
   //and if we're going down or stopped, load passengers
   if( (ptrBuilding->get_floor_req(DN, current_floor) && 
        current_dir != UP) )
      {
      current_dir = DN;  //(in case it was STOP)
      //remove floor request for direction we're going
      ptrBuilding->set_floor_req(current_dir,
                                  current_floor, false);
      if( !loading_timer)                  //load passengers
         loading_timer = LOAD_TIME;
      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<NUM_FLOORS; j++)
      {                                   //check floors above
      if( destination[j] )                //if destinations
         destins_above = true;            //set flag
      if( ptrBuilding->get_floor_req(UP, j) || 
          ptrBuilding->get_floor_req(DN, j) )
         {                                //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->get_floor_req(UP, j) || 
          ptrBuilding->get_floor_req(DN, j) )
         {                                //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<NUM_CARS; 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 loading or unloading,
   if(loading_timer || unloading_timer)  //don't move
      return;
   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()
//--------------------------------------------------------------
void elevator::get_destinations()     //stop, get destinations
   {
   char ustring[BUF_LENGTH];          //utility buffer for input
   int dest_floor;                    //destination floor

   set_cursor_pos(1,22); clear_line();  //clear top line
   set_cursor_pos(1, 22);
   cout << "Car " << (car_number+1)
        << " has stopped at floor " << (current_floor+1)
        << "\nEnter destination floors (0 when finished)";
   for(int j=1; j<NUM_FLOORS; j++)    //get floor requests
      {                               //maximum; usually fewer
      set_cursor_pos(1, 24);
      cout << "Destination " << j << ": ";

      cin.get(ustring, BUF_LENGTH);   //(avoid multiple LFs)
      cin.ignore(10, '\n');      //eat chars, including newline
      dest_floor = atoi(ustring);
      set_cursor_pos(1,24); clear_line(); //clear old input line
      if(dest_floor==0)               //if no more requests,
         {                            //clear bottom three lines
         set_cursor_pos(1,22); clear_line();      
         set_cursor_pos(1,23); clear_line();
         set_cursor_pos(1,24); clear_line();
         return;
         }
      --dest_floor;                   //start at 0, not 1
      if(dest_floor==current_floor)   //chose this very floor
         { --j; continue; }           //   so forget it
      //if we're stopped, first choice made sets direction
      if(j==1 && current_dir==STOP)
         current_dir = (dest_floor < current_floor) ? DN : UP;
      destination[dest_floor] = true; //record selection
      dests_display();                //display destinations
      }
   }  //end get_destinations()
//--------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -