Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iomanip>
- #include <iostream>
- #include <functional>
- #include <string>
- #include <vector>
- using namespace std;
- template<typename T>
- T input(istream& inp, const char* message) {
- cout << message;
- T value{};
- inp >> value;
- inp.ignore(0x1000, '\n');
- return value;
- }
- template<>
- string input<string>(istream& inp, const char* message) {
- cout << message;
- string value;
- getline(inp, value);
- return value;
- }
- struct Pot {
- unsigned short diameter;
- unsigned short quantity;
- float volume;
- string name;
- string material;
- string color;
- Pot() : diameter(0), quantity(0), volume(0) {}
- struct compare_by_diameter {
- constexpr bool operator()(const Pot& a, const Pot& b)const {
- return a.diameter < b.diameter;
- }
- };
- struct compare_by_quantity {
- constexpr bool operator()(const Pot& a, const Pot& b)const {
- return a.quantity < b.quantity;
- }
- };
- struct compare_by_volume {
- constexpr bool operator()(const Pot& a, const Pot& b)const {
- return a.volume < b.volume;
- }
- };
- struct compare_by_name {
- constexpr bool operator()(const Pot& a, const Pot& b)const {
- return a.name < b.name;
- }
- };
- struct compare_by_material {
- constexpr bool operator()(const Pot& a, const Pot& b)const {
- return a.material < b.material;
- }
- };
- struct compare_by_color {
- constexpr bool operator()(const Pot& a, const Pot& b)const {
- return a.color < b.color;
- }
- };
- private:
- friend istream& operator>>(istream& inp, Pot& pot) {
- pot.volume = input<float>(inp, "Объём: ");
- pot.material = input<string>(inp, "Материал: ");
- pot.name = input<string>(inp, "Название: ");
- pot.color = input<string>(inp, "Цвет: ");
- pot.diameter = input<unsigned short>(inp, "Диаметр: ");
- pot.quantity = input<unsigned short>(inp, "Количество: ");
- return inp;
- }
- };
- struct Format {
- streamsize num;
- streamsize vol;
- streamsize mat;
- streamsize nam;
- streamsize clr;
- streamsize dmt;
- streamsize qnt;
- Format() : num(9), vol(11), mat(13), nam(17), clr(14), dmt(16), qnt(15) {}
- void title()const {
- cout << setw(num) << "Кастрюля"
- << setw(vol) << "Объём"
- << setw(mat) << "Материал"
- << setw(nam) << "Название"
- << setw(clr) << "Цвет"
- << setw(dmt) << "Диаметр"
- << setw(qnt) << "Количество"
- << '\n';
- }
- void line() {
- num = 6;
- vol = 13;
- mat = 16;
- nam = 18;
- clr = 14;
- dmt = 11;
- qnt = 13;
- }
- void record(const size_t id, const Pot& pot) {
- cout.setf(ios::fixed);
- cout.precision(1);
- cout << setw(num) << id
- << setw(vol) << pot.volume
- << setw(mat) << pot.material
- << setw(nam) << pot.name
- << setw(clr) << pot.color
- << setw(dmt) << pot.diameter
- << setw(qnt) << pot.quantity
- << '\n';
- }
- };
- class Pots {
- public:
- void add(const Pot& pot) {
- pots.push_back(pot);
- }
- void order(const function<bool(const Pot&, const Pot&)>& cmp) {
- sort(pots.begin(), pots.end(), cmp);
- }
- void show()const {
- Format format;
- format.title();
- format.line();
- size_t id = 0;
- for (const auto& pot : pots) format.record(++id, pot);
- }
- private:
- vector<Pot> pots;
- };
- enum class Choice {
- Add = 1,
- Flush,
- SortByVolume,
- SortByMaterial,
- SortByName,
- SortByColor,
- SortByDiameter,
- SortByQuantity,
- Exit
- };
- void menu() {
- puts("1. Добавить запись");
- puts("2. Очистить экран");
- puts("3. Сортировать по объёму");
- puts("4. Сортировать по материалу");
- puts("5. Сортировать по названию");
- puts("6. Сортировать по цвету");
- puts("7. Сортировать по диаметру");
- puts("8. Сортировать по количеству");
- puts("9. Выход из программы");
- cout << ">>> ";
- }
- template<>
- Pot input<Pot>(istream& inp, const char* message) {
- cout << message;
- Pot value{};
- inp >> value;
- return value;
- }
- void start() {
- Pots pots;
- Pot pot;
- bool flag;
- while (true) {
- flag = true;
- menu();
- int choice;
- cin >> choice;
- switch (Choice(choice)) {
- case Choice::Add:
- pot = input<Pot>(cin, "Заполните поля записи:\n");
- pots.add(pot);
- flag = false;
- break;
- case Choice::Flush:
- system("cls");
- flag = false;
- break;
- case Choice::SortByVolume:
- pots.order(Pot::compare_by_volume());
- break;
- case Choice::SortByMaterial:
- pots.order(Pot::compare_by_material());
- break;
- case Choice::SortByName:
- pots.order(Pot::compare_by_name());
- break;
- case Choice::SortByColor:
- pots.order(Pot::compare_by_color());
- break;
- case Choice::SortByDiameter:
- pots.order(Pot::compare_by_diameter());
- break;
- case Choice::SortByQuantity:
- pots.order(Pot::compare_by_quantity());
- break;
- case Choice::Exit:
- return;
- default:
- flag = false;
- }
- if (flag) pots.show();
- }
- }
- int main() {
- system("chcp 1251 > nul");
- start();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement