数据结构课设——基于C++实现稀疏矩阵的计算

基于C++实现稀疏矩阵的计算

课程设计的主要内容

稀疏矩阵的特点是非零元较少,且其分布无规律,对其进行压缩存储的策略为:仅存储非零元,零元素不存储。请设计一种稀疏矩阵的压缩存储结构并在此结构上完成稀疏矩阵的如下操作:
1.录入稀疏矩阵
2.以完整形式(n行n列形式)输出稀疏矩阵。
3.矩阵相加。
4.矩阵元素修改。
5.矩阵相乘。

算法实现

暂时先把代码全放上来,有时间再进行分块说明。 创作不易,谢谢支持!

#include<stdio.h>
#include<iostream>

#define MaxSize 100
#define ERROR -1
#define OK 1

using namespace std;

//三元组表示法
typedef struct {
	int row, col; //非零值对应的行号与列号
	int item;  //非零值
}Triple;

typedef struct
{
	int rows;//行数
	int cols;//列数
	int nums;//非零个数
	Triple data[MaxSize];
}TSMatrix;

void menu()
{
	system("cls");
	printf("*******************************\n");
	printf("       稀疏矩阵的运算   \n");
	printf("*******************************\n\n");
	printf("        主菜单                \n");
	printf("     1.录入稀疏矩阵           \n");
	printf("     2.输出稀疏矩阵           \n");
	printf("     3.矩阵元素修改           \n");
	printf("     4.稀疏矩阵相加           \n");
	printf("     5.稀疏矩阵相乘           \n");
	printf("     6.退出系统               \n");
	printf("-------------------------------\n");
}

void Init(TSMatrix& A)
{
	A.cols = 0; A.rows = 0; A.nums = 0;
}

//构造三元组表示的稀疏矩阵
void Create(TSMatrix& A, TSMatrix& B)
{ //通过输入非零元的信息来构造稀疏矩阵
	cout << "请分别输入第一个矩阵的行数、列数以及非零元的个数:";
	cin >> A.rows >> A.cols >> A.nums;
	//将非零元填入三元组中
	for (int i = 0; i < A.nums; i++)
	{
		cout << "请按行优先分别输入各个非零元的行号、列号以及值:";
		cin >> A.data[i].row >> A.data[i].col >> A.data[i].item;
	}
	cout << "\n请分别输入第二个矩阵的行数、列数以及非零元的个数:";
	cin >> B.rows >> B.cols >> B.nums;
	for (int i = 0; i < B.nums; i++)
	{
		cout << "请按行优先分别输入各个非零元的行号、列号以及值:";
		cin >> B.data[i].row >> B.data[i].col >> B.data[i].item;
	}
	cout << "录入成功!" << endl;
} //录入稀疏矩阵

int change1(int r, int c, int value, TSMatrix& A) //矩阵的零元素修改为非零元素
{
	if (r > A.rows || c > A.cols) return ERROR; //判断行号列号是否错误
	if (MaxSize == A.nums) return ERROR;  //判断是否会超过界限
	if (0 == value) return ERROR; //判断修改后元素的值是否错误
	int index = 0; //存储元素应该插入的位置
	while (index < A.nums)
	{//寻找到修改元素之后该元素的位置
		if (r > A.data[index].row) index++;
		else if (c > A.data[index].col && r == A.data[index].row)  index++;
		else if (r == A.data[index].row && c == A.data[index].col) return ERROR;
		else break;
	}
	if (index < A.nums)
	{
		//各个元素后移
		for (int i = A.nums; i > index; i--)
			A.data[i] = A.data[i - 1];
	}
	A.data[index].row = r; A.data[index].col = c; A.data[index].item = value; A.nums++; //修改之后,非零元的个数加一
	return OK;
}

