Advertisement
erehh

savarjisho 3 monacemta struqtura

Dec 10th, 2023
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <list>
  5. #include <unordered_map>
  6.  
  7. using namespace std;
  8.  
  9. struct Order {
  10.     long long orderNumber;
  11.     double rating;
  12.  
  13.     //Constructor
  14.     Order(long long num, double rate) {
  15.         orderNumber = num;
  16.         rating = rate;
  17.     }
  18. };
  19.  
  20. class OrderStructure {
  21. private:
  22.     list<Order> orders;
  23.     unordered_map<long long, list<Order>::iterator>orderMap;
  24.  
  25. public:
  26.  
  27.     OrderStructure() = default;
  28.     OrderStructure(const OrderStructure& other) : orders(other.orders), orderMap(other.orderMap) {}
  29.     ~OrderStructure() {}
  30.  
  31.     void addOrder(long long orderNumber, double rating) {
  32.         Order newOrder(orderNumber, rating);
  33.  
  34.         if (rating < 0.75) {
  35.             orders.push_back(newOrder);
  36.         }
  37.         else {
  38.             auto it = orders.begin();
  39.             while (it != orders.end() && it->rating >= rating) {
  40.                 ++it;
  41.             }
  42.             orders.insert(it, newOrder);
  43.         }
  44.         orderMap[orderNumber] = --orders.end();
  45.     }
  46.  
  47.     Order removeOrder() {
  48.         if (!orders.empty()) {
  49.             Order removeOrder = orders.front();
  50.             orderMap.erase(removeOrder.orderNumber);
  51.             orders.pop_front();
  52.             return removeOrder;
  53.         }
  54.         else {
  55.             cerr << "Error, structure is empty" << endl;
  56.             exit(EXIT_FAILURE);
  57.         }
  58.     }
  59.  
  60.     bool isEmpty() const {
  61.         return orders.empty();
  62.     }
  63.  
  64.     void printOrders() const{
  65.         for (const auto& order : orders) {
  66.             cout << "Order Number: " << order.orderNumber << " Rating: " << order.rating << endl;
  67.             cout << endl;
  68.         }
  69.     }
  70. };
  71.  
  72. int main() {
  73.     srand(static_cast<unsigned>(time(nullptr)));
  74.     OrderStructure orderStructure;
  75.  
  76.     // adding rand orders
  77.     for (int i = 0; i < 7; ++i) {
  78.         long long orderNumber = i + 1000;
  79.         double rating = static_cast<double>(rand()) / RAND_MAX;
  80.         orderStructure.addOrder(orderNumber, rating);
  81.     }
  82.  
  83.     cout << "Order List: " << endl;
  84.     orderStructure.printOrders();
  85.     cout << endl;
  86.  
  87.     //removing and printing until the struct is empty
  88.     while (!orderStructure.isEmpty()) {
  89.  
  90.         Order removeOrder = orderStructure.removeOrder();
  91.         cout << "Removed " << removeOrder.orderNumber << " Rating: " << removeOrder.rating << endl;
  92.         cout << endl;
  93.     }
  94.  
  95.     return 0;
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement