-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
155 lines (124 loc) · 3.85 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h>
#include "Queue.h"
#include "Customer.h"
#define closingtime 20
#define numSim 10
#define maxQueueLength 10
int main(int argc, char** argv){
/* Values to keep track of simulation results*/
int fulfilled = 0;
int unfulfilled = 0;
int timedOut = 0;
int timeSpentWaiting = 0;
int timeTakenAfterClosing = 0;
for (int i = 0; i < numSim; i++) {
Queue* q = initQueue();
Customer* servicepoints[5] = { NULL };
enqueue(q, initCustomer(0, 1));
enqueue(q, initCustomer(0, 2));
enqueue(q, initCustomer(10, 3));
enqueue(q, initCustomer(10, 4));
enqueue(q, initCustomer(0, 5));
enqueue(q, initCustomer(10, 6));
printQueue(q);
impatientPeopleLeave(q);
printQueue(q);
printf("%d left in queue", q->Count);
exit(-1);
/* Check for extreme inputs */
if (sizeof(servicepoints) / sizeof(servicepoints[0]) < 1) {
printf("Not enough service points for the simulation!");
exit(-1);
}
for (int i = 0; i < closingtime; i++) {
/*1. Zero or more fulfilled customers have finished being served and leave the system.
2. A service point that is not serving a customer will start to serve the next available customer,
and that customer will leave the queue.
3. If a customer in the queue has reached their waiting tolerance limit, then they leave the queue
and become a timed - out customer.
4. Zero or more customers arrive into the system, and either join the back of the queue, or , if the
queue is full, leave the system as an unfulfilled customer.*/
printQueue(q);
/*1*/
for (int i = 0; i < sizeof(servicepoints) / sizeof(servicepoints[0]); i++) {
if (servicepoints[i]) {
servicepoints[i]->servingTime--;
if (servicepoints[i]->servingTime == 0) {
free(servicepoints[i]);
servicepoints[i] = NULL;
/* Record Statistics */
fulfilled++;
}
}
else {
/*2*/
if (q->Head) {
Customer* nextInLine = dequeue(q);
servicepoints[i] = nextInLine;
/* Record Statistic */
timeSpentWaiting += nextInLine->startingPatience - nextInLine->patienceLevel;
}
}
if (servicepoints[i]) {
printf("[ Patience: %d ServingTime: %d ]", servicepoints[i]->patienceLevel, servicepoints[i]->servingTime);
}
else {
printf("[ TEMPORARILY EMPTY ]");
}
}
printf("\n\n");
/* 3 */
if (q->Count > 0) {
//unfulfilled += removeFromQueue(q); dEpRecAtEd
}
printQueue(q);
/* 4 */
int newCustomers = 1; /*Random Amount*/
for (int i = 0; i < newCustomers; i++) {
enqueue(q, initCustomer(30, 4));
}
}
// After simulation is completed
while (q->Count > 0) {
printQueue(q);
/*1*/
for (int i = 0; i < sizeof(servicepoints) / sizeof(servicepoints[0]); i++) {
if (servicepoints[i]) {
servicepoints[i]->servingTime--;
if (servicepoints[i]->servingTime == 0) {
free(servicepoints[i]);
servicepoints[i] = NULL;
}
}
else {
/*2*/
if (q->Head) {
Customer* nextInLine = dequeue(q);
servicepoints[i] = nextInLine;
/* Record Statistic */
timeSpentWaiting += nextInLine->startingPatience - nextInLine->patienceLevel;
}
}
if (servicepoints[i]) {
printf("[ Patience: %d ServingTime: %d ]", servicepoints[i]->patienceLevel, servicepoints[i]->servingTime);
}
else {
printf("[ TEMPORARILY EMPTY ]");
}
}
printf("\n\n");
/*3*/
//unfulfilled += removeFromQueue(q);
}
printf("Stats: \n Time taken to service all after simulation: %d", timeTakenAfterClosing);
/* Clean up the queue */
free(q);
}
/* Calculating Averages */
fulfilled /= numSim;
unfulfilled /= numSim;
timedOut /= numSim;
timeTakenAfterClosing /= numSim;
printf("\n-----\n\n Avg Fulfilled: %d | Avg Unfulfilled: %d | Avg Timed Out: %d | Avg. Time Taken After Closing: %d", fulfilled, unfulfilled, timedOut, timeTakenAfterClosing);
return 0;
}