Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct date {
- int day_;
- int month_;
- int year_;
- };
- class person {
- public:
- 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) {
- strcpy(last_name_, last_name);
- strcpy(first_name_, first_name);
- strcpy(middle_name_, middle_name);
- number_person_++;
- }
- person() : person(111111, "Ivanov", "Ivan", "Ivanovich", { 1,1,1993 }) {}
- person(const person& other) : person(other.id_, other.last_name_, other.first_name_, other.middle_name_, other.birthday_) {}
- person& operator=(const person& other) {
- if (this != &other) {
- id_ = other.id_;
- birthday_ = other.birthday_;
- delete[] last_name_;
- last_name_ = new char[strlen(other.last_name_) + 1];
- strcpy(last_name_, other.last_name_);
- delete[] first_name_;
- first_name_ = new char[strlen(other.first_name_) + 1];
- strcpy(first_name_, other.first_name_);
- delete[] middle_name_;
- middle_name_ = new char[strlen(other.middle_name_) + 1];
- strcpy(middle_name_, other.middle_name_);
- }
- return *this;
- }
- void print() {
- cout << last_name_ << " " << first_name_ << " " << middle_name_ << ", " << birthday_.day_ << '.' << birthday_.month_ << '.' << birthday_.year_ << ", id - " << id_ << endl;
- }
- static int get_number_person() {
- return number_person_;
- }
- ~person() {
- delete[] last_name_;
- delete[] first_name_;
- delete[] middle_name_;
- number_person_--;
- }
- void set_id(const int id) {
- id_ = id;
- }
- void set_last_name(const char* last_name) {
- delete[] last_name_;
- last_name_ = new char[strlen(last_name) + 1];
- strcpy(last_name_, last_name);
- }
- void set_first_name(const char* first_name) {
- delete[] first_name_;
- first_name_ = new char[strlen(first_name) + 1];
- strcpy(first_name_, first_name);
- }
- void set_middle_name(const char* middle_name) {
- delete[] middle_name_;
- middle_name_ = new char[strlen(middle_name) + 1];
- strcpy(middle_name_, middle_name);
- }
- void set_birthday(const int day, const int month, const int year) {
- birthday_.day_ = day;
- birthday_.month_ = month;
- birthday_.year_ = year;
- }
- int get_id() const {
- return id_;
- }
- private:
- int id_; // идентификационный номер
- char* last_name_; // фамилия
- char* first_name_; // имя
- char* middle_name_; // отчество
- date birthday_; // дата рождения
- static int number_person_;
- };
- class flat {
- public:
- flat() : residents_count_(0), max_count_(5), residents_(new person[max_count_]) {}
- flat(const flat& other) : residents_count_(other.residents_count_), max_count_(other.max_count_), residents_(new person[max_count_]) {
- for (int i = 0; i < residents_count_; i++) {
- residents_[i] = other.residents_[i];
- }
- }
- void add_resident(const person& new_resident) {
- if (residents_count_ == max_count_) {
- person* temp = new person[max_count_ * 2];
- for (int i = 0; i < residents_count_; i++) {
- temp[i] = residents_[i];
- }
- delete[] residents_;
- residents_ = temp;
- max_count_ *= 2;
- }
- residents_[residents_count_] = new_resident;
- residents_count_++;
- }
- void del_resident(const int id) {
- for (int i = 0; i < residents_count_; i++) {
- if (residents_[i].get_id() == id) {
- for (int j = i; j < residents_count_ - 1; j++) {
- residents_[j] = residents_[j + 1];
- }
- residents_count_--;
- }
- }
- }
- void show_residents_() const {
- for (int i = 0; i < residents_count_; i++) {
- residents_[i].print();
- }
- }
- friend class apartment_house;
- ~flat() {
- delete[] residents_;
- }
- private:
- int residents_count_;
- int max_count_;
- person* residents_;
- };
- class apartment_house {
- public:
- apartment_house(const int apartment_count) : total_residents_count_(0), apartment_count_(apartment_count), apartment_(new flat[apartment_count]) {}
- void registration(const int flat_number, const person& new_resident) {
- apartment_[flat_number - 1].add_resident(new_resident);
- total_residents_count_++;
- }
- void unregistration(const int flat_number, const int id) {
- apartment_[flat_number - 1].del_resident(id);
- total_residents_count_--;
- }
- void find_resident_id(const int id) {
- for (int i = 0; i < apartment_count_; i++) {
- for (int j = 0; j < apartment_[i].residents_count_; j++) {
- if (apartment_[i].residents_[j].get_id() == id) {
- apartment_[i].residents_[j].print();
- return;
- }
- }
- }
- cout << "Жильца с таким id нет" << endl;
- }
- int get_total_residents_count() const {
- return total_residents_count_;
- }
- void show_all_residents() const {
- for (int i = 0; i < apartment_count_; i++) {
- apartment_[i].show_residents_();
- }
- }
- ~apartment_house() {
- delete[] apartment_;
- }
- private:
- int total_residents_count_;
- const int apartment_count_;
- flat* apartment_;
- };
- int person::number_person_ = 0;
- int main() {
- setlocale(LC_ALL, "ru");
- person p1(111111, "Ivanov", "Ivan", "Ivanovich", { 13,7,1986 });
- person p2(222222, "Petrov", "Petr", "Petrovich", { 23,8,1997 });
- person p3(333333, "Sidorov", "Petr", "Petrovich", { 12,9,2001 });
- apartment_house my_house(200);
- my_house.registration(1, p1);
- my_house.registration(2, p2);
- my_house.registration(3, p3);
- my_house.find_resident_id(111111);
- my_house.find_resident_id(555555);
- cout << my_house.get_total_residents_count() << endl;
- my_house.show_all_residents();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement