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

加法,乘法,转置:

  1 加法,乘法,转置:

  2 #include <iostream>

  3 #include <cstring>

  4 #include <cstdio>

  5 #include <string>

  6 #include <cmath>

  7 #include <algorithm>

  8 using namespace std;

  9 template<class T> struct Node {

 10         int x, y;

 11         T val;

 12         Node(int x, int y, T val): x(x), y(y), val(val) {}

 13         Node() {}

 14         bool operator < (const Node &_A) const {

 15                 return x < _A.x || x == _A.x && y < _A.y;

 16         }

 17         bool operator == (const Node &_A) const {

 18                 return x == _A.x && y == _A.y;

 19         }

 20         Node operator + (const Node &_A) {

 21                 return Node(x, y, val + _A.val);

 22         }

 23         void swapxy() {

 24                 swap(x, y);

 25         }

 26 };

 27 template<class T> struct Matrix {

 28         #define maxn 10

 29         Node<T> matrix[maxn];

 30         int total, n, m;

 31         void creat() {

 32                 puts("请输入稀疏矩阵中非零元素的个数:");

 33                 cin >> total;

 34                 puts("请输入矩阵的行数:");

 35                 cin >> n;

 36                 puts("请输入矩阵的列数:");

 37                 cin >> m;

 38                 for(int i = 0; i < total; i++) {

 39                         int x, y;

 40                         T val;

 41                         printf("请输入第%d个非零元素的(行标 列标 元素值):", i + 1);

 42                         cin >> x >> y >> val;

 43                         if(x > n || y > m) {

 44                                 puts("输入错误,请重新输入!");

 45                                 i--;

 46                                 continue;

 47                         }

 48                         matrix[i] = Node<T>(x, y, val);

 49                 }

 50                 sort(matrix, matrix + total);

 51         }

 52         void getAns(Matrix &ans, Matrix &tmp) const {

 53                 sort(tmp.matrix, tmp.matrix + tmp.total);

 54                 for(int i = 0; i < tmp.total; i++) {

 55                         if(tmp.matrix[i] == tmp.matrix[i + 1] && i < tmp.total - 1) tmp.matrix[i + 1] = tmp.matrix[i] + tmp.matrix[i + 1];

 56                         else ans.matrix[ans.total++] = tmp.matrix[i];

 57                 }

 58         }

 59         Matrix operator + (const Matrix &_A) const {

 60                 Matrix<T> ans, tmp = *this;

 61                 ans.total = 0;

 62                 ans.n = n;

 63                 ans.m = m;

 64                 for(int i = 0; i < _A.total; i++) {

 65                         tmp.matrix[tmp.total++] = _A.matrix[i];

 66                 }

 67                 getAns(ans, tmp);

 68                 return ans;

 69         }

 70         Matrix operator * (const Matrix &_A) const {

 71                 Matrix<T> ans, tmp;

 72                 ans.total = 0;

 73                 ans.n = n;

 74                 ans.m = _A.m;

 75                 tmp.total = 0;

 76                 for(int i = 0; i < total; i++) {

 77                         for(int j = 0; j < _A.total; j++) {

 78                                 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);

 79                         }

 80                 }

 81                 getAns(ans, tmp);

 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                 sort(matrix, matrix + total);

 90         }

 91         void outp() {

 92                 puts("稀疏矩阵为:");

 93                 int p = 0;

 94                 for(int i = 1; i <= n; i++) {

 95                         for(int j = 1; j <= m; j++) {

 96                                 if(i == matrix[p].x && j == matrix[p].y && p < total) {

 97                                         cout << matrix[p].val << " ";

 98                                         p++;

 99                                 }

100                                 else cout << "0 ";

101                         }

102                         cout << endl;

103                 }

104                 cout << endl;

105         }

106 };

107 int main()

108 {

109         Matrix<int> G, T, H, P;

110         cout << "-----------------A矩阵---------------\n";

111         G.creat();

112         G.outp();

113         cout << "-----------------B矩阵---------------\n";

114         T.creat();

115         T.outp();

116         H = G + T;

117         cout << "-----------------A + B --------------\n";

118         H.outp();

119         cout << "-----------------A + B 的结果转置-----\n";

120         H.transpose();

121         H.outp();

122         P = G * T;

123         cout << "-----------------A * B ----------------\n";

124         P.outp();

125         return 0;

126 }
View Code

 

你可能感兴趣的:(数据结构)