Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- class Matrix {
- int lines;
- int columns;
- double *values;
- public:
- Matrix(int lin, int col): lines(lin), columns(col){
- try {
- values = new double[lines * columns];
- }
- catch (bad_alloc xa) {
- exit(EXIT_FAILURE);
- }
- }
- ~Matrix() { delete [] values; }
- Matrix(const Matrix &M);
- Matrix transpose();
- Matrix Dot(const Matrix &SecondMatrix);
- Matrix operator*(const Matrix &SecondMatrix) const;
- Matrix operator+(const Matrix &SecondMatrix) const;
- Matrix operator-(const Matrix &SecondMatrix) const;
- Matrix& operator*=(const Matrix &SecondMatrix);
- Matrix& operator+=(const Matrix &SecondMatrix);
- Matrix& operator-=(const Matrix &SecondMatrix);
- double& operator()(const int line, const int column);
- double operator()(const int line, const int column) const;
- friend ifstream& operator>>(ifstream &In, const Matrix &M);
- friend ofstream& operator<<(ofstream &Out, const Matrix &M);
- Matrix operator+(const double constant) const;
- Matrix operator-(const double constant) const;
- Matrix operator*(const double constant) const;
- Matrix& operator+=(const double constant);
- Matrix& operator-=(const double constant);
- Matrix& operator*=(const double constant);
- };
- Matrix::Matrix(const Matrix &M)
- {
- try {
- values = new double[M.lines * M.columns];
- }
- catch (bad_alloc xa) {
- exit(EXIT_FAILURE);
- }
- lines = M.lines;
- columns = M.columns;
- for (int i = 0; i < lines * columns; i++)
- values[i] = M.values[i];
- }
- Matrix Matrix::transpose()
- {
- Matrix NewMatrix(columns, lines);
- for (int i = 0; i < NewMatrix.lines; i++)
- for (int j = 0; j < NewMatrix.columns; j++)
- NewMatrix.values[i * NewMatrix.columns + j] = values[j * columns + i];
- return NewMatrix;
- }
- Matrix Matrix::Dot(const Matrix &SecondMatrix)
- {
- if (columns != SecondMatrix.lines)
- return *this;
- Matrix NewMatrix(lines, SecondMatrix.columns);
- for (int i = 0; i < lines; i++)
- for (int j = 0; j < SecondMatrix.columns; j++)
- {
- NewMatrix.values[i * SecondMatrix.columns + j] = 0;
- for (int k = 0; k < columns; k++)
- NewMatrix.values[i * SecondMatrix.columns + j] += values[i * columns + k] * SecondMatrix.values[k * SecondMatrix.columns + j];
- }
- return NewMatrix;
- }
- double Matrix::operator()(const int line, const int column) const
- {
- return values[line * columns + column];
- }
- double& Matrix::operator()(const int line, const int column)
- {
- return values[line * columns + column];
- }
- ifstream& operator>>(ifstream &In, const Matrix &M)
- {
- for (int i = 0; i < M.lines * M.columns; i++)
- In >> M.values[i];
- return In;
- }
- ofstream& operator<<(ofstream &Out, const Matrix &M)
- {
- for (int i = 0; i < M.lines * M.columns; i++)
- {
- Out << M.values[i] << " ";
- if (!((i + 1) % M.columns))
- Out << "\n";
- }
- return Out;
- }
- Matrix& Matrix::operator+=(const Matrix &SecondMatrix)
- {
- if (columns != SecondMatrix.columns || lines != SecondMatrix.lines)
- return *this;
- for (int i = 0; i < lines * columns; i++)
- values[i] += SecondMatrix.values[i];
- return *this;
- }
- Matrix& Matrix::operator-=(const Matrix &SecondMatrix)
- {
- if (columns != SecondMatrix.columns || lines != SecondMatrix.lines)
- return *this;
- for (int i = 0; i < lines * columns; i++)
- values[i] -= SecondMatrix.values[i];
- return *this;
- }
- Matrix& Matrix::operator*=(const Matrix &SecondMatrix)
- {
- if (columns != SecondMatrix.columns || lines != SecondMatrix.lines)
- return *this;
- for (int i = 0; i < lines * columns; i++)
- values[i] *= SecondMatrix.values[i];
- return *this;
- }
- Matrix Matrix::operator+(const Matrix &SecondMatrix) const
- {
- Matrix NewMatrix(*this);
- return NewMatrix += SecondMatrix;
- }
- Matrix Matrix::operator-(const Matrix &SecondMatrix) const
- {
- Matrix NewMatrix(*this);
- return NewMatrix -= SecondMatrix;
- }
- Matrix Matrix::operator*(const Matrix &SecondMatrix) const
- {
- Matrix NewMatrix(*this);
- return NewMatrix *= SecondMatrix;
- }
- Matrix& Matrix::operator+=(const double constant)
- {
- for (int i = 0; i < lines * columns; i++)
- values[i] += constant;
- return *this;
- }
- Matrix& Matrix::operator-=(const double constant)
- {
- return *this += -constant;
- }
- Matrix& Matrix::operator*=(const double constant)
- {
- for (int i = 0; i < lines * columns; i++)
- values[i] *= constant;
- return *this;
- }
- Matrix Matrix::operator+(const double constant) const
- {
- Matrix NewMatrix(*this);
- return NewMatrix += constant;
- }
- Matrix Matrix::operator-(double constant) const
- {
- Matrix NewMatrix(*this);
- return NewMatrix -= constant;
- }
- Matrix Matrix::operator*(double constant) const
- {
- Matrix NewMatrix(*this);
- return NewMatrix *= constant;
- }
- int main()
- {
- ofstream Out("MatrixOut.txt");
- ifstream In("MatrixIn.txt");
- int lin, col;
- if (!Out)
- return 1;
- if (!In)
- return 2;
- In >> lin >> col;
- Matrix MyMatrix(lin, col);
- In >> MyMatrix;
- Out << MyMatrix << MyMatrix.transpose() << '\n';
- Matrix NewMatrix(MyMatrix.Dot(MyMatrix.transpose()));
- Out << NewMatrix << NewMatrix * 2 + 1 << '\n';
- Out << MyMatrix << MyMatrix * MyMatrix;
- Out.close();
- In.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement