1+ #include <stdio.h>
2+ #include <stdlib.h>
3+ #include <time.h>
4+ #include <stdbool.h>
5+
6+ // const number
7+ #define MAX_QUEUE_SIZE 100
8+ #define NUM_OF_COUNTER 4
9+ #define CUSTOMER_THRESHOLD 7
10+ #define SERVICE_TIME_LIMIT 8
11+
12+ // element = customer, QueueType = Queue
13+ typedef struct
14+ {
15+ int id ;
16+ int arrival_time ;
17+ int service_time ;
18+ } element ;
19+
20+ typedef struct
21+ {
22+ element data [MAX_QUEUE_SIZE ];
23+ int front , rear ;
24+ } QueueType ;
25+
26+ // function prototype
27+ void error (char * );
28+ void init_queue (QueueType * );
29+ void queue_print (QueueType * );
30+ int is_empty (QueueType * );
31+ int is_full (QueueType * );
32+ void enqueue (QueueType * , element );
33+ element dequeue (QueueType * );
34+
35+ // main
36+ int main (void )
37+ {
38+ QueueType queue ;
39+ element counter [NUM_OF_COUNTER ];
40+ bool flag [NUM_OF_COUNTER ] = {false};
41+ bool is_counter_full = false;
42+ int minutes = 60 ;
43+ int total_wait = 0 ;
44+ int total_customer = 0 ;
45+ int i ;
46+
47+ // Initialize Queue
48+ init_queue (& queue );
49+
50+ srand (time (NULL ));
51+ for (int clock = 0 ; clock < minutes ; clock ++ )
52+ {
53+ printf ("\nCLOCK - Current time : %d\n" , clock );
54+
55+ // Create Customer
56+ if ((rand () % 10 ) < CUSTOMER_THRESHOLD )
57+ {
58+ element customer ;
59+
60+ customer .id = total_customer ++ ;
61+ customer .arrival_time = clock ;
62+ customer .service_time = rand () % SERVICE_TIME_LIMIT + 1 ;
63+ enqueue (& queue , customer );
64+
65+ printf ("IN - Customer %d is coming in, Arrival time : %d, Estimated service time : %d\n" ,
66+ customer .id , customer .arrival_time , customer .service_time );
67+ }
68+
69+ // Reducing service time & Checking which counters are done
70+ for (i = 0 ; i < NUM_OF_COUNTER ; i ++ )
71+ {
72+ if (flag [i ])
73+ {
74+ if ((counter [i ].service_time -- ) == 0 )
75+ {
76+ flag [i ] = false;
77+ printf ("OUT - Customer %d is done, End time : %d, Used counter : %d\n" , counter [i ].id , clock , i );
78+ }
79+ }
80+ }
81+
82+ // Enter counter
83+ if (!is_empty (& queue ))
84+ {
85+ for (i = 0 ; i < NUM_OF_COUNTER ; i ++ )
86+ {
87+ if (!flag [i ])
88+ {
89+ if (!is_empty (& queue ))
90+ {
91+ flag [i ] = true;
92+ counter [i ] = dequeue (& queue );
93+ }
94+ else
95+ break ;
96+
97+ printf ("START - Customer : %d, Starting time : %d, Waiting time : %d, Counter number : %d\n" ,
98+ counter [i ].id , clock , clock - counter [i ].arrival_time , i );
99+
100+ total_wait += clock - counter [i ].arrival_time ;
101+ }
102+ }
103+ }
104+
105+ // If all counters are full,
106+ if (!is_empty (& queue ))
107+ {
108+ printf ("FULL - All counters are full!!\n" );
109+ printf (" Waiting Customer : " );
110+ queue_print (& queue );
111+ }
112+ }
113+ printf ("\nEND - Average waiting time : %.2f" , (float )total_wait / (float )total_customer );
114+
115+ return 0 ;
116+ }
117+
118+ void error (char * msg )
119+ {
120+ printf ("%s\n" , msg );
121+ exit (1 );
122+ }
123+
124+ void init_queue (QueueType * q )
125+ {
126+ q -> rear = q -> front = 0 ;
127+ }
128+
129+ void queue_print (QueueType * q )
130+ {
131+ for (int i = q -> front + 1 ; i <= q -> rear ; i ++ )
132+ if (i < q -> rear )
133+ printf ("%d, " , q -> data [i ].id );
134+ else
135+ printf ("%d | " , q -> data [i ].id );
136+ printf ("Total : %d\n" , q -> rear - q -> front );
137+ }
138+
139+ int is_empty (QueueType * q )
140+ {
141+ return (q -> rear == q -> front );
142+ }
143+
144+ int is_full (QueueType * q )
145+ {
146+ return ((q -> rear + 1 ) % MAX_QUEUE_SIZE == q -> front );
147+ }
148+
149+ void enqueue (QueueType * q , element item )
150+ {
151+ if (is_full (q ))
152+ error ("Queue is full\n" );
153+
154+ q -> rear = (q -> rear + 1 ) % MAX_QUEUE_SIZE ;
155+ q -> data [q -> rear ] = item ;
156+ }
157+
158+ element dequeue (QueueType * q )
159+ {
160+ if (is_empty (q ))
161+ error ("Queue is empty\n" );
162+
163+ q -> front = (q -> front + 1 ) % MAX_QUEUE_SIZE ;
164+ return q -> data [q -> front ];
165+ }
0 commit comments