Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // для scanf и других
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #include <set>
- // #include <Windows.h>
- using namespace std;
- #define CLEAR_TABLE "Таблица пуста"
- bool string_is_fill(string x) {
- for (int i = 0; i < x.length(); i++) {
- if (!isspace(x.at(i))) return false;
- }
- return true;
- }
- // проверка вовда СТРОКИ
- string read_string(const char* promt) {
- string x;
- cout << promt;
- cin >> x;
- while (x.length() <= 0 && !string_is_fill(x)) {
- cout << "Неверное введенное значение, попробуйте еще." << endl << promt;
- while(getchar() != '\n');
- }
- return x;
- }
- // проверка ввода ЧИСЛА
- double read_double(const char* promt = ""){
- double x;
- cout << promt;
- while (scanf("%lf",&x) != 1) {
- cout << "Неверное введенное значение, попробуйте еще." << endl << promt;
- while(getchar() != '\n');
- }
- return x;
- }
- class table {
- public:
- table(string path = "list.txt");
- ~table();
- void add(string name, string shop, int cost);
- void show();
- void find_by_shop(string);
- void find_by_name(string);
- void find_by_cost(int);
- bool del(string name, string shop);
- void exprt(string note = "list.txt");
- void imprt(string note = "list.txt");
- bool correct();
- class PRICE {
- public:
- PRICE(string shop, string name, int cost) { this->shop = shop; this->name = name; this->cost = cost; };
- ~PRICE() {};
- string name;
- string shop;
- int cost;
- PRICE* next = nullptr;
- string get_name() { return name; };
- string get_shop() { return shop; };
- };
- private:
- string path;
- int size = 0;
- PRICE* top = nullptr;
- };
- table::table(string path) {
- this->path = path;
- }
- table::~table() {
- }
- void table::add(string shop, string name, int cost) {
- if (top == nullptr) {
- top = new PRICE(shop, name, cost);
- size++;
- }
- else {
- PRICE* current = top;
- while (current->next != nullptr) {
- current = current->next;
- }
- current->next = new PRICE(shop, name, cost);
- size++;
- }
- }
- void table::show() {
- if (size == 0) {
- return;
- }
- cout << "|" << setw(12) << "SHOP |" << setw(22) << "NAME |" << setw(12) << "PRICE |" << endl;
- for (PRICE* current = top; current != nullptr; current = current->next) {
- cout << "|" << setw(10) << current->shop << " |";
- cout << setw(20) << current->name << " |" ;
- cout << setw(10) << current->cost << " |" << endl;
- }
- cout << "\n";
- }
- bool table::correct() {
- cout << "Магазины: " << endl;
- set <string> shops;
- for (PRICE* current = top; current; current = current->next) {
- shops.insert(current->shop);
- }
- for (auto s : shops) {
- cout << "-" << s << "\n";
- }
- string choice;
- choice = read_string("Выберите магазин: ");
- set <string> products;
- cout << "Продукты в \"" << choice << "\": " << endl;
- for (PRICE* current = top; current; current = current->next) {
- if (current->shop == choice) {
- products.insert(current->name);
- }
- }
- for (auto P : products) {
- cout << "-" << P << "\n";
- }
- string prod;
- prod = read_string("Выберите продукт: ");
- PRICE* res = top;
- while (res) {
- if (res->name == prod and res->shop == choice) {
- break;
- } else if (res -> next == NULL) return false;
- res = res->next;
- }
- cout << "Выбранный товар \"" << res->get_name() << "\" из магазина " << res->get_shop() << "\n";
- cout << "Новое название товара: ";
- string name = read_string("Новое название товара: ");
- cout << "Новая цена: ";
- int cost = read_double("Новая цена товара: ");
- res->cost = cost;
- res->name = name;
- exprt();
- return true;
- }
- void table::find_by_shop(string shop) {
- if (size == 0) {
- cout << CLEAR_TABLE << endl;
- } else {
- for (PRICE* current = top; current != nullptr; current = current->next) {
- if (current->shop == shop) {
- cout << current->shop << " " << current->name << " " << current->cost << "\n";
- }
- }
- }
- }
- void table::find_by_name(string name) {
- if (size == 0) {
- cout << CLEAR_TABLE << endl;
- } else {
- for (PRICE* current = top; current != nullptr; current = current->next) {
- if (current->name == name) {
- cout << current->shop << " " << current->name << " " << current->cost << "\n";
- }
- }
- }
- }
- void table::find_by_cost(int cost) {
- if (size == 0) {
- cout << CLEAR_TABLE << endl;
- } else {
- for (PRICE* current = top; current != nullptr; current = current->next) {
- if (current->cost == cost) {
- cout << current->shop << " " << current->name << " " << current->cost << "\n";
- }
- }
- }
- }
- bool table::del(string shop, string name) {
- if (size == 0) {
- return false;
- }
- if (size == 1) {
- top = nullptr;
- size--;
- return false;
- }
- PRICE* current = top;
- if ((current->shop == shop) && (current->name == name)) {
- top = top->next;
- return true;
- }
- for (int i = 0; i < size; i++) {
- current = current->next;
- if (current->next != NULL) {
- if ((current->next->shop == shop) && (current->next->name == name)) {
- current->next = current->next->next;
- break;
- }
- } else return false;
- }
- size--;
- return true;
- }
- void table::exprt(string note) {
- PRICE* current = top;
- ofstream file(note);
- for (int i = 0; i < size; i++) {
- file << current->shop << " " << current->name << " " << current->cost << "\n";
- current = current->next;
- }
- file.close();
- }
- void table::imprt(string note) {
- string shop, name;
- int cost;
- ifstream in(note);
- if (in.is_open()) {
- while (!in.eof()) {
- in >> shop;
- in >> name;
- in >> cost;
- add(shop, name, cost);
- }
- del(shop, name);
- }
- else {
- cout << "File not Found: " << note<< "\n";
- }
- in.close();
- }
- double fill() {
- string input;
- while (true) {
- bool error = 0;
- cin >> input;
- for (int i = 0; i < input.size(); i++) {
- if ((isdigit(input[i]) == 0 && input[i] != '.' && input[i] != '-')) {
- error = 1;
- break;
- }
- }
- if (error == 1) {
- cout<<"Enter value without letters\n";
- }
- else {
- break;
- }
- }
- return stoi(input);
- }
- int menu() {
- while (true) {
- cout << "1) Показать список" << endl;
- cout << "2) Ввести продукт" << endl;
- cout << "3) Сохранить таблицу в файл \"list.txt\"" << endl;
- cout << "4) Вывести товары по магазину" << endl;
- cout << "5) Вывести товары по названию" << endl;
- cout << "6) Вывести товары по цене" << endl;
- cout << "7) Удалить по магазину и продукту" << endl;
- cout << "8) Изменить продукт" << endl;
- cout << "0) Выход" << endl;
- int id = read_double(" >> ");
- if (0 <= id <= 8) {
- return id;
- } else {
- cout << "Этого нет в меню" << endl;
- }
- }
- }
- int main() {
- setlocale(LC_ALL, "");
- system("chcp 65001");
- table list;
- list.imprt();
- int v;
- string name, shop;
- int cost;
- while (true) {
- v = menu();
- switch (v) {
- case 1:
- list.show();
- break;
- case 2:
- shop = read_string("Название магазина: ");
- name = read_string("Название товара: ");
- cost = read_double("Цена товара: ");
- list.add(shop, name, cost);
- cout << "Добавлен новый продукт\n";
- break;
- case 3:
- list.exprt("list.txt");
- cout << "Сохранено в файл \"list.txt\"\n";
- break;
- case 4: {
- string shop = read_string("Название магазина: ");
- list.find_by_shop(shop);
- break;
- }
- case 5: {
- string name = read_string("Название продукта: ");
- list.find_by_name(name);
- break;
- }
- case 6: {
- int cost = read_double("Цена: ");
- list.find_by_cost(cost);
- break;
- }
- case 7:
- shop = read_string("Название магазина: ");
- name = read_string("Название товара: ");
- if (list.del(shop, name))
- cout << name << " был удалён." << endl;
- else
- cout << "Такого магазина или товара не существует." << endl;
- break;
- case 8:
- if (list.correct()) {
- cout << "Обновлено." << endl;
- } else {
- cout << "Такого магазина или товара не существует." << endl;
- }
- break;
- case 0:
- exit(0);
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment