• 大小: 11KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-10
  • 语言: 其他
  • 标签: 电梯程序  六层  c++  

资源简介

完整的六层电梯程序,已经试验过,可用,希望对各位有用,有什么建议,欢迎提出

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include “lift.h“

/* some global stats */
int total_waiting = 0;
int no_of_waits = 0;
int max_wait = 0;
int min_wait = 10000000; 

/*
 * Initialize the floors
 */
void init_floors(struct floor_s* floors) 
{
  int i = 0;
  for(i = 0; i < NO_OF_FLOORS; i++) {
    floors[i].floor_number = i;
    floors[i].num_waiting = 0;
    floors[i].num_not_waiting = 0;
  }
}

/*
 * arrive_on_floor is called when a person arrives at a new floor.
 */
void arrive_on_floor(int time struct floor_s* floor struct person_s* person)
{
    person->target_floor = -1;  /* just arrived don‘t want to go
   anywhere yet */
    person->floor = floor->floor_number;

    /* store them in the first free slot in the array */
    floor->people_not_waiting[floor->num_not_waiting] = person;
    floor->num_not_waiting++;

    if (person->start_waiting != -1) {
      total_waiting += time - person->start_waiting;
      printf(“Their journey time was %d\n“ time - person->start_waiting);
      if (time - person->start_waiting > max_wait) {
max_wait = time - person->start_waiting;
      }
      if (time - person->start_waiting < min_wait) {
min_wait = time - person->start_waiting;
      }
      no_of_waits++;
      person->start_waiting = -1;
    }
}

/* 
 * init_people initialized the people to start from the ground floor 
 */
void init_people(struct floor_s* floors struct person_s* people) 
{
  int i = 0;
  for(i = 0; i < NO_OF_PEOPLE; i++) {
    people[i].name[0] = ‘A‘ + i;
    people[i].name[1] = ‘\0‘;
    people[i].start_waiting = -1;
    arrive_on_floor(0 &floors[0] &people[i]);
  }
}

/* 
 * init_lists initializes the lifts to start from the ground floor
 */
void init_lifts(struct lift_s* lifts)
{
  int i;
  for (i = 0; i < NO_OF_LIFTS; i++) {
    lifts[i].lift_id = i;
    lifts[i].occupants = 0;
    lifts[i].position = 0;
    lifts[i].speed = 0;
    lifts[i].last_floor = 0;
    lifts[i].mode = MODE_IDLE;
  }
}

/*
 * queue_for_list is called to add a person who was not previously
 * queuing for a lift to the lift queue on their current floor
 */
void queue_for_lift(struct person_s* person struct floor_s* floor)
{
  int i j = -1;

  /* find the person and remoove them from the pool of people not
     queuing */
  for (i = 0; i < floor->num_not_waiting; i++) {
    if (floor->people_not_waiting[i] == person) {
      for (j = i; j < floor->num_not_waiting-1; j++) {
      
floor->people_not_waiting[j] = floor->people_not_waiting[j+1];
      }
      break;
    }
  }
  assert(j != -1);
  floor->num_not_waiting--;
  floor->people_not_waiting[floor->num_not_waiting] = NULL;

  /* add them to the queue of people waiting for the lift */
  floor->people_waiting[floor->num_waiting] = person;
  floor->num_waiting++;
}

/*
 * decide_action is called to allow a person to randomly decide if
 * they want to go to an

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      33792  2009-03-19 11:15  LIFT\Debug\vc60.idb

     文件      28672  2009-03-19 11:15  LIFT\Debug\vc60.pdb

     文件       4260  2009-03-19 11:15  LIFT\LIFT.dsp

     文件        533  2009-03-19 11:15  LIFT\LIFT.dsw

     文件      33792  2009-03-19 11:15  LIFT\LIFT.ncb

     文件      48640  2009-03-19 11:15  LIFT\LIFT.opt

     文件        771  2009-03-19 11:15  LIFT\LIFT.plg

     文件      14893  2009-03-19 11:15  LIFT\main.cpp

     目录          0  2009-03-19 11:15  LIFT\Debug

     目录          0  2009-03-19 11:15  LIFT

----------- ---------  ---------- -----  ----

               165353                    10


评论

共有 条评论