Created
September 18, 2019 01:25
-
-
Save black7375/167ecd64d6a41df6d2da4848433309d6 to your computer and use it in GitHub Desktop.
친구 코드 리팩토링 해줬던 것(데이터구조)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define MAX_QUEUE_SIZE 100 | |
//-------------------- Queue -------------------- | |
typedef struct //도착 정보를 저장하는 구조체 | |
{ | |
int m; //번호 | |
int arrive; //도착한 시간 | |
} Arrive; | |
typedef Arrive Element; | |
typedef struct CircularQueue { | |
Element data[MAX_QUEUE_SIZE]; | |
int front; | |
int rear; | |
} Queue; | |
void error(char *str) | |
{ | |
printf("%s\n", str); | |
} | |
void init_queue(Queue *q) | |
{ | |
q->front = q->rear = 0; | |
} | |
int is_empty(Queue *q) | |
{ | |
return q->front == q->rear; | |
} | |
int is_full(Queue *q) | |
{ | |
return q->front == (q->rear + 1) % MAX_QUEUE_SIZE; | |
} | |
int size(Queue *q) | |
{ | |
return (q->rear - q->front + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE; | |
} | |
void enqueue(Queue *q, Element val) | |
{ | |
if (is_full(q)) { | |
error("큐 포화 에러"); | |
} | |
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE; | |
q->data[q->rear] = val; | |
} | |
Element dequeue(Queue *q) | |
{ | |
if (is_empty(q)) { | |
error("큐 공백 에러"); | |
} | |
q->front = (q->front + 1) % MAX_QUEUE_SIZE; | |
return q->data[q->front]; | |
} | |
Element peek(Queue *q) | |
{ | |
if (is_empty(q)) { | |
error("큐 공백 에러"); | |
} | |
return q->data[(q->front + 1) % MAX_QUEUE_SIZE]; | |
} | |
//-------------------- Service Couple -------------------- | |
char* man[] = { "성호", "영수", "영호", "영철", "정호", | |
"영진", "병철", "진호", "성수", "재호" }; | |
char* woman[] = { "미경", "미숙", "경희", "경숙", "영숙", | |
"미영", "영미", "정희", "정숙", "현숙" }; | |
//랜덤시 중복되지 않게 이미 뽑은 이름을 저장 | |
int manUsed[10]; | |
int womanUsed[10]; | |
//들어오는 숫자값 이하 수를 랜덤으로 | |
int rand_num(int num) | |
{ | |
return rand() % num; | |
} | |
//중복되지 않는 이름을 랜덤으로 뽑음. | |
int rand_name(int* usedGroup) | |
{ | |
int index = 0; | |
while (usedGroup[index = rand_num(10)] != 0); | |
usedGroup[index] = 1; | |
return index; | |
} | |
//시도 횟수 - 도착시점으로 대기 시간 | |
int wait_time(int count, Arrive* arriveNode) | |
{ | |
return count - arriveNode->arrive; | |
} | |
//큐에 남은 것들의 대기 시간 | |
int remainQueue(Queue* pickQueue, int duration) | |
{ | |
int waiting = 0; | |
while (!is_empty(pickQueue)) | |
{ | |
Arrive q = dequeue(pickQueue); | |
waiting += wait_time(duration, &q); | |
} | |
return waiting; | |
} | |
int main() | |
{ | |
setbuf(stdout, NULL); //output buffer를 없앰. 출력을 바로바로하기 위한 작업 | |
int duration; | |
double rate; | |
int matched = 0; | |
int manWaiting = 0; | |
int womanWaiting = 0; | |
int manCount = 0; | |
int womanCount = 0; | |
//값들 입력받음 | |
printf("시뮬레이션 할 최대 시간 (예: 20) = "); | |
scanf("%d", &duration); | |
printf("단위시간에 도착하는 고객 수 (예: 0.3) = "); | |
scanf("%lf", &rate); | |
srand(time(NULL)); //랜덤 준비 | |
Queue manQueue; | |
Queue womanQueue; | |
init_queue(&manQueue); | |
init_queue(&womanQueue); | |
for (int count = 1; count <= duration; count++) | |
{ | |
printf("[%2d]: ", count); | |
if (rand_num(100) < rate * 100) //rate확률로 새 사람 등록 | |
{ | |
if (rand_num(2) == 0) // 남자 또는 여자. | |
{ | |
int m = rand_name(manUsed); | |
enqueue(&manQueue, (Arrive) { m, count }); | |
printf("남학생 등록: %s", man[m]); | |
manCount++; | |
} | |
else | |
{ | |
int m = rand_name(womanUsed); | |
enqueue(&womanQueue, (Arrive) { m, count }); | |
printf("여학생 등록: %s", woman[m]); | |
womanCount++; | |
} | |
//남자, 여자큐에 모두 사람이 있으면 매칭시켜줌 | |
if (!is_empty(&manQueue) && !is_empty(&womanQueue)) | |
{ | |
Arrive qMan = dequeue(&manQueue ); | |
Arrive qWoman = dequeue(&womanQueue); | |
int qManWait = wait_time(count, &qMan); | |
int qWomanwait = wait_time(count, &qWoman); | |
printf(" ==> 커플 탄생! %s♥️%s (%d, %d 기다림)", | |
man[qMan.m], woman[qWoman.m], qManWait, qWomanwait); | |
manWaiting += qManWait; | |
womanWaiting += qWomanwait; | |
matched++; | |
} | |
} | |
printf("\n"); | |
} | |
int manRemain = size(&manQueue); | |
int womanRemain = size(&womanQueue); | |
//큐에 남은 사람 모두 빼내서 통계내기위해 더해줌 | |
manWaiting += remainQueue(&manQueue, duration); | |
womanWaiting += remainQueue(&manQueue, duration); | |
printf("========================\n"); | |
printf(" 서비스 받은 커플 수 = %d\n", matched); | |
printf(" 현재 대기 남학생 수 = %d\n", manRemain); | |
printf(" 현재 대기 여학생 수 = %d\n", womanRemain); | |
printf(" 전체 남학생 대기 시간 = %d\n", manWaiting); | |
printf(" 전체 여학생 대기 시간 = %d\n", womanWaiting); | |
printf(" 남학생 평균 대기 시간 = %.2lf\n", (double)manWaiting / manCount); | |
printf(" 여학생 평균 대기 시간 = %.2lf\n", (double)womanWaiting / womanCount); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
couple: couple.o | |
gcc couple.o -lm -o couple | |
couple.o: couple.c | |
gcc -c -O2 couple.c | |
clean: | |
rm -f couple couple.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment