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);
}
#include <iostream>
using namespace std;
#define MAXSIZE 10
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;
};
class Sparse :public Matrix
{
private:
int _row;
int _column;
Entry _data[MAXSIZE];
int _h = 0;
public:
int length(){ return _h; }
Sparse(int row, int column)
{
_row = row;
_column = column;
}
virtual int size(int dimension) const
{
if (dimension == 0)
return _row;
else if (dimension == 1)
return _column;
else
return 1;
}
virtual void set(int row, int column, double value)
{
bool done = false;
for (int i = 0; i < _h; i++)
{
if (_data[i].row == row && _data[i].column == column)
{
_data[i].value = value;
done = true;
break;
}
}
if (!done)
{
if (_h < MAXSIZE)
{
_data[_h].row = row;
_data[_h].column = column;
_data[_h].value = value;
_h++;
}
else
throw;
}
}
virtual double get(int row, int column) const
{
for (int i = 0; i < _h; i++)
{
if (_data[i].row == row && _data[i].column == column)
{
return _data[i].value;
}
}
return 0; }
virtual void print()
{
for (int i = 0; i < _h; i++)
{
cout << "(" << _data[i].row << "," << _data[i].column << "," << _data[i].value << ")" << endl;
}
}
Sparse operator +(Sparse another)
{
for (int i = 0; i < another._h; i++)
{
double v = get(another._data[i].row, another._data[i].column);
set(another._data[i].row, another._data[i].column, another._data[i].value + v);
}
return *this;
}
};