Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void MostPopular(string dbName)
- {
- string tabName("Orders");
- dbmsLib::DBTableTxt tab; //empty tab
- string path = "..\\" + dbName + "\\"; //Путь к таблице
- tab.ReadDBTable(path + tabName + ".txt"); //Читаем по пути
- int year;
- cout << "За какой год сделать сравнение: ";
- cin >> year;
- vector<int> OrdersIDinYear;
- for (int i = 0; i < tab.GetSize(); i++) {
- dbmsLib::Row nowrow = tab.GetRow(i);
- dbmsLib::DBDate nowdate = *(dbmsLib::DBDate*)nowrow["OrderDate"];
- if (nowdate.GetYear() == year)
- OrdersIDinYear.push_back(*(int*)nowrow["OrderID"]);
- }
- dbmsLib::DBTableTxt tab2;
- tab2.ReadDBTable("..\\" + dbName + "\\" + "OrderDetails.txt");
- map<string, int> productCount; //Контейнер количество по названию продукта
- for (int i = 0; i < tab2.GetSize(); i++) {
- dbmsLib::Row nowrow = tab2.GetRow(i);
- int OrdId = *(int*)nowrow["OrderID"];
- string index = *(string*)nowrow["ProductID"];
- if (find(OrdersIDinYear.begin(), OrdersIDinYear.end(), OrdId) != end(OrdersIDinYear))
- productCount[index] += *(int*)nowrow["Quantity"];
- }
- auto rez = max_element(productCount.begin(), productCount.end(),
- [](const pair<string, int>& p1, const pair<string, int>& p2) {
- return p1.second < p2.second; }); // Ищем максимальный элемент
- cout << "Самым продаваемым продуктом был " << rez->first << endl;
- cout << "Его купили " << rez->second << " раз" << endl;
- }
Add Comment
Please, Sign In to add comment