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.
______________________________________________________
- Loading branch information
1 parent
a43c979
commit 1f9e303
Showing
1 changed file
with
37 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,48 @@ | ||
|
||
#pragma once | ||
#prag | ||
|
||
#include <iostream> | ||
#include <list> | ||
#include <cmath> | ||
|
||
template <typename T> | ||
class List { | ||
private: | ||
std::list<T> container; | ||
|
||
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; | ||
void fillElements(const std::initializer_list<T>& elements) { | ||
container.assign(elements); | ||
} | ||
|
||
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; | ||
void addElementAtPosition(const T& element, size_t position) { | ||
auto it = container.begin(); | ||
std::advance(it, position); | ||
container.insert(it, element); | ||
} | ||
|
||
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; }; | ||
void removeElementAtPosition(size_t position) { | ||
auto it = container.begin(); | ||
std::advance(it, position); | ||
container.erase(it); | ||
} | ||
|
||
private: | ||
int firstNumber = 0; | ||
double secondNumber = 0.0; | ||
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); | ||
} | ||
|
||
friend Pair operator+(const Pair& lhs, const Pair& rhs) | ||
{ | ||
return Pair(lhs.firstNumber + rhs.firstNumber, lhs.secondNumber + rhs.secondNumber); | ||
void printElements() const { | ||
for (const auto& element : container) { | ||
std::cout << element << " "; | ||
} | ||
std::cout << std::endl; | ||
} | ||
}; | ||
}; |