Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. template<typename T>
  5. class matr {
  6. private:
  7. T **a;
  8. unsigned n, m;
  9. public:
  10. matr();
  11. matr(int n, int m);
  12. void sort();
  13. void read();
  14. void print();
  15. void sortSt();
  16.  
  17. };
  18.  
  19. template<typename T>
  20. matr<T>::matr() {}
  21.  
  22. template<typename T>
  23. matr<T>::matr(int k, int t) {
  24. n = k;
  25. m = t;
  26. if (n < 0 || m < 0) {
  27. throw exception();
  28. }
  29. // try {
  30. a = new T*[n];
  31. for (int i = 0; i < n; ++i) {
  32. a[i] = new T[m];
  33. }
  34. }
  35. //catch (...) {
  36. //cout << "No memory in heap";
  37.  
  38. //}
  39.  
  40.  
  41. //}
  42.  
  43. template<typename T>
  44. void matr<T>::read() {
  45. for (int i = 0; i < n; ++i) {
  46. for (int j = 0; j < m; ++j){
  47. cin >> a[i][j];
  48. }
  49. }
  50. }
  51.  
  52. template<typename T>
  53. void matr<T>::print() {
  54. for (int i = 0; i < n; ++i) {
  55. for (int j = 0; j < m; ++j) {
  56. cout << a[i][j] << " ";
  57. }
  58. cout << endl;
  59. }
  60. }
  61.  
  62. template<typename T>
  63. void matr<T>::sort() {
  64. for (int k = 0; k < n; ++k) {
  65. for (int i = 0; i < n - 1 - k; ++i) {
  66. for (int j = 0; j < m; ++j) {
  67. if (a[i][j] < a[i + 1][j]) {
  68. break;
  69. } else if (a[i][j] > a[i + 1][j]) {
  70. for (int x = 0; x < m; ++x) {
  71. swap(a[i][x], a[i + 1][x]);
  72. }
  73. break;
  74. }
  75. }
  76. }
  77. }
  78. }
  79.  
  80. template<typename T>
  81. void matr<T>::sortSt() {
  82. for (int k = 0; k < m; ++k) {
  83. for (int j = 0; j < m - 1 - j; ++j) {
  84. for (int i = 0; i < n; ++i) {
  85. if (a[i][j] > a[i][j + 1]) {
  86. break;
  87. }
  88. else if (a[i][j] < a[i][j + 1]) {
  89. for (int x = 0; x < n; ++x) {
  90. swap (a[x][j], a[x][j + 1]);
  91. }
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. int main() {
  105. try {
  106. matr<int> arr(-3,3);
  107. arr.read();
  108. cout << endl;
  109. arr.print();
  110. cout << endl;
  111. arr.sortSt();
  112. arr.print();
  113. }
  114. catch (exception& ex) {
  115. cout << "ex";
  116. }
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement