#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_QUEUE_SIZE 10
#define TRUE 1
#define FALSE 0
typedef struct {
int id;
int arrival_time;
int service_time;
} element;
typedef struct {
element queue[MAX_QUEUE_SIZE];
int front, rear;
} QueueType;
QueueType queue;
// 0에서 1사이의 실수 난수 생성 함수
double random() {
return rand()/(double)RAND_MAX;
}
// 시뮬레이션에 필요한 여러가지 상태 변수
int duration=10; // 시물레이션 시간
double arrival_prob=0.7; // 하나의 시간 단위에 도착하는 평균 고객의 수
int max_serv_time=5; // 하나의 고객에 대한 최대 서비스 시간
int clock;
// 시뮬레이션의 결과
int cashier;
int customers; // 전체고객수
int served_customers; // 서비스받은 고객수
int waited_time; // 고객들이 기다린 시간
// 랜덤 숫자를 생성하여 고객이 도착했는지 도착하지 않았는지를 판단
int is_customer_arrived() {
if( random() < arrival_prob )
return TRUE;
else return FALSE;
}
// 공백 상태 검출 함수
int is_empty(QueueType *q)
{
return (q->front == q->rear);
}
// 포화 상태 검출 함수
int is_full(QueueType *q)
{
return ((q->rear+1)%MAX_QUEUE_SIZE == q->front);
}
// 삽입 함수
void enqueue(QueueType *q, element item)
{
if( is_full(q) )
fprintf(stderr,"큐가 포화상태입니다\n");
q->rear = (q->rear+1) % MAX_QUEUE_SIZE;
q->queue[q->rear] = item;
}
// 삭제 함수
element dequeue(QueueType *q)
{
if( is_empty(q) )
fprintf(stderr,"큐가 공백상태입니다\n");
q->front = (q->front+1) % MAX_QUEUE_SIZE;
return q->queue[q->front];
}
// 새로 도착한 고객을 큐에 삽입
void insert_customer(int arrival_time) {
element customer;
customer.id = customers++;
customer.arrival_time = arrival_time;
customer.service_time=(int)(max_serv_time*random()) + 1;
enqueue(&queue, customer);
printf("[고객 %d] %d분에 도착 서비스시간은 %d분\n",
customer.id, customer.arrival_time, customer.service_time);
}
int remove_customer(int cashier) {
element customer;
int service_time=0;
//int servise_timw2=0;
if (is_empty(&queue)) return 0;
customer = dequeue(&queue);
service_time = customer.service_time-1;
served_customers++;
waited_time += clock - customer.arrival_time;
printf("\t[고객 %d] (은행원%d) %d분에 서비스 시작 대기시간은 %d분\n"
,customer.id, cashier, clock, clock - customer.arrival_time);
return service_time;
}
// 통계치를 출력한다.
void print_stat() {
printf("----------------------------------------\n");
printf("서비스받은 고객수 = %d\n",
served_customers);
printf("전체 대기 시간 = %d분\n", waited_time);
printf("1인당 평군 대기 시간 = %f분\n",
(double)waited_time/served_customers);
printf("아직 대기중인 고객수 = %d\n",
customers-served_customers);
printf("----------------------------------------\n");
}
// 시뮬레이션 프로그램
void main()
{
int service_time1=0;
int service_time2=0;
int i;
printf("----------------------------------------\n");
printf("//1분당 고객 도착율 : %f\n",arrival_prob);
printf("//1인 최대 서비스시간 : %d\n",max_serv_time);
printf("----------------------------------------\n");
clock=0;
while(clock < duration){
clock++;
printf("(t=%d) ",clock);
if (is_customer_arrived()) {
insert_customer(clock);
}
if (service_time1 > 0)
service_time1--;
else {
service_time1 = remove_customer(1);
}
if (service_time2 >0)
service_time2--;
else
service_time2 = remove_customer(2);
}
print_stat();
}


http://
학생실습 게시판