Advertisement
Andrey_ZoZ

Untitled

Jul 25th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. class Person
  4. {
  5. private:
  6. static int countPersons;
  7. int idPerson;
  8. int priorityPerson;
  9. public:
  10. Person(int priorityPerson=3) :idPerson{countPersons++} {
  11. if (priorityPerson < 1 or priorityPerson>3) { this->priorityPerson = 3; }
  12. else { this->priorityPerson = priorityPerson; }
  13. }
  14. static int getCountPersons() { return countPersons; }
  15. int getIdPerson(){return idPerson; }
  16. int getPriorityPerson(){return priorityPerson;}
  17. void minucСountPersons()
  18. {
  19. --countPersons;
  20. }
  21. void plusCountPersons()
  22. {
  23. ++countPersons;
  24. }
  25. void changePriority(int priority)
  26. {
  27. priorityPerson=priority;
  28. }
  29. };
  30. int Person::countPersons{ 0 };
  31.  
  32. class TableOut
  33. {
  34. private:
  35. Person* persons;
  36. public:
  37. TableOut()=default;
  38.  
  39. void addPerson(Person& object)
  40. {
  41. Person* temp=persons;
  42. temp[0].plusCountPersons();
  43. persons=new Person[temp[0].getCountPersons()];
  44. for (int index{0}; index<temp[0].getCountPersons()-1; ++index) {
  45. persons[index]=temp[index];
  46. }
  47. persons[temp[0].getCountPersons()-1]=object;
  48. }
  49. void showStat()
  50. {
  51. for (int index{0}; index<persons[0].getCountPersons(); ++index) {
  52. std::cout<<"Person ID "<<persons[index].getIdPerson()<<". Go out "<<index<<"\n";
  53. }
  54. }
  55. };
  56.  
  57. class Printer
  58. {
  59. private:
  60. Person* persons;
  61. TableOut object;
  62. int front{0};
  63. int back;
  64. public:
  65. //Construct
  66. Printer(Person* persons) :persons{ persons }, front{ 0 }, back{persons[0].getCountPersons()-1} {sortPersons(); }
  67.  
  68. //Destruct
  69. ~Printer() { delete[] persons; front = 0, back = 0; }
  70.  
  71. //Methods
  72. void sortPersons();
  73. void getFront();
  74. void putBack(int priorityPerson);
  75. void putBack(const Person* object);
  76. void showStat()
  77. {
  78. object.showStat();
  79. }
  80.  
  81. //Operators
  82. //NONE
  83. };
  84.  
  85. int main()
  86. {
  87. Person* persons=new Person[6]{1,3,1,2,3,3};
  88. Printer printer{persons};
  89. printer.getFront();
  90. printer.showStat();
  91. printer.putBack(2);
  92. printer.getFront();
  93. printer.showStat();
  94. }
  95.  
  96. void Printer::sortPersons()
  97. {
  98. for (int index{0}; persons[0].getCountPersons()-1; ++index) {
  99. int indexForCycleWhile=index+1;
  100. while(persons[indexForCycleWhile-1].getPriorityPerson()>persons[indexForCycleWhile].getPriorityPerson())
  101. {
  102. std::swap(persons[index],persons[index+1]);
  103. }
  104. }
  105.  
  106. }
  107.  
  108. void Printer::getFront()
  109. {
  110. if(front!=back){
  111. Person* temp=persons;
  112. temp[0].minucСountPersons();
  113. persons=new Person[temp[0].getCountPersons()];
  114. for (int index{0}; index<temp[0].getCountPersons(); ++index) {
  115. persons[index]=temp[index+1];
  116. }
  117. object.addPerson(temp[0]);
  118. }
  119. }
  120.  
  121. void Printer::putBack(int priorityPerson)
  122. {
  123. Person* temp=persons;
  124. temp[0].plusCountPersons();
  125. persons=new Person[temp[0].getCountPersons()];
  126. for (int index{0}; index<temp[0].getCountPersons()-1; ++index)
  127. {
  128. persons[index]=temp[index];
  129. }
  130. persons[temp[0].getCountPersons()-1].changePriority(priorityPerson);
  131. sortPersons();
  132. ++back;
  133. }
  134.  
  135. void Printer::putBack(const Person* object)
  136. {
  137. Person* temp=persons;
  138. persons=new Person[temp[0].getCountPersons()+object[0].getCountPersons()];
  139. int index{0};
  140. for (; index<temp[0].getCountPersons(); ++index) {
  141. persons[index]=temp[index];
  142. }
  143. for (int indexNull{0}; index<temp[0].getCountPersons()+object[0].getCountPersons(); ++index,++indexNull) {
  144. persons[index]=object[indexNull];
  145. ++back;
  146. }
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement