数据结构稀疏矩阵

最近事情比较多,匆忙写出来应付检查的代码。

写的既不优雅,又没效率 ,还有大面积的代码重复[没脸见人]

  1 #include <iostream>
  2 #include <cstdio>
  3 using namespace std;
  4 
  5 const int MAXSIZE = 401;
  6 class Triple {
  7 public:
  8     int i, j, value;
  9     Triple(int ai, int aj, int avalue) {
 10         i = ai;
 11         j = aj;
 12         value = avalue;
 13     }
 14 };
 15 class Matrix {
 16 public:
 17     Triple *data[MAXSIZE] = { nullptr };
 18     int row, column;
 19     int currentSize = 0;
 20     int isExist(int i, int j) {
 21         int begin = 1;
 22         while (data[begin] != nullptr) {
 23             if (data[begin]->i == i && data[begin]->j == j) {
 24                 return begin;
 25             }
 26             begin++;
 27         }
 28         return -1;
 29     }
 30     void append(int i, int j, int value) {
 31         int location = isExist(i, j);
 32         if (location == -1) {
 33             currentSize++;
 34             data[currentSize] = new Triple(i, j, value);
 35         }
 36         else {
 37             data[location]->value += value;
 38         }
 39     }
 40     int getValue(int i, int j) {
 41         int location = isExist(i, j);
 42         if (location == -1) {
 43             return 0;
 44         }
 45         else {
 46             return data[location]->value;
 47         };
 48     }
 49 };
 50 int main()
 51 {
 52     Matrix a, b, c;
 53     int m, s, n, v;
 54 
 55     cout << "进行矩阵乘法运算 m*s s*n 输入m s n" <<
 56         "进行矩阵加法计算 m*n 输入 m n 0" <<
 57         "进行矩阵减法计算 m*n 输入 m n -1" << endl;
 58     scanf("%d %d %d", &m, &s, &n);
 59     
 60     
 61     
 62     if (n > 0) {    //乘法
 63         for (int i = 0; i < m; i++) {
 64             for (int j = 0; j < s; j++) {
 65                 scanf("%d", &v);
 66                 a.append(i, j, v);
 67             }
 68         }
 69         cout << "martix 1 completed" << endl;
 70 
 71         for (int i = 0; i < s; i++) {
 72             for (int j = 0; j < n; j++) {
 73                 scanf("%d", &v);
 74                 b.append(i, j, v);
 75             }
 76         }
 77         cout << "martix 2 completed" << endl;
 78 
 79         for (int i = 0; i < m; i++) {
 80             for (int j = 0; j < n; j++) {
 81                 for (int z = 0; z < s; z++) {
 82                     c.append(i, j, a.getValue(i, z) * b.getValue(z, j));
 83                 }
 84             }
 85         }
 86     }
 87     else if (n == 0) {
 88         for (int i = 0; i < m; i++) {
 89             for (int j = 0; j < s; j++) {
 90                 scanf("%d", &v);
 91                 a.append(i, j, v);
 92             }
 93         }
 94         cout << "martix 1 completed" << endl;
 95 
 96         for (int i = 0; i < m; i++) {
 97             for (int j = 0; j < s; j++) {
 98                 scanf("%d", &v);
 99                 b.append(i, j, v);
100             }
101         }
102         cout << "martix 2 completed" << endl;
103 
104         for (int i = 0; i < m; i++) {
105             for (int j = 0; j < s; j++) {
106                     c.append(i, j, a.getValue(i, j) + b.getValue(i, j));
107             }
108         }
109     }
110     else if (n < 0) {
111         for (int i = 0; i < m; i++) {
112             for (int j = 0; j < s; j++) {
113                 scanf("%d", &v);
114                 a.append(i, j, v);
115             }
116         }
117         cout << "martix 1 completed" << endl;
118 
119         for (int i = 0; i < m; i++) {
120             for (int j = 0; j < s; j++) {
121                 scanf("%d", &v);
122                 b.append(i, j, v);
123             }
124         }
125         cout << "martix 2 completed" << endl;
126 
127         for (int i = 0; i < m; i++) {
128             for (int j = 0; j < s; j++) {
129                     c.append(i, j, a.getValue(i, j) - b.getValue(i, j));
130             }
131         }
132     }
133 
134     int p2c = 1;
135     for (int i = 0; i < m; i++) {
136         for (int j = 0; j < s; j++) {
137             cout << c.getValue(i, j) << " ";
138         }
139         cout << endl;
140     }
141     return 0;
142 }
原文地址:https://www.cnblogs.com/BD1A489/p/9996718.html