Advertisement
Guest User

Untitled

a guest
Nov 10th, 2012
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.20 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.  
  11.     printf("~~ FAST FOOD RESTARAUNT SIMULATION ~~\n\nPress 1 to initialize scenario 1. \nPress 2 to initialize scenario 2. \nPress any other key to exit the program.\n\nSelection: ");
  12.     scanf("%d",&selection);
  13.  
  14.     switch(selection)
  15.     {
  16.         case 1:
  17.             scenarioOne();
  18.             break;
  19.         case 2:
  20.             scenarioTwo();
  21.             break;
  22.         default:
  23.             break;
  24.     }
  25. return 0;
  26. }
  27.  
  28. int scenarioOne(void)
  29. {
  30.     printf("SCENARIO ONE CHOSEN.\n\n");
  31.  
  32. /// USER DEFINES CUSTOMERS PER MINUTE
  33.     double r;
  34.     printf("Define the arrival rate of customers per minute: ");
  35.     scanf("%lf",&r);
  36.  
  37. /// CUSTOMERS PER SECOND
  38.     double customerArrive; // the chance a customer will arrive at any given second
  39.     customerArrive=(r*1.0)/60;
  40.     printf("Theoretically, there will be %f customers arriving per second.\n Press 'Enter' to continue.\n",customerArrive);
  41.  
  42.         getchar();
  43.         getchar();
  44.  
  45. /// EACH ITERATION IS A 10-MINUTE INTERVAL
  46.     int i;
  47.     for (i=0; i<18; i++)
  48.         interval(customerArrive);
  49. }
  50.  
  51. int interval(double customerArrive)
  52. {
  53.         int totalCustomer=0; // total # of customers
  54.         int totalWait=0; // total wait time
  55.         int avgWait=0; // AVERAGE wait time
  56.  
  57.         int queue=0; // customer queue count
  58.  
  59.         int cash1empty=1; // whether or not a customer can get served by cash 1
  60.         int cash2empty=1; // whether or not a customer can get served by cash 2
  61.  
  62.         int cash1salad,cash1burger,cash2salad,cash2burger; // different types of orders
  63.  
  64.         /// EACH ITERATION IS ONE SECOND
  65.             int t;
  66.             for (t=0;t<600;t++)
  67.             {
  68.                 /// Generate random value between 0 and 1.
  69.                 double x;
  70.                 x= (double) rand() / (double) RAND_MAX;
  71.  
  72.                 if (customerArrive>=x)
  73.                 {
  74.                     // A customer arrived during this second!
  75.                     totalCustomer++;
  76.                     queue++;
  77.  
  78.                     if (queue > 0)
  79.                     {
  80.  
  81.                         // if both cashiers are empty, then it is 50/50 to decide where to go
  82.                         if ((cash1empty==TRUE)&&(cash2empty==TRUE))
  83.                         {
  84.                             switch((rand()%2))
  85.                             {
  86.                                 case 0:
  87.                                     cash1empty=FALSE;
  88.                                     break;
  89.                                 case 1:
  90.                                     cash2empty=FALSE;
  91.                                     break;
  92.  
  93.                             }
  94.                         }
  95.  
  96.                         if (cash1empty==TRUE)
  97.                         {
  98.                             cash1empty=FALSE;
  99.                             //queue--;
  100.  
  101.                             switch((rand()%2))
  102.                             {
  103.                                 case 0:
  104.                                     cash1salad=(rand()%(66-55)+55);
  105.                                     totalWait+=cash1salad;
  106.                                     break;
  107.                                 case 1:
  108.                                     cash1burger=(rand()%(131-111)+111);
  109.                                     totalWait+=cash1burger;
  110.                                     break;
  111.                             }
  112.                         }
  113.                         else if (cash2empty=TRUE)
  114.                         {
  115.                             cash2empty=FALSE;
  116.                             //queue--;
  117.  
  118.                             switch(rand()%2)
  119.                             {
  120.                                 case 0:
  121.                                     cash2salad=(rand()%(76-65)+65);
  122.                                     totalWait+=cash2salad;
  123.                                     break;
  124.                                 case 1:
  125.                                     cash2burger=(rand()%(141-121)+121);
  126.                                     totalWait+=cash2burger;
  127.                                     break;
  128.                             }
  129.                         }
  130.                     }
  131.                 }
  132.             }
  133.  
  134.             /// extra code: just to check
  135.             // Counter to determine which interval is currently running.
  136.             static int intNum;
  137.             intNum++;
  138.  
  139.  
  140.             /// extra code: just to check
  141.             // customer arrival rate per minute, by probability
  142.             double R;
  143.             R=(totalCustomer*1.0)/600;
  144.             printf("Customers arriving per minute in interval %d: %f\n",intNum,R);
  145.  
  146.  
  147.             avgWait=((totalWait*1.0)/(totalCustomer));
  148.                 if (avgWait>=0)
  149.                 {
  150.                     printf("Average wait time per customer: %d\n",avgWait);
  151.                 }
  152.                 else
  153.                 {
  154.                     printf("No customers arrived in this interval.\n");
  155.                 }
  156.  
  157.             /// extra code : just to check
  158.             printf("Amount of people who got queued: %d\n\n",queue);
  159.  
  160. }
  161.  
  162.  
  163. int scenarioTwo(void)
  164. {
  165.     printf("SCENARIO TWO CHOSEN.\n\n");
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement