1
0

bank.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // bank.cpp -- using the Queue interface
  2. // compile with queue.cpp
  3. #include <iostream>
  4. #include <cstdlib> // for rand() and srand()
  5. #include <ctime> // for time()
  6. #include "queue.h"
  7. const int MIN_PER_HR = 60;
  8. bool newcustomer(double x); // is there a new customer?
  9. int main()
  10. {
  11. using std::cin;
  12. using std::cout;
  13. using std::endl;
  14. using std::ios_base;
  15. // setting things up
  16. std::srand(std::time(0)); // random initializing of rand()
  17. cout << "Case Study: Bank of Heather Automatic Teller\n";
  18. cout << "Enter maximum size of queue: ";
  19. int qs;
  20. cin >> qs;
  21. Queue line(qs); // line queue holds up to qs people
  22. cout << "Enter the number of simulation hours: ";
  23. int hours; // hours of simulation
  24. cin >> hours;
  25. // simulation will run 1 cycle per minute
  26. long cyclelimit = MIN_PER_HR * hours; // # of cycles
  27. cout << "Enter the average number of customers per hour: ";
  28. double perhour; // average # of arrival per hour
  29. cin >> perhour;
  30. double min_per_cust; // average time between arrivals
  31. min_per_cust = MIN_PER_HR / perhour;
  32. Item temp; // new customer data
  33. long turnaways = 0; // turned away by full queue
  34. long customers = 0; // joined the queue
  35. long served = 0; // served during the simulation
  36. long sum_line = 0; // cumulative line length
  37. int wait_time = 0; // time until autoteller is free
  38. long line_wait = 0; // cumulative time in line
  39. // running the simulation
  40. for (int cycle = 0; cycle < cyclelimit; cycle++)
  41. {
  42. if (newcustomer(min_per_cust)) // have newcomer
  43. {
  44. if (line.isfull())
  45. turnaways++;
  46. else
  47. {
  48. customers++;
  49. temp.set(cycle); // cycle = time of arrival
  50. line.enqueue(temp); // add newcomer to line
  51. }
  52. }
  53. if (wait_time <= 0 && !line.isempty())
  54. {
  55. line.dequeue (temp); // attend next customer
  56. wait_time = temp.ptime(); // for wait_time minutes
  57. line_wait += cycle - temp.when();
  58. served++;
  59. }
  60. if (wait_time > 0)
  61. wait_time--;
  62. sum_line += line.queuecount();
  63. }
  64. // reporting results
  65. if (customers > 0)
  66. {
  67. cout << "customers accepted: " << customers << endl;
  68. cout << " customers served: " << served << endl;
  69. cout << " turnaways: " << turnaways << endl;
  70. cout << "average queue size: ";
  71. cout.precision(2);
  72. cout.setf(ios_base::fixed, ios_base::floatfield);
  73. cout << (double) sum_line / cyclelimit << endl;
  74. cout << " average wait time: "
  75. << (double) line_wait / served << " minutes\n";
  76. }
  77. else
  78. cout << "No customers!\n";
  79. cout << "Done!\n";
  80. // cin.get();
  81. // cin.get();
  82. return 0;
  83. }
  84. // x = average time, in minutes, between customers
  85. // return value is true if customer shows up this minute
  86. bool newcustomer(double x)
  87. {
  88. return (std::rand() * x / RAND_MAX < 1);
  89. }