首页 > 代码库 > [数据结构实验:稀疏矩阵]

[数据结构实验:稀疏矩阵]

加法,乘法,转置:

技术分享
  1 #include <iostream>  2 #include <cstring>  3 #include <cstdio>  4 #include <string>  5 #include <cmath>  6 #include <algorithm>  7 using namespace std;  8 template<class T> struct Node {  9         int x, y; 10         T val; 11         Node(int x, int y, T val): x(x), y(y), val(val) {} 12         Node() {} 13         bool operator < (const Node &_A) const { 14                 return x < _A.x || x == _A.x && y < _A.y; 15         } 16         bool operator == (const Node &_A) const { 17                 return x == _A.x && y == _A.y; 18         } 19         Node operator + (const Node &_A) { 20                 return Node(x, y, val + _A.val); 21         } 22         void swapxy() { 23                 swap(x, y); 24         } 25 }; 26 template<class T> struct Matrix { 27         #define maxn 10 28         Node<T> matrix[maxn]; 29         int total, n, m; 30         void creat() { 31                 puts("请输入稀疏矩阵中非零元素的个数:"); 32                 cin >> total; 33                 puts("请输入矩阵的行数:"); 34                 cin >> n; 35                 puts("请输入矩阵的列数:"); 36                 cin >> m; 37                 for(int i = 0; i < total; i++) { 38                         int x, y; 39                         T val; 40                         printf("请输入第%d个非零元素的(行标 列标 元素值):", i + 1); 41                         cin >> x >> y >> val; 42                         if(x > n || y > m) { 43                                 puts("输入错误,请重新输入!"); 44                                 i--; 45                                 continue; 46                         } 47                         matrix[i] = Node<T>(x, y, val); 48                 } 49                 sort(matrix, matrix + total); 50         } 51         Matrix operator + (const Matrix &_A) const { 52                 Matrix<T> ans, tmp = *this; 53                 ans.total = 0; 54                 ans.n = n; 55                 ans.m = m; 56                 for(int i = 0; i < _A.total; i++) { 57                         tmp.matrix[tmp.total++] = _A.matrix[i]; 58                 } 59                 sort(tmp.matrix, tmp.matrix + tmp.total); 60                 for(int i = 0; i < tmp.total; i++) { 61                         if(tmp.matrix[i] == tmp.matrix[i + 1] && i < tmp.total - 1) tmp.matrix[i + 1] = tmp.matrix[i] + tmp.matrix[i + 1]; 62                         else ans.matrix[ans.total++] = tmp.matrix[i]; 63                 } 64                 return ans; 65         } 66         Matrix operator * (const Matrix &_A) const { 67                 Matrix<T> ans, tmp; 68                 ans.total = 0; 69                 ans.n = n; 70                 ans.m = _A.m; 71                 tmp.total = 0; 72                 for(int i = 0; i < total; i++) { 73                         for(int j = 0; j < _A.total; j++) { 74                                 if(matrix[i].y == _A.matrix[j].x) tmp.matrix[tmp.total++] = Node<T>(matrix[i].x, _A.matrix[j].y, matrix[i].val * _A.matrix[j].val); 75                         } 76                 } 77                 sort(tmp.matrix, tmp.matrix + tmp.total); 78                 for(int i = 0; i < tmp.total; i++) { 79                         if(tmp.matrix[i] == tmp.matrix[i + 1] && i < tmp.total - 1) tmp.matrix[i + 1] = tmp.matrix[i] + tmp.matrix[i + 1]; 80                         else ans.matrix[ans.total++] = tmp.matrix[i]; 81                 } 82                 return ans; 83         } 84         void transpose() { 85                 swap(n, m); 86                 for(int i = 0; i < total; i++) { 87                         swap(matrix[i].x, matrix[i].y); 88                 } 89         } 90         void outp() { 91                 puts("稀疏矩阵为:"); 92                 int p = 0; 93                 for(int i = 1; i <= n; i++) { 94                         for(int j = 1; j <= m; j++) { 95                                 if(i == matrix[p].x && j == matrix[p].y && p < total) { 96                                         cout << matrix[p].val << " "; 97                                         p++; 98                                 } 99                                 else cout << "0 ";100                         }101                         cout << endl;102                 }103                 cout << endl;104         }105 };106 int main()107 {108         Matrix<int> G, T, H, P;109         cout << "-----------------A矩阵---------------\n";110         G.creat();111         G.outp();112         cout << "-----------------B矩阵---------------\n";113         T.creat();114         T.outp();115         H = G + T;116         cout << "-----------------A + B --------------\n";117         H.outp();118         cout << "-----------------A + B 的结果转置-----\n";119         H.transpose();120         H.outp();121         P = G * T;122         cout << "-----------------A * B ----------------\n";123         P.outp();124         return 0;125 }
View Code

 

[数据结构实验:稀疏矩阵]