wheelsmanx

queue part 3

Dec 2nd, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include <queue>
  2. #include <vector>
  3. #include <string>
  4. #include <stdlib.h>
  5. #include <iostream>
  6. #include <iomanip>
  7. /* I was confused about the order in which the people are to arrive and how they are to arrive */
  8. using namespace std;
  9.  
  10. class person {
  11. public:
  12. int arivailTime = 1;
  13. int waitTime = 9;
  14. int timeLeft = 0;
  15. int realWaitTime = 0;
  16. int id;
  17. };
  18.  
  19. void header() {
  20. cout << setw(5) << "time";
  21. cout << setw(5) << "line";
  22. }
  23.  
  24. class Gline {
  25. public:
  26. int currentTime = 0;
  27. int maxTime = 120;
  28. queue<person> theLine;
  29. void run() {
  30. for (currentTime = 0; currentTime <= maxTime; currentTime++) {
  31. cout << setw(5) << "[" << setw(3) << currentTime << "]";
  32. cout << setw(2) << "[" << setw(3) << lengthLine() << "]";
  33. cout << setw(2) << "[" << setw(1) << arrive() << "]";
  34. cout << setw(2) << "[" << setw(1) << departure() << "]";
  35. cout << setw(3) << "[" << setw(3) << theLine.front().realWaitTime << "]";
  36. cout << setw(3) << "[" << setw(3) << theLine.front().waitTime + currentTime << "]";
  37. cout << endl;
  38. addWaitTime();
  39. }
  40. }
  41. void addWaitTime() {
  42. queue<person> tempQueue;
  43. queue<person> nextQueue;
  44. tempQueue = theLine;
  45. for (int i = 0; i < theLine.size(); i++) {
  46. // the person at the begining of line A add to their total wait time.
  47. tempQueue.front().realWaitTime = tempQueue.front().realWaitTime + 1;
  48. // add that first person in line A to the line B.
  49. nextQueue.push(tempQueue.front());
  50. // remove the first person in line A
  51. tempQueue.pop();
  52. }
  53. this->theLine = nextQueue;
  54. }
  55. bool departure() {
  56. bool returnObject = false;
  57. if (theLine.front().waitTime == 0) {
  58. returnObject = true;
  59. theLine.pop();
  60. theLine.front().waitTime = theLine.front().waitTime - 1;
  61. cout << "depart" << endl;
  62. }
  63. return returnObject;
  64. }
  65. bool arrive() {
  66. bool returnObject = false;
  67. if (theLine.front().arivailTime == 0) {
  68. person tempPerson;
  69. tempPerson.arivailTime = rand() % 4 + 1;
  70. tempPerson.waitTime = rand() % 4 + 1;
  71. tempPerson.realWaitTime = tempPerson.waitTime;
  72. theLine.push(tempPerson);
  73. theLine.front().arivailTime = theLine.front().arivailTime - 1;
  74. cout << "arrival" << endl;
  75. }
  76. return returnObject;
  77. }
  78. int lengthLine() {
  79. return theLine.size();
  80. }
  81. person nextPerson() {
  82. person returnObject;
  83. queue<person> tempQueue;
  84. tempQueue = theLine;
  85. tempQueue.pop();
  86. returnObject = tempQueue.front();
  87. cout << "nextPerson" << endl;
  88. return returnObject;
  89.  
  90. }
  91. Gline() {
  92. for (int i = 0; i < maxTime + 20; i++) {
  93. person tempPerson;
  94. tempPerson.id = i;
  95. tempPerson.waitTime = rand() % 4 + 1;
  96. tempPerson.arivailTime = rand() % 4 + 1;
  97. tempPerson.realWaitTime = tempPerson.waitTime;
  98. theLine.push(tempPerson);
  99. }
  100. }
  101. void showPeople() {
  102. for (int i = 0; i < theLine.size(); i++) {
  103. cout << theLine.front().id << " " << theLine.front().waitTime << " " << endl;
  104. theLine.pop();
  105. }
  106. }
  107. };
  108.  
  109. void main() {
  110. Gline main;
  111. main.run();
  112. system("pause");
  113. }
Advertisement
Add Comment
Please, Sign In to add comment