3. The class definition for sparse matrix is shown below.
struct Triple
{
int row, col, value;
};
class Matrix; // forward declaration
class MatrixNode
{
friend class Matrix;
friend istream& operator>>(istream&, Matrix&); // for reading in a matrix
private:
MatrixNode *down, *right;
bool head; // a head node or not
union
{ // anonymous union
MatrixNode *next;
Triple triple;
};
MatrixNode(bool, Triple*); // constructor
};
MatrixNode::MatrixNode(bool b, Triple *t) // constructor
{
head = b;
if (b)
{
right = down = this; // row/column header node
}
else
triple = *t; // element node or header node for list of header nodes
}
class Matrix
{
friend istream& operator>>(istream&, Matrix&);
public:
~Matrix(); // destructor
private:
MatrixNode *headnode;
};
Based on this class, do the following tasks.
- Write the C++ function, operator+(const Matrix& b) const, which returns the matrix *this + b.
- Write the C++ function, operator*(const Matrix& b) const, which returns the matrix *this * b.
- Write the C++ function, operator<<(), which outputs a sparse matrix as triples (i, j, aij).
- Write the C++ function, Transpose(), which transpose a sparse matrix.
- Write and test a copy constructor for sparse matrices. What is the computing time of your copy constructor?
Write a client program (main()) to demonstrate those functions you developed.
Write in C++

Step by step
Solved in 2 steps









