1.编写用“三元组表”存储稀疏矩阵,进行矩阵处理的程序。 (1.)矩阵转置; (2.)矩阵加要求用C++
时间: 2024-05-24 12:11:24 浏览: 97
语言实现。
(1)矩阵转置
#include <stdio.h>
#define MAXSIZE 100
typedef struct {
int row, col, val;
} Triple;
typedef struct {
Triple data[MAXSIZE];
int rpos[MAXSIZE + 1];
int rows, cols, nums;
} TSMatrix;
void TransposeTSMatrix(TSMatrix M, TSMatrix *T) {
T->rows = M.cols;
T->cols = M.rows;
T->nums = M.nums;
if (T->nums) {
int col, p, q;
T->data[0].row = M.data[0].col;
T->data[0].col = M.data[0].row;
T->data[0].val = M.data[0].val;
for (col = 0; col < M.cols; col++) {
T->rpos[col] = 0;
}
for (p = 1; p <= M.nums; p++) {
col = M.data[p].col;
T->rpos[col]++;
}
for (col = 1; col < M.cols; col++) {
T->rpos[col] += T->rpos[col - 1];
}
for (p = 1; p <= M.nums; p++) {
col = M.data[p].col;
q = T->rpos[col];
T->data[q].row = M.data[p].col;
T->data[q].col = M.data[p].row;
T->data[q].val = M.data[p].val;
T->rpos[col]++;
}
}
}
int main() {
TSMatrix M, T;
int i, j, k;
printf("请输入矩阵的行数、列数和非零元素个数:\n");
scanf("%d%d%d", &M.rows, &M.cols, &M.nums);
printf("请输入矩阵的三元组表:\n");
for (i = 1; i <= M.nums; i++) {
scanf("%d%d%d", &M.data[i].row, &M.data[i].col, &M.data[i].val);
}
TransposeTSMatrix(M, &T);
printf("矩阵的转置为:\n");
printf("行 列 值\n");
for (k = 1; k <= T.nums; k++) {
printf("%2d %2d %2d\n", T.data[k].row, T.data[k].col, T.data[k].val);
}
return 0;
}
(2)矩阵加
#include <stdio.h>
#define MAXSIZE 100
typedef struct {
int row, col, val;
} Triple;
typedef struct {
Triple data[MAXSIZE];
int rpos[MAXSIZE + 1];
int rows, cols, nums;
} TSMatrix;
void AddTSMatrix(TSMatrix M, TSMatrix N, TSMatrix *Q) {
if (M.rows != N.rows || M.cols != N.cols) {
printf("两个矩阵的行列数不同,无法相加!\n");
return;
}
Q->rows = M.rows;
Q->cols = M.cols;
Q->nums = 0;
int p = 1, q = 1;
while (p <= M.nums && q <= N.nums) {
if (M.data[p].row < N.data[q].row || (M.data[p].row == N.data[q].row && M.data[p].col < N.data[q].col)) {
Q->nums++;
Q->data[Q->nums].row = M.data[p].row;
Q->data[Q->nums].col = M.data[p].col;
Q->data[Q->nums].val = M.data[p].val;
p++;
} else if (M.data[p].row > N.data[q].row || (M.data[p].row == N.data[q].row && M.data[p].col > N.data[q].col)) {
Q->nums++;
Q->data[Q->nums].row = N.data[q].row;
Q->data[Q->nums].col = N.data[q].col;
Q->data[Q->nums].val = N.data[q].val;
q++;
} else {
if (M.data[p].val + N.data[q].val != 0) {
Q->nums++;
Q->data[Q->nums].row = M.data[p].row;
Q->data[Q->nums].col = M.data[p].col;
Q->data[Q->nums].val = M.data[p].val + N.data[q].val;
}
p++;
q++;
}
}
while (p <= M.nums) {
Q->nums++;
Q->data[Q->nums].row = M.data[p].row;
Q->data[Q->nums].col = M.data[p].col;
Q->data[Q->nums].val = M.data[p].val;
p++;
}
while (q <= N.nums) {
Q->nums++;
Q->data[Q->nums].row = N.data[q].row;
Q->data[Q->nums].col = N.data[q].col;
Q->data[Q->nums].val = N.data[q].val;
q++;
}
}
int main() {
TSMatrix M, N, Q;
int i, j, k;
printf("请输入第一个矩阵的行数、列数和非零元素个数:\n");
scanf("%d%d%d", &M.rows, &M.cols, &M.nums);
printf("请输入第一个矩阵的三元组表:\n");
for (i = 1; i <= M.nums; i++) {
scanf("%d%d%d", &M.data[i].row, &M.data[i].col, &M.data[i].val);
}
printf("请输入第二个矩阵的行数、列数和非零元素个数:\n");
scanf("%d%d%d", &N.rows, &N.cols, &N.nums);
printf("请输入第二个矩阵的三元组表:\n");
for (i = 1; i <= N.nums; i++) {
scanf("%d%d%d", &N.data[i].row, &N.data[i].col, &N.data[i].val);
}
AddTSMatrix(M, N, &Q);
printf("两个矩阵相加的结果为:\n");
printf("行 列 值\n");
for (k = 1; k <= Q.nums; k++) {
printf("%2d %2d %2d\n", Q.data[k].row, Q.data[k].col, Q.data[k].val);
}
return 0;
}
阅读全文