From f5438af0ec6cc71ee50cd830e45208bfcd4d14ae Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Tue, 28 Sep 2021 23:57:49 +0300 Subject: [PATCH 1/5] lesson_3 --- lesson_3/main.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 lesson_3/main.cpp diff --git a/lesson_3/main.cpp b/lesson_3/main.cpp new file mode 100644 index 0000000..ba4325c --- /dev/null +++ b/lesson_3/main.cpp @@ -0,0 +1,18 @@ +/* + * main.cpp + * + * Created on: 27 сент. 2021 г. + * Author: alexander + */ +#include +#include + +using namespace std; + +int main() +{ + + + return 0; +} + -- 2.40.1 From 1bc734e2206c0869ec3a3f5cfcdb9c86d244c277 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Thu, 30 Sep 2021 03:46:47 +0300 Subject: [PATCH 2/5] exercise 1,2 --- lesson_3/exercise_1.cpp | 54 ++++++++++++++ lesson_3/exercise_1.hpp | 15 ++++ lesson_3/exercise_2.cpp | 151 ++++++++++++++++++++++++++++++++++++++++ lesson_3/exercise_2.hpp | 47 +++++++++++++ lesson_3/main.cpp | 8 +-- 5 files changed, 271 insertions(+), 4 deletions(-) create mode 100644 lesson_3/exercise_1.cpp create mode 100644 lesson_3/exercise_1.hpp create mode 100644 lesson_3/exercise_2.cpp create mode 100644 lesson_3/exercise_2.hpp diff --git a/lesson_3/exercise_1.cpp b/lesson_3/exercise_1.cpp new file mode 100644 index 0000000..695413f --- /dev/null +++ b/lesson_3/exercise_1.cpp @@ -0,0 +1,54 @@ +/* + * exercise_1.cpp + * + * Created on: 29 сент. 2021 г. + * Author: alexander + */ + +#include +#include "exercise_1.hpp" + +using namespace std; + +void printList(list &l) +{ + cout << "{ "; + + for (list::const_iterator iter = l.begin(); iter != l.end(); ++iter) + { + cout << *iter << (next(iter, 1) == l.end() ? " " : ", "); + } + + cout << '}' << endl; +} + +void pushBackList(list &l) +{ + float sum = 0; + + for (const float &value : l) + { + sum += value; + } + + l.push_back(sum / l.size()); +} + +void exercise_1() +{ + list l { 54.46, 43.32, 37.89, 19.99 }; + size_t countCycle = 15; + + for (size_t i = 0; i < countCycle; ++i) + { + // Печать листа через каждых 5-и добавлений элементов + if (i % 5 == 0) + { + printList(l); + } + + pushBackList(l); + } + + printList(l); +} diff --git a/lesson_3/exercise_1.hpp b/lesson_3/exercise_1.hpp new file mode 100644 index 0000000..2eff723 --- /dev/null +++ b/lesson_3/exercise_1.hpp @@ -0,0 +1,15 @@ +/* + * exercise_1.hpp + * + * Created on: 29 сент. 2021 г. + * Author: alexander + */ + +#pragma once + +#include + +void printList(std::list &l); +void pushBackList(std::list &l); + +void exercise_1(); diff --git a/lesson_3/exercise_2.cpp b/lesson_3/exercise_2.cpp new file mode 100644 index 0000000..ec7837e --- /dev/null +++ b/lesson_3/exercise_2.cpp @@ -0,0 +1,151 @@ +/* + * exercise_2.cpp + * + * Created on: 29 сент. 2021 г. + * Author: alexander + */ + +#include +#include +#include +#include +#include "exercise_2.hpp" + +using namespace std; + +Matrix::Matrix(const int size) : matrixSize(size), matrixCapacity(size * size) +{ + array = new double[matrixCapacity]; +} + +/* + * Перегрузка операторов [][] + */ +Matrix::Iterator::Iterator(double *row) : currentRow(row) +{ +} + +double& Matrix::Iterator::operator[](size_t i) +{ + return *(currentRow + i); +} + +Matrix::Iterator Matrix::operator[](size_t i) +{ + return Iterator(array + i * matrixSize); +} +/* + * Расчет определителя + */ +Matrix Matrix::getModifiedMatrix(Matrix &arr, const size_t item) +{ + Matrix newArray(arr.size() - 1); + size_t currentRow = 0; + + for (size_t i = 0; i < arr.size(); ++i) + { + if (item == i) + { + currentRow = i; + continue; + } + + for (size_t j = 0; j < newArray.size(); ++j) + { + newArray[currentRow][j] = arr[i][j + 1]; + } + + ++currentRow; + } + + return newArray; +} + +double Matrix::calculateDeterminant(Matrix &arr, const size_t size) +{ + double determinant = 0; + + if (size == 1) + { + determinant = arr[0][0]; + } + else if (size == 2) + { + determinant = arr[0][0] * arr[1][1] + arr[1][0] * arr[0][1]; + } + else + { + int sign = 1; + + for (size_t i = 0; i < size; ++i) + { + Matrix newArray = getModifiedMatrix(arr, i); + determinant += sign * arr[i][0] * calculateDeterminant(newArray, size - 1); + sign = -sign; + } + } + + return determinant; +} + +void Matrix::fillRandom() +{ + random_device rd; + mt19937 mersenne(rd()); // x32-битное число + uniform_real_distribution urd(-10.0, 10.0); // диапазон вещественных чисел от -10 до 10 + + for (size_t i = 0; i < matrixCapacity; ++i) + { + *(array + i) = round(urd(mersenne) * 10) / 10; + } + + matrixDeterminant = calculateDeterminant((*this), matrixSize); +} + +void Matrix::print() const +{ + cout << '['; + + for (size_t i = 0; i < matrixCapacity;) + { + cout << setw(5) << *(array + i++); + + if (!(i % matrixSize)) + { + cout << ']' << endl; + + if (i < matrixCapacity) + { + cout << '['; + } + } + else + { + cout << ','; + } + } +} + +size_t Matrix::size() const +{ + return matrixSize; +} + +double Matrix::determinant() const +{ + return matrixDeterminant; +} + +Matrix::~Matrix() +{ + delete[] array; +} + +void exercise_2() +{ + Matrix m(7); + m.fillRandom(); + m.print(); + cout << "Определитель матрицы: " << setprecision(10) << m.determinant() << endl; // манипулятор для вывода без экспоненты +} + diff --git a/lesson_3/exercise_2.hpp b/lesson_3/exercise_2.hpp new file mode 100644 index 0000000..70b4b0b --- /dev/null +++ b/lesson_3/exercise_2.hpp @@ -0,0 +1,47 @@ +/* + * exercise_2.hpp + * + * Created on: 29 сент. 2021 г. + * Author: alexander + */ + +#pragma once + +#include + +/* + * Т.к. определитель (детрминант) вычисляется только для квадратной матрицы, + * то класс инициализируется одним положительным целочисленным значением. + */ +class Matrix +{ +private: + const size_t matrixSize; + const size_t matrixCapacity; + double *array; + double matrixDeterminant { 0.0 }; + + Matrix getModifiedMatrix(Matrix &arr, const size_t item); + double calculateDeterminant(Matrix &arr, const size_t size); +public: + Matrix(const int size); + void fillRandom(); // заполнить массив случайными значениями + void print() const; // распечатать массив + size_t size() const; // получить размер массива + double determinant() const; // получить определитель матрицы + + class Iterator + { + private: + double *currentRow; + public: + Iterator(double *row); + double& operator[](size_t i); + }; + + Iterator operator[](size_t i); + + ~Matrix(); +}; + +void exercise_2(); diff --git a/lesson_3/main.cpp b/lesson_3/main.cpp index ba4325c..c60dec9 100644 --- a/lesson_3/main.cpp +++ b/lesson_3/main.cpp @@ -4,14 +4,14 @@ * Created on: 27 сент. 2021 г. * Author: alexander */ -#include -#include -using namespace std; +#include "exercise_1.hpp" +#include "exercise_2.hpp" int main() { - +// exercise_1(); + exercise_2(); return 0; } -- 2.40.1 From a6b44883a81c2d6e82665268126f98ec1c339673 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Thu, 30 Sep 2021 04:08:40 +0300 Subject: [PATCH 3/5] exercise 1,2 corrected --- lesson_3/exercise_2.cpp | 26 +++++++++++++++----------- lesson_3/exercise_2.hpp | 4 ++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lesson_3/exercise_2.cpp b/lesson_3/exercise_2.cpp index ec7837e..4058438 100644 --- a/lesson_3/exercise_2.cpp +++ b/lesson_3/exercise_2.cpp @@ -61,15 +61,15 @@ Matrix Matrix::getModifiedMatrix(Matrix &arr, const size_t item) return newArray; } -double Matrix::calculateDeterminant(Matrix &arr, const size_t size) +double Matrix::calculateDeterminant(Matrix &arr) { double determinant = 0; - if (size == 1) + if (arr.size() == 1) { determinant = arr[0][0]; } - else if (size == 2) + else if (arr.size() == 2) { determinant = arr[0][0] * arr[1][1] + arr[1][0] * arr[0][1]; } @@ -77,10 +77,10 @@ double Matrix::calculateDeterminant(Matrix &arr, const size_t size) { int sign = 1; - for (size_t i = 0; i < size; ++i) + for (size_t i = 0; i < arr.size(); ++i) { Matrix newArray = getModifiedMatrix(arr, i); - determinant += sign * arr[i][0] * calculateDeterminant(newArray, size - 1); + determinant += sign * arr[i][0] * calculateDeterminant(newArray); sign = -sign; } } @@ -99,7 +99,7 @@ void Matrix::fillRandom() *(array + i) = round(urd(mersenne) * 10) / 10; } - matrixDeterminant = calculateDeterminant((*this), matrixSize); + matrixDeterminant = calculateDeterminant((*this)); } void Matrix::print() const @@ -112,7 +112,7 @@ void Matrix::print() const if (!(i % matrixSize)) { - cout << ']' << endl; + cout << " ]" << endl; if (i < matrixCapacity) { @@ -143,9 +143,13 @@ Matrix::~Matrix() void exercise_2() { - Matrix m(7); - m.fillRandom(); - m.print(); - cout << "Определитель матрицы: " << setprecision(10) << m.determinant() << endl; // манипулятор для вывода без экспоненты + for (size_t i = 1; i < 8; ++i) + { + cout << "Матрица " << i << 'x' << i << ':' << endl; + Matrix m(i); + m.fillRandom(); + m.print(); + cout << "Определитель матрицы: " << setprecision(10) << m.determinant() << "\n\n"; // манипулятор для вывода без экспоненты + } } diff --git a/lesson_3/exercise_2.hpp b/lesson_3/exercise_2.hpp index 70b4b0b..62a4cc9 100644 --- a/lesson_3/exercise_2.hpp +++ b/lesson_3/exercise_2.hpp @@ -11,7 +11,7 @@ /* * Т.к. определитель (детрминант) вычисляется только для квадратной матрицы, - * то класс инициализируется одним положительным целочисленным значением. + * то объект класса инициализируется одним положительным целочисленным значением. */ class Matrix { @@ -22,7 +22,7 @@ private: double matrixDeterminant { 0.0 }; Matrix getModifiedMatrix(Matrix &arr, const size_t item); - double calculateDeterminant(Matrix &arr, const size_t size); + double calculateDeterminant(Matrix &arr); public: Matrix(const int size); void fillRandom(); // заполнить массив случайными значениями -- 2.40.1 From 9b54c0446d2f8169284cb7ea8c01df5e16cc4963 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Thu, 30 Sep 2021 20:08:09 +0300 Subject: [PATCH 4/5] exercises 1,2,3 --- lesson_3/exercise_2.cpp | 33 ++++++++++++++++++++++++++++++--- lesson_3/exercise_2.hpp | 9 +++++++-- lesson_3/exercise_3.cpp | 26 ++++++++++++++++++++++++++ lesson_3/exercise_3.hpp | 10 ++++++++++ lesson_3/main.cpp | 4 +++- 5 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 lesson_3/exercise_3.cpp create mode 100644 lesson_3/exercise_3.hpp diff --git a/lesson_3/exercise_2.cpp b/lesson_3/exercise_2.cpp index 4058438..1962836 100644 --- a/lesson_3/exercise_2.cpp +++ b/lesson_3/exercise_2.cpp @@ -19,21 +19,48 @@ Matrix::Matrix(const int size) : matrixSize(size), matrixCapacity(size * size) } /* - * Перегрузка операторов [][] + * Перегрузка операторов итератора */ -Matrix::Iterator::Iterator(double *row) : currentRow(row) +Matrix::Iterator::Iterator(double *pointer) : current(pointer) { } double& Matrix::Iterator::operator[](size_t i) { - return *(currentRow + i); + return *(current + i); +} + +Matrix::Iterator& Matrix::Iterator::operator ++() +{ + ++current; + return *this; +} + +bool Matrix::Iterator::operator !=(const Iterator &iterator) +{ + return current != iterator.current; +} + +double& Matrix::Iterator::operator *() +{ + return *current; } Matrix::Iterator Matrix::operator[](size_t i) { return Iterator(array + i * matrixSize); } + +Matrix::Iterator Matrix::begin() +{ + return Iterator(array); +} + +Matrix::Iterator Matrix::end() +{ + return Iterator(array + matrixCapacity); +} + /* * Расчет определителя */ diff --git a/lesson_3/exercise_2.hpp b/lesson_3/exercise_2.hpp index 62a4cc9..b7c2560 100644 --- a/lesson_3/exercise_2.hpp +++ b/lesson_3/exercise_2.hpp @@ -33,13 +33,18 @@ public: class Iterator { private: - double *currentRow; + double *current; public: - Iterator(double *row); + Iterator(double *pointer); double& operator[](size_t i); + Iterator& operator ++(); + bool operator !=(const Iterator &iterator); + double& operator *(); }; Iterator operator[](size_t i); + Iterator begin(); + Iterator end(); ~Matrix(); }; diff --git a/lesson_3/exercise_3.cpp b/lesson_3/exercise_3.cpp new file mode 100644 index 0000000..cd7c169 --- /dev/null +++ b/lesson_3/exercise_3.cpp @@ -0,0 +1,26 @@ +/* + * exercise_3.cpp + * + * Created on: 30 сент. 2021 г. + * Author: alexander + */ + +#include +#include +#include "exercise_3.hpp" +#include "exercise_2.hpp" + +using namespace std; + +void exercise_3() +{ + Matrix m(4); + m.fillRandom(); + + for (const double &i : m) + { + cout << setw(5) << i; + } + + cout << endl; +} diff --git a/lesson_3/exercise_3.hpp b/lesson_3/exercise_3.hpp new file mode 100644 index 0000000..b0b26bb --- /dev/null +++ b/lesson_3/exercise_3.hpp @@ -0,0 +1,10 @@ +/* + * exercise_3.hpp + * + * Created on: 30 сент. 2021 г. + * Author: alexander + */ + +#pragma once + +void exercise_3(); diff --git a/lesson_3/main.cpp b/lesson_3/main.cpp index c60dec9..c792793 100644 --- a/lesson_3/main.cpp +++ b/lesson_3/main.cpp @@ -7,11 +7,13 @@ #include "exercise_1.hpp" #include "exercise_2.hpp" +#include "exercise_3.hpp" int main() { -// exercise_1(); + exercise_1(); exercise_2(); + exercise_3(); return 0; } -- 2.40.1 From cbcf15e5ca7beade3365d1ca3d8a039702e1a5f4 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Fri, 1 Oct 2021 19:22:44 +0300 Subject: [PATCH 5/5] replaced addition to subtraction in a 2x2 matrix --- lesson_3/exercise_2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lesson_3/exercise_2.cpp b/lesson_3/exercise_2.cpp index 1962836..74ed2b6 100644 --- a/lesson_3/exercise_2.cpp +++ b/lesson_3/exercise_2.cpp @@ -98,7 +98,7 @@ double Matrix::calculateDeterminant(Matrix &arr) } else if (arr.size() == 2) { - determinant = arr[0][0] * arr[1][1] + arr[1][0] * arr[0][1]; + determinant = arr[0][0] * arr[1][1] - arr[1][0] * arr[0][1]; } else { -- 2.40.1