728x90
반응형
#include <stdio.h>
#include <stdlib.h> // function rand()
//#include <time.h>

#define TRUE	1
#define FALSE	0

#define QUEUE_LEN	40

// Costomer order inteval(second)
#define CUS_COME_TERM	15

// Constant each Burger for Switch
#define CHE_BUR	0
#define BUL_BUR	1 
#define DUB_BUR	2 

// The time it takes to cook a hamberger(second)
#define CHE_TERM	12
#define BUL_TERM	15
#define DUB_TERM	24

typedef struct _queue
{
	int front;
	int rear;
	int Qarr[QUEUE_LEN];
}Queue;

void Init(Queue* copy)
{
	copy->front = 0;
	copy->rear = 0;
}

int Empty(Queue* copy)
{
	if (copy->front == copy->rear)
	{
		return TRUE;
	}
	else
		return FALSE;
}

int NextIndex(int idx)
{
	if (idx == QUEUE_LEN - 1)
		return 0;
	else
		return idx + 1;
}

int Peek(Queue* copy)
{
	if (Empty(copy))
	{
		printf("Queue Memory Error");
		exit(-1);
	}
	
	return copy->Qarr[NextIndex(copy->front)];
}

void Enqueue(Queue* copy, int data)
{
	if (NextIndex(copy->rear) == copy->front)
	{
		printf("Queue Memory Error");
		exit(-1);
	}

	copy->rear = NextIndex(copy->rear);
	copy->Qarr[copy->rear] = data;
}

int Dequeue(Queue* copy)
{
	if (Empty(copy))
	{
		printf("Queue Memory Error");
		exit(-1);
	}

	copy->front = NextIndex(copy->front);
	return copy->Qarr[copy->front];
}

int main(void)
{
	int makeProc = 0;
	
	int cheOrder = 0;
	int bulOrder = 0;
	int dubOrder = 0;

	int sec;

	Queue que;
	Init(&que);

	for (sec = 0; sec < 3600; sec++)
	{
		if (sec % CUS_COME_TERM == 0)
		{
			switch (rand() % 3)
			{
			case CHE_BUR:
				Enqueue(&que, CHE_TERM);
				cheOrder += 1;
				break;

			case BUL_BUR:
				Enqueue(&que, BUL_TERM);
				bulOrder += 1;
				break;

			case DUB_BUR:
				Enqueue(&que, DUB_TERM);
				dubOrder += 1;
				break;
			}
		}

		if (makeProc <= 0 && !Empty(&que))
		{
			makeProc = Dequeue(&que);
		}

		makeProc--;
	}

	printf("Simulation Report\n");
	printf(" - Cheese burger : %d \n", cheOrder);
	printf(" - Bulgogi burger : %d \n", bulOrder);
	printf(" - Double burger : %d \n", dubOrder);
	printf(" - Waiting room size: %d \n", QUEUE_LEN);

	return 0;
}
728x90
반응형

'Data Structure & Algorithm' 카테고리의 다른 글

Expression Tree  (0) 2022.10.31
Binary Tree  (0) 2022.10.30
Circular Queue(based in Array)  (0) 2022.10.29
Spinning Donuts  (0) 2022.10.28
Calculator (stack, postpix)  (0) 2022.10.28

+ Recent posts