Design a class Sparse that implements interface Matrix: Sparse should has the following public object functions in addition:
A constructor Sparse(int rows, int column), which initializes all elements in the matrix to 0's.
A function Sparse Sparse::operator + (Sparse & sparse2), which returns the pair-wise sum of two sparse matrixes.
EXAMPLE INPUT
1000000 1000000
1 1 10
1 1000000 50
1000000 1000000 20
1 1000000 30
1000000 1 40
1 1 -10
EXAMPLE OUTPUT
(1,1000000,80)
(1000000,1,40)
(1000000,1000000,20)
主程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Entry
{
public:
int row;
int column;
double value;
};
class Matrix
{
public:
virtual int size(int dimension) const = 0;
virtual void set(int row, int column,
double value) = 0;
virtual double get(int row, int column)
const = 0;
virtual void print() = 0;
};
#include "source"
#include
using namespace std;
void print(Matrix & matrix) {
matrix.print();
}
void readAndSetElement(Matrix & matrix) {
int row;
int column;
double value;
cin >> row >> column >> value;
matrix.set(row, column, value);
}
void readAndSetMultipleElements(Matrix & matrix, int count) {
for (int i = 0; i < count; ++ i) {
readAndSetElement(matrix);
}
}
int main() {
int rows;
int columns;
cin >> rows >> columns;
Sparse sparse1(rows, columns);
readAndSetMultipleElements(sparse1, 3);
Sparse sparse2(rows, columns);
readAndSetMultipleElements(sparse2, 3);
Sparse sparse3 = sparse1 + sparse2;
print(sparse3);
}