Advertisement
avr39ripe

cppPersonFlatHouseGittAndriyVersion

Jul 29th, 2021
1,555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.54 KB | None | 0 0
  1. 4. Создайте программу, имитирующую многоквартирный дом.
  2. Необходимо иметь классы Человек, Квартира, Дом.
  3. Класс Квартира содержит динамический массив объектов класса Человек.
  4. Класс Дом содержит массив объектов класса Квартира. Каждый из классов содержит
  5. переменные-члены и функции-члены, которые необходимы для предметной области класса.
  6. Обращаем ваше внимание, что память под строковые значения выделяется динамически.
  7. Не забывайте обеспечить классы различными конструкторами (конструктор
  8. копирования обязателен), деструкторами.
  9. Класс Человек следует использовать из Задания 2.
  10.  
  11. #include<iostream>
  12.  
  13. class Date
  14. {
  15.     uint16_t year;
  16.     uint8_t month;
  17.     uint8_t day;
  18.  
  19.     uint8_t checkMonth(uint8_t monthP)
  20.     {
  21.         if (monthP < 1) return 1;
  22.         if (monthP > 12) return 12;
  23.         return monthP;
  24.     }
  25.     uint8_t checkDay(uint8_t dayP, uint8_t monthP, uint16_t yearP)
  26.     {
  27.         const uint8_t maxDays{ Date::daysInMonth(monthP, yearP) };
  28.         if (dayP < 1) return 1;
  29.         if (dayP > maxDays) return maxDays;
  30.         return dayP;
  31.     }
  32. public:
  33.     static uint8_t daysInMonth(uint8_t month, uint16_t year)
  34.     {
  35.         return 30 + (((month < 8) and (month % 2 != 0)) or ((month >= 8) and (month % 2 == 0))) + ((month == 2) * (-2) + isLeapYear(year));
  36.     }
  37.  
  38.     static bool isLeapYear(uint16_t year)
  39.     {
  40.         return year % 400 == 0 or year % 4 == 0 and year % 100 != 0;
  41.     }
  42.     static const uint8_t maxMonth{ 12 };
  43.  
  44.     Date() : Date(1, 1, 1970) {}
  45.     Date(uint8_t dayP, uint8_t monthP, uint16_t yearP)
  46.         : year{ yearP }, month{ checkMonth(monthP) }, day{ checkDay(dayP,month,yearP) } {}
  47.  
  48.     Date& setDay(uint8_t dayP) { day = checkDay(dayP, month, year); return *this; }
  49.     Date& setMonth(uint8_t monthP) { month = checkMonth(monthP); day = checkDay(day, month, year); return *this; }
  50.     Date& setYear(uint16_t yearP) { year = yearP; return *this; }
  51.  
  52.     uint8_t getDay() const { return day; }
  53.     uint8_t getMonth() const { return month; }
  54.     uint16_t getYear() const { return year; }
  55.  
  56.     Date& init(uint8_t dayP, uint8_t monthP, uint16_t yearP);
  57.  
  58.     friend std::ostream& operator<<(std::ostream& out, const Date& object);
  59.     friend std::istream& operator>>(std::istream& in, Date& object);
  60.  
  61.     friend bool operator==(const Date& left, const Date& right);
  62.     friend bool operator!=(const Date& left, const Date& right);
  63. };
  64.  
  65. Date& Date::init(uint8_t dayP, uint8_t monthP, uint16_t yearP)
  66. {
  67.     setDay(dayP); setMonth(monthP); setYear(yearP); return *this;
  68. }
  69.  
  70. std::ostream& operator<<(std::ostream& out, const Date& object)
  71. {
  72.     out << (int)object.day << '.' << (int)object.month << '.' << object.year; return out;
  73. }
  74.  
  75. std::istream& operator>>(std::istream& in, Date& object)
  76. {
  77.     in >> object.day >> object.month >> object.year;
  78.     return in;
  79. }
  80.  
  81. bool operator==(const Date& left, const Date& right)
  82. {
  83.     return (left.day == right.day and left.month == right.month and left.year == right.year);
  84. }
  85.  
  86. bool operator!=(const Date& left, const Date& right)
  87. {
  88.     return !(left == right);
  89. }
  90.  
  91. class Man
  92. {
  93.     uint16_t id;
  94.     char* surname;
  95.     char* name;
  96.     char* secondName;
  97.     Date dateOfBirth;
  98. public:
  99.     Man() : id{ 0 }, surname{ nullptr }, name{ nullptr }, secondName{ nullptr }, dateOfBirth() {}
  100.     Man(uint16_t idP, const char* surnameP, const char* nameP, const char* secondNameP, uint8_t dayP, uint8_t monthP, uint16_t yearP);
  101.     Man(const Man& object);
  102.     Man(Man&& object);
  103.  
  104.     Man& setId(uint16_t idP) { id = idP; return *this; }
  105.  
  106.     Man& setSurname(const char* surnameP);
  107.     Man& setName(const char* nameP);
  108.     Man& setSecondName(const char* secondNameP);
  109.  
  110.     Man& setDateOfBirth(uint8_t dayP, uint8_t monthP, uint16_t yearP) { dateOfBirth.init(dayP, monthP, yearP); return *this; }
  111.  
  112.     Man& init(uint16_t idP, const char* surnameP, const char* nameP, const char* secondNameP, uint8_t dayP, uint8_t monthP, uint16_t yearP);
  113.  
  114.     const uint16_t getId()const { return id; }
  115.     const char* getSurname()const { return surname; }
  116.     const char* getName()const { return name; }
  117.     const char* getSecondName()const { return secondName; }
  118.     uint8_t getDateOfBirthDay()const { return dateOfBirth.getDay(); }
  119.     uint8_t getDateOfBirthMonth()const { return dateOfBirth.getMonth(); }
  120.     uint16_t getDateOfBirthYear()const { return dateOfBirth.getYear(); }
  121.  
  122.     Man& operator=(const Man& object);
  123.     Man& operator=(Man&& object);
  124.  
  125.     friend std::ostream& operator<<(std::ostream& out, const Man& object);
  126.     friend std::istream& operator>>(std::istream& out, Man& object);
  127.     friend bool operator==(const Man& left, const Man& right);
  128.     friend bool operator!=(const Man& left, const Man& right);
  129.  
  130.     ~Man() { delete[]surname; delete[]name; delete[]secondName; }
  131. };
  132.  
  133. Man::Man(uint16_t idP, const char* surnameP, const char* nameP, const char* secondNameP, uint8_t dayP, uint8_t monthP, uint16_t yearP) :
  134.     id{ idP }, surname{ new char[strlen(surnameP) + 1] }, name{ new char[strlen(nameP) + 1] }, secondName{ new char[strlen(secondNameP) + 1] }, dateOfBirth{ dayP,monthP,yearP }
  135. {
  136.     strcpy_s(surname, strlen(surnameP) + 1, surnameP);
  137.     strcpy_s(name, strlen(nameP) + 1, nameP);
  138.     strcpy_s(secondName, strlen(secondNameP) + 1, secondNameP);
  139. }
  140.  
  141. Man::Man(const Man& object) :
  142.     id{ object.id }, surname{ new char[strlen(object.surname) + 1] }, name{ new char[strlen(object.name) + 1] }, secondName{ new char[strlen(object.secondName) + 1] }, dateOfBirth{ object.dateOfBirth }
  143. {
  144.     strcpy_s(surname, strlen(object.surname) + 1, object.surname);
  145.     strcpy_s(name, strlen(object.name) + 1, object.name);
  146.     strcpy_s(secondName, strlen(object.secondName) + 1, object.secondName);
  147. }
  148.  
  149. Man::Man(Man&& object) :
  150.     id{ object.id }, surname{ object.surname }, name{ object.name }, secondName{ object.secondName }, dateOfBirth{ object.dateOfBirth }
  151. {
  152.     object.id = 0;
  153.     object.surname = nullptr;
  154.     object.name = nullptr;
  155.     object.secondName = nullptr;
  156.     object.dateOfBirth.init(0, 0, 0);
  157. }
  158.  
  159. Man& Man::setSurname(const char* surnameP)
  160. {
  161.     delete[] surname;
  162.     surname = new char[strlen(surnameP) + 1];
  163.     strcpy_s(surname, strlen(surnameP) + 1, surnameP);
  164.     return *this;
  165. }
  166.  
  167. Man& Man::setName(const char* nameP)
  168. {
  169.     delete[] name;
  170.     name = new char[strlen(nameP) + 1];
  171.     strcpy_s(name, strlen(nameP) + 1, nameP);
  172.     return *this;
  173. }
  174.  
  175. Man& Man::setSecondName(const char* secondNameP)
  176. {
  177.     delete[] secondName;
  178.     secondName = new char[strlen(secondNameP) + 1];
  179.     strcpy_s(secondName, strlen(secondNameP) + 1, secondNameP);
  180.     return *this;
  181. }
  182.  
  183. Man& Man::init(uint16_t idP, const char* surnameP, const char* nameP, const char* secondNameP, uint8_t dayP, uint8_t monthP, uint16_t yearP)
  184. {
  185.     id = idP;
  186.     setSurname(surnameP); setName(nameP); setSecondName(secondNameP);
  187.     dateOfBirth.init(dayP, monthP, yearP);
  188.     return *this;
  189. }
  190.  
  191. Man& Man::operator=(const Man& object)
  192. {
  193.     if (this == &object) { return *this; }
  194.  
  195.     delete[]surname;
  196.     delete[]name;
  197.     delete[]secondName;
  198.  
  199.     id = object.id;
  200.     surname = new char[strlen(object.surname) + 1];
  201.     name = new char[strlen(object.name) + 1];
  202.     secondName = new char[strlen(object.secondName) + 1];
  203.     dateOfBirth = object.dateOfBirth;
  204.  
  205.     strcpy_s(surname, strlen(object.surname) + 1, object.surname);
  206.     strcpy_s(name, strlen(object.name) + 1, object.name);
  207.     strcpy_s(secondName, strlen(object.secondName) + 1, object.secondName);
  208.  
  209.     return *this;
  210. }
  211.  
  212. Man& Man::operator=(Man&& object)
  213. {
  214.     if (this == &object) { return *this; }
  215.  
  216.     delete[]surname;
  217.     delete[]name;
  218.     delete[]secondName;
  219.  
  220.     id = object.id;
  221.     surname = object.surname;
  222.     name = object.name;
  223.     secondName = object.secondName;
  224.     dateOfBirth = object.dateOfBirth;
  225.  
  226.     object.id = 0;
  227.     object.surname = nullptr;
  228.     object.name = nullptr;
  229.     object.secondName = nullptr;
  230.     object.dateOfBirth.init(0, 0, 0);
  231.  
  232.     return *this;
  233. }
  234.  
  235. std::ostream& operator<<(std::ostream& out, const Man& object)
  236. {
  237.     if (object.id and object.surname and object.name and object.secondName)
  238.     {
  239.         out << '[' << "\033[1;93m" << "id: " << "\033[0m" << object.id << ' ' << object.surname
  240.             << ' ' << object.name << ' ' << object.secondName << ' ' << object.dateOfBirth << ']' << ' ';
  241.     }
  242.     else { out << "[empty person]"; }
  243.     return out;
  244. }
  245.  
  246. std::istream& operator>>(std::istream& in, Man& object)
  247. {
  248.     in >> object.id >> object.surname >> object.name >> object.secondName >> object.dateOfBirth;
  249.     return in;
  250. }
  251.  
  252. bool operator==(const Man& left, const Man& right)
  253. {
  254.     return (left.id == right.id and strcmp(left.surname, right.surname) == 0 and strcmp(left.name, right.name) == 0 and strcmp(left.secondName, right.secondName) == 0 and left.dateOfBirth == right.dateOfBirth);
  255. }
  256.  
  257. bool operator!=(const Man& left, const Man& right) { return !(left == right); }
  258.  
  259. class Flat
  260. {
  261.     uint32_t number;
  262.     uint16_t roomsCount;
  263.     Man* occupants;
  264.     uint16_t occupantsCount;
  265.     void _addOccupantPlace();
  266.     void _removeOccupantAt(const uint16_t idx);
  267. public:
  268.     Flat(const uint32_t numberP, const uint16_t roomsCountP) : number{ numberP }, roomsCount{ roomsCountP }, occupants{ nullptr }, occupantsCount{ 0 } {};
  269.     Flat() : Flat(0, 0) {};
  270.  
  271.     Flat(const Flat& object);
  272.     Flat(Flat&& object);
  273.  
  274.     Flat& operator=(const Flat& object);
  275.     Flat& operator=(Flat&& object);
  276.  
  277.     const uint32_t getNumber() const { return number; };
  278.     const uint16_t getRoomsCount() const { return roomsCount; };
  279.  
  280.     Flat& setNumber(const uint32_t numberP) { number = numberP; return *this; };
  281.     Flat& setRoomsCount(const uint16_t roomsCountP) { roomsCount = roomsCountP; return *this; };
  282.  
  283.     Flat& addOccupant(const Man& object);
  284.     Flat& addOccupant(Man&& object);
  285.     bool removeOccupant(const Man& object);
  286.     bool removeOccupantById(const uint16_t id);
  287.  
  288.     friend std::ostream& operator<<(std::ostream& out, const Flat& object);
  289.  
  290.     ~Flat() { delete[] occupants; }
  291. };
  292.  
  293. void Flat::_addOccupantPlace()
  294. {
  295.     Man* newOccupants{ new Man[occupantsCount + 1] };
  296.     for (uint16_t i{ 0 }; i < occupantsCount; ++i)
  297.     {
  298.         newOccupants[i] = std::move(occupants[i]);
  299.     }
  300.     delete[] occupants;
  301.     occupants = newOccupants;
  302.     ++occupantsCount;
  303. }
  304.  
  305. void Flat::_removeOccupantAt(const uint16_t idx)
  306. {
  307.     Man* newOccupants{ new Man[occupantsCount - 1] };
  308.     for (uint16_t i{ 0 }; i < occupantsCount - 1; ++i)
  309.     {
  310.         i < idx ? newOccupants[i] = std::move(occupants[i]) : newOccupants[i] = std::move(occupants[i + 1]);
  311.     }
  312.     delete[] occupants;
  313.     occupants = newOccupants;
  314.     --occupantsCount;
  315. }
  316.  
  317. Flat& Flat::addOccupant(const Man& object)
  318. {
  319.     _addOccupantPlace();
  320.     occupants[occupantsCount - 1] = object;
  321.     return *this;
  322. }
  323.  
  324. Flat& Flat::addOccupant(Man&& object)
  325. {
  326.     _addOccupantPlace();
  327.     occupants[occupantsCount - 1] = std::move(object);
  328.     return *this;
  329. }
  330.  
  331. bool Flat::removeOccupant(const Man& object)
  332. {
  333.     for (uint16_t i{ 0 }; i < occupantsCount; ++i)
  334.     {
  335.         if (occupants[i] == object)
  336.         {
  337.             _removeOccupantAt(i);
  338.             return true;
  339.         }
  340.     }
  341.     return false;
  342. }
  343.  
  344. bool Flat::removeOccupantById(const uint16_t id)
  345. {
  346.     for (uint16_t i{ 0 }; i < occupantsCount; ++i)
  347.     {
  348.         if (occupants[i].getId() == id)
  349.         {
  350.             _removeOccupantAt(i);
  351.             return true;
  352.         }
  353.     }
  354.     return false;
  355. }
  356.  
  357. Flat::Flat(const Flat& object) :
  358.     number{ object.number }, roomsCount{ object.roomsCount }, occupants{ new Man[object.occupantsCount]{} }, occupantsCount{ object.occupantsCount }
  359. {
  360.     for (int i{ 0 }; i < occupantsCount; ++i)
  361.     {
  362.         occupants[i] = object.occupants[i];
  363.     }
  364. }
  365.  
  366. Flat::Flat(Flat&& object) :
  367.     number{ object.number }, roomsCount{ object.roomsCount }, occupants{ object.occupants }, occupantsCount{ object.occupantsCount }
  368. {
  369.     object.number = 0;
  370.     object.roomsCount = 0;
  371.     object.occupants = nullptr;
  372.     object.occupantsCount = 0;
  373. }
  374.  
  375. Flat& Flat::operator=(const Flat& object)
  376. {
  377.     if (!(this == &object))
  378.     {
  379.         if (occupantsCount != object.occupantsCount)
  380.         {
  381.             delete[]occupants;
  382.             occupants = new Man[object.occupantsCount];
  383.         }
  384.         number = object.number;
  385.         roomsCount = object.roomsCount;
  386.         occupantsCount = object.occupantsCount;
  387.         for (int i{ 0 }; i < occupantsCount; ++i) { occupants[i] = object.occupants[i]; }  
  388.     }
  389.     return *this;
  390. }
  391.  
  392. Flat& Flat::operator=(Flat&& object)
  393. {
  394.     if (!(this == &object))
  395.     {
  396.         delete[]occupants;
  397.         number = object.number;
  398.         roomsCount = object.roomsCount;
  399.         occupants = object.occupants;
  400.         occupantsCount = object.occupantsCount;
  401.  
  402.         object.number = 0;
  403.         object.roomsCount = 0;
  404.         object.occupants = nullptr;
  405.         object.occupantsCount = 0;
  406.     }
  407.     return *this;
  408. }
  409.  
  410. std::ostream& operator<<(std::ostream& out, const Flat& object)
  411. {
  412.     if (object.number and object.roomsCount)
  413.     {
  414.         out << "\033[1;32m" << "Flat #" << object.number << "\033[0m" << ' ' << object.roomsCount << " rooms ";
  415.         if (object.occupantsCount)
  416.         {
  417.             for (uint16_t i{ 0 }; i < object.occupantsCount; ++i) { out << object.occupants[i]; }
  418.             std::cout << ' ';
  419.         }
  420.         else { out << "[no occupants]"; }
  421.     }
  422.     else { out << "[empty flat]"; }
  423.     return out;
  424. }
  425.  
  426. class Home
  427. {
  428.     uint16_t number;
  429.     uint16_t flatsCount;
  430.     Flat* flats;
  431.  
  432. public:
  433.     Home(const uint16_t numberP, const uint16_t flatsCountP) : number{ numberP }, flatsCount{ flatsCountP }, flats{ new Flat[flatsCountP] {} } {}
  434.     Home() : Home(1, 4) {}
  435.  
  436.     Home& setNumber(const uint16_t numberP) { number = numberP; return *this; }
  437.     Home& setFlatCount(const uint16_t flatCountP) { flatsCount = flatCountP; return *this; }
  438.  
  439.     const uint16_t getNumber()const { return number; }
  440.     const uint16_t getFlatCount()const { return flatsCount; }
  441.  
  442.     Home& addFlat(const Flat& object, uint16_t idx);
  443.     Home& addFlat(Flat&& object, uint16_t idx);
  444.     Home& removeFlat(uint16_t idx);
  445.  
  446.     friend std::ostream& operator<<(std::ostream& out, const Home& object);
  447.  
  448.     ~Home() { delete[]flats; }
  449. };
  450.  
  451. Home& Home::addFlat(const Flat& object, uint16_t idx)
  452. {
  453.     flats[idx] = object;
  454.     return *this;
  455. }
  456.  
  457. Home& Home::addFlat(Flat&& object, uint16_t idx)
  458. {
  459.     flats[idx] = std::move(object);
  460.     return *this;
  461. }
  462.  
  463. Home& Home::removeFlat(uint16_t idx)
  464. {
  465.     Flat emptyFlat;
  466.     flats[idx] = emptyFlat;
  467.     return *this;
  468. }
  469.  
  470. std::ostream& operator<<(std::ostream& out, const Home& object)
  471. {
  472.     if (object.number and object.flatsCount and object.flats)
  473.     {
  474.         out << "\033[1;34m" << "Home #" << object.number << "\033[0m" << ' ' << object.flatsCount << " flats:\n";
  475.         if (object.flats)
  476.         {
  477.             for (uint16_t i{ 0 }; i < object.flatsCount; ++i) { out << object.flats[i] << '\n'; }
  478.         }
  479.         else { out << "[empty flats]"; }
  480.     }
  481.     else { out << "[empty home]"; }
  482.     return out;
  483. }
  484.  
  485. int main()
  486. {
  487.     Man* kravchuki{ new Man[3] };
  488.     kravchuki[0].init(1100, "Kravchuk", "Mykola", "Mykolayovich", 14, 7, 1977);
  489.     kravchuki[1].init(1101, "Kravchuk", "Valentyna", "Ivanivna", 10, 4, 1978);
  490.     kravchuki[2].init(1102, "Kravchuk", "Ylia", "Mykolaivna", 6, 6, 2002);
  491.  
  492.     for (int i{ 0 }; i < 3; ++i) { std::cout << kravchuki[i] << '\n'; }
  493.     std::cout << '\n';
  494.    
  495.     Man* hapchuki{ new Man[3] };
  496.     hapchuki[0].init(3230, "Hapchuk", "Ostap", "Petrovich", 6, 5, 1978);
  497.     hapchuki[1].init(3231, "Hapchuk", "Kateryna", "Ivanivna", 4, 4, 1980);
  498.     hapchuki[2].init(3232, "Hapchuk", "Taras", "Ostapovich", 25, 6, 2016);
  499.  
  500.     Man person_1{ 5100,"Levchuk","Stepan","Vasyliovich",15,6,1981 };
  501.     Man person_2{ 5101,"Popova","Olena","Stepanivna",1,4,1984 };
  502.  
  503.     Man person_3{ person_1 };
  504.     Man person_4{ std::move(person_2) };
  505.  
  506.     std::cout << person_1 << '\n' << person_2 << '\n' << '\n' << person_3 << '\n' << person_4 << '\n' << '\n';
  507.  
  508.     Flat flat_1{ 1,2 };
  509.     Flat flat_2{ 2,1 };
  510.     Flat flat_3{ 3,2 };
  511.     Flat flat_4{ 4,1 };
  512.     std::cout << flat_1 << '\n' << flat_2 << '\n' << flat_3 << '\n' << flat_4 << "\n\n";
  513.     for (int i{ 0 }; i < 3; ++i) { flat_1.addOccupant(std::move(kravchuki[i])); }
  514.     flat_2.addOccupant(person_1);
  515.     flat_3.addOccupant(Man{ 1221, "Pinchuk", "Anatoliy", "Mykolayovich", 21, 9, 1978 }).addOccupant(Man{ 1222, "Pinchuk", "Halyna", "Stepanivna", 3, 2, 1981 });
  516.     flat_4.addOccupant(std::move(person_4));
  517.     Flat flat_5{ 1,3 };
  518.     for (int i{ 0 }; i < 3; ++i) { flat_5.addOccupant(std::move(hapchuki[i])); }
  519.  
  520.     std::cout << flat_1 << '\n' << flat_2 << '\n' << flat_3 << '\n' << flat_4 << '\n';
  521.     std::cout << '\n';
  522.     std::cout << person_1 << '\n' << person_4 << '\n' << '\n';
  523.  
  524.     Home home_1;
  525.     Home home_2{ 12,8 };
  526.     std::cout << home_1 << '\n' << home_2 << '\n';
  527.  
  528.     home_1.addFlat(flat_1, 0).addFlat(flat_2, 1).addFlat(flat_3, 2).addFlat(flat_4, 3);
  529.     std::cout << home_1 << '\n';
  530.     home_1.removeFlat(2);
  531.     home_2.addFlat(std::move(flat_5), 0).addFlat(std::move(flat_3), 2);
  532.     std::cout << home_1 << '\n' << home_2 << '\n';
  533.  
  534.     delete[]kravchuki;
  535.     delete[]hapchuki;
  536.  
  537.     return 0;
  538. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement