diff --git a/ProrityQueue_Zaverukha/headers/DataStruct.h b/ProrityQueue_Zaverukha/headers/DataStruct.h new file mode 100644 index 0000000..5b16e3b --- /dev/null +++ b/ProrityQueue_Zaverukha/headers/DataStruct.h @@ -0,0 +1,46 @@ +#pragma once +#include +#include +#include + +using std::vector; +using std::array; +using std::string; + +struct Customer +{ + string surname; + int time; + int sum; + Customer() {}; + Customer(string surname, int time, int sum) : surname(surname), time(time), sum(sum) {}; + bool operator<(Customer other) + { + int t1 = this->time; + int t2 = other.time; + return t1 > t2; + } + string get_data() + { + return surname + " " + std::to_string(time) + " " + std::to_string(sum); + } +}; + +class DataStruct +{ +private: +public: + explicit DataStruct(vector v) {}; + explicit DataStruct(Customer* v, size_t n) {}; + explicit DataStruct(size_t n) {}; + explicit DataStruct() {}; + + virtual bool insert(Customer item) = 0; + virtual bool swap(Customer customer_1, Customer customer_2) = 0; + virtual vector trigger(int sum) = 0; + virtual Customer pop() = 0; + virtual bool is_present(Customer customer) = 0; + virtual bool is_empty() = 0; + virtual ~DataStruct() {}; +}; + diff --git a/ProrityQueue_Zaverukha/headers/PriorityQueue.h b/ProrityQueue_Zaverukha/headers/PriorityQueue.h new file mode 100644 index 0000000..04ddea0 --- /dev/null +++ b/ProrityQueue_Zaverukha/headers/PriorityQueue.h @@ -0,0 +1,44 @@ +#pragma once +#include +#include +#include +#include +#include "DataStruct.h" + +using std::vector; +using std::array; +using std::string; +using std::set; + + + +inline bool operator<(Customer x, Customer y) +{ + return x.time > y.time; +} + +class PriorityQueue : public DataStruct +{ +private: + set customers; +public: + + PriorityQueue(vector v); + PriorityQueue(Customer* v, size_t n); + PriorityQueue(size_t n); + PriorityQueue(); + + bool insert(Customer item); + bool insert(PriorityQueue items); + bool intersection(PriorityQueue items); + bool swap(Customer customer_1, Customer customer_2); + vector trigger(int sum); + Customer pop(); + bool is_present(Customer customer); + bool is_empty(); + + + + ~PriorityQueue(); +}; + diff --git a/ProrityQueue_Zaverukha/src/PriorityQueue.cpp b/ProrityQueue_Zaverukha/src/PriorityQueue.cpp new file mode 100644 index 0000000..64eff9f --- /dev/null +++ b/ProrityQueue_Zaverukha/src/PriorityQueue.cpp @@ -0,0 +1,167 @@ +#include "../headers/PriorityQueue.h" + + + +Customer PriorityQueue::pop() +{ + try + { + if (!is_empty()) + { + Customer item = *(customers.begin()); + customers.erase(*(customers.begin())); + return item; + } + else + { + return Customer("-1", -1, -1); + } + + } + catch (const std::exception&) + { + + } + +} + +bool PriorityQueue::is_present(Customer customer) +{ + return (customers.find(customer) != customers.end()); +} + +PriorityQueue::PriorityQueue(vector v) +{ + for (int i = 0; i < v.size(); ++i) + { + insert(v[i]); + } +} + +PriorityQueue::PriorityQueue(Customer * v, size_t n) +{ + for (int i = 0; i < n; ++i) + { + insert(v[i]); + } +} + +PriorityQueue::PriorityQueue(size_t n) +{ + for (int i = 0; i < n; ++i) + { + insert(Customer(std::to_string(rand()), rand(), rand())); + } +} + +PriorityQueue::PriorityQueue() +{ +} + +bool PriorityQueue::insert(Customer item) +{ + try + { + customers.insert(item); + } + catch (const std::exception&) + { + + return false; + } + return true; +} + +bool PriorityQueue::insert(PriorityQueue items) +{ + try + { + for (set::iterator iter = items.customers.begin(); iter != items.customers.end(); ++iter) + { + Customer item = *iter; + insert(item); + } + } + catch (const std::exception&) + { + return false; + } +} + +bool PriorityQueue::intersection(PriorityQueue items) +{ + try + { + set new_queue; + for (set::iterator iter = items.customers.begin(); iter != items.customers.end(); ++iter) + { + Customer item = *iter; + if (is_present(item)) + { + new_queue.insert(item); + } + } + customers = new_queue; + } + catch (const std::exception&) + { + return false; + } + return true; +} + +bool PriorityQueue::swap(Customer customer_1, Customer customer_2) +{ + try + { + if (is_present(customer_1) && is_present(customer_2)) + { + customers.erase(customer_1); + customers.erase(customer_2); + int time_1 = customer_1.time; + customer_1.time = customer_2.time; + customer_2.time = time_1; + insert(customer_1); + insert(customer_2); + + } + else + { + return false; + } + } + catch (const std::exception&) + { + return false; + } + return false; +} + +vector PriorityQueue::trigger(int sum) +{ + vector popped; + while (!customers.empty()) + { + Customer front = *(customers.begin()); + if (front.sum > sum) + { + popped.push_back(front); + customers.erase(front); + } + else + { + break; + } + } + return popped; +} + +bool PriorityQueue::is_empty() +{ + return customers.empty(); +} + + +PriorityQueue::~PriorityQueue() +{ +} diff --git a/ProrityQueue_Zaverukha/src/main.cpp b/ProrityQueue_Zaverukha/src/main.cpp new file mode 100644 index 0000000..933e084 --- /dev/null +++ b/ProrityQueue_Zaverukha/src/main.cpp @@ -0,0 +1,72 @@ +#include "../headers/PriorityQueue.h" +#include +#include +#include +#include "../tests/tests.h" + +using namespace std; + +int main() +{ + string c[2] = { "add", "pop" }; + vector commands(c, c+2); + PriorityQueue queue; + if (tests::test_insert() && tests::test_pop_and_order(1000)) + cout << "tests: OK\n"; + else + { + cout << "tests failed"; + } + while (true) + { + + try + { + string input; + std::getline(cin, input); + stringstream sin(input); + string command; + sin >> command; + if (command == "add") + { + string surname; + int time, sum; + sin >> surname >> time >> sum; + queue.insert(Customer(surname, time, sum)); + } + else if (command == "trigger") + { + int sum; + sin >> sum; + vector popped; + popped = queue.trigger(sum); + cout << "list of triggered:\n"; + for (int i = 0; i < popped.size(); ++i) + { + cout << popped[i].get_data() << '\n'; + } + } + else if (command == "pop") + { + Customer popped = queue.pop(); + if (popped.get_data() == "-1 -1 -1") + { + cout << "queue is empty\n"; + + } + else + { + cout << popped.get_data() << '\n'; + } + } + else + { + cout << "wrong command\n"; + } + } + catch (const std::exception&) + { + cout << "incorrect input\n"; + } + } +} \ No newline at end of file diff --git a/ProrityQueue_Zaverukha/tests/tests.h b/ProrityQueue_Zaverukha/tests/tests.h new file mode 100644 index 0000000..91504cb --- /dev/null +++ b/ProrityQueue_Zaverukha/tests/tests.h @@ -0,0 +1,57 @@ +#include "../headers/PriorityQueue.h" +#include + +namespace tests +{ + bool test_pop_and_order(int n) + { + PriorityQueue queue(n); + Customer prev = queue.pop(); + for (int i = 1; i < n && queue.is_empty(); ++i) + { + Customer cur = queue.pop(); + if (cur.time > prev.time) + { + return false; + } + } + return true; + } + + + + bool test_pop_untill_empty(PriorityQueue &queue) + { + Customer prev; + if (!queue.is_empty()) + { + prev = queue.pop(); + } + while (!queue.is_empty()) + { + Customer cur = queue.pop(); + if (cur.time > prev.time) + { + return false; + } + } + return true; + } + + bool test_insert() + { + PriorityQueue queue; + Customer customer_1("1", 1, 1), customer_2("1", 2, 1), customer_3("1", 3, 1), customer_4("1", 4, 1); + queue.insert(customer_1); + queue.insert(customer_2); + queue.insert(customer_3); + queue.insert(customer_4); + + if (!(queue.is_present(customer_1) && queue.is_present(customer_2) && queue.is_present(customer_3) && queue.is_present(customer_4))) + { + return false; + } + + return test_pop_untill_empty(queue); + } +} \ No newline at end of file