Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.32 KB | None | 0 0
  1. /* * * *
  2.  * Напишите обьевление класса для связного списка класса узлов обьектов типа
  3.  * ( Worker , *** Pencil , Cat ... ) и его реализацию, включив у нее такие методы :
  4.  * Insert () , Delete () , Display(), Find() и оператор []. протестировать в главной программе
  5.  *
  6.  * * * * */
  7. #include<iostream>
  8. #include<cstdlib>
  9. #include<string>
  10.  
  11. using namespace std;
  12. class Employee;
  13. class EmployeeList;
  14.  
  15. struct Node
  16. {
  17.     Node * next;
  18.     Employee * employee;
  19. };
  20.  
  21. class Employee
  22. {  
  23.     private:
  24.         string name;
  25.         int age;
  26.         int days_of_employment;
  27.         int salary;
  28.  
  29.     public:
  30.         // Setters and Getters
  31.         void set_name(string name) { this->name = name; }
  32.         string get_name() { return this->name; }
  33.         void set_age(int age) { this->age = age; }
  34.         int get_age() { return this->age; }
  35.         void set_days_of_employment(int days) { this->days_of_employment = days; }
  36.         int get_days_of_employment() { return this->days_of_employment; }
  37.         void set_salary(int salary) { this->salary = salary; }
  38.         int get_salary() { return this->salary; }
  39.  
  40.         // Output methods
  41.         string description()
  42.         {
  43.             return "Employee: " + this->name + "\n" +
  44.                    "     age: " + to_string(this->age) + "\n" +
  45.                    "    days: " + to_string(this->days_of_employment) + "\n" +
  46.                    "  salary: " + to_string(this->salary) +"\n\n";
  47.         }
  48.  
  49.         void print_description() { std::cout << description(); }
  50.  
  51.         // Constructor
  52.         Employee( string name, int age = 0, int days_of_employment = 0, int salary = 0 )
  53.         {
  54.             this->name = name;
  55.             this->age = age;
  56.             this->days_of_employment = days_of_employment;
  57.             this->salary = salary;
  58.         }
  59. };
  60.  
  61. class EmployeeList
  62. /* Employee linked List */
  63. {
  64.     public:
  65.         int length;
  66.         Node * head;
  67.  
  68.         EmployeeList();
  69.         ~EmployeeList();
  70.         Employee * operator [](unsigned int i)
  71.         {
  72.             // As of now return the first employee
  73.             return head->employee;
  74.         }
  75.  
  76.         void add_employee( Employee *employee );
  77.         void delete_employee( Employee *employee);
  78.         void print();
  79. };
  80.  
  81. EmployeeList::EmployeeList()
  82. {
  83.     this->length = 0;
  84.     this->head = nullptr;
  85. }
  86.  
  87. EmployeeList::~EmployeeList() { }
  88.  
  89. void EmployeeList::add_employee( Employee * employee)
  90. {
  91.     Node * node = new Node();
  92.     node->employee = employee;
  93.     node->next = this->head;
  94.     this->head = node;
  95.     this->length++;
  96. }
  97.  
  98. void EmployeeList::delete_employee(Employee * employee)
  99. {
  100.     // 1. find employee index
  101.     Node * head = this->head;
  102.  
  103.  
  104.     /*
  105.     for( Node *iterator = head; iterator; iterator = iterator->next)
  106.     {
  107.         std::cout << iterator->employee->description();
  108.         //std::cout << "Employee address: " << employee-> << std::endl;
  109.         //std::cout << "Iterator address: " << iterator->employee << std::endl;
  110.         std::cout << "Employee address: " << iterator->employee << std::endl;
  111.         std::cout << "Iterator next: " << iterator->next << std::endl;
  112.         if( iterator->next == nullptr )
  113.         {
  114.             std::cout << "THIS IS THE END!!!" << std::endl;
  115.  
  116.             }
  117.     }
  118.     // 2. check if it is the first one
  119.     */
  120.    
  121. }
  122.  
  123. void EmployeeList::print()
  124. {
  125.     Node * head = this->head;
  126.     int i = 1;
  127.     while(head)
  128.     {
  129.         cout << head->employee->description();
  130.         head = head->next;
  131.         i++;
  132.     }
  133. }
  134. int main(int argc, char const *argv[])
  135. {
  136.     Employee    employee1 = Employee("Anatoly Shirokov", 31, 365, 280000),
  137.                 employee2 = Employee( "DarkPerl", 32, 780, 180000),
  138.                 employee3 = Employee("Illia Zviagin", 35, 180, 220000),
  139.                 employee4 = Employee("Alex", 18, 12, 6000);
  140.  
  141.     EmployeeList * list = new EmployeeList();
  142.    
  143.     list->add_employee( &employee1);
  144.     list->add_employee( &employee4);
  145.     list->add_employee( &employee2);
  146.     list->add_employee( &employee3);
  147.  
  148.     list->delete_employee( &employee4);
  149.     //list->print();
  150.     std::cout << (&list[0])->description;
  151.  
  152.     delete list;
  153.    
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement