该文章介绍稀疏矩阵的相关用法。
头文件:SMatrix.h
#include <iostream>
const int MAXR = 20; //稀疏矩阵最大行数
const int MAXC = 20; //稀疏矩阵最大列数
const int MaxSize = 100; //三元组顺序表最大元素个数
struct TupNode //单个三元组的类型
{
int r; //行号
int c; //列号
int d; //元素值
};
class SMatrixClass
{
int rows; //行数
int cols; //列数
int nums; //非零元素个数
TupNode *data; //稀疏矩阵对应的三元组顺序表
public:
SMatrixClass(); //构造函数
~SMatrixClass(); //析构函数
void CreateTSMatrix(int A[][MAXC], int m, int n); //创建三元组
bool Setvalue(int i, int j, int x); //三元组元素赋值A[i][j]=x
bool GetValue(int i, int j, int &x); //将指定位置的元素值赋给变量x=A[i][j]
void DispMat(); //输出三元组
void Transpose(SMatrixClass &tb); //矩阵转置
//======稀疏矩阵的其他用法======
friend bool MatAdd(SMatrixClass &a, SMatrixClass &b, SMatrixClass &c);//两个稀疏矩阵相加
};
源文件:SMatrix.cpp
#include "SMatrix.h" //包括稀疏矩阵三元组表示的类声明
using namespace std;
SMatrixClass::SMatrixClass() //构造函数
{
data = new TupNode[MaxSize];
}
SMatrixClass::~SMatrixClass() //析构函数
{
delete[] data;
}
void SMatrixClass::CreateTSMatrix(int A[][MAXC], int m, int n) //创建三元组
{
int i, j;
rows = m;
cols = n;
nums = 0;
for (i = 0; i<m; i++)
{
for (j = 0; j<n; j++)
if (A[i][j] != 0)
{
data[nums].r = i;
data[nums].c = j;
data[nums].d = A[i][j];
nums++;
}
}
}
bool SMatrixClass::Setvalue(int i, int j, int x) //三元组元素赋值A[i][j]=x
{
int k = 0, k1;
if (i<0 || i >= rows || j<0 || j >= cols)
return false;
while (k<nums && i>data[k].r)
k++;
while (k<nums && i == data[k].r && j>data[k].c)
k++;
if (data[k].r == i && data[k].c == j)
data[k].d = x;
else
{
for (k1 = nums - 1; k1 >= k; k1--)
{
data[k1 + 1].r = data[k1].r;
data[k1 + 1].c = data[k1].c;
data[k1 + 1].d = data[k1].d;
}
data[k].r = i;
data[k].c = j;
data[k].d = x;
nums++;
}
return true;
}
bool SMatrixClass::GetValue(int i, int j, int &x) //将指定位置的元素值赋给变量x=A[i][j]
{
int k = 0;
if (i<0 || i >= rows || j<0 || j >= cols)
return false;
while (k<nums && data[k].r<i)
k++;
while (k<nums && data[k].r == i && data[k].c<j)
k++;
if (data[k].r == i && data[k].c == j)
x = data[k].d;
else
x = 0;
return true;
}
void SMatrixClass::DispMat() //输出三元组
{
int i;
if (nums <= 0) return;
cout << "\t" << rows << "\t" << cols << "\t" << nums << endl;
cout << "\t------------------\n";
for (i = 0; i<nums; i++)
cout << "\t" << data[i].r << "\t" << data[i].c << "\t" << data[i].d << endl;
}
void SMatrixClass::Transpose(SMatrixClass &tb) //矩阵转置
{
int p, q = 0, v;
tb.rows = cols;
tb.cols = rows;
tb.nums = nums;
if (nums != 0)
{
for (v = 0; v<cols; v++)
for (p = 0; p<nums; p++)
if (data[p].c == v)
{
tb.data[q].r = data[p].c;
tb.data[q].c = data[p].r;
tb.data[q].d = data[p].d;
q++;
}
}
}
//=================稀疏矩阵的其他用法==============
bool MatAdd(SMatrixClass &a, SMatrixClass &b, SMatrixClass &c)//两个稀疏矩阵相加
{
int i = 0, j = 0, k = 0;
int v;
if (a.rows != b.rows || a.cols != b.cols)
return false;
c.rows = a.rows;
c.cols = a.cols;
while (i<a.nums && j<b.nums)
{
if (a.data[i].r == b.data[j].r)
{
if (a.data[i].c<b.data[j].c)
{
c.data[k].r = a.data[i].r;
c.data[k].c = a.data[i].c;
c.data[k].d = a.data[i].d;
k++; i++;
}
else if (a.data[i].c>b.data[j].c)
{
c.data[k].r = b.data[j].r;
c.data[k].c = b.data[j].c;
c.data[k].d = b.data[j].d;
k++; j++;
}
else //a元素的列号等于b元素的列号
{
v = a.data[i].d + b.data[j].d;
if (v != 0) //只将不为0的结果添加到c中
{
c.data[k].r = a.data[i].r;
c.data[k].c = a.data[i].c;
c.data[k].d = v;
k++;
}
i++; j++;
}
}
else if (a.data[i].r<b.data[j].r) //a元素的行号小于b元素的行号
{
c.data[k].r = a.data[i].r; //将a元素添加到c中
c.data[k].c = a.data[i].c;
c.data[k].d = a.data[i].d;
k++; i++;
}
else //a元素的行号大于b元素的行号
{
c.data[k].r = b.data[j].r; //将b元素添加到c中
c.data[k].c = b.data[j].c;
c.data[k].d = b.data[j].d;
k++; j++;
}
c.nums = k;
}
return true; //成功时返回true
}
主函数:main.cpp
#include "SMatrix.h"
using namespace std;
//==========稀疏矩阵基本用法==============
void main1()
{
SMatrixClass t,tb;
int x;
int a[MAXR][MAXC]={{0,0,1,0,0,0,0},{0,2,0,0,0,0,0},{3,0,0,0,0,0,0},{0,0,0,5,0,0,0},{0,0,0,0,6,0,0},{0,0,0,0,0,7,4}};
t.CreateTSMatrix(a,6,7);
cout << "三元组t表示:\n"; t.DispMat();
cout << "执行A[4][1]=8\n";
t.Setvalue(4,1,8);
cout << "三元组t表示:\n"; t.DispMat();
cout << "求x=A[4][1]\n";
t.GetValue(4,1,x);
cout << "x=" << x << endl;
cout << "t转置为tb\n";
t.Transpose(tb);
cout << "三元组tb表示:\n"; tb.DispMat();
cout << "main函数结束,销毁所有的三元组对象" << endl;
}
//===========两个稀疏矩阵相加==============
void main()
{
SMatrixClass a, b, c;
int x;
int A[MAXR][MAXC] = { { 0, 0, 1, 0, 0, 0, 0 }, { 0, 2, 0, 0, 0, 0, 0 }, { 3, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 5, 0, 0, 0 }, { 0, 0, 0, 0, 6, 0, 0 }, { 0, 0, 0, 0, 0, 7, 4 } };
int B[MAXR][MAXC] = { { 0, 0, 1, 0, 0, 0, 0 }, { 0, 2, 0, 0, 0, 0, 0 }, { 3, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 5, 0, 0, 0 }, { 0, 0, 0, 0, 6, 0, 0 }, { 0, 0, 0, 0, 0, 7, 4 } };
a.CreateTSMatrix(A, 6, 7);
cout << "a三元组表示:\n"; a.DispMat();
b.CreateTSMatrix(B, 6, 7);
cout << "b三元组表示:\n"; b.DispMat();
cout << "c=a+b\n";
MatAdd(a, b, c);
cout << "c三元组表示:\n"; c.DispMat();
cout << "main函数结束,销毁所有的三元组对象" << endl;
}