숫자가 이상하게 나오네요.
유라
아마 변수의 순서가 잘못되었던가
아예 변수 자체가 다른 변수들 끼리 계산하고 있던가
이러고 있는거 같은데
당장 어떻게 해야 되는지 생각이 떠오르지는 않네요
일단 답변이 달리기 전에 해결방법을 찾기를 바라지만
혹시 몰라서 일단 소스파일도 올려봅니다.
현재시각 2분에 고객 1이 들어와서 서비스 시간 2분이 필요하니
4분때 서비스를 끝내야 되는데
5분에 서비스를 끝내고 있고,
대기시간도 뭔가 이상하군요.
대기열이 0이니 대기시간도 0이 되어야 되는데..흠..
////////////////////////////////////////////////////////소스
#include stdio.h
#include conio.h
#include stdlib.h
#include math.h
#include Windows.h
#include time.h
enum
{
BLACK,/* 0 : BLACK */
DARK_BLUE,/* 1 : DARK BLUE */
DARK_GREEN,/* 2 : DARK GREEN */
DARK_SKY_BLUE, /* 3 : DARK SKYBLUE */
DARK_RED, /* 4 : DARK RED */
DARK_VIOLET, /* 5 : DARK PURPLE */
DARK_YELLOW, /* 6 : DARK YELLOW */
GRAY, /* 7 : GRAY */
DARK_GRAY, /* 8 : DARK GRAY */
BLUE, /* 9 : BLUE */
GREEN, /* 10 : GREEN */
SKY_BLUE, /* 11 : SKYBLUE */
RED, /* 12 : RED */
VIOLET, /* 13 : PURPLE */
YELLOW, /* 14 : YELLOW */
WHITE, /* 15 : WHITE */
};
void Setcolor(int back_color, int fore_color) // 배경색, 글자색
{
int color = (back_color * 16) + fore_color;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
#define TRUE 1
#define FALSE 0
#define MAX_QUEUE_SIZE 100
typedef struct
{
int id;
int arrival_time;
int service_time;
int service_time2;
}element;
typedef struct
{
element queue[MAX_QUEUE_SIZE];
int front, rear;
}QueueType;
QueueType queue;
int duration = 10; // 시뮬레이션 시간
double arrival_prob = 0.7; // 하나의 시간 단위에 도착하는 평균 고객의 수
int max_serv_time = 5; // 하나의 고객에 대한 최대 서비스 시간
int customers = 1; // 처음 고객 수
int served_customers; // 서비스 받은 고객 수
int waited_time; // 고객들이 기다린 시간
int clock_temp;
int que_count = 0;
int que_count2 = 0;
double random()
{
return rand() / (double)RAND_MAX;
}
void error(char *message)
{
fprintf(stderr, %s\n, message);
exit(1);
}
void init(QueueType *q)
{
q - front = q - rear = 0;
}
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))
{
error(큐가 포화상태입니다);
}
q - rear = (q - rear + 1) % MAX_QUEUE_SIZE;
q - queue [q - rear] = item;
que_count++;
}
element dequeue(QueueType *q)
{
if (is_empty(q))
{
error(큐가 공백상태입니다);
}
q - front = (q - front + 1) % MAX_QUEUE_SIZE;
que_count--;
return q - queue [q - front];
}
element peek(QueueType *q)
{
if (is_empty(q))
{
error(큐가 공백상태입니다);
}
return q - queue[(q - front + 1) % MAX_QUEUE_SIZE];
}
int is_customer_arrived()
{
if (random() arrival_prob)
{
return TRUE;
}
else return FALSE;
}
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);
Setcolor(15,1);
printf_s(-------------------------------------------\n);
Setcolor(15,9);
printf_s(고객 %d씨가 %d분에 들어오셨습니다.\n, customer.id, customer.arrival_time);
Setcolor(15,12);
printf_s(필요한 서비스 시간은 %d분입니다.\n, customer.service_time);
printf_s(서비스 담당자는 직원 1 입니다.\n);
printf_s(대기열1에 있는 고객수 %d명\n,check_que(que_count));
printf_s(대기열2에 있는 고객수 %d명\n,check_que2(que_count2));
Setcolor(15,1);
printf_s(-------------------------------------------\n\n\n);
}
void init2(QueueType *q)
{
q - front = q - rear = 0;
}
int is_empty2(QueueType *q)
{
return (q - front == q - rear);
}
int is_full2(QueueType *q)
{
return((q - rear + 1) % MAX_QUEUE_SIZE == q - front);
}
void enqueue2(QueueType *q, element item)
{
if (is_full(q))
{
error(큐가 포화상태입니다);
}
q - rear = (q - rear + 1) % MAX_QUEUE_SIZE;
q - queue [q - rear] = item;
que_count2++;
}
element dequeue2(QueueType *q)
{
if (is_empty(q))
{
error(큐가 ?큐가 공백상태입니다);
}
q - front = (q - front + 1) % MAX_QUEUE_SIZE;
que_count2--;
return q - queue [q - front];
}
element peek2(QueueType *q)
{
if (is_empty(q))
{
error(큐가 공백상태입니다);
}
return q - queue[(q - front + 1) % MAX_QUEUE_SIZE];
}
int check_que(int num)
{
if (num0)
{
num-=1;
}
return num;
}; // 1번 대기열
int check_que2(int num)
{
if (num0)
{
num-=1;
}
return num;
}; // 2번 대기열
void insert_customer2(int arrival_time)
{
element customer;
customer.id = customers++;
customer.arrival_time = arrival_time;
customer.service_time2 = (int)(max_serv_time * random()) + 1;
enqueue2(&queue, customer);
Setcolor(15,1);
printf_s(-------------------------------------------\n);
Setcolor(15,9);
printf_s(고객 %d씨가 %d분에 들어오셨습니다.\n, customer.id, customer.arrival_time);
Setcolor(15,12);
printf_s(필요한 서비스 시간은 %d분입니다.\n, customer.service_time2);
printf_s(서비스 담당자는 직원 2 입니다.\n);
printf_s(대기열1에 있는 고객수 %d명\n,check_que(que_count));
printf_s(대기열2에 있는 고객수 %d명\n,check_que2(que_count2));
Setcolor(15,1);
printf_s(-------------------------------------------\n\n\n);
}
int remove_customer(int clock_temp)
{
element customer;
int service_time=0;
if (is_empty(&queue))
{
return 0;
}
customer = dequeue(&queue);
service_time = customer.service_time - 1;
served_customers++;
waited_time += clock_temp - customer.arrival_time;
Setcolor(15,9);
printf_s(고객 %d씨가 %d분에 서비스를 끝냈습니다.\n, customer.id, clock_temp);
Setcolor(15,12);
printf_s(고객 %d씨의 대기시간은 %d분이었습니다.\n, customer.id, waited_time);
return service_time;
}
int remove_customer2(int clock_temp)
{
element customer;
int service_time2=0;
if (is_empty2(&queue))
{
return 0;
}
customer = dequeue2(&queue);
service_time2 = customer.service_time2 - 1;
served_customers++;
waited_time += clock_temp - customer.arrival_time;
Setcolor(15,9);
printf_s(고객 %d씨가 %d분에 서비스를 끝냈습니다.\n, customer.id, clock_temp);
Setcolor(15,12);
printf_s(고객 %d씨의 대기시간은 %d분이었습니다.\n, customer.id, clock_temp - waited_time);
return service_time2;
}
void print_stat(void)
{
Setcolor(15,9);
printf_s(서비스 받은 고객수 = %d\n, served_customers);
Setcolor(15,1);
printf_s(전체 대기 시간 = %d분\n, waited_time);
Setcolor(15,2);
printf_s(1인당 평균 대기 시간 = %.2f분\n, (double)waited_time / served_customers);
Setcolor(15,12);
printf_s(아직 대기중인 고객수 = %d\n, customers - served_customers);
}
int main(void)
{
int service_time = 0;
int service_time2 = 0;
int clock_temp=0;
system(color f0); //배경 흰색 글자 검정색
fflush(stdin);
srand((unsigned int)time(NULL)); //시스템의 시간 값을 이용하여 랜덤하게
while(clock_temp duration)
{
clock_temp++;
Setcolor(15,1);
printf_s(현재시각 = %d\n, clock_temp);
if (is_customer_arrived())
{
if (que_count=que_count2)
{
insert_customer(clock_temp);
}
else if (que_countque_count2)
{
insert_customer2(clock_temp);
}
}
else if (service_time 0)
{
service_time--;
}
else if (service_time2 0)
{
service_time2--;
}
else if (service_time == 0)
{
service_time = remove_customer(clock_temp);
}
else if (service_time2 == 0)
{
service_time2 = remove_customer2(clock_temp);
}
}
print_stat();
_getch();
return 0;
}
-
앤드류
제 생각에는
이벤트(고객이 들어오고, 나가는) 타임 테이블이 있어야 할 것 같습니다. 그리고
큐는 기다리는 고객의 서비스 우선순위를 결정하는데 사용되어야 할 것 같습니다.
초기에 첫번째 고객의 도착 이벤트와 시뮬레이션 종료 이벤트를 발생시키면,
시뮬레이션이 시작되고,
고객이 도착하면, 다음 고객의 도착 이벤트가 등록이되고,
모든 서버가 바쁘면, 큐에 들어가고,
서비스를 받는(노는 서버를 비지하게 만드는) 순간,
나가는 시각이 결정되어, 나가
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2693676 | 열혈강의 c언어 질문입니다 | 하양이 | 2025-05-04 |
2693647 | 12.620000 을요 12.620 으로 어떻게 표현해요? (2) | 파도 | 2025-05-04 |
2693619 | 타이틀 코드.. | 단순드립 | 2025-05-03 |
2693591 | 컴파일 에러에서 질문드립니다 (3) | 게자리 | 2025-05-03 |
2693463 | 동적할당 이용시 fwrite사용을 어떻게 해야하나요..? (10) | 일본어못해요 | 2025-05-02 |
2693387 | 배열문제입니다 수정오류캡쳐했습니다 (6) | 연하얀 | 2025-05-01 |
2693356 | text 입출력 내림차순 질문입니다 ㅠ | 빛글 | 2025-05-01 |
2693328 | C언어를이용해서 .txt파일 외에 다른 확장자 파일 삭제가 가능한지.. (2) | 대나무 | 2025-05-01 |
2693299 | 파일입출력 바이너리파일 | 독특한 | 2025-04-30 |
2693273 | 오류 (1) | 귀1여운렩 | 2025-04-30 |
2693080 | visual studio 2008 express edition 등록키 말인데요 | 얀별 | 2025-04-28 |
2693053 | 배열, 구조체 관련 프로그래밍 질문드립니다. | 싸리 | 2025-04-28 |
2693025 | 프로그램을 짜봤는데요 ㅠㅠ | 상처입은마음 | 2025-04-28 |
2693001 | 워닝문제, 세그멘트결함문제 (1) | 월식 | 2025-04-28 |
2692979 | 라인한줄 이랑.. 소스 설명좀 부탁드려요.. | 이루리 | 2025-04-27 |
2692947 | 이 문제좀 풀어 주세요..ㅜㅜ (1) | 소리 | 2025-04-27 |
2692889 | 함수의 구조체 인자로 받아서 그 인자로 데이터 넣기... | 한뎃집 | 2025-04-27 |
2692862 | 성적 출력 하는 프로그램인데요~!!!도움좀 주세욤.ㅠ | 두빛나래 | 2025-04-26 |
2692831 | if 문 간단해요 빨리좀 ㅠㅠ | 이플 | 2025-04-26 |
2692805 | 실행파일이 이상해요 | 푸헷 | 2025-04-26 |