Commit f11148a0 authored by Бобриков Пётр Алексеевич's avatar Бобриков Пётр Алексеевич
Browse files

Создание макета класса и перегрузки оператора вывода на экран

parents
No related merge requests found
Showing with 146 additions and 0 deletions
+146 -0
main.cpp 0 → 100644
#include <iostream>
#include "matrix.cpp"
#include <iomanip>
#include <string>
using namespace std;
int main()
{
linalg::Matrix a(3, 3);
// cout << a;
return 0;
}
\ No newline at end of file
matrix.cpp 0 → 100644
#include <iostream>
#include "matrix.h"
#include <iomanip>
#include <string>
using namespace linalg;
class Matrix {
public:
double* m_ptr;
int m_rows, m_columns;
Matrix() {
this->m_rows = 0;
this->m_columns = 0;
this->m_ptr = new double[this->m_rows * this->m_columns];
}
Matrix(int rows, int columns = 1) {
this->m_rows = rows;
this->m_columns = columns;
this->m_ptr = new double[this->m_rows * this->m_columns];
for (int i = 0; i < m_columns * m_rows; i++) {
m_ptr[i] = i + 5;
}
}
Matrix(Matrix& orig) {
this->m_rows = 0;
this->m_columns = 0;
this->m_ptr = new double[this->m_rows * this->m_columns];
}
~Matrix() {
delete[] this->m_ptr;
}
double* ptr() {
return this->m_ptr;
}
int rows() {
return this->m_rows;
}
int columns() {
return this->m_columns;
}
bool empty() {
}
/**
, . , .
*/
double* reshape(int rows, int cols) {
return 0;
}
};
std::ostream& linalg::operator << (std::ostream& out, linalg::Matrix& M) {
int rows = M.m_rows;
int columns = M.m_columns;
double* ptr = M.m_ptr;
double max = 0;
int k = 0;
for (int i = 0; i < rows * columns; i++) {
if (ptr[i] < 0) {
ptr[i] = ptr[i] * 10;
}
if(abs(ptr[i]) > max){
max = ptr[i];
}
}
std::string s = std::to_string(max);
int len = size(s);
if (columns == 0 || rows == 0) {
out << "Matrix is empty" << std::endl;
}
else {
while(k < rows * columns)
for (int i = 0; i < rows; i++) {
out << "|";
for (int j = 0; j < columns; j++) {
out << std::setw(max) << ptr[k];
k++;
}
out << "|";
out << std::endl;
k++;
}
out << std::endl;
}
return out;
}
\ No newline at end of file
matrix.h 0 → 100644
#pragma once
#include <iostream>
#include <initializer_list>
namespace linalg {
class Matrix {
public:
double* m_ptr;
int m_rows, m_columns;
Matrix();
Matrix(int rows, int columns = 1);
Matrix(Matrix& orig);
//Matrix (Matrix& orig);
~Matrix();
double* ptr();
int rows();
int columns();
bool empty();
/**
, . , .
*/
double* reshape(int rows, int cols);
friend std::ostream& operator << (std::ostream& x, Matrix& m);
};
}
\ No newline at end of file
test.cpp 0 → 100644
test.h 0 → 100644
#pragma once
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment