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 | #include <stdio.h> #include <stdlib.h> #include <time.h> // ¿ä¼Ò ŸÀÔ typedef struct { int id; int arrival_time; int service_time; int priority; // ¿ì¼±¼øÀ§ Ãß°¡ } 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 queue_print(QueueType* q) { printf("QUEUE(front=%d rear=%d) = ", q->front, q->rear); if (!is_empty(q)) { int i = q->front; do { i = (i + 1) % (MAX_QUEUE_SIZE); printf("ID=%d, Arrival=%d, Service=%d, Priority=%d | ", q->data[i].id, q->data[i].arrival_time, q->data[i].service_time, q->data[i].priority); if (i == q->rear) break; } while (i != q->front); } printf("\n"); } // »ðÀÔ ÇÔ¼ö (¿ì¼±¼øÀ§ ±â¹Ý) void enqueue(QueueType* q, element item) { if (is_full(q)) error("Å¥°¡ Æ÷È»óÅÂÀÔ´Ï´Ù"); // »õ·Î¿î rear À§Ä¡¸¦ ÀúÀå int new_rear = (q->rear + 1) % MAX_QUEUE_SIZE; // ¿ì¼±¼øÀ§¿¡ µû¶ó ÀûÀýÇÑ À§Ä¡¿¡ »ðÀÔ int i = q->rear; // ÇöÀç rear À§Ä¡ // ¿ì¼±¼øÀ§¿¡ µû¶ó ÀûÀýÇÑ À§Ä¡¸¦ ã¾Æ À̵¿ while (i != q->front && q->data[i % MAX_QUEUE_SIZE].priority < item.priority) { // ÇöÀç À§Ä¡¸¦ ÇÑ Ä µÚ·Î À̵¿ q->data[new_rear] = q->data[i % MAX_QUEUE_SIZE]; new_rear = i; // À̵¿ÇÑ À§Ä¡¸¦ »õ rear·Î ¼³Á¤ i = (i - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE; // i¸¦ ÇÑ Ä µÚ·Î À̵¿ (¿øÇü Å¥) } // ÀûÀýÇÑ À§Ä¡¿¡ »õ Ç׸ñ »ðÀÔ q->data[new_rear] = item; q->rear = (q->rear + 1) % MAX_QUEUE_SIZE; // rear¸¦ ¾÷µ¥ÀÌÆ® } // »èÁ¦ ÇÔ¼ö 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]; } int main(void) { int minutes = 60; int total_wait = 0; int total_customers = 0; int service_time = 0; int service_customer; QueueType queue; init_queue(&queue); srand(time(NULL)); for (int clock = 0; clock < minutes; clock++) { printf("½Ã°¢=%d\n", clock); if ((rand() % 10) < 7) { element customer; customer.id = total_customers++; customer.arrival_time = clock; customer.service_time = rand() % 8 + 1; customer.priority = rand() % 10; // ·£´ýÀ¸·Î ¿ì¼±¼øÀ§ ¼³Á¤ enqueue(&queue, customer); printf("\t°í°´ %2dÀÌ %2dºÐ ÀÔÀå ¾÷¹«½Ã°£=%2dºÐ (¿ì¼±¼øÀ§=%d)\n", customer.id, customer.arrival_time, customer.service_time, customer.priority); } if (service_time > 0) { printf("\t\t\t\t\t\t\t °í°´ %2d ¾÷¹«Ã³¸®Áß \n", service_customer); service_time--; } else { 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; } } } printf("Àüü ´ë±â ½Ã°£=%dºÐ\n", total_wait); if (total_customers > 0) { printf("Àüü °í°´ ¼ö=%d, Æò±Õ ´ë±â ½Ã°£=%.2fºÐ\n", total_customers, (float)total_wait / total_customers); } else { printf("°í°´ÀÌ ¾ø½À´Ï´Ù.\n"); } return 0; } | cs |