lesson_3 #3

Open
alexander wants to merge 5 commits from lesson_3 into master
7 changed files with 359 additions and 0 deletions

54
lesson_3/exercise_1.cpp Normal file
View File

@ -0,0 +1,54 @@
/*
* exercise_1.cpp
*
* Created on: 29 сент. 2021 г.
* Author: alexander
*/
#include <iostream>
#include "exercise_1.hpp"
using namespace std;
void printList(list<float> &l)
{
cout << "{ ";
for (list<float>::const_iterator iter = l.begin(); iter != l.end(); ++iter)
{
cout << *iter << (next(iter, 1) == l.end() ? " " : ", ");
}
cout << '}' << endl;
}
void pushBackList(list<float> &l)
{
float sum = 0;
for (const float &value : l)
{
sum += value;
}
l.push_back(sum / l.size());
}
void exercise_1()
{
list<float> 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);
}

15
lesson_3/exercise_1.hpp Normal file
View File

@ -0,0 +1,15 @@
/*
* exercise_1.hpp
*
* Created on: 29 сент. 2021 г.
* Author: alexander
*/
#pragma once
#include <list>
void printList(std::list<float> &l);
void pushBackList(std::list<float> &l);
void exercise_1();

182
lesson_3/exercise_2.cpp Normal file
View File

@ -0,0 +1,182 @@
/*
* exercise_2.cpp
*
* Created on: 29 сент. 2021 г.
* Author: alexander
*/
#include <iostream>
#include <iomanip>
#include <random>
#include <math.h>
#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 *pointer) : current(pointer)
{
}
double& Matrix::Iterator::operator[](size_t 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);
}
/*
* Расчет определителя
*/
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)
{
double determinant = 0;
if (arr.size() == 1)
{
determinant = arr[0][0];
}
else if (arr.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 < arr.size(); ++i)
{
Matrix newArray = getModifiedMatrix(arr, i);
determinant += sign * arr[i][0] * calculateDeterminant(newArray);
sign = -sign;
}
}
return determinant;
}
void Matrix::fillRandom()
{
random_device rd;
mt19937 mersenne(rd()); // x32-битное число
uniform_real_distribution<double> 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));
}
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()
{
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"; // манипулятор для вывода без экспоненты
}
}

52
lesson_3/exercise_2.hpp Normal file
View File

@ -0,0 +1,52 @@
/*
* exercise_2.hpp
*
* Created on: 29 сент. 2021 г.
* Author: alexander
*/
#pragma once
#include <cstddef>
/*
* Т.к. определитель (детрминант) вычисляется только для квадратной матрицы,
* то объект класса инициализируется одним положительным целочисленным значением.
*/
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);
public:
Matrix(const int size);
void fillRandom(); // заполнить массив случайными значениями
void print() const; // распечатать массив
size_t size() const; // получить размер массива
double determinant() const; // получить определитель матрицы
class Iterator
{
private:
double *current;
public:
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();
};
void exercise_2();

26
lesson_3/exercise_3.cpp Normal file
View File

@ -0,0 +1,26 @@
/*
* exercise_3.cpp
*
* Created on: 30 сент. 2021 г.
* Author: alexander
*/
#include <iostream>
#include <iomanip>
#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;
}

10
lesson_3/exercise_3.hpp Normal file
View File

@ -0,0 +1,10 @@
/*
* exercise_3.hpp
*
* Created on: 30 сент. 2021 г.
* Author: alexander
*/
#pragma once
void exercise_3();

20
lesson_3/main.cpp Normal file
View File

@ -0,0 +1,20 @@
/*
* main.cpp
*
* Created on: 27 сент. 2021 г.
* Author: alexander
*/
#include "exercise_1.hpp"
#include "exercise_2.hpp"
#include "exercise_3.hpp"
int main()
{
exercise_1();
exercise_2();
exercise_3();
return 0;
}