1
0

mall.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // mall.c -- use the Queue interface
  2. // compile with queue.c
  3. #include <stdio.h>
  4. #include <stdlib.h> // for rand() and srand()
  5. #include <time.h> // for time()
  6. #include "queue.h" // change Item typedef
  7. #define MIN_PER_HR 60.0
  8. bool newcustomer(double x); // is there a new customer?
  9. Item customertime(long when); // set customer parameters
  10. int main(void)
  11. {
  12. Queue line;
  13. Item temp; // new customer data
  14. int hours; // hours of simulation
  15. int perhour; // average # of arrivals per hour
  16. long cycle, cyclelimit; // loop counter, limit
  17. long turnaways = 0; // turned away by full queue
  18. long customers = 0; // joined the queue
  19. long served = 0; // served during the simulation
  20. long sum_line = 0; // cumulative line length
  21. int wait_time = 0; // time until Sigmund is free
  22. double min_per_cust; // average time between arrivals
  23. long line_wait = 0; // cumulative time in line
  24. InitializeQueue(&line);
  25. srand((unsigned int) time(0)); // random initializing of rand()
  26. puts("Case Study: Sigmund Lander's Advice Booth");
  27. puts("Enter the number of simulation hours:");
  28. scanf("%d", &hours);
  29. cyclelimit = MIN_PER_HR * hours;
  30. puts("Enter the average number of customers per hour:");
  31. scanf("%d", &perhour);
  32. min_per_cust = MIN_PER_HR / perhour;
  33. for (cycle = 0; cycle < cyclelimit; cycle++)
  34. {
  35. if (newcustomer(min_per_cust))
  36. {
  37. if (QueueIsFull(&line))
  38. turnaways++;
  39. else
  40. {
  41. customers++;
  42. temp = customertime(cycle);
  43. EnQueue(temp, &line);
  44. }
  45. }
  46. if (wait_time <= 0 && !QueueIsEmpty(&line))
  47. {
  48. DeQueue (&temp, &line);
  49. wait_time = temp.processtime;
  50. line_wait += cycle - temp.arrive;
  51. served++;
  52. }
  53. if (wait_time > 0)
  54. wait_time--;
  55. sum_line += QueueItemCount(&line);
  56. }
  57. if (customers > 0)
  58. {
  59. printf("customers accepted: %ld\n", customers);
  60. printf(" customers served: %ld\n", served);
  61. printf(" turnaways: %ld\n", turnaways);
  62. printf("average queue size: %.2f\n",
  63. (double) sum_line / cyclelimit);
  64. printf(" average wait time: %.2f minutes\n",
  65. (double) line_wait / served);
  66. }
  67. else
  68. puts("No customers!");
  69. EmptyTheQueue(&line);
  70. puts("Bye!");
  71. return 0;
  72. }
  73. // x = average time, in minutes, between customers
  74. // return value is true if customer shows up this minute
  75. bool newcustomer(double x)
  76. {
  77. if (rand() * x / RAND_MAX < 1)
  78. return true;
  79. else
  80. return false;
  81. }
  82. // when is the time at which the customer arrives
  83. // function returns an Item structure with the arrival time
  84. // set to when and the processing time set to a random value
  85. // in the range 1 - 3
  86. Item customertime(long when)
  87. {
  88. Item cust;
  89. cust.processtime = rand() % 3 + 1;
  90. cust.arrive = when;
  91. return cust;
  92. }