亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? main.cpp

?? 電梯編碼程序
?? CPP
字號:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#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 another floor 
 */
void decide_action(int time, struct person_s* person, struct floor_s* floor)
{
  if (person->target_floor != -1) 
    return;  /* they've already decided where they're going */

  /* give then a 5% change of deciding to use the lift */
  if (random()%100 > 95) {

    /* choose a random floor (but not the one they're already on) */
    person->target_floor = floor->floor_number;
    while (person->target_floor == floor->floor_number) {
      person->target_floor = random()%NO_OF_FLOORS;
    }

    /* add them to the queue */
    queue_for_lift(person, floor);
    printf("Person %s on floor %d decides to go to floor %d\n", person->name,
	   person->floor, person->target_floor);

    /* the wait starts here */
    person->start_waiting = time;
    return;
  } 
  
  /* otherwise do nothing */
}

/*
 * person_enters_lift is called to move a person from a floor to a lift
 */
void person_enters_lift(int time, struct lift_s* lift, struct floor_s* floor) {
  struct person_s * person;
  int i;
  assert(floor->num_waiting > 0);

  person = floor->people_waiting[0];
  /* shuffle everyone else up the queue */
  for (i = 0; i < floor->num_waiting-1; i++) {
    floor->people_waiting[i] = floor->people_waiting[i+1];
  }
  floor->num_waiting--;
  floor->people_waiting[floor->num_waiting] = NULL;

  printf("Person %s enters lift %d on floor %d\n", 
	 person->name, lift->lift_id, floor->floor_number);
  lift->people[lift->occupants] = person;
  lift->occupants++;
}

/* 
 * people_enter_lift is called to move as many people as will fit from
 * a floor queue to a lift
 */
void people_enter_lift(int time, struct lift_s* lift, struct floor_s* floor) {
  while (floor->num_waiting > 0
	 && lift->occupants < MAX_PEOPLE_IN_LIFT) {
    person_enters_lift(time, lift, floor);
  }
}

/*
 * person_leaves_lift is called to move a person from a lift to their
 * target floor (when the lift is at that floor)
 */
void person_leaves_lift(int time, struct person_s* person, 
			struct floor_s* floor, struct lift_s* lift) {
  printf("Person %s leaves lift %d on floor %d\n", 
	 person->name, lift->lift_id, floor->floor_number);
  arrive_on_floor(time, floor, person);
}

/*
 * people_leave_lift is called to move people from a lift to their
 * target floor (when the lift is at that floor)
 */
void people_leave_lift(int time, struct lift_s* lift, struct floor_s* floor) {
  int i, j;
  for (i = 0; i < lift->occupants; i++) {
    while (lift->people[i]->target_floor == floor->floor_number) {
      person_leaves_lift(time, lift->people[i], floor, lift);

      /* shuffle everyone else up to fill the gap */
      for (j = i; j < lift->occupants-1; j++) {
	lift->people[j] = lift->people[j+1];
      }
      lift->occupants--;
    }
  }
}

/*
 * distance_to_target calculates how far the lift still has to go to
 * reach its target floor
 */
int distance_to_target(struct lift_s* lift)
{
  int target_position, dist;
  target_position = lift->next_floor * FEET_PER_FLOOR;
  dist = target_position - lift->position;
  return abs(dist);
}

/*
 * braking_distance calculates the distance a lift will take to stop
 * if it tries to stop from the given speed
 */
int braking_distance(int speed)
{
  int dist = 0;
  speed = abs(speed);
  while(speed > 0) {
    dist += speed;
    speed--;
  }
  return dist;
}


/* 
 * speed_up is called to accelerate a lift
 */
void speed_up(struct lift_s* lift)
{
  if (lift->speed == 0) {
    /* we weren't moving, so figure start in the right direction */
    if (lift->next_floor * FEET_PER_FLOOR > lift->position) 
      lift->speed = 1;
    else 
      lift->speed = -1;
  } else if (lift->speed > 0) {
    /* going up */
    if (lift->speed < MAX_SPEED)
      lift->speed++;
  } else {
    /* going down, so speed must become more negative */
    if (abs(lift->speed) < MAX_SPEED)
      lift->speed--;
  }
}

/* 
 * slow_down is called to deccelerate a lift
 *
 */
void slow_down(struct lift_s* lift)
{
  if (lift->speed == 0)
    /* can't slow down, we're already stopped */
    return;
  if (lift->speed > 0)
    /* going up */
    lift->speed--;
  else
    /* going down */
    lift->speed++;
}

/* 
 * current_floor returns the number of the floor that a lift is stopped at
 */
int current_floor(struct lift_s* lift)
{
  assert(lift->position % FEET_PER_FLOOR == 0);
  return lift->position/FEET_PER_FLOOR;
}

/*
 * button_pressed returns TRUE if any lift button has been pressed on
 * any floor.
 */
