Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "library.h"
- using namespace std;
- SparseMatrix::SparseMatrix(int a , int b)
- {
- rows = a;
- cols = b;
- Start = nullptr;
- End = nullptr;
- }
- SparseMatrix::~SparseMatrix()
- {
- Element* prom;
- Element* t = Start;
- while (t != 0) {
- prom = t;
- t = t->next;
- delete prom;
- }
- }
- void SparseMatrix::addElement(int row, int col, int value)
- {
- Element* new_element = new Element(row, col, value);
- if (Start == nullptr) {
- Start = End = new_element;
- }
- else {
- End->next = new_element;
- End = new_element;
- }
- }
- void SparseMatrix::Read(std::ifstream& file1)
- {
- int row, col, value;
- while (!file1.eof()) {
- file1 >> row >> col >> value;
- addElement(row, col, value);
- }
- Sort();
- }
- void SparseMatrix::Sort()
- {
- Element* cur = Start;
- while (cur != nullptr) {
- Element* min = cur;
- Element* temp = cur->next;
- while (temp != nullptr) {
- if (temp->row < min->row || (temp->row == min->row && temp->col < min->col)) {
- min = temp;
- }
- temp = temp->next;
- }
- swap(cur->row, min->row);
- swap(cur->col, min->col);
- swap(cur->value, min->value);
- cur = cur->next;
- }
- }
- void SparseMatrix::Print(std::ofstream& file2)
- {
- if (Start == nullptr) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- file2 << "0 " ;
- }
- file2 << endl;
- }
- file2 << endl;
- return;
- }
- Element* cur = Start;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- if (cur != nullptr && cur->row == i && cur->col == j) {
- file2 << cur->value << " ";
- cur = cur->next;
- }
- else {
- file2 << "0 ";
- }
- }
- file2 << endl;
- }
- }
- SparseMatrix& SparseMatrix::operator+=(const SparseMatrix& other)
- {
- Element* current = Start;
- Element* otherCurrent = other.Start;
- while (current != nullptr && otherCurrent != nullptr) {
- if (current->row < otherCurrent->row || (current->row == otherCurrent->row && current->col < otherCurrent->col)) {
- current = current->next;
- }
- else if (current->row == otherCurrent->row && current->col == otherCurrent->col) {
- current->value += otherCurrent->value;
- current = current->next;
- otherCurrent = otherCurrent->next;
- }
- else {
- addElement(otherCurrent->row, otherCurrent->col, otherCurrent->value);
- otherCurrent = otherCurrent->next;
- }
- }
- // если в other oстались элементы
- while (otherCurrent != nullptr) {
- addElement(otherCurrent->row, otherCurrent->col, otherCurrent->value);
- otherCurrent = otherCurrent->next;
- }
- Sort();
- return *this;
- }
- SparseMatrix& SparseMatrix::operator-=(const SparseMatrix& other)
- {
- Element* current = Start;
- Element* otherCurrent = other.Start;
- while (current != nullptr && otherCurrent != nullptr) {
- // 1 элемент слева
- if (current->row < otherCurrent->row || (current->row == otherCurrent->row && current->col < otherCurrent->col)) {
- current = current->next;
- }
- // елементы совпадают
- else if (current->row == otherCurrent->row && current->col == otherCurrent->col) {
- current->value -= otherCurrent->value;
- current = current->next;
- otherCurrent = otherCurrent->next;
- }
- // 1 елемент справа
- else {
- addElement(otherCurrent->row, otherCurrent->col, -otherCurrent->value);
- otherCurrent = otherCurrent->next;
- }
- }
- // если в other oстались элементы
- while (otherCurrent != nullptr) {
- addElement(otherCurrent->row, otherCurrent->col, -otherCurrent->value);
- otherCurrent = otherCurrent->next;
- }
- Sort();
- return *this;
- }
- SparseMatrix& SparseMatrix::operator=(const SparseMatrix& other)
- {
- Element* current = Start;
- while (current != nullptr) {
- Element* temp = current;
- current = current->next;
- delete temp;
- }
- Start = nullptr;
- End = nullptr;
- // Копируем элементы из другой матрицы
- Element* otherCurrent = other.Start;
- while (otherCurrent != nullptr) {
- addElement(otherCurrent->row, otherCurrent->col, otherCurrent->value);
- otherCurrent = otherCurrent->next;
- }
- return *this;
- }
- SparseMatrix& SparseMatrix::operator*=(const double a)
- {
- Element* current = Start;
- while (current != nullptr) {
- current->value *= a;
- current = current->next;
- }
- return *this;
- }
- SparseMatrix& SparseMatrix::operator*=(const SparseMatrix& other)
- {
- // res[i][j] += m1[i][k]*m2[k][j] i-кол-во строк 1 j-кол-во столбцов второй k-кол-во столбцов 1
- SparseMatrix result(rows, other.cols);
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < other.cols; ++j) {
- int sum = 0;
- for (int k = 0; k < cols; ++k) {
- Element* current = Start;
- while (current != nullptr) {
- if (current->row == i && current->col == k) {
- Element* otherCurrent = other.Start;
- while (otherCurrent != nullptr) {
- if (otherCurrent->row == k && otherCurrent->col == j) {
- sum += current->value * otherCurrent->value;
- break;
- }
- otherCurrent = otherCurrent->next;
- }
- break;
- }
- current = current->next;
- }
- }
- if (sum != 0) {
- result.addElement(i, j, sum);
- }
- }
- }
- *this = result;
- return *this;
- }
- Element::Element(int a, int b, int c)
- {
- row = a;
- col = b;
- value = c;
- next = nullptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement