Advertisement
NickAndNick

Классификация растений

Mar 27th, 2024 (edited)
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | Software | 0 0
  1. // https://otvet.mail.ru/answer/2051859985
  2. #include <array>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <set>
  6. #include <span>
  7. #include <string>
  8. #include <string_view>
  9.  
  10. using namespace std;
  11.  
  12. class Plant {
  13. public:
  14.     Plant() : quantity(0) {}
  15.  
  16.     bool is_status(const string_view status) const {
  17.         return this->status == status;
  18.     }
  19.  
  20.     bool is_least(const Plant& plant) const {
  21.         return quantity < plant.quantity;;
  22.     }
  23.  
  24.     bool is_family(const string_view family) const {
  25.         return this->family == family;
  26.     }
  27.  
  28.     const string& get_genus() const {
  29.         return genus;
  30.     }
  31. private:
  32.     string genus;
  33.     string family;
  34.     string order;
  35.     string status;
  36.     int quantity;
  37.  
  38.     friend ostream& operator<<(ostream& out, const Plant& plant) {
  39.         return out << "Порядок: " << plant.order << '\n'
  40.             << "Семейство: " << plant.family << '\n'
  41.             << "Род: " << plant.genus << '\n'
  42.             << "Видовое разнообразие: " << plant.quantity << '\n'
  43.             << "Охранный статус: " << plant.status << '\n';
  44.     }
  45.  
  46.     friend istream& operator>>(istream& inp, Plant& plant) {
  47.         cout << "Порядок: ";
  48.         getline(inp, plant.order);
  49.         cout << "Семейство: ";
  50.         getline(inp, plant.family);
  51.         cout << "Род: ";
  52.         getline(inp, plant.genus);
  53.         cout << "Видовое разнообразие: ";
  54.         inp >> plant.quantity;
  55.         inp.ignore(0x1000, '\n');
  56.         cout << "Охранный статус: ";
  57.         getline(inp, plant.status);
  58.         cout.put('\n');
  59.         return inp;
  60.     }
  61. };
  62.  
  63. using plants_t = array<Plant, 10>;
  64.  
  65. void status_show(const plants_t& plants, const string_view status) {
  66.     for (const auto& plant : plants) {
  67.         if (plant.is_status(status)) {
  68.             cout << plant << '\n';
  69.         }
  70.     }
  71. }
  72.  
  73. void genus_least(const plants_t& plants) {
  74.     Plant plant = plants.front();
  75.     for (size_t i = 1; i < plants.size(); ++i) {
  76.         if (plants[i].is_least(plant)) {
  77.             plant = plants[i];
  78.         }
  79.     }
  80.     cout << "Род растений с наименьшим видовым разнообразием: "
  81.          << plant.get_genus() << "\n\n";
  82. }
  83.  
  84. void show_list_genus_from_family(const plants_t& plants, const string_view family) {
  85.     set<string> genera;
  86.     for (const auto& plant : plants) {
  87.         if (plant.is_family(family)) {
  88.             genera.insert(plant.get_genus());
  89.         }
  90.     }
  91.     auto n = 0;
  92.     for (const auto& genus : genera) {
  93.         cout << ++n << ". " << genus << '\n';
  94.     }
  95.     cout.put('\n');
  96. }
  97.  
  98. int choice() {
  99.     puts("Выбирите действие...");
  100.     puts("0. Выход из программы");
  101.     puts("1. Вывести список растений заданного охранного статуса");
  102.     puts("2. Вывести род растений с наименьшим видовым разнообразием");
  103.     puts("3. Вывести список родов растений заданного семейства");
  104.     cout << ">>> ";
  105.     int value;
  106.     cin >> value;
  107.     cin.ignore(0x1000, '\n');
  108.     cout.put('\n');
  109.     return value;
  110. }
  111.  
  112. string query(const char* prompt) {
  113.     string value{ "\n" };
  114.     while (value.front() == '\n') {
  115.         cout << prompt;
  116.         getline(cin, value);
  117.     }
  118.     cout.put('\n');
  119.     return value;
  120. }
  121.  
  122. const auto load() {
  123.     plants_t plants{};
  124.     puts("Введите данные...\n");
  125.     for (auto& plant : plants) {
  126.         cin >> plant;
  127.     }
  128.     return plants;
  129. }
  130.  
  131. int main() {
  132.     system("chcp 1251 > nul");
  133.     const auto plants = load();
  134.     auto run = true;
  135.     while (run) {
  136.         switch (choice()) {
  137.             case 0:
  138.                 run = false;
  139.                 break;
  140.             case 1:
  141.                 status_show(plants, query("Введите охранный статус: "));
  142.                 break;
  143.             case 2:
  144.                 genus_least(plants);
  145.                 break;
  146.             case 3:
  147.                 show_list_genus_from_family(plants, query("Введите семейство: "));
  148.                 break;
  149.             default:
  150.                 puts("Ошибка при выборе\n");
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement