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 | # include <stdio.h> # include <stdlib.h> // ÇÁ·Î±×·¥ 5.2¿¡¼ ´ÙÀ½°ú °°Àº ºÎºÐÀ» º¹»çÇÑ´Ù. // ================ ¿øÇüÅ¥ ÄÚµå ½ÃÀÛ ================= typedef struct { // ¿ä¼Ò ŸÀÔ int id; int arrival_time; int service_time; } element; // ±³Ã¼! // ================ ¿øÇüÅ¥ ÄÚµå Á¾·á ================= // ===== ¿øÇüÅ¥ ÄÚµå ½ÃÀÛ ====== #define MAX_QUEUE_SIZE 100 typedef struct { // Å¥ ŸÀÔ element data[MAX_QUEUE_SIZE]; int front, rear; } QueueType; // ¿À·ù ÇÔ¼ö void error(char *message) { fprintf(stderr, "%s\n", message); exit(1); } // °ø¹é »óÅ °ËÃâ ÇÔ¼ö void init_queue(QueueType *q) { q->front = q->rear = 0; } // °ø¹é »óÅ °ËÃâ ÇÔ¼ö int is_empty(QueueType *q) { return (q->front == q->rear); } // Æ÷È »óÅ °ËÃâ ÇÔ¼ö int is_full(QueueType *q) { return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front); } // ¿øÇüÅ¥ Ãâ·Â ÇÔ¼ö // »ðÀÔ ÇÔ¼ö void enqueue(QueueType *q, element item) { if (is_full(q)) error("Å¥°¡ Æ÷È»óÅÂÀÔ´Ï´Ù"); q->rear = (q->rear + 1) % MAX_QUEUE_SIZE; q->data[q->rear] = item; } // »èÁ¦ ÇÔ¼ö element dequeue(QueueType *q) { if (is_empty(q)) error("Å¥°¡ °ø¹é»óÅÂÀÔ´Ï´Ù"); q->front = (q->front + 1) % MAX_QUEUE_SIZE; return q->data[q->front]; } // »èÁ¦ ÇÔ¼ö element peek(QueueType *q) { if (is_empty(q)) error("Å¥°¡ °ø¹é»óÅÂÀÔ´Ï´Ù"); return q->data[(q->front + 1) % MAX_QUEUE_SIZE]; } // ===== ¿øÇüÅ¥ ÄÚµå ³¡ ====== void customer_service(int banker, int clock, QueueType *queue, int *service_time, int *service_customer, int *total_wait) { if (*service_time > 0) { printf("\t\t\t\t\t ÀºÇà¿ø(%d): °í°´ %2d ¾÷¹«Ã³¸®Áß \n", banker, *service_customer); (*service_time)--; } else { if(*service_customer >=0) printf("\t\t\t\t\t ÀºÇà¿ø(%d): °í°´ %2d ¾÷¹«Á¾·á¡Ú \n", banker, *service_customer); if (!is_empty(queue)) { element customer = dequeue(queue); *service_customer = customer.id; *service_time = customer.service_time; printf("\t°í°´ %2dÀÌ %2dºÐ ½ÃÀÛ ´ë±â½Ã°£=%2dºÐ\n", customer.id, clock, clock - customer.arrival_time); *total_wait += clock - customer.arrival_time; } } } #define NUM_BANKER 3 int main(void) { int minutes = 60; int total_wait = 0; int total_customers = 0; int service_time[NUM_BANKER] = { 0 }; int service_customer[NUM_BANKER] = { -1, -1, -1 }; int num_service; int clock; QueueType queue; init_queue(&queue); srand(34); for (clock = 0; clock < minutes; clock++) { printf("½Ã°¢=%2d\n", clock); if ((rand() % 10) < 7) { element customer; customer.id = total_customers++; customer.arrival_time = clock; customer.service_time = rand() % 10 + 1; enqueue(&queue, customer); printf("\t°í°´ %2dÀÌ %2dºÐ ÀÔÀå ¾÷¹«½Ã°£=%2dºÐ\n", customer.id, customer.arrival_time, customer.service_time); } for (int i = 0; i < NUM_BANKER; i++) { customer_service(i+1, clock, &queue, &service_time[i], &service_customer[i], &total_wait); } num_service = service_customer[0]; for (int i = 1; i < NUM_BANKER; i++) { if (service_customer[i] > num_service) num_service = service_customer[i]; } } printf("Àüü ´ë±â ½Ã°£=%dºÐ \n", total_wait); printf("1Àδç Æò±Õ ´ë±â ½Ã°£=%lfºÐ \n", (double)total_wait/(double)(num_service+1)); return 0; } | cs |