Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <fstream>
  5. #include <vector>
  6. #include <algorithm>
  7. #include "vld.h"
  8. using namespace std;
  9.  
  10. class Exchange
  11. {
  12. private: string _str;
  13.  
  14. public:
  15. Exchange(string x)
  16. {
  17. _str = x;
  18. }
  19. int strToInt()
  20. {
  21. int test = 0;
  22. for (int i = 0; i < _str.length(); i++)
  23. test = test * 10 + _str[i] - '0';
  24. return test;
  25. }
  26. void set(string x)
  27. {
  28. _str = x;
  29. }
  30. };
  31.  
  32.  
  33.  
  34. class PriceList
  35. {
  36. private:
  37. string _nameShop, _nameProd, _adr, _price, _amountProd;
  38. public:
  39.  
  40. void readStr(fstream &fin)
  41. {
  42. fin >> _nameShop >> _adr >> _nameProd >> _amountProd >> _price;
  43. }
  44. string getNameShop()
  45. {
  46. return _nameShop;
  47. }
  48. string getNameProd()
  49. {
  50. return _nameProd;
  51. }
  52. string getAdr()
  53. {
  54. return _adr;
  55. }
  56. string getPrice()
  57. {
  58. return _price;
  59. }
  60. string getAmountProd()
  61. {
  62. return _amountProd;
  63. }
  64. bool operator ()(string str)
  65. {
  66. return _nameProd == str;
  67. }
  68. };
  69.  
  70. class Enter
  71. {
  72. public:
  73. void setPriceName()
  74. {
  75. int i = 0, size = 0;
  76. string strok;
  77. map<int, PriceList> MapL;
  78. vector<PriceList> VecL;
  79. fstream fin = fstream("input.txt", ios::in);
  80. if (fin.good())
  81. {
  82. while (!fin.eof())
  83. {
  84. PriceList List1;
  85. List1.readStr(fin);
  86. VecL.push_back(List1);
  87. MapL.emplace(i, List1);
  88. i++;
  89. }
  90. fin.close();
  91. string str;
  92. int price;
  93. cout << "Введите название товара и сумму: ";
  94. cin >> str >> price;
  95. if (price <= 0)
  96. {
  97. throw exception("Неверно введена сумма!\n");
  98. }
  99. auto itM = MapL.begin();
  100. bool flagStr = false, flagPrice = false;
  101. //Решение через мар
  102. for (itM; itM != MapL.end(); ++itM)
  103. {
  104.  
  105. if (str == (*itM).second.getNameProd() && (*itM).second.getAmountProd()!="0")
  106. {
  107. flagStr = true;
  108. Exchange Ex((*itM).second.getPrice());
  109. int test1;
  110. test1 = Ex.strToInt();
  111. if (test1 <= price)
  112. {
  113. cout << "Название магазина: " << (*itM).second.getNameShop() << " Адрес: " << (*itM).second.getAdr() << " Цена товара: " << test1 << endl;
  114. flagPrice = true;
  115. }
  116.  
  117.  
  118. }
  119. }
  120. if (flagStr == false)
  121. throw exception("Не сущствует товара с данным названием\n");
  122. if (flagStr == true && flagPrice == false)
  123. throw exception("Сумма слишком мала для покупки данного товара");
  124. //Решение через вектор
  125. flagStr = false;
  126. flagPrice = false;
  127. auto itV = find_if(VecL.begin(), VecL.end(), [str](PriceList &PL)
  128. {
  129. return PL.getNameProd() == str;
  130. });
  131. for (itV; itV != VecL.end(); ++itV)
  132. {
  133.  
  134. if (str == (*itV).getNameProd() && (*itV).getAmountProd() != "0")
  135. {
  136. flagStr = true;
  137. Exchange Ex((*itV).getPrice());
  138. int test1;
  139. test1 = Ex.strToInt();
  140. if (test1 <= price)
  141. {
  142. cout << "Название магазина: " << (*itV).getNameShop() << " Адрес: " << (*itV).getAdr() << " Цена товара: " << test1 << endl;
  143. flagPrice = true;
  144. }
  145. }
  146. }
  147. if (flagStr == false)
  148. throw exception("Не сущствует товара с данным названием\n");
  149. if (flagStr == true && flagPrice == false)
  150. throw exception("Сумма слишком мала для покупки данного товара");
  151. }
  152. else
  153. cout << "Невозможно открыть файл" << endl;
  154.  
  155. }
  156. };
  157.  
  158. int main()
  159. {
  160.  
  161. try
  162. {
  163. setlocale(0, "Rus");
  164. system("chcp 1251");
  165. Enter Act;
  166. Act.setPriceName();
  167. return 0;
  168. }
  169. catch (exception& ex)
  170. {
  171. cout << ex.what() << endl;
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement