Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
[task_05] Add task_05 (#95)
Browse files Browse the repository at this point in the history
* first commit

* commit

* commit

* commit

* commit

* commit

* commit

* commit

* :(

* :((

* :(((

* :((((

* :)

* :D

* :(

* ._.

* Smell Fixes

* smell fix

* last fix(i hope)

* commit

* ______________________________________________________

* =0=1=2=3=4=5=6=7=8=9=

* backup

* _
  • Loading branch information
DimaYanuschik authored Jun 20, 2024
1 parent 644c265 commit cbe791a
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 2 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
| 9 | [Хведорец Виктор Сергеевич](https://github.com/ViktorKhvedorets)|[PO-11-220224](./trunk/po0_220224/) |||||| |9|4|
| 10 | [Хлебус Игнат Витальевич](https://github.com/ignat121235)| [PO-11-220225](./trunk/po0_220220/)|||||| | 7|4 |
| 11 | [Юрашевич Виктор Сергеевич](https://github.com/VictorYrman)| [PO-11-220226](./trunk/po0_220226/) |||||| |10|4|
| 12 | [Янущик Дмитрий Дмитриевич](https://github.com/DimaYanuschik)|[PO-11-220227](./trunk/po0_220227/)||||| | |9 |4|

| 12 | [Янущик Дмитрий Дмитриевич](https://github.com/DimaYanuschik)|[PO-11-220227](./trunk/po0_220227/)|||||| |9 |4|
121 changes: 121 additions & 0 deletions trunk/po0_220227/task_05/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@

# Ëàáîðàòîðíàÿ ðàáîòà ¹5 #

## Ïîñëåäîâàòåëüíûå êîíòåéíåðû áèáëèîòåêè STL ##

## Âàðèàíò 12 ##


## Öåëü ðàáîòû ##
Ñîçäàíèå êîíñîëüíîãî ïðèëîæåíèÿ, ñîñòîÿùåãî èç íåñêîëüêèõ ôàéëîâ â ñèñòåìå ïðîãðàììèðîâàíèÿ Visual Studio. Èñïîëüçîâàíèå ïîñëåäîâàòåëüíûõ êîíòåéíåðîâ áèáëèîòåêè STL â ÎÎ ïðîãðàììå.

### Çàäàíèÿ äëÿ ïåðâîé è âòîðîé, òðåòüåé çàäà÷è ###
```c++
template <class T>
T sum_elements(const std::vector<T>& container) {
return std::accumulate(container.begin(), container.end(), T());
}

template <typename T>
T findMinFirstElement(const std::list<T>& container) {
if (container.empty()) {
return T{};
}

auto minIt = std::min_element(container.begin(), container.end(),
[](const T& a, const T& b) { return a.GetFirstNumber() < b.GetFirstNumber(); });
return *minIt;
}

void findAndAddAverage(size_t position) {
if (container.empty()) {
return;
}

T sum = 0;
for (const auto& element : container) {
sum += element;
}
T average = sum / static_cast<T>(container.size());
addElementAtPosition(average, position);
}

### Çàäàíèÿ äëÿ ÷åòâåðòîé è ïÿòîé çàäà÷è ###

```c++
template <typename T>
class PriorityQueue {
private:
std::priority_queue<T> queue;

public:
void fillElements(const std::initializer_list<T>& elements) {
for (const auto& element : elements) {
queue.push(element);
}
}

void addElement(const T& element) {
queue.push(element);
}

void removeElement() {
if (!queue.empty()) {
queue.pop();
}
}

void removeElementsInRange(int minFirstNumber, int maxFirstNumber) {
std::set<T> elementsToRemove;
while (!queue.empty()) {
const T& top = queue.top();
if (top.GetFirstNumber() >= minFirstNumber && top.GetFirstNumber() <= maxFirstNumber) {
elementsToRemove.insert(top);
}
queue.pop();
}

for (const auto& element : elementsToRemove) {
queue.push(element);
}
}

void printElements() const {
std::priority_queue<T> temp = queue;
while (!temp.empty()) {
std::cout << temp.top();
temp.pop();
}
}

T getAverage() const {
T sum = 0;
size_t count = 0;
std::priority_queue<T> temp = queue;
while (!temp.empty()) {
sum += temp.top();
temp.pop();
count++;
}
return sum / static_cast<T>(count);
}

void subtractAverage() {
T average = getAverage();
std::priority_queue<T> temp;
while (!queue.empty()) {
T element = queue.top();
queue.pop();
element -= average;
temp.push(element);
}
queue = std::move(temp);
}
};
```






16 changes: 16 additions & 0 deletions trunk/po0_220227/task_05/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.15.0)

project(LAB_05_220227)

set(CMAKE_CXX_STANDARD 20)

set(CMAKE_CXX_STANDARD_REQUIRED True)

set(SOURCE_FILES main.cpp Pair.cpp)

set(HEADER_FILES Pair.h List.h PQueue.h)

set(ALL_SOURCES ${HEADER_FILSE}
${SOURCE_FILES})

add_executable(${PROJECT_NAME} ${ALL_SOURCES})
48 changes: 48 additions & 0 deletions trunk/po0_220227/task_05/src/List.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <iostream>
#include <list>
#include <cmath>

template <typename T>
class List {
private:
std::list<T> container;

public:
void fillElements(const std::initializer_list<T>& elements) {
container.assign(elements);
}

void addElementAtPosition(const T& element, size_t position) {
auto it = container.begin();
std::advance(it, position);
container.insert(it, element);
}

void removeElementAtPosition(size_t position) {
auto it = container.begin();
std::advance(it, position);
container.erase(it);
}

void findAndAddAverage(size_t position) {
if (container.empty()) {
return;
}

T sum = 0;
for (const auto& element : container) {
sum += element;
}
T average = sum / static_cast<T>(container.size());
addElementAtPosition(average, position);
}

void printElements() const {
for (const auto& element : container) {
std::cout << element << " ";
}
std::cout << std::endl;
}
};
74 changes: 74 additions & 0 deletions trunk/po0_220227/task_05/src/PQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#pragma once

#include <queue>
#include <functional>

class QList {
public:
explicit QList(const std::function<bool(int, int)>& comp = std::less<int>())
: comparator(comp), pq(comp) {}

void fillElements(const std::vector<int>& elements) {
for (int element : elements) {
pq.push(element);
}
}

void addElement(int element) {
pq.push(element);
}

void removeElement() {
if (!pq.empty()) {
pq.pop();
}
}

void removeInRange(int minElement, int maxElement) {
std::priority_queue<int, std::vector<int>, std::function<bool(int, int)>> temp(comparator);
while (!pq.empty()) {
int element = pq.top();
pq.pop();
if (element < minElement || element > maxElement) {
temp.push(element);
}
}
pq = std::move(temp);
}

void printElements() const {
std::priority_queue<int, std::vector<int>, std::function<bool(int, int)>> temp(pq);
while (!temp.empty()) {
std::cout << temp.top() << " ";
temp.pop();
}
std::cout << std::endl;
}

int getAverage() const {
int sum = 0;
size_t count = 0;
std::priority_queue<int, std::vector<int>, std::function<bool(int, int)>> temp(pq);
while (!temp.empty()) {
sum += temp.top();
count++;
temp.pop();
}
return sum / static_cast<int>(count);
}

void subtractAverage() {
int average = getAverage();
std::priority_queue<int, std::vector<int>, std::function<bool(int, int)>> temp(comparator);
while (!pq.empty()) {
int element = pq.top();
pq.pop();
element -= average;
temp.push(element);
}
pq = std::move(temp);
}
private:
std::function<bool(int, int)> comparator = std::less<int>();
std::priority_queue<int, std::vector<int>, std::function<bool(int, int)>> pq;
};
6 changes: 6 additions & 0 deletions trunk/po0_220227/task_05/src/Pair.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "Pair.h"

Pair::Pair(const int fNum, const double sNum)
: firstNumber(fNum), secondNumber(sNum)
{
}
46 changes: 46 additions & 0 deletions trunk/po0_220227/task_05/src/Pair.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <iostream>

class Pair
{
public:
Pair() = default;
Pair(const int firstNumber, const double secondNumber);
Pair(const Pair& other) = default;
~Pair() = default;

Pair& operator=(const Pair& a) = default;
bool operator==(const Pair& a) const = default;

friend std::ostream& operator<<(std::ostream& out, const Pair& a)
{
out << a.GetFirstNumber() << ":" << a.GetSecondNumber() << std::endl;
return out;
}

friend std::istream& operator>>(std::istream& in, Pair& a)
{
std::cout << "Enter first number(int)" << std::endl;
in >> a.firstNumber;
std::cout << "Enter second number(double)" << std::endl;
in >> a.secondNumber;
return in;
}

friend Pair operator+(const Pair& lhs, const Pair& rhs) {
return Pair(lhs.firstNumber + rhs.firstNumber, lhs.secondNumber + rhs.secondNumber);
}

inline void SetFirstNumber(const int fNum) { firstNumber = fNum; };
inline void SetSecondNumber(const double sNum) { secondNumber = sNum; };

inline int GetFirstNumber() const { return firstNumber; };
inline double GetSecondNumber() const { return secondNumber; };

private:
int firstNumber = 0;
double secondNumber = 0.0;


};
Loading

0 comments on commit cbe791a

Please sign in to comment.