Advertisement
Mikhail-Podbolotov

Untitled

Apr 24th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.32 KB | None | 0 0
  1. #include "library.h"
  2. using namespace std;
  3.  
  4. SparseMatrix::SparseMatrix(int a , int b)
  5. {
  6.     rows = a;
  7.     cols = b;
  8.     Start = nullptr;
  9.     End = nullptr;
  10. }
  11.  
  12. SparseMatrix::~SparseMatrix()
  13. {
  14.     Element* prom;
  15.     Element* t = Start;
  16.     while (t != 0) {
  17.         prom = t;
  18.         t = t->next;
  19.         delete prom;
  20.     }
  21. }
  22.  
  23. void SparseMatrix::addElement(int row, int col, int value)
  24. {
  25.     Element* new_element = new Element(row, col, value);
  26.     if (Start == nullptr) {
  27.         Start = End = new_element;
  28.     }
  29.     else {
  30.         End->next = new_element;
  31.         End = new_element;
  32.     }
  33. }
  34.  
  35. void SparseMatrix::Read(std::ifstream& file1)
  36. {
  37.     int row, col, value;
  38.     while (!file1.eof()) {
  39.         file1 >> row >> col >> value;
  40.         addElement(row, col, value);
  41.     }
  42.     Sort();
  43. }
  44.  
  45. void SparseMatrix::Sort()
  46. {
  47.     Element* cur = Start;
  48.     while (cur != nullptr) {
  49.         Element* min = cur;
  50.         Element* temp = cur->next;
  51.         while (temp != nullptr) {
  52.             if (temp->row < min->row || (temp->row == min->row && temp->col < min->col)) {
  53.                 min = temp;
  54.             }
  55.             temp = temp->next;
  56.         }
  57.         swap(cur->row, min->row);
  58.         swap(cur->col, min->col);
  59.         swap(cur->value, min->value);
  60.         cur = cur->next;
  61.     }
  62. }
  63.  
  64. void SparseMatrix::Print(std::ofstream& file2)
  65. {
  66.     if (Start == nullptr) {
  67.         for (int i = 0; i < rows; i++) {
  68.             for (int j = 0; j < cols; j++) {
  69.                 file2 << "0 " ;
  70.             }
  71.             file2 << endl;
  72.         }
  73.         file2 << endl;
  74.         return;
  75.     }
  76.     Element* cur = Start;
  77.     for (int i = 0; i < rows; i++) {
  78.         for (int j = 0; j < cols; j++) {
  79.             if (cur != nullptr && cur->row == i && cur->col == j) {
  80.                 file2 << cur->value << " ";
  81.                 cur = cur->next;
  82.             }
  83.             else {
  84.                 file2 << "0 ";
  85.             }
  86.         }
  87.         file2 << endl;
  88.     }
  89. }
  90. SparseMatrix& SparseMatrix::operator+=(const SparseMatrix& other)
  91. {
  92.     Element* current = Start;
  93.     Element* otherCurrent = other.Start;
  94.     while (current != nullptr && otherCurrent != nullptr) {
  95.         if (current->row < otherCurrent->row || (current->row == otherCurrent->row && current->col < otherCurrent->col)) {
  96.             current = current->next;
  97.         }
  98.         else if (current->row == otherCurrent->row && current->col == otherCurrent->col) {
  99.             current->value += otherCurrent->value;
  100.             current = current->next;
  101.             otherCurrent = otherCurrent->next;
  102.         }
  103.         else {
  104.             addElement(otherCurrent->row, otherCurrent->col, otherCurrent->value);
  105.             otherCurrent = otherCurrent->next;
  106.         }
  107.     }
  108.  
  109.     // если в other oстались элементы
  110.     while (otherCurrent != nullptr) {
  111.         addElement(otherCurrent->row, otherCurrent->col, otherCurrent->value);
  112.         otherCurrent = otherCurrent->next;
  113.     }
  114.     Sort();
  115.     return *this;
  116. }
  117. SparseMatrix& SparseMatrix::operator-=(const SparseMatrix& other)
  118. {
  119.     Element* current = Start;
  120.     Element* otherCurrent = other.Start;
  121.     while (current != nullptr && otherCurrent != nullptr) {
  122.         // 1 элемент слева
  123.         if (current->row < otherCurrent->row || (current->row == otherCurrent->row && current->col < otherCurrent->col)) {
  124.             current = current->next;
  125.         }
  126.         // елементы совпадают
  127.         else if (current->row == otherCurrent->row && current->col == otherCurrent->col) {
  128.             current->value -= otherCurrent->value;
  129.             current = current->next;
  130.             otherCurrent = otherCurrent->next;
  131.         }
  132.         // 1 елемент справа
  133.         else {
  134.             addElement(otherCurrent->row, otherCurrent->col, -otherCurrent->value);
  135.             otherCurrent = otherCurrent->next;
  136.         }
  137.     }
  138.  
  139.     // если в other oстались элементы
  140.     while (otherCurrent != nullptr) {
  141.         addElement(otherCurrent->row, otherCurrent->col, -otherCurrent->value);
  142.         otherCurrent = otherCurrent->next;
  143.     }
  144.     Sort();
  145.     return *this;
  146. }
  147. SparseMatrix& SparseMatrix::operator=(const SparseMatrix& other)
  148. {
  149.     Element* current = Start;
  150.     while (current != nullptr) {
  151.         Element* temp = current;
  152.         current = current->next;
  153.         delete temp;
  154.     }
  155.     Start = nullptr;
  156.     End = nullptr;
  157.  
  158.     // Копируем элементы из другой матрицы
  159.     Element* otherCurrent = other.Start;
  160.     while (otherCurrent != nullptr) {
  161.         addElement(otherCurrent->row, otherCurrent->col, otherCurrent->value);
  162.         otherCurrent = otherCurrent->next;
  163.     }
  164.  
  165.     return *this;
  166. }
  167. SparseMatrix& SparseMatrix::operator*=(const double a)
  168. {
  169.     Element* current = Start;
  170.     while (current != nullptr) {
  171.         current->value *= a;
  172.         current = current->next;
  173.     }
  174.     return *this;
  175. }
  176.  
  177. SparseMatrix& SparseMatrix::operator*=(const SparseMatrix& other)
  178. {
  179.     // res[i][j] += m1[i][k]*m2[k][j] i-кол-во строк 1 j-кол-во столбцов второй k-кол-во столбцов 1
  180.     SparseMatrix result(rows, other.cols);
  181.     for (int i = 0; i < rows; ++i) {
  182.         for (int j = 0; j < other.cols; ++j) {
  183.             int sum = 0;
  184.             for (int k = 0; k < cols; ++k) {
  185.                 Element* current = Start;
  186.                 while (current != nullptr) {
  187.                     if (current->row == i && current->col == k) {
  188.                         Element* otherCurrent = other.Start;
  189.                         while (otherCurrent != nullptr) {
  190.                             if (otherCurrent->row == k && otherCurrent->col == j) {
  191.                                 sum += current->value * otherCurrent->value;
  192.                                 break;
  193.                             }
  194.                             otherCurrent = otherCurrent->next;
  195.                         }
  196.                         break;
  197.                     }
  198.                     current = current->next;
  199.                 }
  200.             }
  201.             if (sum != 0) {
  202.                 result.addElement(i, j, sum);
  203.             }
  204.         }
  205.     }
  206.     *this = result;
  207.     return *this;
  208. }
  209.  
  210. Element::Element(int a, int b, int c)
  211. {
  212.     row = a;
  213.     col = b;
  214.     value = c;
  215.     next = nullptr;
  216. }
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement