Advertisement
Andrey_ZoZ

Error...

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