Advertisement
Guest User

Untitled

a guest
May 26th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. class Matrix {
  6.   int lines;
  7.   int columns;
  8.   double *values;
  9.  
  10. public:
  11.   Matrix(int lin, int col): lines(lin), columns(col){
  12.     try {
  13.       values = new double[lines * columns];
  14.     }
  15.     catch (bad_alloc xa) {
  16.       exit(EXIT_FAILURE);
  17.     }
  18.   }
  19.   ~Matrix() { delete [] values; }
  20.   Matrix(const Matrix &M);
  21.  
  22.   Matrix transpose();
  23.   Matrix Dot(const Matrix &SecondMatrix);
  24.  
  25.   Matrix operator*(const Matrix &SecondMatrix) const;
  26.   Matrix operator+(const Matrix &SecondMatrix) const;
  27.   Matrix operator-(const Matrix &SecondMatrix) const;
  28.                                          
  29.   Matrix& operator*=(const Matrix &SecondMatrix);
  30.   Matrix& operator+=(const Matrix &SecondMatrix);
  31.   Matrix& operator-=(const Matrix &SecondMatrix);
  32.                                            
  33.   double& operator()(const int line, const int column);
  34.   double operator()(const int line, const int column) const;
  35.                                                  
  36.   friend ifstream& operator>>(ifstream &In, const Matrix &M);
  37.   friend ofstream& operator<<(ofstream &Out, const Matrix &M);
  38.                                                          
  39.   Matrix operator+(const double constant) const;
  40.   Matrix operator-(const double constant) const;
  41.   Matrix operator*(const double constant) const;
  42.                                      
  43.   Matrix& operator+=(const double constant);
  44.   Matrix& operator-=(const double constant);
  45.   Matrix& operator*=(const double constant);
  46. };
  47.    
  48. Matrix::Matrix(const Matrix &M)
  49. {
  50.   try {
  51.     values = new double[M.lines * M.columns];
  52.   }
  53.   catch (bad_alloc xa) {
  54.     exit(EXIT_FAILURE);
  55.   }
  56.   lines = M.lines;
  57.   columns = M.columns;
  58.                        
  59.   for (int i = 0; i < lines * columns; i++)
  60.     values[i] = M.values[i];
  61. }
  62.  
  63. Matrix Matrix::transpose()
  64. {
  65.   Matrix NewMatrix(columns, lines);
  66.                                    
  67.   for (int i = 0; i < NewMatrix.lines; i++)
  68.     for (int j = 0; j < NewMatrix.columns; j++)
  69.       NewMatrix.values[i * NewMatrix.columns + j] = values[j * columns + i];
  70.   return NewMatrix;
  71. }
  72.  
  73. Matrix Matrix::Dot(const Matrix &SecondMatrix)
  74. {
  75.   if (columns != SecondMatrix.lines)
  76.     return *this;
  77.  
  78.   Matrix NewMatrix(lines, SecondMatrix.columns);
  79.   for (int i = 0; i < lines; i++)
  80.     for (int j = 0; j < SecondMatrix.columns; j++)
  81.     {
  82.       NewMatrix.values[i * SecondMatrix.columns + j] = 0;
  83.       for (int k = 0; k < columns; k++)
  84.         NewMatrix.values[i * SecondMatrix.columns + j] += values[i * columns + k] * SecondMatrix.values[k * SecondMatrix.columns + j];
  85.     }
  86.   return NewMatrix;
  87. }
  88.  
  89. double Matrix::operator()(const int line, const int column) const
  90. {
  91.   return values[line * columns + column];
  92. }
  93.  
  94. double& Matrix::operator()(const int line, const int column)
  95. {
  96.   return values[line * columns + column];
  97. }
  98.  
  99. ifstream& operator>>(ifstream &In, const Matrix &M)
  100. {
  101.   for (int i = 0; i < M.lines * M.columns; i++)
  102.     In >> M.values[i];
  103.   return In;
  104. }
  105.  
  106. ofstream& operator<<(ofstream &Out, const Matrix &M)
  107. {
  108.   for (int i = 0; i < M.lines * M.columns; i++)
  109.   {
  110.     Out << M.values[i] << " ";
  111.     if (!((i + 1) % M.columns))
  112.       Out << "\n";
  113.   }
  114.   return Out;
  115. }
  116.  
  117. Matrix& Matrix::operator+=(const Matrix &SecondMatrix)
  118. {
  119.   if (columns != SecondMatrix.columns || lines != SecondMatrix.lines)
  120.     return *this;
  121.  
  122.   for (int i = 0; i < lines * columns; i++)
  123.     values[i] += SecondMatrix.values[i];
  124.  
  125.   return *this;
  126. }
  127.  
  128. Matrix& Matrix::operator-=(const Matrix &SecondMatrix)
  129. {
  130.   if (columns != SecondMatrix.columns || lines != SecondMatrix.lines)
  131.     return *this;
  132.  
  133.   for (int i = 0; i < lines * columns; i++)
  134.     values[i] -= SecondMatrix.values[i];
  135.  
  136.   return *this;
  137. }
  138.  
  139. Matrix& Matrix::operator*=(const Matrix &SecondMatrix)
  140. {
  141.   if (columns != SecondMatrix.columns || lines != SecondMatrix.lines)
  142.     return *this;
  143.  
  144.   for (int i = 0; i < lines * columns; i++)
  145.     values[i] *= SecondMatrix.values[i];
  146.  
  147.   return *this;
  148. }
  149.  
  150. Matrix Matrix::operator+(const Matrix &SecondMatrix) const
  151. {
  152.   Matrix NewMatrix(*this);
  153.   return NewMatrix += SecondMatrix;
  154. }
  155.  
  156. Matrix Matrix::operator-(const Matrix &SecondMatrix) const
  157. {
  158.   Matrix NewMatrix(*this);
  159.   return NewMatrix -= SecondMatrix;
  160. }
  161.  
  162. Matrix Matrix::operator*(const Matrix &SecondMatrix) const
  163. {
  164.   Matrix NewMatrix(*this);
  165.   return NewMatrix *= SecondMatrix;
  166. }
  167.  
  168.  
  169. Matrix& Matrix::operator+=(const double constant)
  170. {
  171.   for (int i = 0; i < lines * columns; i++)
  172.     values[i] += constant;
  173.   return *this;
  174. }
  175.  
  176. Matrix& Matrix::operator-=(const double constant)
  177. {
  178.   return *this += -constant;
  179. }
  180.  
  181. Matrix& Matrix::operator*=(const double constant)
  182. {
  183.   for (int i = 0; i < lines * columns; i++)
  184.     values[i] *= constant;
  185.   return *this;
  186. }
  187.  
  188. Matrix Matrix::operator+(const double constant) const
  189. {
  190.   Matrix NewMatrix(*this);
  191.   return NewMatrix += constant;
  192. }
  193.  
  194. Matrix Matrix::operator-(double constant) const
  195. {
  196.   Matrix NewMatrix(*this);
  197.   return NewMatrix -= constant;
  198. }
  199.  
  200. Matrix Matrix::operator*(double constant) const
  201. {
  202.   Matrix NewMatrix(*this);
  203.   return NewMatrix *= constant;
  204. }
  205.  
  206. int main()
  207. {
  208.   ofstream Out("MatrixOut.txt");
  209.   ifstream In("MatrixIn.txt");
  210.  
  211.   int lin, col;
  212.  
  213.   if (!Out)
  214.     return 1;
  215.   if (!In)
  216.     return 2;
  217.  
  218.   In >> lin >> col;
  219.  
  220.   Matrix MyMatrix(lin, col);
  221.   In >> MyMatrix;
  222.   Out << MyMatrix << MyMatrix.transpose() << '\n';
  223.   Matrix NewMatrix(MyMatrix.Dot(MyMatrix.transpose()));
  224.   Out << NewMatrix << NewMatrix * 2 + 1 << '\n';
  225.   Out << MyMatrix << MyMatrix * MyMatrix;
  226.  
  227.   Out.close();
  228.   In.close();
  229.   return 0;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement