Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <map>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- #include "vld.h"
- using namespace std;
- class Exchange
- {
- private: string _str;
- public:
- Exchange(string x)
- {
- _str = x;
- }
- int strToInt()
- {
- int test = 0;
- for (int i = 0; i < _str.length(); i++)
- test = test * 10 + _str[i] - '0';
- return test;
- }
- void set(string x)
- {
- _str = x;
- }
- };
- class PriceList
- {
- private:
- string _nameShop, _nameProd, _adr, _price, _amountProd;
- public:
- void readStr(fstream &fin)
- {
- fin >> _nameShop >> _adr >> _nameProd >> _amountProd >> _price;
- }
- string getNameShop()
- {
- return _nameShop;
- }
- string getNameProd()
- {
- return _nameProd;
- }
- string getAdr()
- {
- return _adr;
- }
- string getPrice()
- {
- return _price;
- }
- string getAmountProd()
- {
- return _amountProd;
- }
- bool operator ()(string str)
- {
- return _nameProd == str;
- }
- };
- class Enter
- {
- public:
- void setPriceName()
- {
- int i = 0, size = 0;
- string strok;
- map<int, PriceList> MapL;
- vector<PriceList> VecL;
- fstream fin = fstream("input.txt", ios::in);
- if (fin.good())
- {
- while (!fin.eof())
- {
- PriceList List1;
- List1.readStr(fin);
- VecL.push_back(List1);
- MapL.emplace(i, List1);
- i++;
- }
- fin.close();
- string str;
- int price;
- cout << "Введите название товара и сумму: ";
- cin >> str >> price;
- if (price <= 0)
- {
- throw exception("Неверно введена сумма!\n");
- }
- auto itM = MapL.begin();
- bool flagStr = false, flagPrice = false;
- //Решение через мар
- for (itM; itM != MapL.end(); ++itM)
- {
- if (str == (*itM).second.getNameProd() && (*itM).second.getAmountProd()!="0")
- {
- flagStr = true;
- Exchange Ex((*itM).second.getPrice());
- int test1;
- test1 = Ex.strToInt();
- if (test1 <= price)
- {
- cout << "Название магазина: " << (*itM).second.getNameShop() << " Адрес: " << (*itM).second.getAdr() << " Цена товара: " << test1 << endl;
- flagPrice = true;
- }
- }
- }
- if (flagStr == false)
- throw exception("Не сущствует товара с данным названием\n");
- if (flagStr == true && flagPrice == false)
- throw exception("Сумма слишком мала для покупки данного товара");
- //Решение через вектор
- flagStr = false;
- flagPrice = false;
- auto itV = find_if(VecL.begin(), VecL.end(), [str](PriceList &PL)
- {
- return PL.getNameProd() == str;
- });
- for (itV; itV != VecL.end(); ++itV)
- {
- if (str == (*itV).getNameProd() && (*itV).getAmountProd() != "0")
- {
- flagStr = true;
- Exchange Ex((*itV).getPrice());
- int test1;
- test1 = Ex.strToInt();
- if (test1 <= price)
- {
- cout << "Название магазина: " << (*itV).getNameShop() << " Адрес: " << (*itV).getAdr() << " Цена товара: " << test1 << endl;
- flagPrice = true;
- }
- }
- }
- if (flagStr == false)
- throw exception("Не сущствует товара с данным названием\n");
- if (flagStr == true && flagPrice == false)
- throw exception("Сумма слишком мала для покупки данного товара");
- }
- else
- cout << "Невозможно открыть файл" << endl;
- }
- };
- int main()
- {
- try
- {
- setlocale(0, "Rus");
- system("chcp 1251");
- Enter Act;
- Act.setPriceName();
- return 0;
- }
- catch (exception& ex)
- {
- cout << ex.what() << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement