简体   繁体   中英

Restaurant simulation

#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;
}

Hi, I have this program that is suppose to simulate a restaurant that only serves a salad or burger, there are only two cashiers and only one line up that the customers can line up in to get served.

I am not sure why it is not working but as everything makes logical sense I believe, but when i run this program is just prints out the average waiting time as it is suppose to.

But the problem is that the average wait time that is printed is the same in every interval. Which is not what is I want.

Logically I think the average wait time should be different in each interval because customers are being generated at random. However this is not the case as the average wait time is always the same, how can i fix this?

As well the total number of customers is the same for each interval when is should be different because the customers are generated at random, how can i fix this problem as well?

It looks like you are looping srand. So it may give you the same rand() results at a given second. Stack overflow is not a debuging forum though ;) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM