Advertisement
NickAndNick

Сортировка по полям структуры

Jun 24th, 2023 (edited)
996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.04 KB | Software | 0 0
  1. #include <algorithm>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <functional>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. template<typename T>
  11. T input(istream& inp, const char* message) {
  12.     cout << message;
  13.     T value{};
  14.     inp >> value;
  15.     inp.ignore(0x1000, '\n');
  16.     return value;
  17. }
  18.  
  19. template<>
  20. string input<string>(istream& inp, const char* message) {
  21.     cout << message;
  22.     string value;
  23.     getline(inp, value);
  24.     return value;
  25. }
  26.  
  27. struct Pot {
  28.     unsigned short diameter;
  29.     unsigned short quantity;
  30.     float volume;
  31.     string name;
  32.     string material;
  33.     string color;
  34.     Pot() : diameter(0), quantity(0), volume(0) {}
  35.     struct compare_by_diameter {
  36.         constexpr bool operator()(const Pot& a, const Pot& b)const {
  37.             return a.diameter < b.diameter;
  38.         }
  39.     };
  40.     struct compare_by_quantity {
  41.         constexpr bool operator()(const Pot& a, const Pot& b)const {
  42.             return a.quantity < b.quantity;
  43.         }
  44.     };
  45.     struct compare_by_volume {
  46.         constexpr bool operator()(const Pot& a, const Pot& b)const {
  47.             return a.volume < b.volume;
  48.         }
  49.     };
  50.     struct compare_by_name {
  51.         constexpr bool operator()(const Pot& a, const Pot& b)const {
  52.             return a.name < b.name;
  53.         }
  54.     };
  55.     struct compare_by_material {
  56.         constexpr bool operator()(const Pot& a, const Pot& b)const {
  57.             return a.material < b.material;
  58.         }
  59.     };
  60.     struct compare_by_color {
  61.         constexpr bool operator()(const Pot& a, const Pot& b)const {
  62.             return a.color < b.color;
  63.         }
  64.     };
  65. private:
  66.     friend istream& operator>>(istream& inp, Pot& pot) {
  67.         pot.volume = input<float>(inp, "Объём: ");
  68.         pot.material = input<string>(inp, "Материал: ");
  69.         pot.name = input<string>(inp, "Название: ");
  70.         pot.color = input<string>(inp, "Цвет: ");
  71.         pot.diameter = input<unsigned short>(inp, "Диаметр: ");
  72.         pot.quantity = input<unsigned short>(inp, "Количество: ");
  73.         return inp;
  74.     }
  75. };
  76.  
  77. struct Format {
  78.     streamsize num;
  79.     streamsize vol;
  80.     streamsize mat;
  81.     streamsize nam;
  82.     streamsize clr;
  83.     streamsize dmt;
  84.     streamsize qnt;
  85.     Format() : num(9), vol(11), mat(13), nam(17), clr(14), dmt(16), qnt(15) {}
  86.     void title()const {
  87.         cout << setw(num) << "Кастрюля"
  88.             << setw(vol) << "Объём"
  89.             << setw(mat) << "Материал"
  90.             << setw(nam) << "Название"
  91.             << setw(clr) << "Цвет"
  92.             << setw(dmt) << "Диаметр"
  93.             << setw(qnt) << "Количество"
  94.             << '\n';
  95.     }
  96.     void line() {
  97.         num = 6;
  98.         vol = 13;
  99.         mat = 16;
  100.         nam = 18;
  101.         clr = 14;
  102.         dmt = 11;
  103.         qnt = 13;
  104.     }
  105.     void record(const size_t id, const Pot& pot) {
  106.         cout.setf(ios::fixed);
  107.         cout.precision(1);
  108.         cout << setw(num) << id
  109.             << setw(vol) << pot.volume
  110.             << setw(mat) << pot.material
  111.             << setw(nam) << pot.name
  112.             << setw(clr) << pot.color
  113.             << setw(dmt) << pot.diameter
  114.             << setw(qnt) << pot.quantity
  115.             << '\n';
  116.     }
  117. };
  118.  
  119. class Pots {
  120. public:
  121.     void add(const Pot& pot) {
  122.         pots.push_back(pot);
  123.     }
  124.     void order(const function<bool(const Pot&, const Pot&)>& cmp) {
  125.         sort(pots.begin(), pots.end(), cmp);
  126.     }
  127.     void show()const {
  128.         Format format;
  129.         format.title();
  130.         format.line();
  131.         size_t id = 0;
  132.         for (const auto& pot : pots) format.record(++id, pot);
  133.     }
  134. private:
  135.     vector<Pot> pots;
  136. };
  137.  
  138. enum class Choice {
  139.     Add = 1,
  140.     Flush,
  141.     SortByVolume,
  142.     SortByMaterial,
  143.     SortByName,
  144.     SortByColor,
  145.     SortByDiameter,
  146.     SortByQuantity,
  147.     Exit
  148. };
  149.  
  150. void menu() {
  151.     puts("1. Добавить запись");
  152.     puts("2. Очистить экран");
  153.     puts("3. Сортировать по объёму");
  154.     puts("4. Сортировать по материалу");
  155.     puts("5. Сортировать по названию");
  156.     puts("6. Сортировать по цвету");
  157.     puts("7. Сортировать по диаметру");
  158.     puts("8. Сортировать по количеству");
  159.     puts("9. Выход из программы");
  160.     cout << ">>> ";
  161. }
  162.  
  163. template<>
  164. Pot input<Pot>(istream& inp, const char* message) {
  165.     cout << message;
  166.     Pot value{};
  167.     inp >> value;
  168.     return value;
  169. }
  170.  
  171. void start() {
  172.     Pots pots;
  173.     Pot pot;
  174.     bool flag;
  175.     while (true) {
  176.         flag = true;
  177.         menu();
  178.         int choice;
  179.         cin >> choice;
  180.         switch (Choice(choice)) {
  181.             case Choice::Add:
  182.                 pot = input<Pot>(cin, "Заполните поля записи:\n");
  183.                 pots.add(pot);
  184.                 flag = false;
  185.                 break;
  186.             case Choice::Flush:
  187.                 system("cls");
  188.                 flag = false;
  189.                 break;
  190.             case Choice::SortByVolume:
  191.                 pots.order(Pot::compare_by_volume());
  192.                 break;
  193.             case Choice::SortByMaterial:
  194.                 pots.order(Pot::compare_by_material());
  195.                 break;
  196.             case Choice::SortByName:
  197.                 pots.order(Pot::compare_by_name());
  198.                 break;
  199.             case Choice::SortByColor:
  200.                 pots.order(Pot::compare_by_color());
  201.                 break;
  202.             case Choice::SortByDiameter:
  203.                 pots.order(Pot::compare_by_diameter());
  204.                 break;
  205.             case Choice::SortByQuantity:
  206.                 pots.order(Pot::compare_by_quantity());
  207.                 break;
  208.             case Choice::Exit:
  209.                 return;
  210.             default:
  211.                 flag = false;
  212.         }
  213.         if (flag) pots.show();
  214.     }
  215. }
  216.  
  217. int main() {
  218.     system("chcp 1251 > nul");
  219.     start();
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement