원형 덱을 이용한 은행 시뮬레이션(c언어)

임승혁·2021년 2월 8일
0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct {
	int id;
	int arrivalTime;
	int serviceTime;
}element;

#define MAX_QUEUE_SIZE 10 // 한 은행원이 받을 수 있는 고객의 MAX

typedef struct {
	element data[MAX_QUEUE_SIZE];
	int front, rear;
}QueueType;

void error(const char* message) {

	fprintf(stderr, "%s\n", message);
	exit(1);
}

void initQueue(QueueType* q) {

	q->front = q->rear = 0;
}//front 와 rear 의 값을 0 으로 초기화
int isEmpty(QueueType* q) {
	return (q->front == q->rear);
}// front 와 rear이 같다면 front가 전에 있던 데이터들을 삭제하였다는것.

int isFull(QueueType* q) {

	return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front);
}//원형큐로써 rear이 MAX_QUEUE_SIZE - 1 에 위치해 있다면 + 1 을 할시 front 와 값이 같아지고 이것은 즉 full 이라는 의미.

void enqueue(QueueType* q, element item) { // 삽입 rear을 이용
	if (isFull(q))
		error("큐가 포화상태입니다");
	q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;//자기 자신의 값을 불러오는것.
	q->data[q->rear] = item; //rear의 위치에 item을 넣음.
} 
element dequeue(QueueType* q) { // 삭제 front를 이용

	if (isEmpty(q))
		error("큐가 공백상태입니다");
	q->front = (q->front + 1) % MAX_QUEUE_SIZE;//앞에 데이터부터 나가기 때문에 front를 이용하는 것, front위치의 값은 기본적으로 공백으로 설정해 놓기 때문에 + 1 을 해야 데이터값이 시작된다.
	return q->data[q->front]; // front위치의 +1 한 곳의 데이터.
}

int main()
{
	int minutes = 60; // 제한 시간
	int totalWait = 0;
	int totalCustomers = 0;
	int serviceTime = 0;
	int serviceCustomer;
	QueueType queue;
	initQueue(&queue);

	srand(time(NULL));
	for (int clock = 0; clock < minutes; clock++) {

		printf("현재시각=%d\n", clock);
		if ((rand() % 10) < 5) {
			element customer;
			customer.id = ++totalCustomers;
			customer.arrivalTime = clock;
			customer.serviceTime = rand() % 5 + 1;//1분 ~ 5분 처리 시간.
			enqueue(&queue, customer);
			printf("고객 %d이 %d분에 들어옵니다. 업무처리시간 = %d분\n", customer.id, customer.arrivalTime, customer.serviceTime);
		}
		if (serviceTime > 0) {

			printf("고객 %d이 업무처리중입니다. \n", serviceCustomer);
			serviceTime--;
		}
		else {
			if (!isEmpty(&queue)) {

				element customer = dequeue(&queue);
				serviceCustomer = customer.id;
				serviceTime = customer.serviceTime;
				printf("고객 %d이 %d분에 업무를 시작합니다. 대기시간은 %d분이었습니다.\n\n", customer.id, clock, clock - customer.arrivalTime);

			}
		}
	}

	printf("전체 대기 사간 = %d분 \n", totalWait);
	return 0;
}

설명 : c언어를 활용한 원형 덱을 이용해서 작성한 은행 시뮬레이션

profile
한성공대생

0개의 댓글