当前位置:   article > 正文

C++ Primer Pluse(第6版)中文版 第12章编程练习第5题_heather银行进行的研究表明,atm客户不希望排队时间不超过 1分钟。使用程序清单12.

heather银行进行的研究表明,atm客户不希望排队时间不超过 1分钟。使用程序清单12.

题目:

     Heather银行进行的研究表明,ATM客户不希望排队时间不超过1分钟。请使用书中程序清单12.10中的模拟,找出要使平均等候时间为1分钟,每小时到达的客户数应为多少(试验时间不短于100小时)?


思路:

     通过网络搜索,分析网友的回答,解决这个问题有两种思路:

     一、直接使用书中的例题源代码12.10、12.11和12.12,通过输入不同的参数,人工找出运行结果里平均等候时间为1分种的客户数。

     (参考:1、http://zhidao.baidu.com/link?url=2GVmQkvQYIStDYIUDuxvCh_lib0CLHBjEwseIOPKkuIp-V6oLsrwtTyOmY5XKlsE8OaYAmLMshyDeANPXzw8ba  2、http://blog.csdn.net/qq844352155/article/details/24327151

     二、问题的关键在于对“平均等候时间为1分钟”条件的设定。简单修改一下12.12中的部分代码,通过设定条件,自动获取平均等候时间为1分钟的客户数(参考:http://tieba.baidu.com/p/1826136344)。我理解,不太可能完全获得平均值时间正好为1分钟的值,只能是非常接近。应用常用的知识点,使一个浮点数无限接近于某一个值(本题中是1.0)的办法,程序中使用了fabs((double)line_wait / served-1)<0.1 这个条件表达式。在测试过程中发现,设置的精度不能太高,否则得到的结果是每小时到达的客户接近或者60,那么客户到达的平均时间间隔少于1分钟,根据书中所述,此时是无效的。现将12.12的修改代码附上,结合书中的12.10、12.11即可运行。


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


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/142752
推荐阅读
相关标签
  

闽ICP备14008679号