思路:

首先是要了解矩阵的加减运算法则,即同坐标的进行加减 ,所以只需要将两个矩阵中相同的点进行加减,不同的只需要在三元数组中,要一个新的空间进行存储即可

#include<iostream>
using namespace std;

typedef
	struct Node {
		int row; // 行
		int col; // 列
		int data;
	}Node;

void initList(Node *, int);					// 初始化三元数组
int inputList(Node *, int[][3], int, int);	// 将 稀疏矩阵的数据存入三元数组中
void printList(Node *, int);				// 打印三元数组
void transpositionList(Node * list, int);	// 矩阵的转置
void sort(Node *, int);						// 矩阵转置后的对 行 进行排序

void puls(Node *, int&, Node *, int);		// 矩阵相加,结果为 前一个矩阵
int check(Node *, int,int ,int );			// 在第二个矩阵中 查找 和 第一个矩阵相同的位置坐标,并返回 数据

void Minus(Node *, int&, Node *, int);		// 矩阵相减,结果为 前一个矩阵

int main(void) {
	int arr1[5][3] = { {0,0,1},
					   {2,0,0},
					   {0,3,0},
					   {4,0,0},
					   {5,0,0} };
	Node list1[5 * 3];
	initList(list1, 15);
	int len1 = inputList(list1, arr1, 5, 3);
	cout << "第一个转置前:" << endl;
	printList(list1, len1);
	transpositionList(list1, len1);
	cout << "第一个转置后:" << endl;
	printList(list1, len1);
	cout << "=======================" << endl;
	int arr2[5][3] = { {0,0,1},
					   {2,0,0},
					   {0,3,0},
					   {4,0,0},
					   {5,0,1} };
	Node list2[5 * 3];
	initList(list2, 15);
	int len2 = inputList(list2, arr2, 5, 3);
	cout << "第二个转置前:" << endl;
	printList(list2, len2);
	transpositionList(list2, len2);
	cout << "第二个转置后:" << endl;
	printList(list2, len2);

	cout << "=======================" << endl;
	cout << "矩阵相加:" << endl;
	puls(list1, len1, list2, len2);
	printList(list1, len1);

	cout << "=======================" << endl;
	cout << "矩阵相减:" << endl;
	Minus(list1, len1, list2, len2);
	printList(list1, len1);
	return 0;
}
// 在一个存储中查找相同坐标的点,并返回这个点的数据域部分,并且将此数据域改为 0 ,以便两个矩阵有不相交的点的运算
int check(Node * list2, int len2, int row, int col) {
	for (int i = 0; i < len2; i++)
	{
		if (list2[i].row == row && list2[i].col == col) {
			int tmpNumber = list2[i].data;
			list2[i].data = 0;
			return tmpNumber;
		}
	}
	return 0;
}

void Minus(Node * list1, int& len1, Node * list2, int len2)
{
	for (int i = 0; i < len1; i++)
	{
		list1[i].data -= check(list2, len2, list1[i].row, list1[i].col);
		//cout << check(list2, len2, list1[i].row, list1[i].col) << " ";
	}

	for (int i = 0; i < len2; i++)
	{
		if (list2[i].data != 0)
		{
			list1[len1] = list2[i];
			list1[len1].data = -list1[len1].data;
			len1++;
		}
	}
	sort(list1, len1);
}

void puls(Node * list1, int& len1, Node * list2, int len2) 
{
	for (int i = 0; i < len1;i ++)
	{
		list1[i].data += check(list2, len2, list1[i].row, list1[i].col);
	}

	for (int i = 0; i < len2; i++)
	{
		if (list2[i].data != 0)
		{
			list1[len1++] = list2[i];
		}
	}
	sort(list1,len1);
}

void printList(Node * list, int n)
{
	for (int i = 0; i < n; i++)
	{
		if (list[i].data != 0)
		{
			cout << list[i].row << " " << list[i].col << " " << list[i].data << endl;
		}
	}
	cout << endl;
}

void transpositionList(Node * list, int len)
{
	for (int i = 0; i < len; i++)
	{
		int tmp = list[i].row;
		list[i].row = list[i].col;
		list[i].col = tmp;
	}
	sort(list, len);
}

void sort(Node * list, int len)
{
	for (int i = 1; i < len; i++)
	{
		for (int j = 0; j < len - i; j++)
		{
			if (list[j].row > list[j + 1].row)
			{
				Node tmp = list[j];
				list[j] = list[j + 1];
				list[j + 1] = tmp;
			}
		}
	}
}

int inputList(Node * list, int a[][3], int row, int col)
{
	int count = 0;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (a[i][j] != 0)
			{
				list[count].row = i;
				list[count].col = j;
				list[count++].data = a[i][j];
			}
		}
	}
	return count;
}

void initList(Node * list, int n)
{
	int i;
	for (i = 0; i < n; i++) 
	{
		list[i].col = 0;
		list[i].row = 0;
		list[i].data = 0;
	}
}