Stolar228

Untitled

Dec 13th, 2022
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.94 KB | None | 0 0
  1.  
  2. // для scanf и других
  3. #define _CRT_SECURE_NO_WARNINGS
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <iomanip>
  8. #include <string>
  9. #include <set>
  10.  
  11. // #include <Windows.h>
  12.  
  13. using namespace std;
  14.  
  15. #define CLEAR_TABLE "Таблица пуста"
  16.  
  17. bool string_is_fill(string x) {
  18.     for (int i = 0; i < x.length(); i++) {
  19.         if (!isspace(x.at(i))) return false;
  20.     }
  21.     return true;
  22. }
  23.  
  24. // проверка вовда СТРОКИ
  25. string read_string(const char* promt) {
  26.     string x;
  27.     cout << promt;
  28.     cin >> x;
  29.     while (x.length() <= 0 && !string_is_fill(x)) {
  30.     cout << "Неверное введенное значение, попробуйте еще." << endl << promt;
  31.     while(getchar() != '\n');
  32.   }
  33.     return x;
  34. }
  35.  
  36. // проверка ввода ЧИСЛА
  37. double read_double(const char* promt = ""){
  38.     double x;
  39.   cout << promt;
  40.   while (scanf("%lf",&x) != 1) {
  41.     cout << "Неверное введенное значение, попробуйте еще." << endl << promt;
  42.     while(getchar() != '\n');
  43.   }
  44.   return x;
  45. }
  46.  
  47. class table {
  48. public:
  49.   table(string path = "list.txt");
  50.   ~table();
  51.   void add(string name, string shop, int cost);
  52.   void show();
  53.   void find_by_shop(string);
  54.   void find_by_name(string);
  55.   void find_by_cost(int);
  56.   bool del(string name, string shop);
  57.   void exprt(string note = "list.txt");
  58.   void imprt(string note = "list.txt");
  59.   bool correct();
  60.  
  61. class PRICE {
  62. public:
  63.   PRICE(string shop, string name, int cost) { this->shop = shop; this->name = name; this->cost = cost; };
  64.   ~PRICE() {};
  65.   string name;
  66.   string shop;
  67.   int cost;
  68.   PRICE* next = nullptr;
  69.   string get_name() { return name; };
  70.   string get_shop() { return shop; };
  71. };
  72.  
  73. private:
  74.   string path;
  75.   int size = 0;
  76.   PRICE* top = nullptr;
  77. };
  78.  
  79. table::table(string path) {
  80.   this->path = path;
  81. }
  82.  
  83. table::~table() {
  84. }
  85.  
  86.  
  87. void table::add(string shop, string name, int cost) {
  88.   if (top == nullptr) {
  89.     top = new PRICE(shop, name, cost);
  90.     size++;
  91.   }
  92.   else {
  93.     PRICE* current = top;
  94.     while (current->next != nullptr) {
  95.       current = current->next;
  96.  
  97.     }
  98.     current->next = new PRICE(shop, name, cost);
  99.     size++;
  100.  
  101.   }
  102. }
  103.  
  104. void table::show() {
  105.   if (size == 0) {
  106.     return;
  107.   }
  108.     cout << "|" << setw(12) << "SHOP |" << setw(22) << "NAME |" << setw(12) << "PRICE |" << endl;
  109.   for (PRICE* current = top; current != nullptr; current = current->next) {
  110.     cout << "|" << setw(10) << current->shop << " |";
  111.         cout << setw(20) << current->name << " |" ;
  112.         cout << setw(10) << current->cost << " |" << endl;
  113.   }
  114.   cout << "\n";
  115. }
  116. bool table::correct() {
  117.   cout << "Магазины: " << endl;
  118.   set <string> shops;
  119.   for (PRICE* current = top; current; current = current->next) {
  120.       shops.insert(current->shop);
  121.   }
  122.   for (auto s : shops) {
  123.       cout << "-" << s << "\n";
  124.   }
  125.  
  126.   string choice;
  127.   choice = read_string("Выберите магазин: ");
  128.  
  129.   set <string> products;
  130.   cout << "Продукты в \"" << choice << "\": " << endl;
  131.   for (PRICE* current = top; current; current = current->next) {
  132.     if (current->shop == choice) {
  133.       products.insert(current->name);
  134.     }
  135.   }
  136.   for (auto P : products) {
  137.     cout << "-" << P << "\n";
  138.   }
  139.  
  140.   string prod;
  141.     prod = read_string("Выберите продукт: ");
  142.  
  143.   PRICE* res = top;
  144.   while (res) {
  145.     if (res->name == prod and res->shop == choice) {
  146.       break;
  147.     } else if (res -> next == NULL) return false;
  148.     res = res->next;
  149.   }
  150.   cout << "Выбранный товар \"" << res->get_name() << "\" из магазина " << res->get_shop() << "\n";
  151.   cout << "Новое название товара: ";
  152.   string name = read_string("Новое название товара: ");
  153.     cout << "Новая цена: ";
  154.   int cost = read_double("Новая цена товара: ");
  155.  
  156.   res->cost = cost;
  157.   res->name = name;
  158.   exprt();
  159.     return true;
  160. }
  161.  
  162. void table::find_by_shop(string shop) {
  163.   if (size == 0) {
  164.     cout << CLEAR_TABLE << endl;
  165.   } else {
  166.     for (PRICE* current = top; current != nullptr; current = current->next) {
  167.       if (current->shop == shop) {
  168.         cout << current->shop << " " << current->name << " " << current->cost << "\n";
  169.       }
  170.     }
  171.   }
  172. }
  173.  
  174. void table::find_by_name(string name) {
  175.   if (size == 0) {
  176.     cout << CLEAR_TABLE << endl;
  177.   } else {
  178.     for (PRICE* current = top; current != nullptr; current = current->next) {
  179.       if (current->name == name) {
  180.         cout << current->shop << " " << current->name << " " << current->cost << "\n";
  181.       }
  182.     }
  183.   }
  184. }
  185.  
  186. void table::find_by_cost(int cost) {
  187.   if (size == 0) {
  188.     cout << CLEAR_TABLE << endl;
  189.   } else {
  190.     for (PRICE* current = top; current != nullptr; current = current->next) {
  191.       if (current->cost == cost) {
  192.         cout << current->shop << " " << current->name << " " << current->cost << "\n";
  193.       }
  194.     }
  195.   }
  196. }
  197.  
  198. bool table::del(string shop, string name) {
  199.   if (size == 0) {
  200.     return false;
  201.   }
  202.  
  203.   if (size == 1) {
  204.     top = nullptr;
  205.     size--;
  206.     return false;
  207.   }
  208.  
  209.   PRICE* current = top;
  210.   if ((current->shop == shop) && (current->name == name)) {
  211.     top = top->next;
  212.     return true;
  213.   }
  214.   for (int i = 0; i < size; i++) {
  215.     current = current->next;
  216.         if (current->next != NULL) {
  217.         if ((current->next->shop == shop) && (current->next->name == name)) {
  218.                 current->next = current->next->next;
  219.                 break;
  220.             }
  221.     } else return false;
  222.   }
  223.  
  224.   size--;
  225.     return true;
  226. }
  227.  
  228. void table::exprt(string note) {
  229.   PRICE* current = top;
  230.   ofstream file(note);
  231.   for (int i = 0; i < size; i++) {
  232.     file << current->shop << " " << current->name << " " << current->cost << "\n";
  233.     current = current->next;
  234.   }
  235.   file.close();
  236. }
  237.  
  238. void table::imprt(string note) {
  239.   string shop, name;
  240.   int cost;
  241.   ifstream in(note);
  242.   if (in.is_open()) {
  243.     while (!in.eof()) {
  244.       in >> shop;
  245.       in >> name;
  246.       in >> cost;
  247.  
  248.       add(shop, name, cost);
  249.  
  250.     }
  251.     del(shop, name);
  252.   }
  253.   else {
  254.     cout << "File not Found: " << note<< "\n";
  255.   }
  256.   in.close();
  257. }
  258.  
  259. double fill() {
  260.   string input;
  261.   while (true) {
  262.     bool error = 0;
  263.     cin >> input;
  264.     for (int i = 0; i < input.size(); i++) {
  265.       if ((isdigit(input[i]) == 0 && input[i] != '.' && input[i] != '-')) {
  266.         error = 1;
  267.         break;
  268.       }
  269.     }
  270.     if (error == 1) {
  271.       cout<<"Enter value without letters\n";
  272.     }
  273.     else {
  274.       break;
  275.     }
  276.  
  277.   }
  278.  
  279.   return stoi(input);
  280. }
  281.  
  282. int menu() {
  283.   while (true) {
  284.     cout << "1) Показать список" << endl;
  285.     cout << "2) Ввести продукт" << endl;
  286.     cout << "3) Сохранить таблицу в файл \"list.txt\"" << endl;
  287.     cout << "4) Вывести товары по магазину" << endl;
  288.     cout << "5) Вывести товары по названию" << endl;
  289.     cout << "6) Вывести товары по цене" << endl;
  290.     cout << "7) Удалить по магазину и продукту" << endl;
  291.     cout << "8) Изменить продукт" << endl;
  292.     cout << "0) Выход" << endl;
  293.     int id = read_double(" >> ");
  294.     if (0 <= id <= 8) {
  295.       return id;
  296.  
  297.     } else {
  298.       cout << "Этого нет в меню" << endl;
  299.     }
  300.   }
  301. }
  302.  
  303. int main() {
  304.  
  305.     setlocale(LC_ALL, "");
  306.  
  307.   system("chcp 65001");
  308.  
  309.   table list;
  310.   list.imprt();
  311.  
  312.   int v;
  313.  
  314.   string name, shop;
  315.   int cost;
  316.   while (true) {
  317.     v = menu();
  318.  
  319.     switch (v) {
  320.     case 1:
  321.       list.show();
  322.       break;
  323.  
  324.     case 2:
  325.             shop = read_string("Название магазина: ");
  326.             name = read_string("Название товара: ");
  327.             cost = read_double("Цена товара: ");
  328.  
  329.       list.add(shop, name, cost);
  330.       cout << "Добавлен новый продукт\n";
  331.       break;
  332.  
  333.     case 3:
  334.       list.exprt("list.txt");
  335.       cout << "Сохранено в файл \"list.txt\"\n";
  336.       break;
  337.  
  338.     case 4: {
  339.             string shop = read_string("Название магазина: ");
  340.       list.find_by_shop(shop);
  341.       break;
  342.     }
  343.  
  344.     case 5: {
  345.             string name = read_string("Название продукта: ");
  346.       list.find_by_name(name);
  347.       break;
  348.     }
  349.  
  350.     case 6: {
  351.       int cost = read_double("Цена: ");
  352.       list.find_by_cost(cost);
  353.       break;
  354.     }
  355.  
  356.     case 7:
  357.             shop = read_string("Название магазина: ");
  358.             name = read_string("Название товара: ");
  359.  
  360.       if (list.del(shop, name))
  361.           cout << name << " был удалён." << endl;
  362.             else
  363.                 cout << "Такого магазина или товара не существует." << endl;
  364.       break;
  365.  
  366.     case 8:
  367.       if (list.correct()) {
  368.                 cout << "Обновлено." << endl;
  369.             } else {
  370.                 cout << "Такого магазина или товара не существует." << endl;
  371.             }
  372.       break;
  373.  
  374.         case 0:
  375.             exit(0);
  376.             break;
  377.  
  378.     default:
  379.       break;
  380.     }
  381.   }
  382.  
  383. }
  384.  
Advertisement
Add Comment
Please, Sign In to add comment