Skip to content

Commit 42f4d2a

Browse files
committed
code
0 parents  commit 42f4d2a

File tree

4 files changed

+530
-0
lines changed

4 files changed

+530
-0
lines changed

1.c

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <time.h>
4+
#include <stdbool.h>
5+
6+
// const number
7+
#define MAX_QUEUE_SIZE 100
8+
#define NUM_OF_COUNTER 4
9+
#define CUSTOMER_THRESHOLD 7
10+
#define SERVICE_TIME_LIMIT 8
11+
12+
// element = customer, QueueType = Queue
13+
typedef struct
14+
{
15+
int id;
16+
int arrival_time;
17+
int service_time;
18+
} element;
19+
20+
typedef struct
21+
{
22+
element data[MAX_QUEUE_SIZE];
23+
int front, rear;
24+
} QueueType;
25+
26+
// function prototype
27+
void error(char*);
28+
void init_queue(QueueType*);
29+
void queue_print(QueueType*);
30+
int is_empty(QueueType*);
31+
int is_full(QueueType*);
32+
void enqueue(QueueType*, element);
33+
element dequeue(QueueType*);
34+
35+
// main
36+
int main(void)
37+
{
38+
QueueType queue;
39+
element counter[NUM_OF_COUNTER];
40+
bool flag[NUM_OF_COUNTER] = {false};
41+
bool is_counter_full = false;
42+
int minutes = 60;
43+
int total_wait = 0;
44+
int total_customer = 0;
45+
int i;
46+
47+
// Initialize Queue
48+
init_queue(&queue);
49+
50+
srand(time(NULL));
51+
for(int clock = 0; clock < minutes; clock++)
52+
{
53+
printf("\nCLOCK - Current time : %d\n", clock);
54+
55+
// Create Customer
56+
if((rand() % 10) < CUSTOMER_THRESHOLD)
57+
{
58+
element customer;
59+
60+
customer.id = total_customer++;
61+
customer.arrival_time = clock;
62+
customer.service_time = rand() % SERVICE_TIME_LIMIT + 1;
63+
enqueue(&queue, customer);
64+
65+
printf("IN - Customer %d is coming in, Arrival time : %d, Estimated service time : %d\n",
66+
customer.id, customer.arrival_time, customer.service_time);
67+
}
68+
69+
// Reducing service time & Checking which counters are done
70+
for(i = 0; i < NUM_OF_COUNTER; i++)
71+
{
72+
if(flag[i])
73+
{
74+
if((counter[i].service_time--) == 0)
75+
{
76+
flag[i] = false;
77+
printf("OUT - Customer %d is done, End time : %d, Used counter : %d\n", counter[i].id, clock, i);
78+
}
79+
}
80+
}
81+
82+
// Enter counter
83+
if(!is_empty(&queue))
84+
{
85+
for(i = 0; i < NUM_OF_COUNTER; i++)
86+
{
87+
if(!flag[i])
88+
{
89+
if(!is_empty(&queue))
90+
{
91+
flag[i] = true;
92+
counter[i] = dequeue(&queue);
93+
}
94+
else
95+
break;
96+
97+
printf("START - Customer : %d, Starting time : %d, Waiting time : %d, Counter number : %d\n",
98+
counter[i].id, clock, clock - counter[i].arrival_time, i);
99+
100+
total_wait += clock - counter[i].arrival_time;
101+
}
102+
}
103+
}
104+
105+
// If all counters are full,
106+
if(!is_empty(&queue))
107+
{
108+
printf("FULL - All counters are full!!\n");
109+
printf(" Waiting Customer : ");
110+
queue_print(&queue);
111+
}
112+
}
113+
printf("\nEND - Average waiting time : %.2f", (float)total_wait / (float)total_customer);
114+
115+
return 0;
116+
}
117+
118+
void error(char *msg)
119+
{
120+
printf("%s\n", msg);
121+
exit(1);
122+
}
123+
124+
void init_queue(QueueType *q)
125+
{
126+
q->rear = q->front = 0;
127+
}
128+
129+
void queue_print(QueueType *q)
130+
{
131+
for(int i = q->front + 1; i <= q->rear; i++)
132+
if(i < q->rear)
133+
printf("%d, ", q->data[i].id);
134+
else
135+
printf("%d | ", q->data[i].id);
136+
printf("Total : %d\n", q->rear - q->front);
137+
}
138+
139+
int is_empty(QueueType *q)
140+
{
141+
return (q->rear == q->front);
142+
}
143+
144+
int is_full(QueueType *q)
145+
{
146+
return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front);
147+
}
148+
149+
void enqueue(QueueType *q, element item)
150+
{
151+
if(is_full(q))
152+
error("Queue is full\n");
153+
154+
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
155+
q->data[q->rear] = item;
156+
}
157+
158+
element dequeue(QueueType *q)
159+
{
160+
if(is_empty(q))
161+
error("Queue is empty\n");
162+
163+
q->front = (q->front + 1) % MAX_QUEUE_SIZE;
164+
return q->data[q->front];
165+
}

hanoi_loop.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Data Structure Assignment
2+
// Coding Hanoi-Tower by Loop & Stack
3+
// 2017920044 이관희
4+
5+
#include <stdio.h>
6+
7+
// stack배열의 크기
8+
#define MAX 100
9+
10+
// 함수 프로토타입
11+
void move(int, int, int);
12+
void init_stack(void);
13+
int push(int);
14+
int pop(void);
15+
int is_stack_empty(void);
16+
void hanoi_tower(int, int, int, int);
17+
18+
// 전역변수 선언
19+
int move_number = 0;
20+
int stack[MAX];
21+
int top;
22+
23+
int main(void)
24+
{
25+
// 원판 개수를 저장하는 변수
26+
int n;
27+
28+
// 원판 개수 사용자로부터 입력 받기
29+
printf("원판 개수를 입력하세요 : ");
30+
scanf("%d", &n);
31+
32+
// 하노이탑 재귀함수 호출
33+
hanoi_tower(n, 'A', 'B', 'C');
34+
// 원판을 움직인 횟수 출력
35+
printf("원판을 %d번 옮기셨습니다.\n", move_number);
36+
37+
// 프로그램 종료.
38+
return 0;
39+
}
40+
41+
// "원반을 from에서 to로 옮긴다" 를 출력하는 함수
42+
void move(int n, int from, int to)
43+
{
44+
printf("원판 %d을 %c에서 %c로 옮긴다.\n", n, from, to);
45+
move_number++;
46+
}
47+
48+
// 스택을 초기화하는 함수
49+
void init_stack(void)
50+
{
51+
top = -1;
52+
}
53+
54+
// 스택 push 함수
55+
int push(int t)
56+
{
57+
if(top >= MAX - 1)
58+
{
59+
printf("\nStack overflow");
60+
return -1;
61+
}
62+
stack[++top] = t;
63+
64+
return t;
65+
}
66+
67+
// 스택 pop 함수
68+
int pop(void)
69+
{
70+
if(top < 0)
71+
{
72+
printf("\nStack underflow");
73+
return -1;
74+
}
75+
return stack[top--];
76+
}
77+
78+
// 스택이 비었는지 확인하는 함수
79+
int is_stack_empty()
80+
{
81+
return (top > -1) ? 0 : 1;
82+
}
83+
84+
// 하노이탑을 구현하는 함수
85+
void hanoi_tower(int n, int from, int temp, int to)
86+
{
87+
init_stack();
88+
89+
// move_number++;
90+
// if(n == 1)
91+
// printf("원판 1을 %c에서 %c로 옮긴다.\n", from, to);
92+
// else
93+
// {
94+
// hanoi_tower(n - 1, from, to, temp);
95+
// printf("원판 %d을 %c에서 %c로 옮긴다.\n", n, from, to);
96+
// hanoi_tower(n - 1, temp, from, to);
97+
// }
98+
99+
// 위에 주석처리된 코드를 loop & stack으로 구현한 코드
100+
// 재귀함수의 호출과 리턴 과정을 추적하여 stack에 쌓는다
101+
while(1)
102+
{
103+
while(n > 1)
104+
{
105+
push(to);
106+
push(temp);
107+
push(from);
108+
push(n);
109+
n--;
110+
push(to);
111+
to = temp;
112+
temp = pop();
113+
}
114+
115+
printf("원판 1을 %c에서 %c로 옮긴다.\n", from, to);
116+
move_number++;
117+
118+
if(!is_stack_empty())
119+
{
120+
n = pop();
121+
from = pop();
122+
temp = pop();
123+
to = pop();
124+
125+
move(n, from, to);
126+
127+
n--;
128+
push(from);
129+
from = temp;
130+
temp = pop();
131+
}
132+
else
133+
break;
134+
}
135+
}

hanoi_recursion.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Data Structure Assignment
2+
// Coding Hanoi-Tower by Recursion
3+
// 2017920044 이관희
4+
5+
#include <stdio.h>
6+
7+
// 함수 프로토타입
8+
void hanoi_tower(int, char, char, char);
9+
10+
// 전역변수 선언
11+
int move_number = 0;
12+
13+
int main(void)
14+
{
15+
// 원판 개수를 저장하는 변수
16+
int n;
17+
18+
// 원판 개수 사용자로부터 입력 받기
19+
printf("원판 개수를 입력하세요 : ");
20+
scanf("%d", &n);
21+
22+
// 하노이탑 재귀함수 호출
23+
hanoi_tower(n, 'A', 'B', 'C');
24+
// 원판을 움직인 횟수 출력
25+
printf("원판을 %d번 옮기셨습니다.\n", move_number);
26+
27+
// 프로그램 종료.
28+
return 0;
29+
}
30+
31+
// 하노이탑을 구현하는 함수
32+
void hanoi_tower(int n, char from, char temp, char to)
33+
{
34+
move_number++;
35+
36+
if(n == 1)
37+
// 재귀함수 마지막 호출
38+
39+
// (n == 1)일 때, 남은 원판은 제일 위에 있는 원판 '1' 이다
40+
printf("원판 1을 %c에서 %c로 옮긴다.\n", from, to);
41+
else
42+
{
43+
// (n >= 2)일 때의 실행
44+
45+
// 'n - 1'개의 원판을 'A' -> 'B'로 옮긴다
46+
hanoi_tower(n - 1, from, to, temp);
47+
// n에 해당하는 원판을 (from -> to) 로 옮긴다
48+
printf("원판 %d을 %c에서 %c로 옮긴다.\n", n, from, to);
49+
// 'n - 1'개의 원판을 'B' -> 'C'로 옮긴다
50+
hanoi_tower(n - 1, temp, from, to);
51+
}
52+
}

0 commit comments

Comments
 (0)