int button_pressed(struct floor_s* floors) 
{
  int i;
  for (i = 0; i < NO_OF_FLOORS; i++) {
    if (floors[i].num_waiting > 0) {
      printf("Button pressed: TRUE\n");
      return TRUE; 
    }
  }
  /* printf("Button pressed: FALSE\n"); */
  return FALSE; 
}

/*
 * decide_next_floor is called when the lift doors have closed, and a
 * lift is about to depart.  It chooses the next floor the lift will
 * stop at.  Essentially it is the lift scheduling algorithm, and so
 * this determines the performance of the lift system
 */
int decide_next_floor(struct lift_s* lift, struct floor_s* floors)
{
  int closest_floor = -1, this_floor, i;
  
  /*
  int current_floor(struct lift_s* lift)
{
  assert(lift->position % FEET_PER_FLOOR == 0);
  return lift->position/FEET_PER_FLOOR;
}
*/
  this_floor = current_floor(lift);
  printf("Lift %d (on floor %d) is deciding the next floor\n",
	 lift->lift_id, this_floor);

  /* is there anyone in the lift?  if so go where they want */
  /* find the closest floor from those that the people in the lift
     want to visit */
  if (lift->occupants > 0) {
    for (i = 0; i < lift->occupants; i++) {
      printf("Passenger %s chose floor %d\n", lift->people[i]->name,
	    lift->people[i]->target_floor);
      if (closest_floor == -1) {
	       /* first time through the loop */
	       closest_floor = lift->people[i]->target_floor;
      } else if ( abs(lift->people[i]->target_floor - this_floor) <
		     abs(closest_floor - this_floor) ) {
	       /* this floor is closer than the previous closest */
	       closest_floor = lift->people[i]->target_floor;
      }
    }
    printf("The closest chosen floor is %d\n", closest_floor);
    assert(closest_floor != -1);
    return closest_floor;
  }

  /* there's no-one in the lift, so we must choose from one of the
     floors where people are waiting */
  for (i = 0; i < NO_OF_FLOORS; i++) {
    printf("There's no-one in the lift\n");
    if (i == this_floor) continue; /* must move to a different floor */
    if (floors[i].num_waiting > 0) {
      printf("Button is pressed on floor %d\n", i);
    }
    if ( (floors[i].num_waiting > 0) &&
	 (abs(i - this_floor) < abs(closest_floor - this_floor) 
	 || closest_floor == -1)) {
      closest_floor = i;
    }
  }

  /* if we haven't got an answer, don't move the lift */
  if (closest_floor == -1)
    closest_floor = this_floor;
  else
    printf("The closest chosen floor is %d\n", closest_floor);
  return closest_floor;
}


/*
 * move_lift implements the full lift state machine.  The doors open,
 * then they stay open for a little while, during which people leave
 * and enter the lift.  The doors close, then the lift decides when
 * floor to visit next.  It accelerates, then it decelerates, stops,
 * and the doors open again.
 */
void move_lift(int time, struct lift_s* lift, struct floor_s* floors) 
{
  int distance_to_go;

  switch(lift->mode) {
  case MODE_OPENING:
    if (lift->timer < DOOR_OPENING_DELAY) {
      /* doors are opening, but not yet fully open */
      lift->timer++;
    } else {
      /* doors are fully open */
      lift->timer = 0;
      lift->mode = MODE_OPEN;
      printf("Lift %d, doors now open\n", lift->lift_id);
    }
    break;
  case MODE_OPEN:
    /* allow people in while there is space */
    people_leave_lift(time, lift, &floors[current_floor(lift)]);
    people_enter_lift(time, lift, &floors[current_floor(lift)]);

    /* close the doors if the timer has expired */
    if (lift->timer < DOOR_OPEN_DELAY) {
      /* not yet time */
      lift->timer++;
    } else {
      /* time to close the doors */
      lift->timer = 0;
      printf("Lift %d, doors starting to close\n", lift->lift_id);
      lift->mode = MODE_CLOSING;
    }
    break;
  case MODE_CLOSING:
    /* model doors closing */
    if (lift->timer < DOOR_CLOSING_DELAY) {
      /* doors closing, but not yet closed */
      lift->timer++;
      break;
    }

    /* doors are closed, what happens next? */
    printf("Lift %d, doors now closed\n", lift->lift_id);
    lift->timer = 0;
    if (lift->occupants == 0 && button_pressed(floors) == FALSE) {
      /* there's nowhere to go, so just go idle */
      lift->mode = MODE_IDLE;
      lift->next_floor = -1;
      printf("Lift %d Idle\n", lift->lift_id);
    } else {
      /* start the lift moving */
      lift->next_floor = decide_next_floor(lift, floors);
      lift->last_floor = current_floor(lift);
      lift->mode = MODE_MOVING;
      printf("Lift %d starting towards floor %d\n", 
	     lift->lift_id, lift->next_floor);
    }
    break;
  case MODE_MOVING:
    /* lift is moving; control the speed to get there quickly without
       overshooting */
    lift->position += lift->speed;
    printf("Lift %d moving, position: %d, speed %d\n", 
	   lift->lift_id, lift->position, lift->speed);
    distance_to_go = distance_to_target(lift);
    if (distance_to_go == 0) {
      lift->speed = 0;
      lift->next_floor = -1;
      lift->timer = 0;
      lift->mode = MODE_OPENING;
      printf("Lift %d arrived at position %d, doors opening\n",
	     lift->lift_id, lift->position);
    } else if (distance_to_go > 
	       (abs(lift->speed) + braking_distance(lift->speed))) {
      /* speed up */
      speed_up(lift);
    } else if (distance_to_go >= braking_distance(lift->speed)){
      /* this speed is OK */
    } else {
      /* slow_down */
      slow_down(lift);
    }
    break;
  case MODE_IDLE:
    /* the lift is idle */
    if (button_pressed(floors) == TRUE) {
      /* someone called the lift */
      lift->next_floor = decide_next_floor(lift, floors);
      lift->mode = MODE_MOVING;
      printf("Lift %d starting towards floor %d\n", 
	     lift->lift_id, lift->next_floor);
    }
    break;
  }
}

/*
 * main loop, all the main paramaters are in lift.h
 */

int main(int arc, char **argv) 
{
  int time;
  struct floor_s floors[NO_OF_FLOORS];
  struct person_s people[NO_OF_PEOPLE];
  struct lift_s lifts[NO_OF_LIFTS];

  init_floors(floors);
  init_people(floors, people);
  init_lifts(lifts);

  srandom(0);  /* seed the random number generator so we always get
		  the same answer */

  /* run the simulator for the prescribed time */
  for (time = 0; time < MAX_TIME; time++) {
    int i;
    printf("t=%d\n", time);
    for (i = 0; i < NO_OF_PEOPLE; i++) {
      if (people[i].floor != -1) {
	/* give each person a chance to decide what to do */
	decide_action(time, &people[i], &floors[people[i].floor]);
      } else {
	/* they're in the lift, so they don't get a choice */
      }
    }
    for (i = 0; i < NO_OF_LIFTS; i++) {
      move_lift(time, &lifts[i], floors);
    }
  }

  printf("-------------------------------------------------------------\n");
  printf("Lift Statistics:\n");
  printf("  Total waiting: %d\n", total_waiting);
  printf("  No. of completed journeys: %d\n", no_of_waits);
  printf("  Mean wait time: %f\n", (double)total_waiting / no_of_waits);
  printf("  Min wait time: %d\n", min_wait);
  printf("  Max wait time: %d\n", max_wait);
	 
  exit(0);
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
美女尤物国产一区| 奇米色777欧美一区二区| 亚洲色欲色欲www| 午夜激情久久久| 久久99在线观看| www.日韩大片| 在线观看91精品国产入口| 欧美一三区三区四区免费在线看| 久久女同精品一区二区| 亚洲人123区| 国产一区二区按摩在线观看| 欧美综合色免费| 久久午夜免费电影| 五月天一区二区| 成人av小说网| 日韩欧美激情四射| 亚洲欧美一区二区三区久本道91 | 欧美一级一级性生活免费录像| 久久久蜜臀国产一区二区| 国产综合久久久久影院| 色综合久久99| 欧美四级电影在线观看| 久久亚洲精精品中文字幕早川悠里 | 国产成人精品亚洲777人妖| 97久久精品人人爽人人爽蜜臀 | 亚洲一区二区三区在线播放 | 免费成人性网站| 97久久精品人人做人人爽 | 国产综合久久久久影院| 色婷婷综合久色| 成人午夜视频网站| 精品一区二区综合| 欧美精品一二三四| 中文字幕在线一区二区三区| 亚洲电影第三页| 欧美亚洲免费在线一区| 亚洲国产精品成人综合| 国产福利不卡视频| 精品国产乱码久久久久久牛牛| 精品88久久久久88久久久| 久88久久88久久久| 日韩一区二区三区视频在线观看| 性做久久久久久久免费看| 色综合亚洲欧洲| 综合久久综合久久| bt7086福利一区国产| 国产女同互慰高潮91漫画| 国精品**一区二区三区在线蜜桃| 欧美一区永久视频免费观看| 国产精品夜夜嗨| 成人av在线观| 色婷婷综合久久久中文一区二区| 国产成人精品亚洲午夜麻豆| 这里是久久伊人| 亚洲123区在线观看| 欧美性猛交xxxx黑人交| 一区二区三区四区视频精品免费| 色哟哟一区二区| 亚洲一区二区精品久久av| 日本精品裸体写真集在线观看 | 日韩午夜小视频| 免费高清在线视频一区·| 日韩三级.com| 国产精品1024| 亚洲视频一区二区免费在线观看| av一二三不卡影片| 亚洲另类中文字| 91精品欧美久久久久久动漫 | 天天影视色香欲综合网老头| 欧美精品一卡两卡| 久久aⅴ国产欧美74aaa| 国产欧美日韩在线看| 91污在线观看| 6080yy午夜一二三区久久| 日韩不卡手机在线v区| 精品入口麻豆88视频| 成人性视频免费网站| 国产精品成人一区二区艾草| 国产亚洲成aⅴ人片在线观看 | 欧美一级艳片视频免费观看| 蜜臀久久久久久久| 91麻豆精品国产综合久久久久久| 久久精品久久99精品久久| 久久久电影一区二区三区| 91在线porny国产在线看| 三级影片在线观看欧美日韩一区二区| 精品日韩一区二区三区| proumb性欧美在线观看| 日本亚洲三级在线| 国产精品少妇自拍| 欧美精品一卡两卡| 高清成人在线观看| 日本韩国一区二区三区视频| 亚洲精品国产无天堂网2021| 欧美色偷偷大香| 1区2区3区精品视频| 久久99国产精品成人| 久久久精品免费观看| 色噜噜狠狠色综合中国| 经典三级在线一区| 一区二区日韩电影| 国产精品久久久久久久久久久免费看| 久久久国产精品麻豆| 欧美伊人久久久久久午夜久久久久| 色哟哟一区二区在线观看| 欧美日韩亚洲综合| 欧美日韩精品一区二区三区蜜桃| 欧洲国内综合视频| 欧美三级资源在线| 欧美日韩一区二区三区四区| 99视频有精品| 日韩免费高清电影| 精品理论电影在线| 欧美激情一区二区在线| 国模娜娜一区二区三区| 精品一区二区三区免费毛片爱| 国产一区在线观看视频| 国产在线国偷精品免费看| 精品一区二区在线看| 东方欧美亚洲色图在线| 色综合婷婷久久| 91精品啪在线观看国产60岁| 久久久久88色偷偷免费| 中文字幕中文乱码欧美一区二区| 一区二区免费在线| 麻豆精品视频在线观看免费| 国产精品自拍在线| 91色在线porny| 91精品综合久久久久久| 久久久久久久电影| 中文字幕字幕中文在线中不卡视频| 亚洲成a人片在线不卡一二三区| 久久免费偷拍视频| 国产精品嫩草久久久久| 日韩精品一级中文字幕精品视频免费观看 | 亚洲精品一区二区三区福利 | 日本美女视频一区二区| 日韩欧美一二区| 国产无遮挡一区二区三区毛片日本| 国产精品网站一区| 亚洲国产精品麻豆| 国产乱对白刺激视频不卡| 色婷婷久久一区二区三区麻豆| 欧美日韩国产在线观看| 久久蜜桃一区二区| 亚洲麻豆国产自偷在线| 日韩电影网1区2区| 成人黄色软件下载| 欧美一区二区三区视频在线 | 99久久精品国产麻豆演员表| 91.xcao| 日韩伦理免费电影| 国产一区二区视频在线| 色综合天天视频在线观看| 久久综合精品国产一区二区三区| 日韩专区一卡二卡| 在线亚洲一区二区| 自拍偷拍亚洲综合| 99麻豆久久久国产精品免费| 亚洲国产精华液网站w| 国产精品小仙女| 久久久久久久免费视频了| 精品亚洲欧美一区| 精品免费一区二区三区| 老司机免费视频一区二区| 日韩欧美亚洲国产精品字幕久久久| 天堂影院一区二区| 欧美精品在线观看一区二区| 日韩二区在线观看| 欧美一二三四区在线| 免费人成精品欧美精品| 日韩一本二本av| 紧缚捆绑精品一区二区| 久久久精品中文字幕麻豆发布| 国产成人精品综合在线观看| 国产三级一区二区三区| 粉嫩av一区二区三区在线播放| 国产精品久久久久aaaa| 99视频精品全部免费在线| 亚洲精品视频在线观看网站| 欧美色图免费看| 美女性感视频久久| 国产性色一区二区| 91福利区一区二区三区| 91精品国产免费| 国产精品夫妻自拍| 精品日韩99亚洲| 国产精品国产三级国产普通话蜜臀 | a在线播放不卡| 免费的国产精品| 日本美女一区二区三区视频| 亚洲成人久久影院| 另类小说综合欧美亚洲| 精品久久久久久综合日本欧美 | 日韩经典一区二区| 欧美va在线播放| 成人精品在线视频观看| 亚洲资源在线观看| 91精品国产免费| 成+人+亚洲+综合天堂|