This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
644c265
commit cbe791a
Showing
8 changed files
with
442 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
``` | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
||
}; |
Oops, something went wrong.