Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zaverukha Bogdan. Group II. Project C++ (PriorityQueue) Project 6 #14

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions ProrityQueue_Zaverukha/headers/DataStruct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once
#include <vector>
#include <array>
#include <string>

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<Customer> 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<Customer> trigger(int sum) = 0;
virtual Customer pop() = 0;
virtual bool is_present(Customer customer) = 0;
virtual bool is_empty() = 0;
virtual ~DataStruct() {};
};

44 changes: 44 additions & 0 deletions ProrityQueue_Zaverukha/headers/PriorityQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once
#include <vector>
#include <array>
#include <string>
#include <set>
#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<Customer> customers;
public:

PriorityQueue(vector<Customer> 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<Customer> trigger(int sum);
Customer pop();
bool is_present(Customer customer);
bool is_empty();



~PriorityQueue();
};

167 changes: 167 additions & 0 deletions ProrityQueue_Zaverukha/src/PriorityQueue.cpp
Original file line number Diff line number Diff line change
@@ -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<Customer> 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<Customer>::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<Customer> new_queue;
for (set<Customer>::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<Customer> PriorityQueue::trigger(int sum)
{
vector<Customer> 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()
{
}
72 changes: 72 additions & 0 deletions ProrityQueue_Zaverukha/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "../headers/PriorityQueue.h"
#include <iostream>
#include <string>
#include <sstream>
#include "../tests/tests.h"

using namespace std;

int main()
{
string c[2] = { "add", "pop" };
vector<string> 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<Customer> 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";
}
}
}
57 changes: 57 additions & 0 deletions ProrityQueue_Zaverukha/tests/tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "../headers/PriorityQueue.h"
#include <algorithm>

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);
}
}