Advertisement
Guest User

realiseFile

a guest
May 13th, 2020
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include "[lab5]Header.h"
  5.  
  6. void Exception::display() {
  7.     std::cout << "Exception! - #" << code_ << ", " << cause_ << '\n';
  8. }
  9.  
  10. Exception::Exception(int code, char* cause) {
  11.     this->cause_ = cause;
  12.     this->code_ = code;
  13. }
  14. template <class own>
  15. own& matrix<own>:: operator()(const int i, const int j) {
  16.     try
  17.     {
  18.         if (i >= n_ || j >= m_) {
  19.             throw Exception(111, (char*)"Out of the range!");
  20.         }
  21.         return point_[m_ * i + j];
  22.     }
  23.     catch (Exception& ex)
  24.     {
  25.         ex.display();
  26.     }
  27. }
  28. template<class own>
  29. matrix<own>::matrix()
  30. {
  31.     this->n_ = 0;
  32.     this->m_ = 0;
  33.     this->point_ = nullptr;
  34. }
  35. template<class own>
  36. matrix<own>::matrix(int n, int m)
  37. {
  38.     this->n_ = n;
  39.     this->m_ = m;
  40.     point_ = new own[getSize()];
  41.     for (int i = 0; i < this->getSize(); i++) {
  42.         this->point_[i] = i;
  43.     }
  44. }
  45. template<class own>
  46. matrix<own>::matrix(matrix& other) {
  47.  
  48.     try
  49.     {
  50.         if (this->getSize() != other.getSize()) {
  51.             throw std::exception("Matriсes are not equals");
  52.         }
  53.         this->n_ = other.n_;
  54.         this->m_ = other.m_;
  55.  
  56.         delete point_;
  57.         point_ = new own[getSize()];
  58.         for (int i = 0; i < other.getSize(); i++) {
  59.             this->point_[i] = other.point_[i];
  60.         }
  61.     }
  62.     catch (const std::exception& ex)
  63.     {
  64.         std::cout << ex.what();
  65.     }
  66.  
  67. }
  68. template<class own>
  69. matrix<own>::~matrix()
  70. {
  71.     delete[]point_;
  72. }
  73. template<class own>
  74. int matrix<own>::getSize() {
  75.     return n_ * m_;
  76. }
  77. template<class own>
  78. void matrix<own>::printMtr()
  79. {
  80.     for (int i = 0; i < getSize(); i++) {
  81.         std::cout << point_[i] << ' ';
  82.     }
  83. }
  84. template<class own>
  85. std::ostream& operator<< (std::ostream& out, const matrix<char>& mtr) {
  86.     out << "Matrix is:\n";
  87.     int cur = 0;
  88.     for (int i = 0; i < mtr.n_; i++) {
  89.         for (int j = 0; j < mtr.m_; j++) {
  90.             out << mtr.point_[cur++] << ' ';
  91.         }
  92.         out << '\n';
  93.     }
  94.     out << '\n';
  95.     return out;
  96. }
  97. template<class own>
  98. std::istream& operator>>(std::istream& in, matrix<char>& mtr) {
  99.     for (int i = 0; i < mtr.getSize(); i++) {
  100.         in >> mtr.point_[i];
  101.     }
  102.     return in;
  103. }
  104.  
  105. template<typename T1>
  106. void swapT1toT2(T1& a, T1& b) {
  107.     T1 temp = b;
  108.     b = a;
  109.     a = temp;
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement