2013-11-26 5 views
0
#include <stdio.h> 
#include <time.h> 
#include <stdlib.h> 
#define Empty 1 
#define Full 0 

float ChanceOfArrival (float CustomersPerMinute) 
{ 
float CustomersArrivalChance; 
int i; 

/*Customers arriving per second */ 

CustomersArrivalChance = (CustomersPerMinute/60) * 100; 

printf ("The chance of customers arriving is: %0.3f%%\n", CustomersArrivalChance); 

/* each 10 minute interval */ 

for (i = 0; i <= 18; i++) 
{ 
    intervals (CustomersArrivalChance); 
} 

return CustomersArrivalChance; 
} 

int intervals (CustomersArrivalChance) 
{ 

int totalCustomers = 0, totalWait = 0, queue = 0, SecondsInterval, waitingCustomers = 0; 
int Cash1Salad, Cash1Buger, Cash2Salad, Cash2Burger; 
int Cash1 = 1, Cash2 = 1; 
int cointoss; 
int x, Empty1, Empty2; 
int CustomersServed = 0; 
float RatePerMinute = 0, AverageWait = 0; 
static int intervalNumber; 
srand(time(NULL)); 

/*What could possibly happen every second in a 10 minute interval */ 
for (SecondsInterval = 0; SecondsInterval <= 600; SecondsInterval++) 
{ 
    x = rand() % 101; 

    if (CustomersArrivalChance >= x) 
    { 
     /*Customers Arrive this second */ 

     totalCustomers++; 
     queue++; 
     /*Choosing a cash at random */ 

     cointoss = rand()%2; 
    if (queue > 0) 
    { 



     /* Cash 1 is open cash 2 is busy so the customer goes to cash 1 and chooses 
     at random what they want to eat */ 
     if ((Cash1 == Empty) && (Cash2 != Empty) || (cointoss == 1)) 
     { 
      Cash1 = Full; 
      queue--; 

      switch ((rand()%2)) 
      { 
       case 0: 
        Cash1Salad = rand()% 66 + 55; 
        totalWait = totalWait + Cash1Salad; 
        Empty1 = Cash1Salad; 
        CustomersServed++; 
        break; 
       case 1: 
        Cash1Buger = rand()% 130 + 111; 
        totalWait = totalWait + Cash1Buger; 
        Empty1 = Cash2Burger; 
        CustomersServed++; 
        break; 
      } 
     } 

     /* Cash 1 is busy cash 2 is open customer goes to cash 2 and chooses what they want */ 
     else if (Cash2 = Empty) 
     { 
      Cash2 = Full; 
      queue--; 

      switch ((rand()%2)) 
      { 
       case 0: 
        Cash2Salad = rand()% 75 + 65; 
        totalWait = totalWait + Cash2Salad; 
        Empty2 = Cash2Salad; 
        CustomersServed++; 
        break; 
       case 1: 
        Cash2Burger = rand()% 140 + 121; 
        totalWait = totalWait + Cash2Burger; 
        Empty2 = Cash2Burger; 
        CustomersServed++; 
        break; 
      } 
     } 

     /*Both cashes are busy so the customer has to wait until one cash opens */ 
     else 
     { 
      totalWait++; 
      waitingCustomers++; 

     } 

     /*Clearing Cash 1 if someone went there */ 
     if (Empty1 > 0) 
     { 
      Empty1--; 
     } 
     /*empty1 is equal to 0 then cash 1 is empty */ 
     else 
     { 
      Cash1 = Empty; 
     } 
     /*Clearing cash 2 is someone went there */ 
     if (Empty2 > 0) 
     { 
      Empty2--; 
     } 
     /*empty2 is equal to 0 then cash 2 is empty */ 
     else 
     { 
      Cash2 = Empty; 
     } 
    } 
    } 
    else 
    { 
     /*nothing happens because no customer showed up */ 
    } 

} 

intervalNumber++; 

AverageWait = ((totalWait*1.0)/ (totalCustomers)); 
printf ("The average waiting time per customer in seconds is %0.2f in the interval  %d\n\n", AverageWait, intervalNumber); 

printf ("The total customers that arrived in the interval %d is %d\n\n", intervalNumber, totalCustomers); 

} 


int main (void) 
{ 
    float CustomersPerMinute; 

    printf ("Enter in the number of customers you want to arrive per minute:\n"); 
    scanf ("%f", &CustomersPerMinute); 

    ChanceOfArrival(CustomersPerMinute); 


    return 0; 
} 

안녕하세요, 저는이 프로그램을 통해 샐러드 또는 햄버거만을 제공하는 레스토랑을 시뮬레이션한다고 가정합니다.이 프로그램은 고객이 제공 할 수있는 두 명의 계산원과 단 하나의 라인 만 있습니다.식당 시뮬레이션

나는 왜 그것이 작동하지 않지만 모든 것이 논리적으로 이해한다고 생각하지만,이 프로그램을 실행하면 평균 대기 시간이 인쇄됩니다.

그러나 인쇄되는 평균 대기 시간은 모든 간격으로 동일해야합니다. 내가 원하는 게 아니야.

논리적으로 나는 고객이 무작위로 생성되기 때문에 평균 대기 시간이 각 간격마다 달라야한다고 생각합니다. 그러나 평균 대기 시간이 항상 같기 때문에 이것이 아닌 경우 어떻게 해결할 수 있습니까?

고객이 무작위로 생성 되었기 때문에 is가 다를 때 총 고객 수는 각 간격마다 동일합니다.이 문제도 어떻게 해결할 수 있습니까?

+2

나는 당신에게 한 마디가있다 : debug : – Drakosha

+1

그게 문제인지는 모르겠지만'else if (Cash2 = Empty)'에'=='를 쓰고 싶을 것이다. (알맞은 컴파일러가 거기에 경고를 내야합니다 ...) –

답변

2

srand를 반복하는 것처럼 보입니다. 그래서 주어진 초에 동일한 rand() 결과를 줄 수 있습니다. 스택 오버플로는 디버깅 포럼이 아닙니다.).

+0

함수 외부에 srand를 옮겨 놓았습니다. – Ptrickono

+0

@Ptrickono 귀하의 질문에 가장 잘 맞는 답변을 "수락"하는 것을 잊지 마십시오. – Schollii

관련 문제