1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include <stdio.h> #include <stdlib.h> typedef struct { int row; int col; int value; } Element;
typedef struct { int rows; int cols; int count; Element* elements; } SparseMatrix;
SparseMatrix* createSparseMatrix(int rows, int cols, int count) { SparseMatrix* matrix = (SparseMatrix*)malloc(sizeof(SparseMatrix)); matrix->rows = rows; matrix->cols = cols; matrix->count = count; matrix->elements = (Element*)malloc(count * sizeof(Element)); return matrix; }
void addElement(SparseMatrix* matrix, int row, int col, int value) { if (matrix->count >= matrix->rows * matrix->cols) { printf("Matrix is full. Cannot add more elements.\n"); return; }
Element element = {row, col, value}; matrix->elements[matrix->count++] = element; }
SparseMatrix* addSparseMatrices(const SparseMatrix* matrixA, const SparseMatrix* matrixB) { if (matrixA->rows != matrixB->rows || matrixA->cols != matrixB->cols) { printf("Matrix dimensions do not match.\n"); return NULL; }
SparseMatrix* resultMatrix = createSparseMatrix(matrixA->rows, matrixA->cols, 0);
int i = 0, j = 0; while (i < matrixA->count && j < matrixB->count) { Element elementA = matrixA->elements[i]; Element elementB = matrixB->elements[j];
if (elementA.row < elementB.row || (elementA.row == elementB.row && elementA.col < elementB.col)) { addElement(resultMatrix, elementA.row, elementA.col, elementA.value); i++; } else if (elementA.row > elementB.row || (elementA.row == elementB.row && elementA.col > elementB.col)) { addElement(resultMatrix, elementB.row, elementB.col, elementB.value); j++; } else { int sum = elementA.value + elementB.value; if (sum != 0) { addElement(resultMatrix, elementA.row, elementA.col, sum); } i++; j++; } }
while (i < matrixA->count) { Element elementA = matrixA->elements[i]; addElement(resultMatrix, elementA.row, elementA.col, elementA.value); i++; } while (j < matrixB->count) { Element elementB = matrixB->elements[j]; addElement(resultMatrix, elementB.row, elementB.col, elementB.value); j++; } return resultMatrix; } void printSparseMatrix(const SparseMatrix* matrix) { for (int i = 0; i < matrix->count; i++) { Element element = matrix->elements[i]; printf("(%d, %d): %d\n", element.row, element.col, element.value); } } int main() { SparseMatrix* matrixA = createSparseMatrix(3, 3, 3); addElement(matrixA, 0, 0, 1); addElement(matrixA, 1, 1, 2); addElement(matrixA, 2, 2, 3); SparseMatrix* matrixB = createSparseMatrix(3, 3, 2); addElement(matrixB, 0, 0, 4); addElement(matrixB, 2, 2, 5); SparseMatrix* matrixC = addSparseMatrices(matrixA, matrixB); printSparseMatrix(matrixC); free(matrixA->elements); free(matrixA); free(matrixB->elements); free(matrixB); free(matrixC->elements); free(matrixC); return 0; }
|