Aqui está uma implementação completa para a classe SparseMatrix com as funções adicionais no arquivo main.cpp. Esta implementação inclui todas as verificações de erros e exceções solicitadas no enunciado.
Arquivo node.h:
#ifndef NODE_H
#define NODE_H
class Node {
public:
int row;
int col;
double value;
Node *nextRow;
Node *nextCol;
Node(int row, int col, double value);
};
#endif
Arquivo SparseMatrix.h:
#ifndef SPARSEMATRIX_H
#define SPARSEMATRIX_H
#include "Node.h"
#include <string>
class SparseMatrix {
public:
SparseMatrix(int m, int n);
~SparseMatrix();
void insert(int i, int j, double value);
double get(int i, int j);
void print();
private:
int m;
int n;
Node** rowHeads;
Node** colHeads;
};
SparseMatrix* readSparseMatrix(std::string filename);
SparseMatrix* sum(SparseMatrix* A, SparseMatrix* B);
SparseMatrix* multiply(SparseMatrix* A, SparseMatrix* B);
#endif
Arquivo SparseMatrix.cpp:
#include "SparseMatrix.h"
#include <iostream>
#include <stdexcept>
#include <fstream>
Node::Node(int row, int col, double value) : row(row), col(col), value(value), nextRow(nullptr), nextCol(nullptr) {}
SparseMatrix::SparseMatrix(int m, int n) : m(m), n(n) {
if (m <= 0 || n <= 0) {
throw std::invalid_argument("dimensions must be positive");
}
rowHeads = new Node*[m]();
colHeads = new Node*[n]();
}
SparseMatrix::~SparseMatrix() {
for (int i = 0; i < m; ++i) {
Node *curr = rowHeads[i];
while (curr) {
Node *temp = curr;
curr = curr->nextRow;
delete temp;
}
}
delete[] rowHeads;
delete[] colHeads;
}
void SparseMatrix::insert(int i, int j, double value) {
if (i < 0 || i >= m || j < 0 || j >= n) {
throw std::out_of_range("indices out of range");
}
if (value == 0) {
return;
}
Node *newNode = new Node(i, j, value);
Node *rowPrev = nullptr;
Node *rowCurr = rowHeads[i];
Node *colPrev = nullptr;
Node *colCurr = colHeads[j];
// insert in row
while (rowCurr && rowCurr->col < j) {
rowPrev = rowCurr;
rowCurr = rowCurr->nextRow;
}
if (rowCurr && rowCurr->col == j) {
rowCurr->value = value;
delete newNode;
} else {
newNode->nextRow = rowCurr;
if (rowPrev) {
rowPrev->nextRow = newNode;
} else {
rowHeads[i] = newNode;
}
}
// insert in column
while (colCurr && colCurr->row < i) {
colPrev = colCurr;
colCurr = colCurr->nextCol;
}
if (colCurr && colCurr->row == i) {
colCurr->value = value;
} else {
newNode->nextCol = colCurr;
if (colPrev) {
colPrev->nextCol = newNode;
} else {
colHeads[j] = newNode;
}
}
}
double SparseMatrix::get(int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n) {
throw std::out_of_range("indices out of range");
}
Node *curr = rowHeads[i];
while (curr && curr->col <= j) {
if (curr->col == j) {
return curr->value;
}
curr = curr->nextRow;
}
return 0;
}
void SparseMatrix::print() {
for (int i = 0; i < m; ++i) {
Node *curr = rowHeads[i];
for (int j = 0; j < n; ++j) {
if (curr && curr->col == j) {
std::cout << curr->value << " ";
curr = curr->nextRow;
} else {
std::cout << "0 ";
}
}
std::cout << std::endl;
}
}
SparseMatrix* readSparseMatrix(std::string filename) {
std::ifstream inputFile(filename);
if (!inputFile) {
throw std::runtime_error("file not found");
}
int m, n;
inputFile >> m >> n;
SparseMatrix *matrix = new SparseMatrix(m, n);
int i, j;
double value;
while (inputFile >> i >> j >> value) {
matrix->insert(i - 1, j - 1, value);
}
inputFile.close();
return matrix;
}
SparseMatrix* sum(SparseMatrix* A, SparseMatrix* B) {
if (A == nullptr || B == nullptr) {
throw std::invalid_argument("matrices must not be null");
}
if (A->m != B->m || A->n != B->n) {
throw std::invalid_argument("matrices dimensions must be equal");
}
SparseMatrix *C = new SparseMatrix(A->m, A->n);
for (int i = 0; i < A->m; ++i) {
Node *currA = A->rowHeads[i];
Node *currB = B->rowHeads[i];
while (currA || currB) {
if (currA && (!currB || currA->col < currB->col)) {
C->insert(i, currA->col, currA->value);
currA = currA->nextRow;
} else if (currB && (!currA || currB->col < currA->col)) {
C->insert(i, currB->col, currB->value);
currB = currB->nextRow;
} else {
C->insert(i, currA->col, currA->value + currB->value);
currA = currA->nextRow;
currB = currB->nextRow;
}
}
}
return C;
}
SparseMatrix* multiply(SparseMatrix* A, SparseMatrix* B) {
if (A == nullptr || B == nullptr) {
throw std::invalid_argument("matrices must not be null");
}
if (A->n != B->m) {
throw std::invalid_argument("matrices dimensions must be compatible");
}
SparseMatrix *C = new SparseMatrix(A->m, B->n);
for (int i = 0; i < A->m; ++i) {
for (int j = 0; j < B->n; ++j) {
double value = 0;
Node *currA = A->rowHeads[i];
Node *currB = B->colHeads[j];
while (currA && currB) {
if (currA->col < currB->row) {
currA = currA->nextRow;
} else if (currB->row < currA->col) {
currB = currB->nextCol;
} else {
value += currA->value * currB->value;
currA = currA->nextRow;
currB = currB->nextCol;
}
}
if (value != 0) {
C->insert(i, j, value);
}
}
}
return C;
}
Arquivo main.cpp
#include "SparseMatrix.h"
#include <iostream>
int main() {
try {
SparseMatrix *matrixA = readSparseMatrix("matrizA.txt");
SparseMatrix *matrixB = readSparseMatrix("matrizB.txt");
std::cout << "Matrix A:" << std::endl;
matrixA->print();
std::cout << "Matrix B:" << std::endl;
matrixB->print();
SparseMatrix *matrixSum = sum(matrixA, matrixB);
std::cout << "Matrix A + B:" << std::endl;
matrixSum->print();
delete matrixSum;
SparseMatrix *matrixMult = multiply(matrixA, matrixB);
std::cout << "Matrix A * B:" << std::endl;
matrixMult->print();
delete matrixMult;
delete matrixA;
delete matrixB;
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
Use esta implementação como modelo e modifique-a conforme necessário para o seu projeto.