C语言代码实现稀疏矩阵运算器
时间: 2023-11-06 10:04:42 浏览: 130
以下是C语言代码实现稀疏矩阵运算器的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int row;
int col;
int value;
} Element;
typedef struct{
int row;
int col;
int num;
Element* data;
} SparseMatrix;
SparseMatrix* create_sparse_matrix(int row, int col, int num){
SparseMatrix* matrix = (SparseMatrix*)malloc(sizeof(SparseMatrix));
matrix->row = row;
matrix->col = col;
matrix->num = num;
matrix->data = (Element*)malloc(num * sizeof(Element));
return matrix;
}
void destroy_sparse_matrix(SparseMatrix* matrix){
free(matrix->data);
free(matrix);
}
SparseMatrix* add_sparse_matrix(SparseMatrix* matrix1, SparseMatrix* matrix2){
if(matrix1->row != matrix2->row || matrix1->col != matrix2->col){
printf("Error: matrix size mismatch!\n");
return NULL;
}
SparseMatrix* result = create_sparse_matrix(matrix1->row, matrix1->col, 0);
int p1 = 0, p2 = 0;
while(p1 < matrix1->num && p2 < matrix2->num){
if(matrix1->data[p1].row < matrix2->data[p2].row ||
(matrix1->data[p1].row == matrix2->data[p2].row && matrix1->data[p1].col < matrix2->data[p2].col)){
result->data = (Element*)realloc(result->data, (result->num + 1) * sizeof(Element));
result->data[result->num].row = matrix1->data[p1].row;
result->data[result->num].col = matrix1->data[p1].col;
result->data[result->num].value = matrix1->data[p1].value;
p1++;
}
else if(matrix1->data[p1].row > matrix2->data[p2].row ||
(matrix1->data[p1].row == matrix2->data[p2].row && matrix1->data[p1].col > matrix2->data[p2].col)){
result->data = (Element*)realloc(result->data, (result->num + 1) * sizeof(Element));
result->data[result->num].row = matrix2->data[p2].row;
result->data[result->num].col = matrix2->data[p2].col;
result->data[result->num].value = matrix2->data[p2].value;
p2++;
}
else{
result->data = (Element*)realloc(result->data, (result->num + 1) * sizeof(Element));
result->data[result->num].row = matrix1->data[p1].row;
result->data[result->num].col = matrix1->data[p1].col;
result->data[result->num].value = matrix1->data[p1].value + matrix2->data[p2].value;
p1++;
p2++;
}
result->num++;
}
while(p1 < matrix1->num){
result->data = (Element*)realloc(result->data, (result->num + 1) * sizeof(Element));
result->data[result->num].row = matrix1->data[p1].row;
result->data[result->num].col = matrix1->data[p1].col;
result->data[result->num].value = matrix1->data[p1].value;
p1++;
result->num++;
}
while(p2 < matrix2->num){
result->data = (Element*)realloc(result->data, (result->num + 1) * sizeof(Element));
result->data[result->num].row = matrix2->data[p2].row;
result->data[result->num].col = matrix2->data[p2].col;
result->data[result->num].value = matrix2->data[p2].value;
p2++;
result->num++;
}
return result;
}
void print_sparse_matrix(SparseMatrix* matrix){
printf("Sparse matrix:\n");
for(int i = 0; i < matrix->num; i++){
printf("(%d, %d) = %d\n", matrix->data[i].row, matrix->data[i].col, matrix->data[i].value);
}
}
int main(){
SparseMatrix* matrix1 = create_sparse_matrix(3, 3, 3);
matrix1->data[0].row = 0;
matrix1->data[0].col = 0;
matrix1->data[0].value = 1;
matrix1->data[1].row = 1;
matrix1->data[1].col = 1;
matrix1->data[1].value = 2;
matrix1->data[2].row = 2;
matrix1->data[2].col = 2;
matrix1->data[2].value = 3;
print_sparse_matrix(matrix1);
SparseMatrix* matrix2 = create_sparse_matrix(3, 3, 3);
matrix2->data[0].row = 0;
matrix2->data[0].col = 0;
matrix2->data[0].value = 4;
matrix2->data[1].row = 1;
matrix2->data[1].col = 1;
matrix2->data[1].value = 5;
matrix2->data[2].row = 2;
matrix2->data[2].col = 2;
matrix2->data[2].value = 6;
print_sparse_matrix(matrix2);
SparseMatrix* result = add_sparse_matrix(matrix1, matrix2);
print_sparse_matrix(result);
destroy_sparse_matrix(matrix1);
destroy_sparse_matrix(matrix2);
destroy_sparse_matrix(result);
return 0;
}
```
该示例代码实现了创建稀疏矩阵、销毁稀疏矩阵、稀疏矩阵加法和打印稀疏矩阵等功能。其中,稀疏矩阵使用了三元组(行号、列号、值)来存储。在稀疏矩阵加法中,首先比较两个矩阵当前位置的行号和列号,如果相等则将两个矩阵对应位置的值相加;否则将行号和列号较小的位置的值加入结果矩阵中。最后,将未遍历完的元素加入结果矩阵中。
阅读全文