Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template<typename T>
- class matr {
- private:
- T **a;
- unsigned n, m;
- public:
- matr();
- matr(int n, int m);
- void sort();
- void read();
- void print();
- void sortSt();
- };
- template<typename T>
- matr<T>::matr() {}
- template<typename T>
- matr<T>::matr(int k, int t) {
- n = k;
- m = t;
- if (n < 0 || m < 0) {
- throw exception();
- }
- // try {
- a = new T*[n];
- for (int i = 0; i < n; ++i) {
- a[i] = new T[m];
- }
- }
- //catch (...) {
- //cout << "No memory in heap";
- //}
- //}
- template<typename T>
- void matr<T>::read() {
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j){
- cin >> a[i][j];
- }
- }
- }
- template<typename T>
- void matr<T>::print() {
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- cout << a[i][j] << " ";
- }
- cout << endl;
- }
- }
- template<typename T>
- void matr<T>::sort() {
- for (int k = 0; k < n; ++k) {
- for (int i = 0; i < n - 1 - k; ++i) {
- for (int j = 0; j < m; ++j) {
- if (a[i][j] < a[i + 1][j]) {
- break;
- } else if (a[i][j] > a[i + 1][j]) {
- for (int x = 0; x < m; ++x) {
- swap(a[i][x], a[i + 1][x]);
- }
- break;
- }
- }
- }
- }
- }
- template<typename T>
- void matr<T>::sortSt() {
- for (int k = 0; k < m; ++k) {
- for (int j = 0; j < m - 1 - j; ++j) {
- for (int i = 0; i < n; ++i) {
- if (a[i][j] > a[i][j + 1]) {
- break;
- }
- else if (a[i][j] < a[i][j + 1]) {
- for (int x = 0; x < n; ++x) {
- swap (a[x][j], a[x][j + 1]);
- }
- break;
- }
- }
- }
- }
- }
- int main() {
- try {
- matr<int> arr(-3,3);
- arr.read();
- cout << endl;
- arr.print();
- cout << endl;
- arr.sortSt();
- arr.print();
- }
- catch (exception& ex) {
- cout << "ex";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement