/* enter_floor: makes a person with id id stand at floor floor */ static void enter_floor( lift_type lift, int id, int floor) { int i; int floor_index; int found; /* stand at floor */ found = 0; for (i = 0; i < MAX_N_PERSONS && !found; i++) { if (lift->persons_to_enter[floor][i].id == NO_ID) { found = 1; floor_index = i; } } if (!found) { lift_panic("cannot enter floor"); } /* enter floor at index floor_index */ lift->persons_to_enter[floor][floor_index].id = id; lift->persons_to_enter[floor][floor_index].to_floor = NO_FLOOR; }
/* leave_floor: makes a person with id id at enter_floor leave enter_floor */ static void leave_floor(lift_type lift, int id, int enter_floor) /* fig_end lift_c_prot */ { int i; int floor_index; int found; /* leave the floor */ found = 0; for (i = 0; i < MAX_N_PERSONS && !found; i++) { if (lift->persons_to_enter[enter_floor][i].id == id) { found = 1; floor_index = i; } } if (!found) { lift_panic("cannot leave floor"); } /* leave floor at index floor_index */ lift->persons_to_enter[enter_floor][floor_index].id = NO_ID; lift->persons_to_enter[enter_floor][floor_index].to_floor = NO_FLOOR; }
static void exit_lift(lift_type lift, int id){ int i; int sucess = 0; for(i=0; i < MAX_N_PASSENGERS; i++){ if(lift->passengers_in_lift[i].id == id){ lift->passengers_in_lift[i].id = NO_ID; lift->passengers_in_lift[i].to_floor = NO_FLOOR; sucess = 1; break; } } if(!sucess){lift_panic("Could not exit lift!");} }
static void enter_lift(lift_type lift, int id, int destination_floor) { int i; int sucess = 0; for(i = 0; i < MAX_N_PASSENGERS; i++){ if(lift->passengers_in_lift[i].id == NO_ID){ lift->passengers_in_lift[i].id = id; lift->passengers_in_lift[i].to_floor = destination_floor; sucess = 1; break; } } if(!sucess){lift_panic("Could not enter lift!");} }
int leave_lift(lift_type lift, int id, int to_floor) { int found = 0; int my_lift_index = 0; int i; /* where am I? */ for (i = 0; i < MAX_N_PASSENGERS && !found; i++) { if (lift->passengers_in_lift[i].id == id ) { found = 1; my_lift_index = i; } } if (!found) { lift_panic("I'm not in the lift! I'm looost!"); return 0; } // Move person out of el systemo lift->passengers_in_lift[my_lift_index].id = NO_ID; lift->passengers_in_lift[my_lift_index].to_floor = NO_FLOOR; return 1; }