Advertisement
Guest User

Untitled

a guest
Nov 15th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define TRUE 1
  5. #define FALSE 0
  6.  
  7. int main(void)
  8. {
  9.     int selection;
  10.     srand(time(NULL));
  11.  
  12.     printf("~~ FAST FOOD RESTARAUNT SIMULATION ~~\n\nPress 1 to initialize scenario 1. \nPress 2 to initialize scenario 2. \nAny other key will exit the program.\n\nSelection: ");
  13.     scanf("%d",&selection);
  14.  
  15.     switch(selection)
  16.     {
  17.         case 1:
  18.             scenarioOne();
  19.             break;
  20.         case 2:
  21.             scenarioTwo();
  22.             break;
  23.         default:
  24.             break;
  25.     }
  26. return 0;
  27. }
  28.  
  29. int scenarioOne(void)
  30. {
  31.     printf("SCENARIO ONE CHOSEN.\n\n");
  32.  
  33. /// USER DEFINES CUSTOMERS PER MINUTE
  34.     double r;
  35.     printf("Define the arrival rate of customers per minute: ");
  36.     scanf("%lf",&r);
  37.  
  38. /// CUSTOMERS PER SECOND
  39.     double customerArrive; // the chance a customer will arrive at any given second
  40.     customerArrive=(r*1.0)/60;
  41.     printf("Theoretically, there will be %f customers arriving per second.\n Press 'Enter' to continue.\n",customerArrive);
  42.  
  43.         getchar();
  44.         getchar();
  45.  
  46. /// EACH ITERATION IS A 10-MINUTE INTERVAL
  47. // THIS IS WRONG. TWO FOR-LOOPS CANNOT MAKE TIME A CONTINUOUS FUNCTION...
  48.     int i;
  49.     for (i=0; i<18; i++)
  50.         interval(customerArrive);
  51. }
  52.  
  53. int interval(double customerArrive)
  54. {
  55.         // Counter to determine which interval is currently running.
  56.         static int intNum;
  57.         intNum++;
  58.         printf("---------------------------INTERVAL %d------------------------\n\n",intNum);
  59.  
  60.         int totalCustomer=0; // total # of customers
  61.         int completedOrder=0; // total # of completed orders
  62.  
  63.         int totalOrderWait=0; // total wait time for ORDERS
  64.         int totalDelayWait=0; // total wait time for DELAYS
  65.         int avgWait=0; // AVERAGE wait time
  66.  
  67.         int delay1=0; // delay time
  68.         int delay2=0; // delay time
  69.  
  70.         int queue=0; // customer queue count
  71.  
  72.         int cash1empty=1; // whether or not a customer can get served by cash 1
  73.         int cash2empty=1; // whether or not a customer can get served by cash 2
  74.  
  75.         int cash1salad,cash1burger,cash2salad,cash2burger; // different types of orders
  76.  
  77.         /// EACH ITERATION IS ONE SECOND
  78.             int t;
  79.             for (t=0;t<600;t++)
  80.             {
  81.                 /// Generate random value between 0 and 1. Used as a decimal to compare to chance of customer arrival
  82.                 double x;
  83.                 x= (double) rand() / (double) RAND_MAX;
  84.  
  85.                 if (customerArrive>=x)
  86.                 {
  87.                     // A customer arrived during this second!
  88.                     totalCustomer++;
  89.                     queue++;
  90.  
  91.                 }
  92.                 if (queue > 0)
  93.                 {
  94.  
  95.                     // In the case that both cashiers are empty, the program must decide where the customer goes with a coin toss
  96.                     if ((cash1empty==TRUE)&&(cash2empty==TRUE))
  97.                     {
  98.                         switch((rand()%2))
  99.                         {
  100.                             case 0:
  101.                                 cash2empty=FALSE;
  102.                                 break;
  103.                             case 1:
  104.                                 cash1empty=FALSE;
  105.                                 break;
  106.                         }
  107.                     }
  108.                     if (delay1>0)
  109.                     {
  110.                         delay1--;
  111.                         totalDelayWait++;
  112.                         cash1empty=FALSE;
  113.                     }
  114.                     else
  115.                         cash1empty=TRUE;
  116.  
  117.                     if (cash1empty==TRUE)
  118.                     {
  119.                         cash1empty=FALSE;
  120.                         queue--;
  121.                         printf("Cash 1 is being used.\n");
  122.  
  123.  
  124.                         switch((rand()%2))
  125.                         {
  126.                             case 0:
  127.                                 cash1salad=(rand()%(66-55)+55);
  128.                                 printf("At t=%d, salad order took %d seconds at Cash 1.\n\n",t,cash1salad);
  129.                                 delay1=cash1salad;
  130.                                 totalOrderWait+=cash1salad;
  131.                                 completedOrder++;
  132.  
  133.                                 break;
  134.                             case 1:
  135.                                 cash1burger=(rand()%(131-111)+111);
  136.                                 printf("At t=%d, burger order took %d seconds at Cash 1.\n\n",t,cash1burger);
  137.                                 delay1=cash1burger;
  138.                                 totalOrderWait+=cash1burger;
  139.                                 completedOrder++;
  140.                                 break;
  141.                         }
  142.                     }
  143.  
  144.                     else if (cash2empty==TRUE)
  145.                     {
  146.                         cash2empty=FALSE;
  147.                         queue--;
  148.                         printf("Cash 2 is being used.\n");
  149.  
  150.  
  151.                         switch(rand()%2)
  152.                         {
  153.                             case 0:
  154.                                 cash2salad=(rand()%(76-65)+65);
  155.                                 printf("At t=%d, salad order took %d seconds at Cash 2.\n\n",t,cash2salad);
  156.                                 delay2=cash2salad;
  157.                                 totalOrderWait+=cash2salad;
  158.                                 completedOrder++;
  159.                                 break;
  160.                             case 1:
  161.                                 cash2burger=(rand()%(141-121)+121);
  162.                                 printf("At t=%d, burger order took %d seconds at Cash 2. \n\n",t,cash2burger);
  163.                                 delay2=cash2burger;
  164.                                 totalOrderWait+=cash2burger;
  165.                                 completedOrder++;
  166.                                 break;
  167.                         }
  168.                     }
  169.                 }
  170.             }
  171.  
  172.             // customer arrival rate per minute, by probability
  173.             double R;
  174.             R=(totalCustomer*1.0)/600;
  175.             printf("Customers arriving per minute in interval %d: %f\n",intNum,R);
  176.             if (completedOrder>0)
  177.             {
  178.                 avgWait=((totalOrderWait+totalDelayWait)/(completedOrder));
  179.  
  180.                 printf("Amount of customers that showed up: %d\n",totalCustomer);
  181.                 printf("Amount of customers that had their order completed: %d\n",completedOrder);
  182.  
  183.                 printf("In total, %d seconds were spent by people lining up.\n",totalDelayWait);
  184.                 printf("In total, %d seconds were spent by people taking orders.\n",totalOrderWait);
  185.  
  186.                     if (avgWait>=0)
  187.                     {
  188.                         printf("Average wait time per customer: %d\n\n",avgWait);
  189.                     }
  190.             }
  191.             else
  192.             {
  193.                 printf("No customers arrived in this interval.\n\n");
  194.             }
  195.             printf("-----------------------END OF INTERVAL %d---------------------\n\n",intNum);
  196.  
  197. }
  198.  
  199. int scenarioTwo(void)
  200. {
  201.     printf("SCENARIO TWO CHOSEN.\n\n");
  202.     // scenario two is not yet programmed...
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement