早教吧 育儿知识 作业答案 考试题库 百科 知识分享

设稀疏矩阵采用三元组顺序表存储结构,编写函数实现稀疏矩阵的转置运算B=AT(这里注意是A的T次方),已知稀疏矩阵A中的非零元三元组的排列次序是先按行下标排列,在行下标相同时按列下标

题目详情

▼优质解答
答案和解析
#include
#include
#include
#define OK 1
#define ERROR 0
#define MAXSIZE 12500
typedef int Status;
typedef int ElemType;

typedef struct
{
int i,j;
ElemType e;
}Triple;
typedef struct
{
Triple date[MAXSIZE+1];
int mu,nu,tu;
}TSMatrix;

Status CreatSMatrix( TSMatrix *M)
//建立三元组
{
int row,col,date,k;
printf("请输入行数列数和非零元个数\n");
scanf("%d,%d,%d",&(*M).mu,&(*M).nu,&(*M).tu);
while( M->mu nu tu > ( M->mu * M->nu ) || M->tu > MAXSIZE)
{
printf("输入不正确,请重新输入\n");
fflush(stdin);
scanf("%d,%d,%d",&M->mu,&M->nu,&M->tu);
}
(*M).date[0].i = 0;
for( k = 1; k date[k].i = row;
M->date[k].j = col;
M->date[k].e = date;
}
printf("输入非空元素组成的三元组完毕!\n");
return OK;
}
Status comp( int a, int b)
//比较两个数字的大小AddSmatrix函数使用
{
int i;
if( a < b)
i = 1;
if( a == b)
i = 0;
if( a > b)
i = -1;
return i;
}
Status AddSMatrix( TSMatrix &A, TSMatrix &B, TSMatrix *C)
//矩阵的相加
{
Triple *Ap,*Bp,*Ae,*Be,*Ch,*Ce;
if( A.mu != B.mu || A.nu != B.nu)
{
printf("\nA and B is not compared\n");
return ERROR;
}
// if( (*C).date )
// free( (*C).date );
C->mu = A.mu;
C->nu = A.nu;
Ap = &A.date[1];
Bp = &B.date[1];
Ae = &A.date[A.tu];
Be = &B.date[B.tu];
Ch = Ce = C->date;
while( Ap i ) )
{
case 1:
{
*Ce = *Ap;
Ap++;
}break;
case -1:
{
*Ce = *Bp;
Bp++;
}break;
case 0:
{
switch( comp(Ap->j,Bp->j) )
{
case 0:
{
*Ce = *Ap;
Ce->e += Bp->e;
if( !Ce->e )
Ce--;
Ap++;
Bp++;
}break;
case 1:
{
*Ce = *Ap;
Ap++;
}break;
case -1:
{
*Ce = *Bp;
Bp++;
}
}
}break;
}
}
if( Ap > Ae)
while( Bp Be)
while( Ap tu = Ce - Ch;
return OK;
}
Status Transpose( TSMatrix M, TSMatrix &T)
//采用三元组表存储表示,求稀疏矩阵M的转置矩阵T
{
int k;
T.mu = M.nu;
T.nu = M.mu;
T.tu = M.tu;
if( T.tu )
{
for( k = 1; k
看了设稀疏矩阵采用三元组顺序表存储...的网友还看了以下: