键盘输入两个稀疏矩阵,求相加,减,乘,及任一矩阵的转置结果,C++
时间: 2023-07-11 22:32:22 浏览: 112
c语言实现稀疏矩阵的加减乘,转置
5星 · 资源好评率100%
以下是 C++ 代码实现:
```c++
#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
// 稀疏矩阵结构体
struct SparseMatrix {
int m, n, num; // 矩阵的行数、列数、非零元素个数
int data[MAX_SIZE][3]; // 矩阵中每个非零元素的行、列、值
};
// 读入稀疏矩阵
void readSparseMatrix(SparseMatrix& matrix) {
cin >> matrix.m >> matrix.n >> matrix.num;
for (int i = 0; i < matrix.num; i++) {
cin >> matrix.data[i][0] >> matrix.data[i][1] >> matrix.data[i][2];
}
}
// 打印稀疏矩阵
void printSparseMatrix(SparseMatrix matrix) {
for (int i = 0; i < matrix.num; i++) {
cout << matrix.data[i][0] << " " << matrix.data[i][1] << " " << matrix.data[i][2] << endl;
}
}
// 稀疏矩阵相加
SparseMatrix addSparseMatrix(SparseMatrix matrix1, SparseMatrix matrix2) {
SparseMatrix result;
result.m = matrix1.m;
result.n = matrix1.n;
result.num = 0;
int i = 0, j = 0;
while (i < matrix1.num && j < matrix2.num) {
if (matrix1.data[i][0] < matrix2.data[j][0] || (matrix1.data[i][0] == matrix2.data[j][0] && matrix1.data[i][1] < matrix2.data[j][1])) {
result.data[result.num][0] = matrix1.data[i][0];
result.data[result.num][1] = matrix1.data[i][1];
result.data[result.num][2] = matrix1.data[i][2];
i++;
} else if (matrix1.data[i][0] > matrix2.data[j][0] || (matrix1.data[i][0] == matrix2.data[j][0] && matrix1.data[i][1] > matrix2.data[j][1])) {
result.data[result.num][0] = matrix2.data[j][0];
result.data[result.num][1] = matrix2.data[j][1];
result.data[result.num][2] = matrix2.data[j][2];
j++;
} else {
result.data[result.num][0] = matrix1.data[i][0];
result.data[result.num][1] = matrix1.data[i][1];
result.data[result.num][2] = matrix1.data[i][2] + matrix2.data[j][2];
i++;
j++;
}
result.num++;
}
while (i < matrix1.num) {
result.data[result.num][0] = matrix1.data[i][0];
result.data[result.num][1] = matrix1.data[i][1];
result.data[result.num][2] = matrix1.data[i][2];
i++;
result.num++;
}
while (j < matrix2.num) {
result.data[result.num][0] = matrix2.data[j][0];
result.data[result.num][1] = matrix2.data[j][1];
result.data[result.num][2] = matrix2.data[j][2];
j++;
result.num++;
}
return result;
}
// 稀疏矩阵相减
SparseMatrix subSparseMatrix(SparseMatrix matrix1, SparseMatrix matrix2) {
SparseMatrix result;
result.m = matrix1.m;
result.n = matrix1.n;
result.num = 0;
int i = 0, j = 0;
while (i < matrix1.num && j < matrix2.num) {
if (matrix1.data[i][0] < matrix2.data[j][0] || (matrix1.data[i][0] == matrix2.data[j][0] && matrix1.data[i][1] < matrix2.data[j][1])) {
result.data[result.num][0] = matrix1.data[i][0];
result.data[result.num][1] = matrix1.data[i][1];
result.data[result.num][2] = matrix1.data[i][2];
i++;
} else if (matrix1.data[i][0] > matrix2.data[j][0] || (matrix1.data[i][0] == matrix2.data[j][0] && matrix1.data[i][1] > matrix2.data[j][1])) {
result.data[result.num][0] = matrix2.data[j][0];
result.data[result.num][1] = matrix2.data[j][1];
result.data[result.num][2] = -1 * matrix2.data[j][2];
j++;
} else {
result.data[result.num][0] = matrix1.data[i][0];
result.data[result.num][1] = matrix1.data[i][1];
result.data[result.num][2] = matrix1.data[i][2] - matrix2.data[j][2];
i++;
j++;
}
result.num++;
}
while (i < matrix1.num) {
result.data[result.num][0] = matrix1.data[i][0];
result.data[result.num][1] = matrix1.data[i][1];
result.data[result.num][2] = matrix1.data[i][2];
i++;
result.num++;
}
while (j < matrix2.num) {
result.data[result.num][0] = matrix2.data[j][0];
result.data[result.num][1] = matrix2.data[j][1];
result.data[result.num][2] = -1 * matrix2.data[j][2];
j++;
result.num++;
}
return result;
}
// 稀疏矩阵相乘
SparseMatrix mulSparseMatrix(SparseMatrix matrix1, SparseMatrix matrix2) {
SparseMatrix result;
result.m = matrix1.m;
result.n = matrix2.n;
result.num = 0;
int row[MAX_SIZE][MAX_SIZE] = {0}; // 存储 matrix1 中每一行非零元素的列号
int col[MAX_SIZE][MAX_SIZE] = {0}; // 存储 matrix2 中每一列非零元素的行号
for (int i = 0; i < matrix1.num; i++) {
row[matrix1.data[i][0]][matrix1.data[i][1]] = matrix1.data[i][2];
}
for (int i = 0; i < matrix2.num; i++) {
col[matrix2.data[i][1]][matrix2.data[i][0]] = matrix2.data[i][2];
}
for (int i = 0; i < matrix1.m; i++) {
for (int j = 0; j < matrix2.n; j++) {
int sum = 0;
for (int k = 0; k < matrix1.n; k++) {
sum += row[i][k] * col[j][k];
}
if (sum != 0) {
result.data[result.num][0] = i;
result.data[result.num][1] = j;
result.data[result.num][2] = sum;
result.num++;
}
}
}
return result;
}
// 稀疏矩阵转置
SparseMatrix transposeSparseMatrix(SparseMatrix matrix) {
SparseMatrix result;
result.m = matrix.n;
result.n = matrix.m;
result.num = matrix.num;
int count[MAX_SIZE] = {0}; // 存储每一列的非零元素个数
int pos[MAX_SIZE + 1] = {0}; // 存储每一列第一个非零元素的位置
for (int i = 0; i < matrix.num; i++) {
count[matrix.data[i][1]]++;
}
pos[0] = 0;
for (int i = 1; i <= matrix.n; i++) {
pos[i] = pos[i - 1] + count[i - 1];
}
for (int i = 0; i < matrix.num; i++) {
int col = matrix.data[i][1];
result.data[pos[col]][0] = matrix.data[i][1];
result.data[pos[col]][1] = matrix.data[i][0];
result.data[pos[col]][2] = matrix.data[i][2];
pos[col]++;
}
return result;
}
int main() {
SparseMatrix matrix1, matrix2;
readSparseMatrix(matrix1);
readSparseMatrix(matrix2);
SparseMatrix result = addSparseMatrix(matrix1, matrix2);
cout << "Add:" << endl;
printSparseMatrix(result);
result = subSparseMatrix(matrix1, matrix2);
cout << "Subtract:" << endl;
printSparseMatrix(result);
result = mulSparseMatrix(matrix1, matrix2);
cout << "Multiply:" << endl;
printSparseMatrix(result);
result = transposeSparseMatrix(matrix1);
cout << "Transpose:" << endl;
printSparseMatrix(result);
return 0;
}
```
输入格式为:
```
m1 n1 num1
r1 c1 v1
r2 c2 v2
...
m2 n2 num2
r1 c1 v1
r2 c2 v2
...
```
其中 `m1` 为矩阵 1 的行数,`n1` 为矩阵 1 的列数,`num1` 为矩阵 1 中非零元素的个数,后面 `num1` 行分别表示矩阵 1 中每个非零元素的行、列、值。`m2`、`n2`、`num2`、矩阵 2 类似。
阅读全文