# include # include // 프로그램 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; }