int change2(int r, int c, int value, TSMatrix& A) //将非零元素改成非零元素
{
	if (r > A.rows || c > A.cols) return ERROR; //判断行号列号是否错误
	if (0 == value) return ERROR; //判断是否会超过界限
	int index = 0; //存储元素应该插入的位置
	while (index < A.nums)
	{//寻找需要修改的元素对应的下标
		if (r > A.data[index].row) index++;
		else if (r == A.data[index].row && c > A.data[index].col) index++;
		else if (r == A.data[index].row && c == A.data[index].col)
		{
			A.data[index].item = value;
			return OK;
		}
		else return ERROR;
	}
	if (index == A.nums) return ERROR;
}

int change3(int r, int c, TSMatrix& A) //将非零元素修改成零元素
{
	if (r > A.rows || c > A.cols) return ERROR; //判断行号列号是否错误
	int index = 0; //存储元素应该插入的位置
	while (index < A.nums)
	{
		//寻找需要修改的元素的下标
		if (r > A.data[index].row) index++;
		else if (r == A.data[index].row && c > A.data[index].col) index++;
		else if (r == A.data[index].row && c == A.data[index].col)
		{
			//各个元素前移
			for (int i = index; i < A.nums; i++) A.data[i] = A.data[i + 1];
			A.nums--; //修改之后非零元的个数减一
			return OK;
		}
		else return ERROR;
	}
	if (index == A.nums) return ERROR;
}

//寻找指定元素
int get(int r, int c, TSMatrix A)
{
	if (r > A.rows || c > A.cols) return ERROR; //判断所寻找的元素的行号列号是否正确
	for (int i = 0; i < A.nums; i++)
	{//寻找指定的元素,并返回
		if (A.data[i].row == r && A.data[i].col == c) return A.data[i].item;
	}
	return 0; //若三元组中没有该元素,则该元素为0
}

int Add(TSMatrix A, TSMatrix B, TSMatrix& C)
{
	if (A.cols != B.cols || A.rows != B.rows) return ERROR; //判断A,B两个矩阵能否进行加法
	else
	{
		C.rows = A.rows; C.cols = A.cols; C.nums = 0; //初始化C矩阵
		//进行元素相加
		for (int i = 1; i <= A.rows; i++)
		{
			for (int j = 1; j <= A.cols; j++)
			{
				if (ERROR != get(i, j, A) || ERROR != get(i, j, B))//有效性判断
				{
					int sum = get(i, j, A) + get(i, j, B);
					if (sum != 0) change1(i, j, sum, C);//相加之后的元素加入C对应的三元组中
				}
				else return ERROR;
			}
		}
	}
	return OK;
}

int Mult(TSMatrix A, TSMatrix B, TSMatrix& C)
{
	if (A.cols != B.rows && B.cols != A.rows) return ERROR; //判断A,B两个矩阵能否进行乘法运算
	if (A.cols == B.rows)
	{
		C.rows = A.rows; C.cols = B.cols; C.nums = 0; //初始化C矩阵
		for (int i = 1; i <= A.rows; i++)
		{
			for (int j = 1; j <= B.cols; j++)
			{
				int sum = 0;
				for (int z = 1; z <= A.cols; z++)
				{
					if (ERROR != get(i, z, A) || ERROR != get(z, j, B))//有效性判断
						sum += get(i, z, A) * get(z, j, B);
					else return ERROR;
				}
				if (sum != 0) change1(i, j, sum, C);  //若所得结果不为0,则加入到稀疏矩阵C中
			}
		}
	}
	else
	{
		C.rows = B.rows; C.cols = A.cols; C.nums = 0; 初始化C矩阵
		for (int i = 1; i <= B.rows; i++)
		{
			for (int j = 1; j <= A.cols; j++)
			{
				int sum = 0;
				for (int z = 1; z <= B.cols; z++)
				{
					if (ERROR != get(z, j, A) || ERROR != get(i, z, B))//有效性判断
						sum += get(i, z, B) * get(z, j, A);
					else return ERROR;
				}
				if (sum != 0) change1(i, j, sum, C);  //若所得结果不为0,则加入到稀疏矩阵C中
			}
		}
	}
	return OK;
}

void Print(TSMatrix A)
{
	int index = 0; //元素的下标
	cout << "\n该矩阵有" << A.rows << "行 " << A.cols << "列," << "共" << A.nums << "个非零元" << endl;
	for (int i = 1; i <= A.rows; i++)
	{
		for (int j = 1; j <= A.cols; j++)
		{
			if (i == A.data[index].row && j == A.data[index].col)  //找到非零元素并输出
			{
				cout << A.data[index].item << "  ";
				index++;
			}
			else cout << 0 << "  ";  //输出0元素
		}
		cout << endl;
	}
	system("pause");
}

void r1(TSMatrix& A, TSMatrix& B)  //功能一
{
	system("cls");
	Create(A, B);  //录入稀疏矩阵
	system("pause");
}

void r2(TSMatrix A, TSMatrix B) //功能二:输出稀疏矩阵
{
	system("cls");
	int x;
	cout << "请选择需要输出的矩阵:" << endl;
	cout << "\t1. 第一个矩阵\n";
	cout << "\t2. 第二个矩阵\n";
	cout << "选择:"; cin >> x;
	switch (x)  //选择需要输出的矩阵
	{
	case 1: cout << "矩阵为:";  Print(A); break;
	case 2: cout << "矩阵为:";  Print(B); break;
	default:
		break;
	}
}

void modify(TSMatrix& A) //修改方式的选择
{
	cout << endl;
	int x, z;
	cout << "请选择需要进行的修改:" << endl;
	cout << "1. 将零元素修改为非零元素\n";
	cout << "2. 将非零元素改成非零元素\n";
	cout << "3. 将非零元素修改成零元素\n";
	cout << "选择:"; cin >> x;
	switch (x)  //选择修改方式
	{
	case 1: int x1, y1, v1; cout << "\n请分别输入需要修改的元素的行号、列号以及值:"; cin >> x1 >> y1 >> v1; z = change1(x1, y1, v1, A); break;
	case 2: int x2, y2, v2; cout << "\n请分别输入需要修改的元素的行号、列号以及值:"; cin >> x2 >> y2 >> v2; z = change2(x2, y2, v2, A); break;
	case 3: int x3, y3; cout << "\n请分别输入需要修改的元素的行号、列号:"; cin >> x3 >> y3; z = change3(x3, y3, A); break;
	default:
		break;
	}
	if (z == OK)
	{
		cout << "修改成功" << endl;
		cout << "修改后的矩阵为:";
		Print(A);
	}
	else {
		cout << "错误!";
		system("pause");
	}
}

void r3(TSMatrix& A, TSMatrix& B) //功能三:矩阵元素的修改
{
	int x;
	system("cls");
	cout << "请选择需要修改的矩阵:" << endl;
	cout << "\t1. 第一个矩阵\n";
	cout << "\t2. 第二个矩阵\n";
	cout << "选择:"; cin >> x;
	switch (x)  //选择需要修改的矩阵
	{
	case 1: modify(A); break;
	case 2: modify(B); break;
	default:
		break;
	}
}

void r4(TSMatrix A, TSMatrix B) //功能四:矩阵的相加
{
	int z;
	TSMatrix C;
	z = Add(A, B, C);
	if (z == OK)
	{
		cout << "相加之后的矩阵为:";
		Print(C);
	}
	else cout << "错误!";
}

void r5(TSMatrix A, TSMatrix B) //功能五:矩阵的相乘
{
	int z;
	TSMatrix C;
	z = Mult(A, B, C);
	if (z == OK)
	{
		cout << "相乘之后的矩阵为:";
		Print(C);
	}
	else cout << "错误!";
}

int main()
{
	int n;
	TSMatrix A, B;
	Init(A);
	Init(B);
	while (1)
	{
		menu();
		cin >> n;
		switch (n)
		{
		case 1: r1(A, B); break;
		case 2: r2(A, B); break;
		case 3: r3(A, B); break;
		case 4: r4(A, B); break;
		case 5: r5(A, B); break;
		case 6: exit(0);
		default:
			break;
		}

	}
}

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值