Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://otvet.mail.ru/answer/2051859985
- #include <array>
- #include <cstdlib>
- #include <iostream>
- #include <set>
- #include <span>
- #include <string>
- #include <string_view>
- using namespace std;
- class Plant {
- public:
- Plant() : quantity(0) {}
- bool is_status(const string_view status) const {
- return this->status == status;
- }
- bool is_least(const Plant& plant) const {
- return quantity < plant.quantity;;
- }
- bool is_family(const string_view family) const {
- return this->family == family;
- }
- const string& get_genus() const {
- return genus;
- }
- private:
- string genus;
- string family;
- string order;
- string status;
- int quantity;
- friend ostream& operator<<(ostream& out, const Plant& plant) {
- return out << "Порядок: " << plant.order << '\n'
- << "Семейство: " << plant.family << '\n'
- << "Род: " << plant.genus << '\n'
- << "Видовое разнообразие: " << plant.quantity << '\n'
- << "Охранный статус: " << plant.status << '\n';
- }
- friend istream& operator>>(istream& inp, Plant& plant) {
- cout << "Порядок: ";
- getline(inp, plant.order);
- cout << "Семейство: ";
- getline(inp, plant.family);
- cout << "Род: ";
- getline(inp, plant.genus);
- cout << "Видовое разнообразие: ";
- inp >> plant.quantity;
- inp.ignore(0x1000, '\n');
- cout << "Охранный статус: ";
- getline(inp, plant.status);
- cout.put('\n');
- return inp;
- }
- };
- using plants_t = array<Plant, 10>;
- void status_show(const plants_t& plants, const string_view status) {
- for (const auto& plant : plants) {
- if (plant.is_status(status)) {
- cout << plant << '\n';
- }
- }
- }
- void genus_least(const plants_t& plants) {
- Plant plant = plants.front();
- for (size_t i = 1; i < plants.size(); ++i) {
- if (plants[i].is_least(plant)) {
- plant = plants[i];
- }
- }
- cout << "Род растений с наименьшим видовым разнообразием: "
- << plant.get_genus() << "\n\n";
- }
- void show_list_genus_from_family(const plants_t& plants, const string_view family) {
- set<string> genera;
- for (const auto& plant : plants) {
- if (plant.is_family(family)) {
- genera.insert(plant.get_genus());
- }
- }
- auto n = 0;
- for (const auto& genus : genera) {
- cout << ++n << ". " << genus << '\n';
- }
- cout.put('\n');
- }
- int choice() {
- puts("Выбирите действие...");
- puts("0. Выход из программы");
- puts("1. Вывести список растений заданного охранного статуса");
- puts("2. Вывести род растений с наименьшим видовым разнообразием");
- puts("3. Вывести список родов растений заданного семейства");
- cout << ">>> ";
- int value;
- cin >> value;
- cin.ignore(0x1000, '\n');
- cout.put('\n');
- return value;
- }
- string query(const char* prompt) {
- string value{ "\n" };
- while (value.front() == '\n') {
- cout << prompt;
- getline(cin, value);
- }
- cout.put('\n');
- return value;
- }
- const auto load() {
- plants_t plants{};
- puts("Введите данные...\n");
- for (auto& plant : plants) {
- cin >> plant;
- }
- return plants;
- }
- int main() {
- system("chcp 1251 > nul");
- const auto plants = load();
- auto run = true;
- while (run) {
- switch (choice()) {
- case 0:
- run = false;
- break;
- case 1:
- status_show(plants, query("Введите охранный статус: "));
- break;
- case 2:
- genus_least(plants);
- break;
- case 3:
- show_list_genus_from_family(plants, query("Введите семейство: "));
- break;
- default:
- puts("Ошибка при выборе\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement