fork download
  1. #include "iostream"
  2.  
  3. template<typename Func1, typename Func2, typename Func3>
  4. void matPassPrint(int s, Func1 func1, Func2 func2, Func3 func3){
  5. for(int i = 0; i < s; ++i){
  6. func1(i);
  7. for(int j = 0; j < s; ++j)
  8. func2(i, j);
  9. func3();
  10. }}
  11.  
  12. class Mat{
  13. private:
  14. int** data;
  15. unsigned int size;
  16. public:
  17. void print(){
  18. matPassPrint(this->size,
  19. [](int i){},
  20. [this](int i, int j){std::cout << "A[" << i << "][" << j << "] = " << this->data[i][j] << " ";},
  21. [](){std::cout << std::endl;});
  22. }
  23. Mat(){
  24. this->size = 3;
  25. std::cout << "Введите размер квадратной матрицы: " << this->size;
  26. // TODO std::cin >> this->size;
  27. std::cout << std::endl << "Введите элементы матрицы: " << std::endl;
  28. this->data = new int*[this->size];
  29. matPassPrint(this->size,
  30. [this](int i){this->data[i] = new int[this->size];},
  31. [this](int i, int j){this->data[i][j] = std::rand() % 100; std::cout << "A[" << i << "][" << j << "] = " << this->data[i][j];}, // TODO std::cin >> this->data[i][j];},
  32. [](){std::cout << std::endl;});
  33. }
  34. ~Mat(){
  35. for(int i = 0; i < this->size; ++i)
  36. delete[] this->data[i];
  37. delete[] this->data;
  38. }
  39. void replaceRow(int s1, int s2){
  40. int* temp = this->data[s1];
  41. this->data[s1] = this->data[s2];
  42. this->data[s2] = temp;
  43. }
  44. void transpose(){
  45. int** newMat = new int*[this->size];
  46. matPassPrint(this->size,
  47. [this, newMat](int i){newMat[i] = new int[this->size];},
  48. [this, newMat](int i, int j){newMat[i][j] = this->data[j][i];},
  49. [](){});
  50. for(int i = 0; i < this->size; ++i){
  51. delete[] this->data[i];
  52. this->data[i] = newMat[i];
  53. }
  54. delete[] newMat;
  55. }
  56. };
  57.  
  58. int main(){
  59. setlocale(LC_ALL, "");
  60. Mat mat = Mat();
  61. std::cout << std::endl << "Первоначальная матрица:" << std::endl;
  62. mat.print();
  63. int s1 = 1, s2 = 2;
  64. std::cout << std::endl << "Введите строчки для перемены мест (нумерация строчек начинается с 0): " << s1 << ", " << s2 << std::endl;
  65. // TODO std::cin >> s1;
  66. // TODO std::cin >> s2;
  67. mat.replaceRow(s1, s2);
  68. std::cout << std::endl << "Матрица после замены мест: " << std::endl;
  69. mat.print();
  70. mat.transpose();
  71. std::cout << std::endl << "Матрица после транспонирования: " << std::endl;
  72. mat.print();
  73. return 0;
  74. }
Success #stdin #stdout 0s 5464KB
stdin
Standard input is empty
stdout
Введите размер квадратной матрицы: 3
Введите элементы матрицы: 
A[0][0] = 83A[0][1] = 86A[0][2] = 77
A[1][0] = 15A[1][1] = 93A[1][2] = 35
A[2][0] = 86A[2][1] = 92A[2][2] = 49

Первоначальная матрица:
A[0][0] = 83 A[0][1] = 86 A[0][2] = 77 
A[1][0] = 15 A[1][1] = 93 A[1][2] = 35 
A[2][0] = 86 A[2][1] = 92 A[2][2] = 49 

Введите строчки для перемены мест (нумерация строчек начинается с 0): 1, 2

Матрица после замены мест: 
A[0][0] = 83 A[0][1] = 86 A[0][2] = 77 
A[1][0] = 86 A[1][1] = 92 A[1][2] = 49 
A[2][0] = 15 A[2][1] = 93 A[2][2] = 35 

Матрица после транспонирования: 
A[0][0] = 83 A[0][1] = 86 A[0][2] = 15 
A[1][0] = 86 A[1][1] = 92 A[1][2] = 93 
A[2][0] = 77 A[2][1] = 49 A[2][2] = 35