-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotel.c
80 lines (65 loc) · 1.88 KB
/
hotel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hotel.h"
void person_arrives(char *command, queue_t *floors, int current_time)
{
int arrival;
int destination;
//useless string used just to contain the string in the command
char mock[MAX_INPUT_LINE_LENGTH];
//reading arrival and destination
sscanf(command, "%s %d %d", mock, &arrival, &destination);
//queueing the person at the proper floor's queue
queue_element(
floors + arrival,
get_person(destination, arrival, current_time),
current_time
);
}
void time_step_elevator(elevator_t *elevator,queue_t *floors, int total_time)
{
//first of all the people that reached the correct destination exit the elevator
exit_people(elevator);
/*
if the queue at the current floor is not empty anyone who
can enters the elevator
*/
if(!queue_is_empty(floors + elevator->current_floor))
{
enter_people(elevator, floors + elevator->current_floor, total_time);
}
}
void time_step(elevator_t *elevators, queue_t *floors, int total_time)
{
//the elevators moove
forward_time(elevators, ELEVATORS_NUMBER, floors, total_time);
//the elevators let the people in and out at the current floor
time_step_elevator(elevators, floors, total_time);
time_step_elevator(elevators + 1, floors, total_time);
}
void print_status(elevator_t *elevators, int elevators_number)
{
printf("PRINT_STATUS:\n\n");
//temp string that will contain the elevators infos
char temp[MAX_ELEVATOR_STRING_LENGTH];
for(int i = 0; i < elevators_number; i++)
{
printf("Elevator %d:\n", i + 1);
printf("%s", elevator_to_string(temp, elevators + i));
}
}
void initialize_floors(queue_t *floors)
{
for(int i = 0; i < MAX_FLOOR; i++)
{
floors[i] = get_queue();
}
}
void free_floors_queues(queue_t *floors)
{
for(int i = 0; i < MAX_FLOOR; i++)
{
free_queue(floors + i, free);
}
}