Advertisement
erehh

mesame savarjisho monacemta struqtura

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