Advertisement
35657

Untitled

Mar 9th, 2024
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.63 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. struct date {
  7.     int day_;
  8.     int month_;
  9.     int year_;
  10. };
  11.  
  12. class person {
  13.  
  14. public:
  15.     person(const int id, const char* last_name, const char* first_name, const char* middle_name, const date birthday) : id_(id), last_name_(new char[strlen(last_name) + 1]), first_name_(new char[strlen(first_name) + 1]), middle_name_(new char[strlen(middle_name) + 1]), birthday_(birthday) {
  16.         strcpy(last_name_, last_name);
  17.         strcpy(first_name_, first_name);
  18.         strcpy(middle_name_, middle_name);
  19.         number_person_++;
  20.     }
  21.  
  22.     person() : person(111111, "Ivanov", "Ivan", "Ivanovich", { 1,1,1993 }) {}
  23.  
  24.     person(const person& other) : person(other.id_, other.last_name_, other.first_name_, other.middle_name_, other.birthday_) {}
  25.  
  26.     person& operator=(const person& other) {
  27.         if (this != &other) {
  28.             id_ = other.id_;
  29.             birthday_ = other.birthday_;
  30.             delete[] last_name_;
  31.             last_name_ = new char[strlen(other.last_name_) + 1];
  32.             strcpy(last_name_, other.last_name_);
  33.             delete[] first_name_;
  34.             first_name_ = new char[strlen(other.first_name_) + 1];
  35.             strcpy(first_name_, other.first_name_);
  36.             delete[] middle_name_;
  37.             middle_name_ = new char[strlen(other.middle_name_) + 1];
  38.             strcpy(middle_name_, other.middle_name_);
  39.         }
  40.         return *this;
  41.     }
  42.  
  43.     void print() {
  44.         cout << last_name_ << " " << first_name_ << " " << middle_name_ << ", " << birthday_.day_ << '.' << birthday_.month_ << '.' << birthday_.year_ << ", id - " << id_ << endl;
  45.     }
  46.  
  47.     static int get_number_person() {
  48.         return number_person_;
  49.     }
  50.  
  51.     ~person() {
  52.         delete[] last_name_;
  53.         delete[] first_name_;
  54.         delete[] middle_name_;
  55.         number_person_--;
  56.     }
  57.  
  58.     void set_id(const int id) {
  59.         id_ = id;
  60.     }
  61.  
  62.  
  63.     void set_last_name(const char* last_name) {
  64.         delete[] last_name_;
  65.         last_name_ = new char[strlen(last_name) + 1];
  66.         strcpy(last_name_, last_name);
  67.     }
  68.  
  69.     void set_first_name(const char* first_name) {
  70.         delete[] first_name_;
  71.         first_name_ = new char[strlen(first_name) + 1];
  72.         strcpy(first_name_, first_name);
  73.     }
  74.  
  75.     void set_middle_name(const char* middle_name) {
  76.         delete[] middle_name_;
  77.         middle_name_ = new char[strlen(middle_name) + 1];
  78.         strcpy(middle_name_, middle_name);
  79.     }
  80.  
  81.     void set_birthday(const int day, const int month, const int year) {
  82.         birthday_.day_ = day;
  83.         birthday_.month_ = month;
  84.         birthday_.year_ = year;
  85.     }
  86.  
  87.     int get_id() const {
  88.         return id_;
  89.     }
  90.  
  91. private:
  92.     int id_; // идентификационный номер
  93.     char* last_name_; // фамилия
  94.     char* first_name_; // имя
  95.     char* middle_name_; // отчество
  96.     date birthday_; // дата рождения
  97.     static int number_person_;
  98. };
  99.  
  100. class flat {
  101.  
  102. public:
  103.  
  104.     flat() : residents_count_(0), max_count_(5), residents_(new person[max_count_]) {}
  105.  
  106.     flat(const flat& other) : residents_count_(other.residents_count_), max_count_(other.max_count_), residents_(new person[max_count_]) {
  107.         for (int i = 0; i < residents_count_; i++) {
  108.             residents_[i] = other.residents_[i];
  109.         }
  110.     }
  111.  
  112.     void add_resident(const person& new_resident) {
  113.         if (residents_count_ == max_count_) {
  114.             person* temp = new person[max_count_ * 2];
  115.             for (int i = 0; i < residents_count_; i++) {
  116.                 temp[i] = residents_[i];
  117.             }
  118.             delete[] residents_;
  119.             residents_ = temp;
  120.             max_count_ *= 2;
  121.         }
  122.         residents_[residents_count_] = new_resident;
  123.         residents_count_++;
  124.     }
  125.  
  126.     void del_resident(const int id) {
  127.         for (int i = 0; i < residents_count_; i++) {
  128.             if (residents_[i].get_id() == id) {
  129.                 for (int j = i; j < residents_count_ - 1; j++) {
  130.                     residents_[j] = residents_[j + 1];
  131.                 }
  132.                 residents_count_--;
  133.             }
  134.         }
  135.     }
  136.  
  137.     void show_residents_() const {
  138.         for (int i = 0; i < residents_count_; i++) {
  139.             residents_[i].print();
  140.         }
  141.     }
  142.  
  143.     friend class apartment_house;
  144.  
  145.     ~flat() {
  146.         delete[] residents_;
  147.     }
  148.  
  149. private:
  150.     int residents_count_;
  151.     int max_count_;
  152.     person* residents_;
  153. };
  154.  
  155. class apartment_house {
  156.  
  157. public:
  158.  
  159.     apartment_house(const int apartment_count) : total_residents_count_(0), apartment_count_(apartment_count), apartment_(new flat[apartment_count]) {}
  160.  
  161.     void registration(const int flat_number, const person& new_resident) {
  162.         apartment_[flat_number - 1].add_resident(new_resident);
  163.         total_residents_count_++;
  164.     }
  165.    
  166.     void unregistration(const int flat_number, const int id) {
  167.         apartment_[flat_number - 1].del_resident(id);
  168.         total_residents_count_--;
  169.     }
  170.  
  171.     void find_resident_id(const int id) {
  172.         for (int i = 0; i < apartment_count_; i++) {
  173.             for (int j = 0; j < apartment_[i].residents_count_; j++) {
  174.                 if (apartment_[i].residents_[j].get_id() == id) {
  175.                     apartment_[i].residents_[j].print();
  176.                     return;
  177.                 }
  178.             }
  179.         }
  180.         cout << "Жильца с таким id нет" << endl;
  181.     }
  182.  
  183.     int get_total_residents_count() const {
  184.         return total_residents_count_;
  185.     }
  186.  
  187.     void show_all_residents() const {
  188.         for (int i = 0; i < apartment_count_; i++) {
  189.             apartment_[i].show_residents_();
  190.         }
  191.     }
  192.  
  193.     ~apartment_house() {
  194.         delete[] apartment_;
  195.     }
  196.  
  197. private:
  198.     int total_residents_count_;
  199.     const int apartment_count_;
  200.     flat* apartment_;
  201. };
  202.  
  203. int person::number_person_ = 0;
  204.  
  205. int main() {
  206.  
  207.     setlocale(LC_ALL, "ru");
  208.  
  209.     person p1(111111, "Ivanov", "Ivan", "Ivanovich", { 13,7,1986 });
  210.     person p2(222222, "Petrov", "Petr", "Petrovich", { 23,8,1997 });
  211.     person p3(333333, "Sidorov", "Petr", "Petrovich", { 12,9,2001 });
  212.    
  213.     apartment_house my_house(200);
  214.  
  215.     my_house.registration(1, p1);
  216.     my_house.registration(2, p2);
  217.     my_house.registration(3, p3);
  218.  
  219.     my_house.find_resident_id(111111);
  220.     my_house.find_resident_id(555555);
  221.  
  222.     cout << my_house.get_total_residents_count() << endl;
  223.  
  224.     my_house.show_all_residents();
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement