fork download
  1. // #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. //#include <cstdlib>
  5. #include <ctime>
  6. //#include <exception>
  7. #include <stdexcept>
  8. //#include <matrix.h>
  9.  
  10. using namespace std;
  11.  
  12. class Array
  13. {
  14. protected:
  15. int** _data;
  16. int _cols, _rows;
  17. public:
  18. Array(void) : _data(0), _rows(0),_cols(0) {};
  19. Array(int rows, int cols, bool fill=false, int filler=0)
  20. : _rows(rows), _cols(cols), _data(0)
  21. {
  22. _data = new int* [_rows];
  23. for (int i=0; i<_rows; i++)
  24. {
  25. _data[i] = new int[_cols];
  26. if (fill)
  27. for (int j=0; j<_cols; j++)
  28. _data[i][j] = filler;
  29. }
  30. }
  31.  
  32. Array(const Array& other) :
  33. _cols(other.cols()),
  34. _rows(other.rows())
  35. {
  36. _data = new int* [_rows];
  37. for (int i=0; i<_rows; i++)
  38. {
  39. _data[i] = new int[_cols];
  40. for (int j=0; j<_cols; j++)
  41. _data[i][j] = other._data[i][j];
  42. }
  43. }
  44.  
  45. // транспонируем матрицу
  46. const Array transpon() const
  47. {
  48. Array C(_cols, _rows);
  49. for (int i=0; i<_cols; i++)
  50. for (int j=0; j<_rows; j++)
  51. C[i][j] = _data[j][i];
  52. return C;
  53.  
  54. }
  55. // сложение двух матриц
  56. const Array operator+(const Array& other) const
  57. {
  58. if ((_cols != other.cols()) || (_rows != other.rows()))
  59. throw out_of_range("матрицы не имеют");
  60. Array C(_rows, _cols);
  61. for (int i=0; i<C.rows(); i++)
  62. for (int j=0; j<C.cols(); j++)
  63. C._data[i][j] = _data[i][j] + other._data[i][j];
  64. return C;
  65.  
  66. }
  67. //вычитание двух матриц
  68. const Array operator-(const Array& other) const
  69. {
  70. if ((_cols != other.cols()) || (_rows != other.rows()))
  71. throw out_of_range("Multiplied matrixes do not have same number of cols and rows.");
  72. Array C(_rows, _cols);
  73. for (int i=0; i<C.rows(); i++)
  74. for (int j=0; j<C.cols(); j++)
  75. C._data[i][j] = _data[i][j] - other._data[i][j];
  76. return C;
  77. }
  78.  
  79. // умножение двух матриц
  80. const Array operator*(const Array& other) const
  81. {
  82. if (_cols != other.rows())
  83. throw out_of_range("Multiplied matrixes do not have same number of cols and rows.");
  84. Array C(_rows, other.cols(), true);
  85. for (int i=0; i<C.rows(); i++)
  86. for (int j=0; j<C.cols(); j++)
  87. for (int k=0; k<_cols; k++)
  88. C[i][j] += _data[i][k]*other._data[k][j];
  89. return C;
  90. }
  91.  
  92. //сдвиг на 1 влево
  93. const Array operator<<(const Array& other) const
  94. {
  95. // void left( int a[], int N){
  96. // int temp=a[0];
  97. // for (int i=0; i<N-1;i++) a[i]=a[i+1];
  98. // a[N-1]=temp;
  99. }
  100.  
  101. // присваивание матрицы
  102. Array& operator= (const Array& other)
  103. {
  104. if (this!=&other)
  105. {
  106. if (_data!=0)
  107. {
  108. for (int i=0; i<_rows; i++)
  109. delete [] _data[i];
  110. delete [] _data;
  111. }
  112. _rows = other.rows();
  113. _cols = other.cols();
  114.  
  115. _data = new int* [_rows];
  116. for (int i=0; i<_rows; i++)
  117. _data[i] = new int[_cols];
  118.  
  119. for (int i=0; i<_rows; i++)
  120. for (int j=0; j<_cols; j++)
  121. _data[i][j] = other._data[i][j];
  122. }
  123. return *this;
  124. }
  125.  
  126. friend ostream& operator<< (ostream& o, const Array& a)
  127. {
  128. for (int i=0; i<a._rows; i++)
  129. {
  130. for (int j=0; j<a._cols; j++)
  131. o << setw(4) << a._data[i][j];
  132. o << endl;
  133. }
  134. return o;
  135. }
  136.  
  137. void fill_random()
  138. {
  139. for (int i=0; i<_rows; i++)
  140. for (int j=0; j<_cols; j++)
  141. _data[i][j] = rand()%100+1;
  142. }
  143. int* operator[](int i) { return _data[i]; }
  144. int& at(int i, int j) { return _data[i][j]; }
  145. const int cols() const { return _cols; }
  146. const int rows() const { return _rows; }
  147.  
  148. ~Array()
  149. {
  150. if (_data!=0)
  151. {
  152. for (int i=0; i<_rows; i++)
  153. delete [] _data[i];
  154. delete [] _data;
  155. }
  156. }
  157. };
  158.  
  159. int main()
  160. {
  161. srand(static_cast<unsigned int>(time(0)));
  162.  
  163. int N,M,K;
  164. cin>>N>>M>>K;
  165. Array x(N, M, true, 1);
  166. Array y(N, M, true, 1);
  167. Array z(N, K);
  168. z.fill_random();
  169.  
  170. cout << (x*y) << endl;
  171. cout << (x+y) << endl;
  172. cout << z << endl << z.transpon() << endl;
  173.  
  174. return 0;
  175. }
Success #stdin #stdout 0s 3236KB
stdin
2
2
2
stdout
   2   2
   2   2

   2   2
   2   2

  11   9
  83   4

  11  83
   9   4