옵션 |
|
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 | #include<stdio.h> #include<stdlib.h> #include<string.h> #define SIZE 1024 typedef struct QueueNode{ char name[1024]; }element; int front, rear; void init(){ front = rear = 0; } int is_empty() { return front == rear; } int is_full() { return (rear + 1) % SIZE == front; } void enqueue(element *q, char name[]) { if (is_full()) { fprintf(stderr, "Queue is full.\n"); exit(1); } rear = rear + 1 % SIZE; strcpy_s(q[rear].name, SIZE, name); } element *dequeue(element *q) { if (is_empty()){ fprintf(stderr, "Queue is empty!\n"); exit(2); } front = front + 1 % SIZE; return q[front].name; } element *peek(element *q) { if (is_empty()){ fprintf(stderr, "Queue is empty!\n"); exit(2); } return q[front].name; } main() { element male[SIZE]; element female[SIZE]; init(); printf("미팅주선 프로그램입니다.\n\n"); for (;;){ char name[SIZE] = { 0 }; char sex; printf("고객이름 : "); gets_s(name, sizeof(name)); fflush(stdin); printf("성별을 입력하세요(f or m) "); sex = getc(stdin); fflush(stdin); if (sex == 'z') break; switch (sex){ case 'm': enqueue(&male[rear], name); break; case 'f': enqueue(&female[rear], name); break; default: fprintf(stderr, "Only accept m or f!\n\n"); exit(1); } if (peek(&male[front]) == 0 || peek(&female[front]) == 0){ printf("\n아직 대상자가 없습니다. 기다려주십시요.\n"); } else{ printf("커플이 탄생했습니다! "); printf("%s과 ", dequeue(&male[front])); printf("%s \n\n", dequeue(&female[front])); } } } | cs |