#include #include #define numOfMinutes 100 struct RecType { int info; struct RecType *next; }; typedef struct RecType *PtrType; PtrType Q = NULL, QTail = NULL; empty() { return Q == NULL; } void enq(int t) { PtrType tmp = (PtrType) malloc(sizeof(struct RecType)); tmp->info = t; tmp->next = NULL; if (!Q) Q = QTail = tmp; else { QTail->next = tmp; QTail = tmp; } } deq() { PtrType tmp = Q; int t; Q = Q->next; t = tmp->info; free(tmp); return t; } float ScanQ() { PtrType tmp = Q; int WaitingTime; for (WaitingTime = 0; tmp; WaitingTime += tmp->info, tmp = tmp->next); return WaitingTime/60.0; } int option(int percents[]) { register int i, choice = random(100)+1, perc; for (perc = percents[0], i = 0; perc < choice; perc += percents[i+1], i++); return i; } main() { int arrivals[] = {15,20,25,10,30}; int service[] = {0,0,0,10,5,10,10,0,15,25,10,15}; int clerks[] = {0,0,0,0,0}, clerkssize = sizeof(clerks)/sizeof(int); int j, customers, t, i, thereIsLine = 0; float maxWait = 0.0, currWait = 0.0; for (t = 1; t <= numOfMinutes; t++) { printf("t = %d ",t); for (i = 0; i < clerkssize; i++)/* after each minute subtract */ if (clerks[i] < 60) /* at most 60 seconds from time */ clerks[i] = 0; /* left to service the current */ else clerks[i] -= 60; /* customer by clerk i; */ customers = option(arrivals); for (i = 0; i < customers; i++) /* enq all new customers (or rather */ enq(option(service)*10); /* service time they require); */ for (i = 0; i < clerkssize && !empty(); ) /* deq customers */ if (clerks[i] < 60) /* when clerks are available; */ clerks[i] += deq(); /* assign more than one customer */ else i++; /* to a clerk if service time */ /* is still below 60 sec; */ if (!empty(Q)) { thereIsLine++; currWait = ScanQ(); if (maxWait < currWait) maxWait = currWait; } } printf("\nFor %d clerks, there was a line %.1f%% of the time;\n", clerkssize,(float)thereIsLine/numOfMinutes*100.0); printf("maximum wait time was %.1f min.\n",maxWait); }