Advertisement
gamesrsly

ClassSaleVectorSetSearch

Oct 29th, 2019
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <list>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <map>
  9. #include <set>
  10.  
  11. using namespace std;
  12.  
  13. class Sale {
  14.     std::string m_town, m_product = "";
  15.     double m_price = 0.0;
  16.     int m_quantity = 0;
  17. public:
  18.     Sale();
  19.     Sale(std::string &town, std::string &product, double &price, int &quantity) {
  20.         m_town = town;
  21.         m_product = product;
  22.         m_price = price;
  23.         m_quantity = quantity;
  24.     }
  25.    
  26.     std::string getTownName() const {
  27.         return m_town;
  28.     }
  29.    
  30.     double sumProduct() {
  31.         return m_price * m_quantity;
  32.     }
  33.    
  34.     bool operator< (const Sale &right) const {
  35.         return m_town < right.m_town;
  36.     }
  37.    
  38.     bool operator== (const Sale &right) const {
  39.         return this->m_town == right.m_town;
  40.     }
  41. };
  42.  
  43.  
  44. int main() {
  45.     int numCities = 0;
  46.     std::string town = "";
  47.     std::string product = "";
  48.     double price = 0.0;
  49.     int quantity = 0;
  50.     std::vector<Sale> saleV;
  51.     std::set<Sale> saleS;
  52.     cout.setf(ios::fixed);
  53.     cout.precision(2);
  54.    
  55.     cin.setf(ios::fixed);
  56.     cin.precision(2);
  57.    
  58.     cin >> numCities;
  59.    
  60.     while(std::cin >> town && std::cin >> product && std::cin >> price && std::cin >> quantity && numCities != 0) {
  61.         Sale s(town, product, price, quantity);
  62.        
  63.         saleV.push_back(s);
  64.         saleS.insert(s);
  65.        
  66.         --numCities;
  67.         if(numCities == 0) {
  68.             break;
  69.         }
  70.     }
  71.    
  72.     double sum = 0.0;
  73.    
  74.     //sort(saleV.begin(), saleV.end());
  75.    
  76.     for(auto it = saleS.begin(); it != saleS.end(); ++it) {
  77.         std::string temp = it->getTownName();
  78.         std::vector<Sale>::iterator iter = saleV.begin();
  79.         sum = 0.0;
  80.         while((iter = std::find(iter, saleV.end(), *it)) != saleV.end())
  81.         {
  82.             sum += iter->sumProduct();
  83.             iter++;
  84.         }
  85.         cout << it->getTownName() << " -> " << sum;
  86.         cout << endl;
  87.     }
  88.    
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement