三元组表实现稀疏矩阵存储
/*
稀疏矩阵三元组表
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#define ElemType int //矩阵数据类型
#define OK 0 //操作成功执行
#define ERROR -1 //操作失败
#define OVERFLOW -2 //溢出
typedef int Status;
#define MAXSIZE 12000 //最大非零元数
typedef struct Triple { //三元组
int i, j; //行列下标
ElemType e; //非零元元素值
}Triple;
typedef struct TSMatrix { //矩阵
Triple data[MAXSIZE + 1]; //三元组表
int mu, nu, tu; //矩阵的行列数、非零元个数
}TSMatrix;
TSMatrix *CreateSMatrix(); //1.创建
Status DestroySMatrix(TSMatrix *M); //2.销毁
Status PrintSMatrix(TSMatrix *M); //3.打印
Status CopySMatrix(TSMatrix *M, TSMatrix *T); //4.复制
Status AddSMatrix(TSMatrix *M, TSMatrix *N, TSMatrix *Q); //5.矩阵求和
Status SubtSMatrix(TSMatrix *M, TSMatrix *N, TSMatrix *Q); //6.矩阵求差
Status MultSMatrix(TSMatrix *M, TSMatrix *N, TSMatrix *Q); //7.矩阵相乘
Status TransposeSMatrix(TSMatrix *M, TSMatrix *T); //8.矩阵转置
TSMatrix *CreateSMatrix() //1.创建
{
TSMatrix *M;
M = (TSMatrix *)malloc(sizeof(TSMatrix));
std::cout << "请输入矩阵的行数、列数、非零元个数:";
std::cin >> M->mu >> M->nu >> M->tu;
for (int i = 0; i < M->tu; i++)
{
std::cout << "请输入第" << i + 1 << "个非零元的行下标、列下标、元素值:";
std::cin >> M->data[i].i >> M->data[i].j >> M->data[i].e;
}
return M;
}
Status DestroySMatrix(TSMatrix *M) //2.销毁
{
return OK;
}
Status PrintSMatrix(TSMatrix *M) //3.打印
{
int i, j, t, flag;
for (i = 0; i < M->mu; i++)
{
for (j = 0; j < M->nu; j++)
{
flag = 0;
for (t = 0; t < M->tu; t++)
{
if (i == M->data[t].i&&j == M->data[t].j) //非零元
{
std::cout << M->data[t].e << " ";
t++;
flag = 1;
}
}
if (flag == 0)std::cout << 0 << " ";
}
std::cout << std::endl;
}
return OK;
}
Status CopySMatrix(TSMatrix *M, TSMatrix *T) //4.复制
{
return OK;
}
TSMatrix *AddSMatrix(TSMatrix *M, TSMatrix *N) //5.矩阵求和
{
TSMatrix *Q;
Q = (TSMatrix *)malloc(sizeof(TSMatrix));
int i, j, tm = 0, tn = 0, tq = 0;
if (M->mu != N->mu || M->mu != N->nu)
{
std::cout << "两个矩阵的行列数不相等,无法求和!";
return NULL;
}
Q->mu = M->mu;
Q->nu = M->nu;
for (i = 0; i < M->mu; i++)
{
for (j = 0; j < M->nu; j++)
{
if ((i == M->data[tm].i&&j == M->data[tm].j) && !(i == N->data[tn].i&&j == N->data[tn].j)) //M为非零元
{
Q->data[tq].i = i;
Q->data[tq].j = j;
Q->data[tq].e = M->data[tm].e;
tm++;
tq++;
}
else if (!(i == M->data[tm].i&&j == M->data[tm].j) && (i == N->data[tn].i&&j == N->data[tn].j)) //N为非零元
{
Q->data[tq].i = i;
Q->data[tq].j = j;
Q->data[tq].e = N->data[tn].e;
tn++;
tq++;
}
else if ((i == M->data[tm].i&&j == M->data[tm].j) || (i == N->data[tn].i&&j == N->data[tn].j)) //M和N均为非零元
{
Q->data[tq].i = i;
Q->data[tq].j = j;
Q->data[tq].e = M->data[tm].e + N->data[tn].e;
tm++;
tn++;
tq++;
}
else
{
;
}
}
}
tq++;
Q->tu = tq;
return Q;
}
Status SubtSMatrix(TSMatrix *M, TSMatrix *N, TSMatrix *Q) //6.矩阵求差
{
return OK;
}
Status MultSMatrix(TSMatrix *M, TSMatrix *N, TSMatrix *Q) //7.矩阵相乘
{
return OK;
}
TSMatrix *TransposeSMatrix(TSMatrix *M) //8.矩阵转置
{
int i, j, t, t_new = 0, temp;
TSMatrix *T;
T = (TSMatrix *)malloc(sizeof(TSMatrix));
T->mu = M->nu;
T->nu = M->mu;
T->tu = M->tu;
/*
int p, q, col;
if (M->tu)
{
q = 1;
for (col = 0; col < M->nu; col++)
{
for (p = 0; p < M->tu; p++)
{
if (M->data[p].j == col)
{
T->data[q].i = M->data[p].j;
T->data[q].j = M->data[p].i;
T->data[q].e = M->data[p].e;
q++;
}
}
}
}
*/
if (M->tu)
{
for (i = 0; i < M->tu; i++)
{
T->data[i].i = M->data[i].j;
T->data[i].j = M->data[i].i;
T->data[i].e = M->data[i].e;
}
}
return T;
}
int main(int argc, char *argv[])
{
TSMatrix *M, *N, *T, *TT;
M = CreateSMatrix();
N = CreateSMatrix();
std::cout << "矩阵M:" << std::endl;
PrintSMatrix(M);
std::cout << "矩阵N:" << std::endl;
PrintSMatrix(N);
T = AddSMatrix(M, N);
std::cout << "T=M+N:" << std::endl;
PrintSMatrix(T);
TT = TransposeSMatrix(T);
std::cout << "矩阵T的转置:" << std::endl;
PrintSMatrix(TT);
return 0;
}
网友评论